Ejemplo n.º 1
0
        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 (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);
        }