2using System.Collections.Generic;
3using Unity.Collections;
15 public List<Camera>
cameras {
get;
private set; }
31 protected IGltfReadable
gltf;
35 protected Dictionary<uint, GameObject>
nodes;
57 nodes =
new Dictionary<uint, GameObject>();
68 var go =
new GameObject();
69 go.transform.localScale = scale;
70 go.transform.localPosition = position;
71 go.transform.localRotation = rotation;
72 nodes[nodeIndex] = go;
75 public void SetParent(uint nodeIndex, uint parentIndex)
77 if (
nodes[nodeIndex] ==
null ||
nodes[parentIndex] ==
null)
79 logger?.Error(LogCode.HierarchyInvalid);
82 nodes[nodeIndex].transform.SetParent(
nodes[parentIndex].transform,
false);
87 nodes[nodeIndex].name = name ?? $
"Node-{nodeIndex}";
94 int[] materialIndices,
96 uint? rootJoint =
null,
97 float[] morphTargetWeights =
null,
98 int primitiveNumeration = 0
103 if (primitiveNumeration == 0)
106 meshGo =
nodes[nodeIndex];
110 meshGo =
new GameObject(meshName);
111 meshGo.transform.SetParent(
nodes[nodeIndex].transform,
false);
116 var hasMorphTargets = mesh.blendShapeCount > 0;
117 if (joints ==
null && !hasMorphTargets)
119 var mf = meshGo.AddComponent<MeshFilter>();
121 var mr = meshGo.AddComponent<MeshRenderer>();
126 var smr = meshGo.AddComponent<SkinnedMeshRenderer>();
130 var bones =
new Transform[joints.Length];
131 for (var j = 0; j < bones.Length; j++)
133 var jointIndex = joints[j];
134 bones[j] =
nodes[jointIndex].transform;
137 if (rootJoint.HasValue)
139 smr.rootBone =
nodes[rootJoint.Value].transform;
142 smr.sharedMesh = mesh;
143 if (morphTargetWeights !=
null)
145 for (var i = 0; i < morphTargetWeights.Length; i++)
147 var weight = morphTargetWeights[i];
148 smr.SetBlendShapeWeight(i, weight);
154 var materials =
new Material[materialIndices.Length];
155 for (var index = 0; index < materials.Length; index++)
157 var material =
gltf.GetMaterial(materialIndices[index]) ??
gltf.GetDefaultMaterial();
158 materials[index] = material;
161 renderer.sharedMaterials = materials;
168 int[] materialIndices,
170 NativeArray<Vector3>? positions,
171 NativeArray<Quaternion>? rotations,
172 NativeArray<Vector3>? scales,
173 int primitiveNumeration = 0
176 var materials =
new Material[materialIndices.Length];
177 for (var index = 0; index < materials.Length; index++)
179 var material =
gltf.GetMaterial(materialIndices[index]) ??
gltf.GetDefaultMaterial();
180 material.enableInstancing =
true;
181 materials[index] = material;
184 for (var i = 0; i < instanceCount; i++)
186 var meshGo =
new GameObject($
"{meshName}_i{i}");
187 var t = meshGo.transform;
188 t.SetParent(
nodes[nodeIndex].transform,
false);
189 t.localPosition = positions?[i] ?? Vector3.zero;
190 t.localRotation = rotations?[i] ?? Quaternion.identity;
191 t.localScale = scales?[i] ?? Vector3.one;
193 var mf = meshGo.AddComponent<MeshFilter>();
195 Renderer renderer = meshGo.AddComponent<MeshRenderer>();
196 renderer.sharedMaterials = materials;
200 public virtual void AddScene(
string name, uint[] nodeIndices)
203 go.transform.SetParent(
parent,
false);
205 if (nodeIndices !=
null)
207 foreach (var nodeIndex
in nodeIndices)
209 if (
nodes[nodeIndex] !=
null)
211 nodes[nodeIndex].transform.SetParent(go.transform,
false);
212 nodes[nodeIndex].name =
nodes[nodeIndex].name.Replace(
"(Clone)",
"");
223 public void AddScene(
string name, uint[] nodeIndices, AnimationClip[] animationClips)
void AddCamera(Camera camera)
bool skinUpdateWhenOffscreen
void CreateNode(uint nodeIndex, Vector3 position, Quaternion rotation, Vector3 scale)
GLTFastInstantiator(IGltfReadable gltf, Transform parent, ICodeLogger logger=null, Settings settings=null)
void AddScene(string name, uint[] nodeIndices, AnimationClip[] animationClips)
virtual void SetNodeName(uint nodeIndex, string name)
void AddPrimitiveInstanced(uint nodeIndex, string meshName, Mesh mesh, int[] materialIndices, uint instanceCount, NativeArray< Vector3 >? positions, NativeArray< Quaternion >? rotations, NativeArray< Vector3 >? scales, int primitiveNumeration=0)
void SetParent(uint nodeIndex, uint parentIndex)
virtual void AddScene(string name, uint[] nodeIndices)
virtual void AddPrimitive(uint nodeIndex, string meshName, Mesh mesh, int[] materialIndices, uint[] joints=null, uint? rootJoint=null, float[] morphTargetWeights=null, int primitiveNumeration=0)
Dictionary< uint, GameObject > nodes
SceneInstance sceneInstance
Contains information about the latest instance of a glTF scene
void AddCamera(uint nodeIndex, uint cameraIndex)
static GLTFastLoader instance
GameObject LastLoadedGameObject