public IEnumerator TestGltfCustomAttributesData() { // Load glTF string path = AssetDatabase.GUIDToAssetPath(CubeCustomAttrGuid); var task = GltfUtility.ImportGltfObjectFromPathAsync(path); yield return(WaitForTask(task)); GltfObject gltfObject = task.Result; yield return(null); // Check for custom vertex data is a list of 10s gltfObject.meshes[0].primitives[0].Attributes.TryGetValue("_CUSTOM_ATTR", out var customAttrIdx); GltfAccessor accessor = gltfObject.GetAccessor(customAttrIdx); var intArray = accessor.GetIntArray(false); foreach (var item in intArray) { Assert.AreEqual(10, item); } }
private static async Task <Mesh> ConstructMeshPrimitiveAsync(GltfObject gltfObject, GltfMeshPrimitive meshPrimitive) { if (Application.isPlaying) { await BackgroundThread; } GltfAccessor positionAccessor = null; GltfAccessor normalsAccessor = null; GltfAccessor textCoord0Accessor = null; GltfAccessor textCoord1Accessor = null; GltfAccessor textCoord2Accessor = null; GltfAccessor textCoord3Accessor = null; GltfAccessor colorAccessor = null; GltfAccessor indicesAccessor = null; GltfAccessor tangentAccessor = null; GltfAccessor weight0Accessor = null; GltfAccessor joint0Accessor = null; int vertexCount = 0; if (meshPrimitive.Attributes.POSITION >= 0) { positionAccessor = gltfObject.accessors[meshPrimitive.Attributes.POSITION]; positionAccessor.BufferView = gltfObject.bufferViews[positionAccessor.bufferView]; positionAccessor.BufferView.Buffer = gltfObject.buffers[positionAccessor.BufferView.buffer]; vertexCount = positionAccessor.count; } if (meshPrimitive.Attributes.NORMAL >= 0) { normalsAccessor = gltfObject.accessors[meshPrimitive.Attributes.NORMAL]; normalsAccessor.BufferView = gltfObject.bufferViews[normalsAccessor.bufferView]; normalsAccessor.BufferView.Buffer = gltfObject.buffers[normalsAccessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TEXCOORD_0 >= 0) { textCoord0Accessor = gltfObject.accessors[meshPrimitive.Attributes.TEXCOORD_0]; textCoord0Accessor.BufferView = gltfObject.bufferViews[textCoord0Accessor.bufferView]; textCoord0Accessor.BufferView.Buffer = gltfObject.buffers[textCoord0Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TEXCOORD_1 >= 0) { textCoord1Accessor = gltfObject.accessors[meshPrimitive.Attributes.TEXCOORD_1]; textCoord1Accessor.BufferView = gltfObject.bufferViews[textCoord1Accessor.bufferView]; textCoord1Accessor.BufferView.Buffer = gltfObject.buffers[textCoord1Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TEXCOORD_2 >= 0) { textCoord2Accessor = gltfObject.accessors[meshPrimitive.Attributes.TEXCOORD_2]; textCoord2Accessor.BufferView = gltfObject.bufferViews[textCoord2Accessor.bufferView]; textCoord2Accessor.BufferView.Buffer = gltfObject.buffers[textCoord2Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TEXCOORD_3 >= 0) { textCoord3Accessor = gltfObject.accessors[meshPrimitive.Attributes.TEXCOORD_3]; textCoord3Accessor.BufferView = gltfObject.bufferViews[textCoord3Accessor.bufferView]; textCoord3Accessor.BufferView.Buffer = gltfObject.buffers[textCoord3Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.COLOR_0 >= 0) { colorAccessor = gltfObject.accessors[meshPrimitive.Attributes.COLOR_0]; colorAccessor.BufferView = gltfObject.bufferViews[colorAccessor.bufferView]; colorAccessor.BufferView.Buffer = gltfObject.buffers[colorAccessor.BufferView.buffer]; } if (meshPrimitive.indices >= 0) { indicesAccessor = gltfObject.accessors[meshPrimitive.indices]; indicesAccessor.BufferView = gltfObject.bufferViews[indicesAccessor.bufferView]; indicesAccessor.BufferView.Buffer = gltfObject.buffers[indicesAccessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TANGENT >= 0) { tangentAccessor = gltfObject.accessors[meshPrimitive.Attributes.TANGENT]; tangentAccessor.BufferView = gltfObject.bufferViews[tangentAccessor.bufferView]; tangentAccessor.BufferView.Buffer = gltfObject.buffers[tangentAccessor.BufferView.buffer]; } if (meshPrimitive.Attributes.WEIGHTS_0 >= 0) { weight0Accessor = gltfObject.accessors[meshPrimitive.Attributes.WEIGHTS_0]; weight0Accessor.BufferView = gltfObject.bufferViews[weight0Accessor.bufferView]; weight0Accessor.BufferView.Buffer = gltfObject.buffers[weight0Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.JOINTS_0 >= 0) { joint0Accessor = gltfObject.accessors[meshPrimitive.Attributes.JOINTS_0]; joint0Accessor.BufferView = gltfObject.bufferViews[joint0Accessor.bufferView]; joint0Accessor.BufferView.Buffer = gltfObject.buffers[joint0Accessor.BufferView.buffer]; } if (Application.isPlaying) { await Update; } var mesh = new Mesh { indexFormat = vertexCount > 65535 ? IndexFormat.UInt32 : IndexFormat.UInt16, }; if (positionAccessor != null) { mesh.vertices = await positionAccessor.GetVector3Array(); } if (normalsAccessor != null) { mesh.normals = await normalsAccessor.GetVector3Array(); } if (textCoord0Accessor != null) { mesh.uv = await textCoord0Accessor.GetVector2Array(); } if (textCoord1Accessor != null) { mesh.uv2 = await textCoord1Accessor.GetVector2Array(); } if (textCoord2Accessor != null) { mesh.uv3 = await textCoord2Accessor.GetVector2Array(); } if (textCoord3Accessor != null) { mesh.uv4 = await textCoord3Accessor.GetVector2Array(); } if (colorAccessor != null) { mesh.colors = await colorAccessor.GetColorArray(); } if (indicesAccessor != null) { mesh.triangles = await indicesAccessor.GetIntArray(); } if (tangentAccessor != null) { mesh.tangents = await tangentAccessor.GetVector4Array(); } if (weight0Accessor != null && joint0Accessor != null) { mesh.boneWeights = CreateBoneWeightArray(await joint0Accessor.GetVector4Array(false), await weight0Accessor.GetVector4Array(false), vertexCount); } mesh.RecalculateBounds(); meshPrimitive.SubMesh = mesh; return(mesh); }
private static async Task <Mesh> ConstructMeshPrimitiveAsync(GltfObject gltfObject, GltfMeshPrimitive meshPrimitive) { if (gltfObject.UseBackgroundThread) { await BackgroundThread; } GltfAccessor positionAccessor = null; GltfAccessor normalsAccessor = null; GltfAccessor textCoord0Accessor = null; GltfAccessor textCoord1Accessor = null; GltfAccessor textCoord2Accessor = null; GltfAccessor textCoord3Accessor = null; GltfAccessor colorAccessor = null; GltfAccessor indicesAccessor = null; GltfAccessor tangentAccessor = null; GltfAccessor weight0Accessor = null; GltfAccessor joint0Accessor = null; int vertexCount = 0; positionAccessor = gltfObject.GetAccessor(meshPrimitive.Attributes.POSITION); if (positionAccessor != null) { vertexCount = positionAccessor.count; } normalsAccessor = gltfObject.GetAccessor(meshPrimitive.Attributes.NORMAL); textCoord0Accessor = gltfObject.GetAccessor(meshPrimitive.Attributes.TEXCOORD_0); textCoord1Accessor = gltfObject.GetAccessor(meshPrimitive.Attributes.TEXCOORD_1); textCoord2Accessor = gltfObject.GetAccessor(meshPrimitive.Attributes.TEXCOORD_2); textCoord3Accessor = gltfObject.GetAccessor(meshPrimitive.Attributes.TEXCOORD_3); colorAccessor = gltfObject.GetAccessor(meshPrimitive.Attributes.COLOR_0); indicesAccessor = gltfObject.GetAccessor(meshPrimitive.indices); tangentAccessor = gltfObject.GetAccessor(meshPrimitive.Attributes.TANGENT); weight0Accessor = gltfObject.GetAccessor(meshPrimitive.Attributes.WEIGHTS_0); joint0Accessor = gltfObject.GetAccessor(meshPrimitive.Attributes.JOINTS_0); if (gltfObject.UseBackgroundThread) { await Update; } var mesh = new Mesh { indexFormat = vertexCount > UInt16.MaxValue ? IndexFormat.UInt32 : IndexFormat.UInt16, }; if (positionAccessor != null) { mesh.vertices = positionAccessor.GetVector3Array(); } if (normalsAccessor != null) { mesh.normals = normalsAccessor.GetVector3Array(); } if (textCoord0Accessor != null) { mesh.uv = textCoord0Accessor.GetVector2Array(); } if (textCoord1Accessor != null) { mesh.uv2 = textCoord1Accessor.GetVector2Array(); } if (textCoord2Accessor != null) { mesh.uv3 = textCoord2Accessor.GetVector2Array(); } if (textCoord3Accessor != null) { mesh.uv4 = textCoord3Accessor.GetVector2Array(); } if (colorAccessor != null) { mesh.colors = colorAccessor.GetColorArray(); } if (indicesAccessor != null) { mesh.triangles = indicesAccessor.GetIntArray(); } if (tangentAccessor != null) { mesh.tangents = tangentAccessor.GetVector4Array(); } if (weight0Accessor != null && joint0Accessor != null) { mesh.boneWeights = CreateBoneWeightArray(joint0Accessor.GetVector4Array(false), weight0Accessor.GetVector4Array(false), vertexCount); } mesh.RecalculateBounds(); meshPrimitive.SubMesh = mesh; return(mesh); }
private static Mesh ConstructMeshPrimitive(GltfObject gltfObject, GltfMeshPrimitive meshPrimitive) { GltfAccessor positionAccessor = null; GltfAccessor normalsAccessor = null; GltfAccessor textCoord0Accessor = null; GltfAccessor textCoord1Accessor = null; GltfAccessor textCoord2Accessor = null; GltfAccessor textCoord3Accessor = null; GltfAccessor colorAccessor = null; GltfAccessor indicesAccessor = null; GltfAccessor tangentAccessor = null; GltfAccessor weight0Accessor = null; GltfAccessor joint0Accessor = null; int vertexCount = 0; if (meshPrimitive.Attributes.POSITION >= 0) { positionAccessor = gltfObject.accessors[meshPrimitive.Attributes.POSITION]; positionAccessor.BufferView = gltfObject.bufferViews[positionAccessor.bufferView]; positionAccessor.BufferView.Buffer = gltfObject.buffers[positionAccessor.BufferView.buffer]; vertexCount = positionAccessor.count; } if (meshPrimitive.Attributes.NORMAL >= 0) { normalsAccessor = gltfObject.accessors[meshPrimitive.Attributes.NORMAL]; normalsAccessor.BufferView = gltfObject.bufferViews[normalsAccessor.bufferView]; normalsAccessor.BufferView.Buffer = gltfObject.buffers[normalsAccessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TEXCOORD_0 >= 0) { textCoord0Accessor = gltfObject.accessors[meshPrimitive.Attributes.TEXCOORD_0]; textCoord0Accessor.BufferView = gltfObject.bufferViews[textCoord0Accessor.bufferView]; textCoord0Accessor.BufferView.Buffer = gltfObject.buffers[textCoord0Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TEXCOORD_1 >= 0) { textCoord1Accessor = gltfObject.accessors[meshPrimitive.Attributes.TEXCOORD_1]; textCoord1Accessor.BufferView = gltfObject.bufferViews[textCoord1Accessor.bufferView]; textCoord1Accessor.BufferView.Buffer = gltfObject.buffers[textCoord1Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TEXCOORD_2 >= 0) { textCoord2Accessor = gltfObject.accessors[meshPrimitive.Attributes.TEXCOORD_2]; textCoord2Accessor.BufferView = gltfObject.bufferViews[textCoord2Accessor.bufferView]; textCoord2Accessor.BufferView.Buffer = gltfObject.buffers[textCoord2Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TEXCOORD_3 >= 0) { textCoord3Accessor = gltfObject.accessors[meshPrimitive.Attributes.TEXCOORD_3]; textCoord3Accessor.BufferView = gltfObject.bufferViews[textCoord3Accessor.bufferView]; textCoord3Accessor.BufferView.Buffer = gltfObject.buffers[textCoord3Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.COLOR_0 >= 0) { colorAccessor = gltfObject.accessors[meshPrimitive.Attributes.COLOR_0]; colorAccessor.BufferView = gltfObject.bufferViews[colorAccessor.bufferView]; colorAccessor.BufferView.Buffer = gltfObject.buffers[colorAccessor.BufferView.buffer]; } if (meshPrimitive.indices >= 0) { indicesAccessor = gltfObject.accessors[meshPrimitive.indices]; indicesAccessor.BufferView = gltfObject.bufferViews[indicesAccessor.bufferView]; indicesAccessor.BufferView.Buffer = gltfObject.buffers[indicesAccessor.BufferView.buffer]; } if (meshPrimitive.Attributes.TANGENT >= 0) { tangentAccessor = gltfObject.accessors[meshPrimitive.Attributes.TANGENT]; tangentAccessor.BufferView = gltfObject.bufferViews[tangentAccessor.bufferView]; tangentAccessor.BufferView.Buffer = gltfObject.buffers[tangentAccessor.BufferView.buffer]; } if (meshPrimitive.Attributes.WEIGHTS_0 >= 0) { weight0Accessor = gltfObject.accessors[meshPrimitive.Attributes.WEIGHTS_0]; weight0Accessor.BufferView = gltfObject.bufferViews[weight0Accessor.bufferView]; weight0Accessor.BufferView.Buffer = gltfObject.buffers[weight0Accessor.BufferView.buffer]; } if (meshPrimitive.Attributes.JOINTS_0 >= 0) { joint0Accessor = gltfObject.accessors[meshPrimitive.Attributes.JOINTS_0]; joint0Accessor.BufferView = gltfObject.bufferViews[joint0Accessor.bufferView]; joint0Accessor.BufferView.Buffer = gltfObject.buffers[joint0Accessor.BufferView.buffer]; } var mesh = new Mesh { indexFormat = vertexCount > 65535 ? IndexFormat.UInt32 : IndexFormat.UInt16, vertices = positionAccessor?.GetVector3Array(), normals = normalsAccessor?.GetVector3Array(), uv = textCoord0Accessor?.GetVector2Array(), uv2 = textCoord1Accessor?.GetVector2Array(), uv3 = textCoord2Accessor?.GetVector2Array(), uv4 = textCoord3Accessor?.GetVector2Array(), colors = colorAccessor?.GetColorArray(), triangles = indicesAccessor?.GetIntArray(), tangents = tangentAccessor?.GetVector4Array() }; if (weight0Accessor != null && joint0Accessor != null) { mesh.boneWeights = CreateBoneWeightArray(joint0Accessor.GetVector4Array(false), weight0Accessor.GetVector4Array(false), vertexCount); } mesh.RecalculateBounds(); return(mesh); }