예제 #1
0
        void ParseMesh(ModelMesh modelMesh, BabylonScene scene, BabylonSkeleton skeleton)
        {
            var proxyID   = ProxyMesh.CreateBabylonMesh(modelMesh.Name, scene);
            int indexName = 0;

            foreach (var part in modelMesh.MeshParts)
            {
                var material = exportedMaterials.First(m => m.Name == part.Effect.GetHashCode().ToString());

                var indices = new ushort[part.PrimitiveCount * 3];
                part.IndexBuffer.GetData(part.StartIndex * 2, indices, 0, indices.Length);

                if (part.VertexBuffer.VertexDeclaration.VertexStride > PositionNormalTextured.Stride)
                {
                    var mesh     = new Mesh <PositionNormalTexturedWeights>(material);
                    var vertices = new PositionNormalTexturedWeights[part.NumVertices];

                    part.VertexBuffer.GetData(part.VertexOffset * part.VertexBuffer.VertexDeclaration.VertexStride, vertices, 0, vertices.Length, part.VertexBuffer.VertexDeclaration.VertexStride);

                    for (int index = 0; index < vertices.Length; index++)
                    {
                        vertices[index].TextureCoordinates.Y = 1.0f - vertices[index].TextureCoordinates.Y;
                    }

                    mesh.AddPart(indexName.ToString(), vertices.ToList(), indices.Select(i => (int)i).ToList());
                    mesh.CreateBabylonMesh(scene, proxyID, skeleton);
                }
                else
                {
                    var mesh     = new Mesh <PositionNormalTextured>(material);
                    var vertices = new PositionNormalTextured[part.NumVertices];
                    part.VertexBuffer.GetData(part.VertexOffset * PositionNormalTextured.Stride, vertices, 0, vertices.Length, PositionNormalTextured.Stride);

                    for (int index = 0; index < vertices.Length; index++)
                    {
                        vertices[index].TextureCoordinates.Y = 1.0f - vertices[index].TextureCoordinates.Y;
                    }

                    mesh.AddPart(indexName.ToString(), vertices.ToList(), indices.Select(i => (int)i).ToList());
                    mesh.CreateBabylonMesh(scene, proxyID, skeleton);
                }

                indexName++;
            }
        }
예제 #2
0
    public void LoadMesh(string respath, string name, Action <IProxy <Mesh> > finish)
    {
        var mesh = assetMgrIO.GetMesh(name);

        if (mesh != null)
        {
            var pmesh = new ProxyMesh(mesh, name);
            assetMgrIO.MeshAddRef(name);
            finish(pmesh);
        }
        else
        {
            ResmgrNative.Instance.LoadBytesFromCache(respath + "/mesh/" + name + ".mesh.bytes",
                                                     name,
                                                     (bytes, tagName) =>
            {
                Mesh lmesh = AssetMgr.ReadMesh(bytes, tagName);
                assetMgrIO.AddMesh(tagName, lmesh);
                var pmesh = new ProxyMesh(lmesh, name);
                assetMgrIO.MeshAddRef(tagName);
                finish(pmesh);
            });
        }
    }
예제 #3
0
        public void GenerateBabylonFile(string file, string outputFile, bool skinned)
        {
            var text = File.ReadAllText(file);

            Environment.CurrentDirectory = Path.GetDirectoryName(file);

            // Scene
            var scene = new BabylonScene(Path.GetDirectoryName(outputFile));

            // Loading
            var objDocument = new Document <ObjLine>(text);

            materials.Clear();

            int currentProgression = 0;

            int total = objDocument.Blocks.Sum(lines => lines.Count);

            string           currentName     = "";
            StandardMaterial currentMaterial = null;

            foreach (var lines in objDocument.Blocks)
            {
                if (lines[0].Tokens[0] == lines[0].BlockSperator)
                {
                    AppendNewPart(currentName, currentMaterial);
                }
                foreach (ObjLine line in lines)
                {
                    currentProgression++;
                    ReportProgressChanged((currentProgression * 100) / total);

                    switch (line.Header)
                    {
                    case ObjHeader.Vertices:
                        positions.Add(line.ToVector3());
                        break;

                    case ObjHeader.TextureCoordinates:
                        Vector2 tv = line.ToVector2();
                        textureCoordinates.Add(tv);
                        break;

                    case ObjHeader.Normals:
                        normals.Add(line.ToVector3());
                        break;

                    case ObjHeader.Group:
                        currentName = line.Tokens[1];
                        break;

                    case ObjHeader.Faces:
                        AppendFace(line);
                        break;

                    case ObjHeader.MaterialLibrary:
                        ImportMaterialLibraries(line, scene);
                        break;

                    case ObjHeader.Material:
                        string materialName = line.Tokens[1];
                        currentMaterial = materials.FirstOrDefault(e => e.Name == materialName);
                        break;
                    }
                }
            }

            if (positions.Count > 0)
            {
                AppendNewPart(currentName, currentMaterial);
            }

            if (meshParts.Count > 1)
            {
                var proxyID = ProxyMesh.CreateBabylonMesh(currentName, scene);
                foreach (Mesh <PositionNormalTextured> part in meshParts.Values)
                {
                    part.CreateBabylonMesh(scene, proxyID);
                }
            }
            else
            {
                meshParts.Values.First().CreateBabylonMesh(scene);
            }

            // Output
            scene.Prepare();
            using (var outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
            {
                var ser = new DataContractJsonSerializer(typeof(BabylonScene));
                ser.WriteObject(outputStream, scene);
            }
        }