### Export with Draco Compression Enabled Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ExportRuntime.md Apply Draco compression to meshes during export. This requires the 'Draco for Unity' package to be installed. Vertex attributes can be preserved using the `PreservedVertexAttributes` mask. ```csharp using System.IO; using GLTFast.Export; // ... var export = new GameObjectExport(new ExportSettings { Format = GltfFormat.Binary, // Enable Draco compression DracoCompression = true, // Preserve vertex colors and normals PreservedVertexAttributes = MeshExport.VertexAttribute.Color | MeshExport.VertexAttribute.Normal }); export.AddScene("MyScene", new GameObject[] { myGameObject }); await export.SaveToFileAndDispose("myGameObject.glb"); ``` -------------------------------- ### GltfImport Scene Instantiation Methods Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UpgradeGuides.md Provides methods for instantiating scenes from glTF assets. Supports instantiating the main scene or a specific scene by index, with options for using a parent transform or a custom instantiator. ```csharp // To get the number of scenes public int sceneCount; // Returns the default scene's index public int? defaultSceneIndex; // Methods for instantiation public bool InstantiateMainScene( Transform parent ); public bool InstantiateMainScene(IInstantiator instantiator); public bool InstantiateScene( Transform parent, int sceneIndex = 0); public bool InstantiateScene( IInstantiator instantiator, int sceneIndex = 0 ); ``` -------------------------------- ### Synchronous Scene Instantiation in gltfast Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UpgradeGuides.md This code demonstrates the older, synchronous method for instantiating a glTF scene. Use the async variant for better performance with large scenes. ```csharp async void Start() { var gltf = new GltfImport(); var success = await gltf.Load("file:///path/to/file.gltf"); if(!success) return; // Old, sync instantiation success = gltf.InstantiateMainScene(transform); if(success) Debug.Log("glTF instantiated successfully!"); } ``` -------------------------------- ### Asynchronous Scene Instantiation in gltfast Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UpgradeGuides.md This code shows the recommended asynchronous method for instantiating a glTF scene. It allows instantiation to be spread over multiple frames, improving frame rate for large scenes. ```csharp async void Start() { var gltf = new GltfImport(); var success = await gltf.Load("file:///path/to/file.gltf"); if(!success) return; // New, async instantiation success = await gltf.InstantiateMainSceneAsync(transform); if(success) Debug.Log("glTF instantiated successfully!"); } ``` -------------------------------- ### Configure glTF Import Settings Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ImportRuntime.md Customize glTF loading behavior by providing an ImportSettings instance to the Load method. This allows for fine-tuning of various import options. ```csharp using UnityEngine; using GLTFast; using System.IO; using System.Threading; using Cysharp.Threading.Tasks; public class ImportSettingsExample : MonoBehaviour { async void Start() { var gltf = new GltfImport(); // Custom import settings var importSettings = new ImportSettings { AnimateVertex = true, UseMeshPrimitives = true, FileConflictResolution = FileConflictResolution.OverwriteExisting }; byte[] gltfBytes = System.IO.File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "scene.glb")); var success = await gltf.Load(gltfBytes, uri: new System.Uri("file://" + Path.Combine(Application.streamingAssetsPath, "scene.glb")), importSettings: importSettings); if (success) { await InstantiateScene(gltf); } } async UniTask InstantiateScene(GltfImport gltf) { await gltf.InstantiateMainSceneAsync(); } } ``` -------------------------------- ### Access SceneInstance for Adjustments Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ImportRuntime.md Demonstrates how to access the SceneInstance after a glTF scene has been loaded for further modifications. This is useful for accessing animations, cameras, and lights. ```cs async void SceneInstanceAccess(string gltfFileUrl) { var settings = new ImportSettings { AnimateFrame = true, }; var result = await GltfAsset.Load(gltfFileUrl, settings: settings); if (!result.Success) return; var sceneInstance = result.Scene .CreateGameObject(name: "MyScene") .GetComponent(); // Access animations, cameras, lights etc. // For example, to access all cameras: foreach (var camera in sceneInstance.Cameras) { Debug.Log($"Found camera: {camera.name}"); } } ``` -------------------------------- ### Load glTF from Memory (Byte Array) Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ImportRuntime.md Load a glTF asset from a byte array in memory. Provide the original URI if it's a glTF-binary file to resolve relative URIs in non-self-contained glTFs. ```csharp using UnityEngine; using GLTFast; using System.IO; using System.Threading; using Cysharp.Threading.Tasks; public class LoadGltfFromMemory : MonoBehaviour { async void Start() { var gltf = new GltfImport(); // Load from byte array byte[] gltfBytes = System.IO.File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "scene.glb")); var success = await gltf.Load(gltfBytes, uri: new System.Uri("file://" + Path.Combine(Application.streamingAssetsPath, "scene.glb"))); if (success) { // Instantiate scene await InstantiateScene(gltf); } } async UniTask InstantiateScene(GltfImport gltf) { // Instantiate the main scene await gltf.InstantiateMainSceneAsync(); } } ``` -------------------------------- ### glTFast Repository Structure Overview Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/sources.md This is a visual representation of the glTFast monorepo structure. The actual glTFast package is located in 'Packages/com.unity.cloud.gltfast'. ```none ├── Packages │ ├── com.unity.cloud.gltfast │ └── com.unity.cloud.gltfast.tests │ └── Assets~ └── Projects ├── glTFast-Test └── ... ``` -------------------------------- ### Runtime Loading via Script in Unity Source: https://github.com/atteneder/gltfast/blob/openupm/README.md Use this C# script to load a glTF asset from a URL at runtime. Ensure the GLTFast package is added to your GameObject. ```csharp var gltf = gameObject.AddComponent(); gltf.url = "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/Duck/glTF/Duck.gltf"; ``` -------------------------------- ### Simple GameObject Export Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ExportRuntime.md Export a GameObject hierarchy to a glTF file. Ensure the `GLTFast.Export` namespace is available. ```csharp using System.IO; using GLTFast.Export; // ... var export = new GameObjectExport(new ExportSettings { Format = GltfFormat.Binary }); export.AddScene("MyScene", new GameObject[] { myGameObject }); // Save to a file and dispose the export object. // The GameObjectExport instance becomes invalid after this call. await export.SaveToFileAndDispose("myGameObject.glb"); ``` -------------------------------- ### Add glTFast and Newtonsoft JSON Dependencies Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UseCaseCustomExtras.md Add these lines to your manifest.json file to include the necessary glTFast and Newtonsoft JSON packages. Replace '' with the desired version. ```json { "dependencies": { // Add these lines: // Replace "" with the version you wish to install "com.unity.cloud.gltfast": "", "com.unity.nuget.newtonsoft-json": "" // Other dependencies... } } ``` -------------------------------- ### Custom Post-Loading Behavior and Instantiation Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ImportRuntime.md Perform custom actions after a glTF asset has been loaded asynchronously. This includes custom instantiation logic by providing an IInstantiator implementation. ```csharp using UnityEngine; using GLTFast; using System.IO; using System.Threading; using Cysharp.Threading.Tasks; public class CustomInstantiation : MonoBehaviour { async void Start() { var gltf = new GltfImport(); byte[] gltfBytes = System.IO.File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "scene.glb")); var success = await gltf.Load(gltfBytes, uri: new System.Uri("file://" + Path.Combine(Application.streamingAssetsPath, "scene.glb"))); if (success) { // Instantiate with custom instantiator await gltf.InstantiateMainSceneAsync(new GameObjectInstantiator(null, gameObject)); } } } ``` -------------------------------- ### Load glTF via GltfAsset Component Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ImportRuntime.md Use the GltfAsset component to load glTF assets from a URI. Ensure file paths are prefixed with 'file://' in the Unity Editor and on certain platforms. ```csharp using UnityEngine; public class LoadViaComponent : MonoBehaviour { public GltfAsset gltfAsset; async void Start() { bool success = await gltfAsset.Load("Assets/Scenes/scene.gltf"); if (success) { // Instantiate scene } } } ``` -------------------------------- ### Custom glTF Import Behavior Script Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UseCaseCustomExtras.md Implement a custom glTF import behavior by inheriting from GltFast.IGltFastInstantiator. This script handles the instantiation of glTF objects and custom data deserialization. ```csharp [code-cs [custom-gltf-import](../DocExamples/CustomGltfImport.cs#CustomGltfImport)] ``` -------------------------------- ### Add Assembly References for glTFast Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UseCaseCustomExtras.md In your assembly definition file (.asmdef), add 'glTFast' and 'glTFast.Newtonsoft' to the 'references' array to enable compilation of custom scripts that use these libraries. ```json "references": [ // Add these lines: "glTFast", "glTFast.Newtonsoft" // Other references... ] ``` -------------------------------- ### Custom Defer Agent for GltfImport Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ImportRuntime.md Pass a custom defer agent to an individual GltfImport instance for granular control over loading behavior. This allows for fine-tuning performance on a per-import basis. ```csharp var deferAgent = new TimeBudgetPerFrameDeferAgent(TimeSpan.FromSeconds(0.01)); var settings = new ImportSettings { DeferAgent = deferAgent }; var gltf = new GltfImport(settings); await gltf.Load("MyModel.gltf"); // ... gltf.Dispose(); ``` -------------------------------- ### Clone glTFast Repository Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/sources.md Use this command to clone the glTFast repository from GitHub. Ensure Git LFS is handled correctly by your Git client. ```sh git clone git@github.com:Unity-Technologies/com.unity.cloud.gltfast.git ``` -------------------------------- ### Legacy IMaterialGenerator.GenerateMaterial Method Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UpgradeGuides.md The previous signature for generating materials, which received separate arrays for textures, schema images, and image variants. ```csharp Material GenerateMaterial(Schema.Material gltfMaterial, ref Schema.Texture[] textures, ref Schema.Image[] schemaImages, ref Dictionary[] imageVariants); ``` -------------------------------- ### Play Animation Automatically on Load Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UpgradeGuides.md Use GameObjectInstantiator to access the SceneInstance and control animation playback. Ensure legacyAnimation is not null before calling Play(). ```csharp async void Start() { var gltfImport = new GltfImport(); await gltfImport.Load("test.gltf"); var instantiator = new GameObjectInstantiator(gltfImport,transform); var success = gltfImport.InstantiateMainScene(instantiator); if (success) { // Get the SceneInstance to access the instance's properties var sceneInstance = instantiator.SceneInstance; // Play the default (i.e. the first) animation clip var legacyAnimation = instantiator.SceneInstance.LegacyAnimation; if (legacyAnimation != null) { legacyAnimation.Play(); } } } ``` -------------------------------- ### Updated IMaterialGenerator.GenerateMaterial Method Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UpgradeGuides.md The current signature for generating materials, which accepts an `IGltfReadable` interface for more flexible access to loaded glTF data. ```csharp Material GenerateMaterial(Schema.Material gltfMaterial, IGltfReadable gltf); ``` -------------------------------- ### Export GameObject with Local Transform Discarded Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/ExportRuntime.md Export a GameObject, discarding its original transform by providing an identity matrix as the scene origin. This ensures the exported glTF node's position is relative to the scene origin. ```csharp using System.IO; using GLTFast.Export; using Unity.Mathematics; // ... var export = new GameObjectExport(new ExportSettings { Format = GltfFormat.Binary }); // Add scene with identity matrix to discard original transform export.AddScene("MyScene", new GameObject[] { myGameObject }, float4x4.identity); await export.SaveToFileAndDispose("myGameObject.glb"); ``` -------------------------------- ### Extra Data Class for Deserialization Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UseCaseCustomExtras.md Define a C# class to represent the custom data structure you expect to find in the glTF 'extras' property. This class is used with Newtonsoft JSON for deserialization. ```csharp [code-cs [extra-data](../DocExamples/ExtraData.cs#ExtraData)] ``` -------------------------------- ### Add Custom Data to glTF Asset Source: https://github.com/atteneder/gltfast/blob/openupm/Documentation~/UseCaseCustomExtras.md Include custom data within the 'extras' property of a glTF JSON object's node definition. This allows for custom metadata associated with scene objects. ```json "nodes": [ { // Example of mesh data in a glTF "mesh": 0, "name": "Cube", // Add these lines: "extras": { "some-extra-key": "some-extra-value" } } ] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.