public static string ToJson(this glTFMesh self)
        {
            var f = new JsonFormatter();

            GltfSerializer.Serialize_gltf_meshes_ITEM(f, self);
            return(f.ToString());
        }
Пример #2
0
        public static SkinningInfo Create(GltfData data, glTFMesh mesh, glTFPrimitives primitives)
        {
            var hasMorphTarget = HasMorphTarget(mesh);

            var positions = data.GLTF.accessors[primitives.attributes.POSITION];
            var skinning  = new SkinningInfo
            {
                Joints  = primitives.GetJoints(data, positions.count),
                Weights = primitives.GetWeights(data, positions.count),
            };

            if (skinning.Joints != null)
            {
                // use SkinnedMeshRenderer
                return(skinning);
            }
            else if (!hasMorphTarget)
            {
                // use MeshRenderer
                return(skinning);
            }
            else
            {
                // use SkinnedMeshRenderer without boneWeight.
                // https://github.com/vrm-c/UniVRM/issues/1675
                return(new SkinningInfo
                {
                    ShouldSetRendererNodeAsBone = true,
                    Joints = _ => (0, 0, 0, 0),
                    Weights = _ => (1, 0, 0, 0), // assign weight 1
                });
Пример #3
0
        static Mesh FromGltf(Vrm10ImportData storage, glTFMesh x, glTFPrimitives primitive, bool isShared)
        {
            var mesh = new Mesh((TopologyType)primitive.mode)
            {
                VertexBuffer = primitive.attributes.FromGltf(storage)
            };

            if (isShared)
            {
                // create joined index buffer
                mesh.IndexBuffer = storage.CreateAccessor(x.primitives.Select(y => y.indices).ToArray());
            }
            else
            {
                mesh.IndexBuffer = storage.CreateAccessor(primitive.indices);
            }

            if (mesh.IndexBuffer == null)
            {
                var indices = Enumerable.Range(0, mesh.VertexBuffer.Count).ToArray();
                var na      = storage.Data.NativeArrayManager.CreateNativeArray(indices);
                mesh.IndexBuffer = new BufferAccessor(storage.Data.NativeArrayManager, na.Reinterpret <byte>(4), AccessorValueType.UNSIGNED_INT, AccessorVectorType.SCALAR, na.Length);
            }

            {
                gltf_mesh_extras_targetNames.TryGet(x, out List <string> targetNames);

                for (int i = 0; i < primitive.targets.Count; ++i)
                {
                    var gltfTarget = primitive.targets[i];

                    string targetName = null;
                    {
                        targetName = targetNames[i];
                    }
                    var target = new MorphTarget(targetName)
                    {
                        VertexBuffer = gltfTarget.FromGltf(storage)
                    };

                    // validate count
                    foreach (var kv in target.VertexBuffer)
                    {
                        if (kv.Value.Count != mesh.VertexBuffer.Count)
                        {
                            throw new Exception();
                        }
                    }

                    mesh.MorphTargets.Add(target);
                }
            }

            return(mesh);
        }
Пример #4
0
        public static MeshGroup FromGltf(this glTFMesh x, Vrm10ImportData storage)
        {
            var group = new MeshGroup(x.name);

            if (x.primitives.Count == 1)
            {
                var primitive     = x.primitives[0];
                var mesh          = primitive.FromGltf(storage, x);
                var materialIndex = primitive.material;

                mesh.Submeshes.Add(
                    new Submesh(0, mesh.IndexBuffer.Count, materialIndex));

                group.Meshes.Add(mesh);
            }
            else if (!x.AllPrimitivesHasSameVertexBuffer())
            {
                int offset = 0;
                foreach (var primitive in x.primitives)
                {
                    var mesh          = primitive.FromGltf(storage, x);
                    var materialIndex = primitive.material;

                    mesh.Submeshes.Add(
                        new Submesh(offset, mesh.IndexBuffer.Count, materialIndex));
                    offset += mesh.IndexBuffer.Count;

                    group.Meshes.Add(mesh);
                }
            }
            else
            {
                //
                // obsolete
                //
                // for VRM

                var mesh   = x.SharedBufferFromGltf(storage);
                int offset = 0;
                foreach (var primitive in x.primitives)
                {
                    var materialIndex = primitive.material;
                    var count         = storage.Gltf.accessors[primitive.indices].count;
                    mesh.Submeshes.Add(
                        new Submesh(offset, count, materialIndex));
                    offset += count;
                }

                group.Meshes.Add(mesh);
            }

            return(group);
        }
Пример #5
0
        public static glTFMesh ExportMeshGroup(this MeshGroup src, List <object> materials, Vrm10Storage storage, ExportArgs option)
        {
            var mesh = new glTFMesh
            {
                name = src.Name
            };

            foreach (var x in src.Meshes)
            {
                // MeshとSubmeshがGltfのPrimitiveに相当する?
                x.ExportMesh(materials, storage, mesh, option);
            }

            return(mesh);
        }
Пример #6
0
        static Mesh FromGltf(Vrm10Storage storage, glTFMesh x, glTFPrimitives primitive, bool isShared)
        {
            var mesh = new Mesh((TopologyType)primitive.mode)
            {
                VertexBuffer = primitive.attributes.FromGltf(storage)
            };

            if (isShared)
            {
                // create joined index buffer
                mesh.IndexBuffer = storage.CreateAccessor(x.primitives.Select(y => y.indices).ToArray());
            }
            else
            {
                mesh.IndexBuffer = storage.CreateAccessor(primitive.indices);
            }

            {
                gltf_mesh_extras_targetNames.TryGet(x, out List <string> targetNames);

                for (int i = 0; i < primitive.targets.Count; ++i)
                {
                    var gltfTarget = primitive.targets[i];

                    string targetName = null;
                    {
                        targetName = targetNames[i];
                    }
                    var target = new MorphTarget(targetName)
                    {
                        VertexBuffer = gltfTarget.FromGltf(storage)
                    };

                    // validate count
                    foreach (var kv in target.VertexBuffer)
                    {
                        if (kv.Value.Count != mesh.VertexBuffer.Count)
                        {
                            throw new Exception();
                        }
                    }

                    mesh.MorphTargets.Add(target);
                }
            }

            return(mesh);
        }
Пример #7
0
        public static bool AllPrimitivesHasSameVertexBuffer(this glTFMesh m)
        {
            if (m.primitives.Count <= 1)
            {
                return(true);
            }

            var first = m.primitives[0];

            for (int i = 1; i < m.primitives.Count; ++i)
            {
                if (!first.HasSameVertexBuffer(m.primitives[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #8
0
        /// <summary>
        /// ModelExporter.Export で作られた Model.MeshGroups[*] を GLTF 化する
        /// </summary>
        /// <param name="src"></param>
        /// <param name="materials"></param>
        /// <param name="storage"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static glTFMesh ExportMeshGroup(this MeshGroup src, List <object> materials, Vrm10Storage storage, ExportArgs option)
        {
            var gltfMesh = new glTFMesh
            {
                name = src.Name
            };

            if (src.Meshes.Count != 1)
            {
                throw new NotImplementedException();
            }

            foreach (var prim in src.Meshes[0].ExportMeshDivided(materials, storage, option))
            {
                gltfMesh.primitives.Add(prim);
            }

            var targetNames = src.Meshes[0].MorphTargets.Select(x => x.Name).ToArray();

            gltf_mesh_extras_targetNames.Serialize(gltfMesh, targetNames);

            return(gltfMesh);
        }
Пример #9
0
 /// <summary>
 /// IndexBuffer毎に異なるVertexBufferを参照する
 ///
 ///  VertexBuffer
 ///  +--------+ +--------+ +--------+
 ///  |0       | |1       | |2       |
 ///  +--------+ +--------+ +--------+
 ///       A         A        A
 ///       |         |        |
 ///  +---------+--------+--------+
 ///  | submesh0|submesh1|submesh2|
 ///  +---------+--------+--------+
 ///  IndexBuffer
 /// </summary>
 public static Mesh FromGltf(this glTFPrimitives primitive, Vrm10Storage storage, glTFMesh x)
 {
     return(FromGltf(storage, x, primitive, false));
 }
Пример #10
0
        static void ExportMesh(this Mesh mesh, List <object> materials, Vrm10Storage storage, glTFMesh gltfMesh, ExportArgs option)
        {
            //
            // primitive share vertex buffer
            //
            var attributeAccessorIndexMap = mesh.VertexBuffer
                                            .ToDictionary(
                kv => kv.Key,
                kv => kv.Value.AddAccessorTo(
                    storage, 0, option.sparse,
                    kv.Key == VertexBuffer.PositionKey ? (Action <ArraySegment <byte>, glTFAccessor>)Vec3MinMax : null
                    )
                );

            List <Dictionary <string, int> > morphTargetAccessorIndexMapList = null;

            if (mesh.MorphTargets.Any())
            {
                morphTargetAccessorIndexMapList = new List <Dictionary <string, int> >();
                foreach (var morphTarget in mesh.MorphTargets)
                {
                    var dict = new Dictionary <string, int>();

                    foreach (var kv in morphTarget.VertexBuffer)
                    {
                        if (option.removeTangent && kv.Key == VertexBuffer.TangentKey)
                        {
                            // remove tangent
                            continue;
                        }
                        if (option.removeMorphNormal && kv.Key == VertexBuffer.NormalKey)
                        {
                            // normal normal
                            continue;
                        }
                        if (kv.Value.Count != mesh.VertexBuffer.Count)
                        {
                            throw new Exception("inavlid data");
                        }
                        var accessorIndex = kv.Value.AddAccessorTo(storage, 0,
                                                                   option.sparse,
                                                                   kv.Key == VertexBuffer.PositionKey ? (Action <ArraySegment <byte>, glTFAccessor>)Vec3MinMax : null);
                        dict.Add(kv.Key, accessorIndex);
                    }

                    morphTargetAccessorIndexMapList.Add(dict);
                }
            }

            var drawCountOffset = 0;

            foreach (var y in mesh.Submeshes)
            {
                // index
                // slide index buffer accessor
                var indicesAccessorIndex = ExportIndices(storage, mesh.IndexBuffer, drawCountOffset, y.DrawCount, option);
                drawCountOffset += y.DrawCount;

                var prim = new glTFPrimitives
                {
                    mode       = (int)mesh.Topology,
                    material   = y.Material,
                    indices    = indicesAccessorIndex,
                    attributes = new glTFAttributes(),
                };
                gltfMesh.primitives.Add(prim);

                // attribute
                foreach (var kv in mesh.VertexBuffer)
                {
                    var attributeAccessorIndex = attributeAccessorIndexMap[kv.Key];

                    switch (kv.Key)
                    {
                    case VertexBuffer.PositionKey: prim.attributes.POSITION = attributeAccessorIndex; break;

                    case VertexBuffer.NormalKey: prim.attributes.NORMAL = attributeAccessorIndex; break;

                    case VertexBuffer.ColorKey: prim.attributes.COLOR_0 = attributeAccessorIndex; break;

                    case VertexBuffer.TexCoordKey: prim.attributes.TEXCOORD_0 = attributeAccessorIndex; break;

                    case VertexBuffer.TexCoordKey2: prim.attributes.TEXCOORD_1 = attributeAccessorIndex; break;

                    case VertexBuffer.JointKey: prim.attributes.JOINTS_0 = attributeAccessorIndex; break;

                    case VertexBuffer.WeightKey: prim.attributes.WEIGHTS_0 = attributeAccessorIndex; break;
                    }
                }

                // morph target
                if (mesh.MorphTargets.Any())
                {
                    foreach (var(t, accessorIndexMap) in
                             Enumerable.Zip(mesh.MorphTargets, morphTargetAccessorIndexMapList, (t, v) => (t, v)))
                    {
                        var target = new gltfMorphTarget();
                        prim.targets.Add(target);

                        foreach (var kv in t.VertexBuffer)
                        {
                            if (!accessorIndexMap.TryGetValue(kv.Key, out int targetAccessorIndex))
                            {
                                continue;
                            }
                            switch (kv.Key)
                            {
                            case VertexBuffer.PositionKey:
                                target.POSITION = targetAccessorIndex;
                                break;

                            case VertexBuffer.NormalKey:
                                target.NORMAL = targetAccessorIndex;
                                break;

                            case VertexBuffer.TangentKey:
                                target.TANGENT = targetAccessorIndex;
                                break;

                            default:
                                throw new NotImplementedException();
                            }
                        }
                    }
                }
            }

            // target name
            if (mesh.MorphTargets.Any())
            {
                gltf_mesh_extras_targetNames.Serialize(gltfMesh, mesh.MorphTargets.Select(z => z.Name));
            }
        }
Пример #11
0
 /// <summary>
 /// VertexBufferはひとつでIndexBufferの参照が異なる
 ///
 ///  VertexBuffer
 ///  +----------------------------------+
 ///  |                                  |
 ///  +----------------------------------+
 ///       A         A        A
 ///       |         |        |
 ///  +---------+--------+--------+
 ///  | submesh0|submesh1|submesh2|
 ///  +---------+--------+--------+
 ///  IndexBuffer
 /// </summary>
 public static Mesh SharedBufferFromGltf(this glTFMesh x, Vrm10Storage storage)
 {
     // 先頭を使う
     return(FromGltf(storage, x, x.primitives[0], true));
 }
Пример #12
0
 /// <summary>
 /// IndexBuffer毎に異なるVertexBufferを参照する
 ///
 ///  VertexBuffer
 ///  +--------+ +--------+ +--------+
 ///  |0       | |1       | |2       |
 ///  +--------+ +--------+ +--------+
 ///       A         A        A
 ///       |         |        |
 ///  +---------+--------+--------+
 ///  | submesh0|submesh1|submesh2|
 ///  +---------+--------+--------+
 ///  IndexBuffer
 /// </summary>
 static Mesh FromGltf(this glTFPrimitives primitive, Vrm10ImportData storage, glTFMesh x)
 {
     return(FromGltf(storage, x, primitive, false));
 }