Ejemplo n.º 1
0
        protected virtual void CreateMeshObject(GLTF.Schema.Mesh mesh, Transform parent, int meshId)
        {
            if (_assetCache.MeshCache[meshId] == null)
            {
                _assetCache.MeshCache[meshId] = new MeshCacheData[mesh.Primitives.Count];
            }

            for (int i = 0; i < mesh.Primitives.Count; ++i)
            {
                var primitive    = mesh.Primitives[i];
                var primitiveObj = CreateMeshPrimitive(primitive, meshId, i);
                primitiveObj.transform.SetParent(parent, false);
                primitiveObj.SetActive(true);
            }
        }
Ejemplo n.º 2
0
        protected virtual void BuildAttributesForMeshes()
        {
            for (int i = 0; i < _root.Meshes.Count; ++i)
            {
                GLTF.Schema.Mesh mesh = _root.Meshes[i];
                if (_assetCache.MeshCache[i] == null)
                {
                    _assetCache.MeshCache[i] = new MeshCacheData[mesh.Primitives.Count];
                }

                for (int j = 0; j < mesh.Primitives.Count; ++j)
                {
                    _assetCache.MeshCache[i][j] = new MeshCacheData();
                    var primitive = mesh.Primitives[j];
                    BuildMeshAttributes(primitive, i, j);
                }
            }
        }
Ejemplo n.º 3
0
        //
        private void ExportMesh()
        {
            var mesh = new GLTF.Schema.Mesh();

            mesh.Primitives = new List <MeshPrimitive>();
            if (this._target.GetComponent <UnityEngine.SkinnedMeshRenderer>() != null)
            {
                var skin     = this._target.GetComponent <UnityEngine.SkinnedMeshRenderer>();
                var parent   = _target.parent;
                var allBones = _getAllChildren(parent);

                var gltfSKIN = new Skin
                {
                    Joints = new List <NodeId>()
                };
                _root.Nodes = new List <Node>();
                _root.Skins = new List <Skin>();
                _root.Skins.Add(gltfSKIN);

                foreach (var bone in allBones)
                {
                    var translation = bone.localPosition;
                    var rotation    = bone.localRotation;
                    var scale       = bone.localScale;
                    var node        = new Node
                    {
                        Name        = bone.gameObject.name,
                        Translation = new GLTF.Math.Vector3(translation.x, translation.y, translation.z),
                        Rotation    = new GLTF.Math.Quaternion(rotation.x, rotation.y, rotation.z, rotation.w),
                        Scale       = new GLTF.Math.Vector3(scale.x, scale.y, scale.z),
                    };

                    if (bone.childCount > 0)
                    {
                        // Debug.logger.Log(bone.childCount);
                        node.Children = new List <NodeId>();
                        for (var i = 0; i < bone.childCount; i++)
                        {
                            node.Children.Add(
                                new NodeId
                            {
                                Id   = allBones.IndexOf(bone.GetChild(i)),
                                Root = _root
                            }
                                );
                        }
                    }
                    _root.Nodes.Add(node);
                }

                if (skin.rootBone != null)
                {
                    gltfSKIN.Skeleton = new NodeId
                    {
                        Id   = allBones.IndexOf(skin.rootBone),
                        Root = _root
                    };
                }

                foreach (var bone in skin.bones)
                {
                    gltfSKIN.Joints.Add(
                        new NodeId
                    {
                        Id   = allBones.IndexOf(bone),
                        Root = _root
                    }
                        );
                }

                mesh.Primitives.AddRange(ExportPrimitive(skin.sharedMesh, skin.sharedMaterials, gltfSKIN));
            }
            else if (this._target.GetComponent <UnityEngine.ParticleSystemRenderer>() != null)
            {
                var renderer = this._target.GetComponent <UnityEngine.ParticleSystemRenderer>();
                mesh.Primitives.AddRange(ExportPrimitive(renderer.mesh, renderer.sharedMaterials));
            }
            else
            {
                var filter   = this._target.GetComponent <UnityEngine.MeshFilter>();
                var renderer = this._target.GetComponent <UnityEngine.MeshRenderer>();
                mesh.Primitives.AddRange(ExportPrimitive(filter.sharedMesh, renderer.sharedMaterials));
            }

            var id = new MeshId
            {
                Id   = _root.Meshes.Count,
                Root = _root
            };

            _root.Meshes.Add(mesh);
        }
        private List<Dictionary<string, AccessorId>> GenerateMorphTargets(UnityEngine.Mesh mesh, GLTF.Schema.Mesh m)
        {
            if (mesh.blendShapeCount <= 0)
            {
                return new List<Dictionary<string, AccessorId>>();
            }

            if (_mesh2targets.ContainsKey(mesh))
            {
                return _mesh2targets[mesh];
            }

            var targets = new List<Dictionary<string, AccessorId>>();
            var targetNames = new JArray();
            m.Extras = new JProperty("extras", new JObject(new JProperty("targetNames", targetNames)));

            int stride = 0;
            for (int i = 0; i < mesh.blendShapeCount; i += 1)
            {
                stride += 3 * 4;
                if (mesh.normals.Length > 0)
                {
                    stride += 3 * 4;
                }
                if (mesh.tangents.Length > 0)
                {
                    stride += 4 * 4;
                }
            }
            var bufferView = CreateByteBufferView(mesh.name + "-targets", stride * mesh.vertexCount, stride);

            Vector3[] vertices = new Vector3[mesh.vertexCount];
            Vector3[] normals = new Vector3[mesh.normals.Length];
            Vector3[] tangents = new Vector3[mesh.tangents.Length];
            int offset = 0;

            m.Weights = new List<double>();

            for (int i = 0; i < mesh.blendShapeCount; i += 1)
            {
                var name = mesh.GetBlendShapeName(i);
                var target = new Dictionary<string, AccessorId>();
                targets.Add(target);

                targetNames.Add(name);
                m.Weights.Add(mesh.GetBlendShapeFrameWeight(i, 0));

                mesh.GetBlendShapeFrameVertices(i, 0, vertices, normals, tangents);

                target.Add("POSITION", PackAttrToBuffer(bufferView, vertices, offset, (Vector3[] data, int index) => { return Utils.ConvertVector3LeftToRightHandedness(ref data[index]); }));
                target["POSITION"].Value.Name += "-" + name + "-POSITION";
                offset += 3 * 4;

                if (mesh.normals.Length > 0)
                {
                    target.Add("NORMAL", PackAttrToBuffer(bufferView, normals, offset, (Vector3[] data, int index) => { return Utils.ConvertVector3LeftToRightHandedness(ref data[index]); }));
                    target["NORMAL"].Value.Name += "-" + name + "-NORMAL";
                    offset += 3 * 4;
                }

                if (mesh.tangents.Length > 0)
                {
                    target.Add("TANGENT", PackAttrToBuffer(bufferView, tangents, offset, (Vector3[] data, int index) => { return Utils.ConvertVector3LeftToRightHandedness(ref data[index]); }));
                    target["TANGENT"].Value.Name += "-" + name + "-TANGENT";
                    offset += 4 * 3;
                }
            }

            _mesh2targets.Add(mesh, targets);

            return targets;
        }
Ejemplo n.º 5
0
 public virtual void Import(EditorImporter importer, UnityEngine.Mesh mesh, GLTF.Schema.Mesh gltfMesh, Extension extension)
 {
 }
 public static void Import(string extensionName, EditorImporter importer, UnityEngine.Mesh mesh, GLTF.Schema.Mesh gltfMesh, Extension extension)
 {
     if (Name2Extensions.ContainsKey(extensionName))
     {
         Name2Extensions[extensionName].Import(importer, mesh, gltfMesh, extension);
     }
 }