Convert Figma logo to code with AI

atteneder logoglTFast

Efficient glTF 3D import / export package for Unity

1,255
251
1,255
101

Top Related Projects

Blender glTF 2.0 importer and exporter

Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.

103,278

JavaScript 3D Library.

🇨🇭 A React renderer for Three.js

16,746

:a: Web framework for building virtual reality experiences.

Quick Overview

glTFast is a Unity package that provides efficient loading and rendering of glTF 3D models. It aims to be a fast, lightweight, and easy-to-use solution for integrating glTF assets into Unity projects, supporting both runtime and editor-time loading.

Pros

  • Fast loading and rendering performance
  • Supports latest glTF 2.0 specification features
  • Seamless integration with Unity's rendering pipeline
  • Active development and community support

Cons

  • May have limited support for some advanced glTF features
  • Requires Unity 2019.3 or newer
  • Documentation could be more comprehensive
  • Some features may require additional setup or plugins

Code Examples

Loading a glTF model at runtime:

using GLTFast;
using UnityEngine;

public class GLTFLoader : MonoBehaviour
{
    public string gltfUrl = "https://example.com/model.gltf";

    async void Start()
    {
        var gltf = new GltfImport();
        bool success = await gltf.Load(gltfUrl);
        if (success)
        {
            await gltf.InstantiateMainSceneAsync(transform);
        }
    }
}

Loading a glTF model with custom material generator:

using GLTFast;
using UnityEngine;

public class CustomMaterialLoader : MonoBehaviour
{
    public string gltfUrl = "https://example.com/model.gltf";

    async void Start()
    {
        var gltf = new GltfImport(null, new CustomMaterialGenerator());
        bool success = await gltf.Load(gltfUrl);
        if (success)
        {
            await gltf.InstantiateMainSceneAsync(transform);
        }
    }
}

public class CustomMaterialGenerator : IMaterialGenerator
{
    // Implement custom material generation logic here
}

Exporting a Unity GameObject to glTF:

using GLTFast.Export;
using UnityEngine;

public class GLTFExporter : MonoBehaviour
{
    public GameObject objectToExport;

    public async void ExportToGLTF()
    {
        var export = new GameObjectExport();
        await export.AddGameObject(objectToExport);
        byte[] bytes = await export.SaveToByteArray();
        // Use the bytes array to save or transmit the glTF data
    }
}

Getting Started

  1. Install the glTFast package via Unity Package Manager:

    • Open Package Manager (Window > Package Manager)
    • Click the "+" button and choose "Add package from git URL"
    • Enter: https://github.com/atteneder/glTFast.git
  2. Add the following using statement to your script:

    using GLTFast;
    
  3. Use the GltfImport class to load and instantiate glTF models in your Unity scene.

For more detailed instructions and advanced usage, refer to the official documentation on the GitHub repository.

Competitor Comparisons

Blender glTF 2.0 importer and exporter

Pros of glTF-Blender-IO

  • Specifically designed for Blender integration, offering seamless workflow for Blender users
  • Supports advanced Blender-specific features and materials
  • Maintained by Khronos Group, ensuring alignment with glTF standards

Cons of glTF-Blender-IO

  • Limited to Blender ecosystem, not suitable for general-purpose glTF handling
  • May have slower performance compared to glTFast for real-time applications

Code Comparison

glTF-Blender-IO (Python):

class glTF2ImportUserExtension:
    def __init__(self):
        self.properties = bpy.context.scene.glTF2UserExtensions

    def gather_import_properties(self):
        return self.properties.get_dict()

glTFast (C#):

public class GltfImport : IDisposable
{
    public async Task<bool> LoadGltfBinary(byte[] data, string uri = null)
    {
        return await LoadGltf(data, uri, GltfMimeType.GlbBinary);
    }
}

The code snippets demonstrate the different approaches: glTF-Blender-IO focuses on Blender-specific implementation, while glTFast provides a more general-purpose C# implementation for Unity.

Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.

Pros of Babylon.js

  • More comprehensive 3D engine with a wider range of features
  • Larger community and ecosystem, providing better support and resources
  • Better documentation and learning materials

Cons of Babylon.js

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to its extensive feature set
  • May be overkill for simple glTF loading and rendering tasks

Code Comparison

Babylon.js:

BABYLON.SceneLoader.Append("", "model.gltf", scene, function (scene) {
    // Scene is loaded
});

glTFast:

var gltf = new GLTFast.GltfImport();
await gltf.Load("model.gltf");
gltf.InstantiateMainScene(transform);

Summary

Babylon.js is a full-featured 3D engine with extensive capabilities, while glTFast is a lightweight, specialized glTF loader for Unity. Babylon.js offers more features and community support but comes with a larger footprint and complexity. glTFast is simpler and more focused on efficient glTF loading in Unity projects. The choice between them depends on the specific project requirements and the development environment.

103,278

JavaScript 3D Library.

Pros of three.js

  • More comprehensive 3D library with a wider range of features and capabilities
  • Larger community and ecosystem, resulting in more resources, plugins, and support
  • Supports multiple 3D file formats, not limited to glTF

Cons of three.js

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to its extensive feature set
  • Less specialized for glTF handling compared to glTFast

Code Comparison

three.js:

const loader = new THREE.GLTFLoader();
loader.load('model.gltf', (gltf) => {
  scene.add(gltf.scene);
}, undefined, (error) => {
  console.error('An error occurred:', error);
});

glTFast:

var gltf = new GLTFast.GltfImport();
await gltf.Load("model.gltf");
var gameObject = new GameObject();
gltf.InstantiateMainScene(gameObject.transform);

The code snippets demonstrate the basic process of loading a glTF model in each library. three.js uses a more JavaScript-centric approach with callbacks, while glTFast employs C# async/await syntax for Unity integration.

🇨🇭 A React renderer for Three.js

Pros of react-three-fiber

  • Seamless integration with React ecosystem and state management
  • Declarative approach to 3D scene creation, aligning with React paradigms
  • Rich ecosystem of hooks and utilities for common 3D tasks

Cons of react-three-fiber

  • Steeper learning curve for developers not familiar with React
  • Potential performance overhead due to React's reconciliation process
  • Limited to web-based applications, unlike glTFast's Unity compatibility

Code Comparison

react-three-fiber:

function Scene() {
  return (
    <Canvas>
      <mesh>
        <boxGeometry args={[1, 1, 1]} />
        <meshStandardMaterial color="hotpink" />
      </mesh>
    </Canvas>
  );
}

glTFast:

public class GLTFLoader : MonoBehaviour
{
    public string gltfUrl = "https://example.com/model.gltf";
    async void Start() {
        var gltf = await GLTFast.GltfImport.LoadGltfBinary(gltfUrl);
        gltf.InstantiateMainScene(transform);
    }
}

The code examples highlight the declarative nature of react-three-fiber versus the more imperative approach of glTFast. react-three-fiber leverages JSX for scene description, while glTFast uses C# and Unity's component system for 3D model loading and instantiation.

16,746

:a: Web framework for building virtual reality experiences.

Pros of A-Frame

  • Comprehensive WebVR/AR framework with a higher-level API
  • Extensive ecosystem with numerous components and extensions
  • Easier for beginners to create 3D/VR experiences using HTML-like syntax

Cons of A-Frame

  • Larger file size and potentially slower performance for simple scenes
  • Less flexibility for low-level GPU optimizations
  • May be overkill for projects that only need glTF loading functionality

Code Comparison

A-Frame example:

<a-scene>
  <a-gltf-model src="model.gltf"></a-gltf-model>
</a-scene>

glTFast example:

var gltf = new GLTFast.GltfImport();
await gltf.Load("model.gltf");
gltf.InstantiateMainScene(transform);

Summary

A-Frame is a full-featured WebVR/AR framework that provides an easy-to-use, declarative approach to creating 3D and VR experiences. It offers a rich ecosystem and is suitable for a wide range of projects. glTFast, on the other hand, is a focused glTF loader for Unity, providing efficient loading and rendering of glTF models. While A-Frame is more beginner-friendly and versatile, glTFast may offer better performance for specific use cases involving glTF models in Unity projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

glTFast

codecov

Unity and glTF logos side by side

openupm

NOTE: It's recommended to switch to Unity glTFast, a fork of this package that is developed and supported by Unity. Consult the upgrade guide for details.

glTFast enables use of glTF™ (GL Transmission Format) asset files in Unity®.

It focuses on speed, memory efficiency and a small build footprint while also providing:

  • 100% glTF 2.0 specification compliance
  • Ease of use
  • Robustness and Stability
  • Customization and extensibility for advanced users

Features

glTFast supports the full glTF 2.0 specification and many extensions. It works with Universal, High Definition and the Built-In Render Pipelines on all platforms.

See the comprehensive list of supported features and extensions.

Workflows

There are four use-cases for glTF within Unity

Schematic diagram of the four glTF workflows

Read more about the workflows in the documentation.

Installing

NOTE: Consider switching to Unity glTFast, a fork of this package that is developed and supported by Unity. Consult the upgrade guide for details.

Installation instruction for the original glTFast.

Usage

You can load a glTF asset from an URL or a file path.

Runtime Loading via Component

Add a GltfAsset component to a GameObject.

GltfAsset component

Runtime Loading via Script

var gltf = gameObject.AddComponent<GLTFast.GltfAsset>();
gltf.url = "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF/Duck.gltf";

See Runtime Loading via Script in the documentation for more details and instructions how to customize the loading behavior via script.

Editor Import

Move or copy glTF files into your project's Assets folder, similar to other 3D formats:

Editor Import

Unity glTFast will import them to native Unity prefabs and add them to the asset database.

See Editor Import in the documentation for details.

Editor Export

The main menu has a couple of entries for glTF export under File > Export and glTFs can also be created via script.

Project Setup

Materials and Shader Variants

❗ IMPORTANT ❗

Unity glTFast uses custom shader graphs that you have to include in builds in order to make materials work. If materials are fine in the Unity Editor but not in builds, chances are some shaders (or variants) are missing.

Read the section Materials and Shader Variants in the Documentation for details.

Contribution

See CONTRIBUTING.md.

License

Copyright 2023 Unity Technologies and the Unity glTFast authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use files in this repository except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Trademarks

Unity® is a registered trademark of Unity Technologies.

Khronos® is a registered trademark and glTF™ is a trademark of The Khronos Group Inc.