public async Task Write()
        {
            var mesh      = new GpuMesh("geometry");
            var scene     = new SceneAsset("main");
            var container = new ContentContainer();

            container.Scenes.Add(scene);
            container.Meshes.Add(mesh);

            var writer       = new GltfSharpWriter();
            var memoryStream = new MemoryStream();
            await writer.WriteAsync(memoryStream, container);
        }
Exemple #2
0
        private void CalculateTangents(GpuMesh mesh)
        {
            foreach (var bufferAndPrimitives in mesh.GroupPrimitives())
            {
                var bufferView = bufferAndPrimitives.BufferView;
                if (bufferView.GetStream(StreamKey.TexCoord0) == null)
                {
                    continue;
                }

                throw new NotImplementedException();
            }
            //if (mesh.GetStream(StreamKey.TexCoord0) == null)
            //{
            //    return;
            //}
            //var tangents = new ArrayMeshStream<Vector3>(mesh.Count, mesh.GetStream(StreamKey.Position).ConverterFactory);
            //mesh.SetStream(StreamKey.Tangent, tangents);

            //foreach (var submesh in mesh.Submeshes)
            //{
            //    switch (submesh.VertexSourceType)
            //    {
            //        case VertexSourceType.Triangles:
            //            CalculateTangentsForTriangles(mesh, submesh, tangents);
            //            break;
            //        case VertexSourceType.Points:
            //        case VertexSourceType.Lines:
            //        case VertexSourceType.LineLoop:
            //        case VertexSourceType.LineStrip:
            //            break;
            //        default:
            //            throw new NotImplementedException(submesh.VertexSourceType.ToString());
            //    }
            //}

            //for (var index = 0; index < tangents.Count; index++)
            //{
            //    var tangent = tangents[index];
            //    var l = tangent.LengthSquared;
            //    if (l < 1e-6f)
            //    {
            //        tangents[index] = Vector3.UnitX;
            //    }
            //    else
            //    {
            //        tangents[index] = tangent * (1.0f / (float)Math.Sqrt(l));
            //    }
            //}
        }
        public Mesh(GraphicsDevice device, MeshData meshData, bool readWrite = false)
        {
            this.meshData  = meshData;
            this.readWrite = readWrite;
            subMeshes      = new GpuMesh[meshData.subMeshes.Length];
            bounds         = new AabbBounds(meshData.subMeshes[0].vertices[0].position);

            for (int i = 0; i < meshData.subMeshes.Length; i++)
            {
                var subMesh = meshData.subMeshes[i];
                subMeshes[i] = new GpuMesh(device, subMesh.vertices, subMesh.indices, readWrite);
                foreach (var vertex in subMesh.vertices)
                {
                    bounds = bounds.Encapsulate(vertex.position);
                }
            }
        }
Exemple #4
0
        private IEnumerable <IMesh> PaintSingleStreamMesh(GpuMesh mesh)
        {
            foreach (var bufferAndPrimitives in mesh.GroupPrimitives())
            {
                var bufferView = bufferAndPrimitives.BufferView;
                if (bufferView.GetStream(StreamKey.Color) != null)
                {
                    continue;
                }
                var meshStream             = bufferView.GetStream(StreamKey.Position);
                var streamConverterFactory = meshStream.ConverterFactory;
                var colors = new Vector4[meshStream.Count];
                new Span <Vector4>(colors).Fill(_color);
                var arrayMeshStream = new ArrayMeshStream <Vector4>(colors, streamConverterFactory);
                bufferView.SetStream(StreamKey.Color, arrayMeshStream);
            }

            yield return(mesh);
        }
        private IMesh TransformMesh(string id, Mesh mesh, Skin skin)
        {
            var gpuMesh = new GpuMesh(id);

            gpuMesh.Primitives.Capacity = mesh.Primitives.Count;

            var bufferViews = new Dictionary <BufferViewKey, BufferViewReader>();

            //for (var targetIndex = 0; targetIndex < primitive.MorphTargetsCount; ++targetIndex)
            //{
            //    foreach (var vertexAccessor in primitive.GetMorphTargetAccessors(targetIndex))
            //    {
            //        var key = "TARGET_"+vertexAccessor.Key+ targetIndex;
            //        var accessor = vertexAccessor.Value;
            //        accessorCollection.Register(key, accessor)
            //    }
            //}

            foreach (var primitive in mesh.Primitives)
            {
                var vertexAccessors = primitive.VertexAccessors;
                var bufferViewKey   = new BufferViewKey(vertexAccessors);
                if (!bufferViews.TryGetValue(bufferViewKey, out var bufferViewReader))
                {
                    bufferViewReader = new BufferViewReader {
                        BufferView = new MeshBufferView()
                    };
                    foreach (var stream in bufferViewKey)
                    {
                        var streamReader = CreateStream(stream);
                        bufferViewReader.StreamReaders.Add(streamReader);
                        if (streamReader != null)
                        {
                            bufferViewReader.BufferView.SetStream(stream.Key, streamReader.GetMeshStream());
                        }
                    }
                }

                var anyStreamReader = bufferViewReader.StreamReaders.FirstOrDefault(_ => _ != null);
                if (anyStreamReader == null)
                {
                    continue;
                }

                var offset = anyStreamReader.GetMeshStream().Count;
                var(topology, indices) = TransformPrimitive(primitive);

                var indexMap        = new DictionaryMeshStream <int>(StreamConverterFactory.Default);
                var remappedIndices = new List <int>();
                foreach (var index in indices)
                {
                    remappedIndices.Add(indexMap.Add(index) + offset);
                }
                foreach (var stream in bufferViewKey.Zip(bufferViewReader.StreamReaders,
                                                         (k, r) => new { Key = k, Reader = r }))
                {
                    if (stream.Reader != null)
                    {
                        var accessor = vertexAccessors[stream.Key.AcessorKey];
                        stream.Reader.Append(accessor, indexMap);
                    }
                }

                gpuMesh.Primitives.Add(new GpuPrimitive(topology, remappedIndices, bufferViewReader.BufferView));
            }

            return(gpuMesh);
        }