示例#1
0
        //private async void Read3Dxml(string fileName)
        //{
        //    Model3Dxml model = await Read3dxml.Read3Dxml(fileName);
        //}


        //private async void writetoglTF(string fileName)
        //{
        //    Model3Dxml model = await Read3dxml.Read3Dxml(fileName);



        //    ToVertex modeldata = new ToVertex(model.ProductStructure.Instance3DList, model.ProductStructure.ReferenceRepList);

        //    modeldata.getprimitivelist();
        //    ThreedxmlTogltf changefile = new ThreedxmlTogltf();
        //    changefile.getfromprimitivedata(modeldata.allvertexindex, modeldata.allvertexpostion, model.Header.Title);
        //    GltfModel _model = changefile.writeToGLtf();
        //    var json = _model.ToJson(_model);

        //    //  文件保存路径

        //    var file = @"C:\Users\gxh-PC\Desktop\001.gltf";    // 保存的文件路径

        //    File.WriteAllText(file, json);

        //}

        private async void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sd = new SaveFileDialog();

            sd.Filter = "3D Object (*.gltf)|*.gltf";
            if (sd.ShowDialog() == DialogResult.OK)
            {
                label2.Text = sd.FileName;
            }

            Model3Dxml model = await Read3dxml.Read3Dxml(label1.Text);

            textBox1.AppendText("已读取3dxml文件数据!  \r\n\n");

            ToVertex modeldata = new ToVertex(model.ProductStructure.Instance3DList, model.ProductStructure.ReferenceRepList);

            modeldata.getprimitivelist();

            ThreedxmlTogltf changefile = new ThreedxmlTogltf();

            changefile.getfromprimitivedata(modeldata.allvertexindex, modeldata.allvertexpostion, model.Header.Title);

            GltfModel _model = changefile.writeToGLtf();
            var       json   = _model.ToJson(_model);

            //  文件保存路径

            //var file = @"C:\Users\gxh-PC\Desktop\001.gltf";    // 保存的文件路径
            var file = label2.Text;

            File.WriteAllText(file, json);
            textBox1.AppendText("写入gltf文档成功!  \r\n\n");
        }
示例#2
0
        public SharpGLTF.Schema2.Image ReadImage(string imagePath, GltfModel gltfModel, string imageName)
        {
            var image = gltfModel.gltf.CreateImage(imageName);

            image.SetSatelliteFile(imagePath);
            return(image);
        }
示例#3
0
        /// <summary>
        /// run converter
        /// </summary>
        public void Run()
        {
            if (_model == null)
            {
                _model = new GltfModel();
                //TODO:
                if (_objModel == null)
                {
                    using (_objParser)
                    {
                        _objModel = _objParser.GetModel();
                    }
                }
                _model.Scenes.Add(new Scene());
                var u32IndicesEnabled = RequiresUint32Indices(_objModel);
                var meshes            = _objModel.Geometries;
                var meshesLength      = meshes.Count;
                for (var i = 0; i < meshesLength; i++)
                {
                    var mesh      = meshes[i];
                    var meshIndex = AddMesh(_objModel, mesh, u32IndicesEnabled);
                    AddNode(mesh.Id, meshIndex, null);
                }

                if (_model.Images.Count > 0)
                {
                    _model.Samplers.Add(new Sampler
                    {
                        MagFilter = MagFilter.Linear,
                        MinFilter = MinFilter.NearestMipmapLinear,
                        WrapS     = WrappingMode.Repeat,
                        WrapT     = WrappingMode.Repeat
                    });
                }

                var allBuffers = AddBuffers(_options.Name);
                _model.Buffers.Add(new Gltf.Buffer
                {
                    Name       = _options.Name,
                    ByteLength = allBuffers.Count
                });
                var boundary = 4;
                FillImageBuffers(allBuffers, boundary);


                if (!_options.Binary)
                {
                    _model.Buffers[0].Uri = "data:application/octet-stream;base64," + Convert.ToBase64String(allBuffers.ToArray());
                }
                else
                {
                    _glb = GltfToGlb(allBuffers);
                }
                _model.Clean();
            }
        }
示例#4
0
        public IEnumerable <Bitmap> CreateTextures(GltfModel gltfModel)
        {
            List <Bitmap> channelImages = new List <Bitmap>();
            var           i             = 0;

            foreach (var item in gltfModel.gltf.LogicalImages)
            {
                var img = Bitmap.FromStream(item.OpenImageFile());
                img.Save("wwwwroot/Textures/Texture" + i + ".png", ImageFormat.Png);
                i++;
            }
            return(channelImages);
        }
        public IActionResult EditModel()
        {
            EditGltfService gltfService = new EditGltfService(_env);
            GltfModel       gltfModel   = new GltfModel();

            //model populaten met ingeladen gltf
            var Currentmodel = gltfService.PopulateGltfModel(Path.Combine(_env.WebRootPath, "Objects", "CurrentGlb.glb"));

            IndexViewModel indexViewModel = new IndexViewModel()
            {
                GltfModel = Currentmodel
            };


            return(View(indexViewModel));
        }
示例#6
0
        //Create GltfModel
        public GltfModel PopulateGltfModel(string objectAdres)
        {
            //Load Object
            GltfModel gltfModel = new GltfModel();

            gltfModel.gltf = SharpGLTF.Schema2.ModelRoot.Load(objectAdres);
            gltfModel.Name = Path.GetFileName(objectAdres);
            //Object property lists
            List <Meshes> meshesInGltf = new List <Meshes>();


            //Populate Models
            foreach (var item in gltfModel.gltf.LogicalMeshes)
            {
                Meshes mesh = new Meshes();
                mesh.Mesh = item;
                meshesInGltf.Add(mesh);
            }
            gltfModel.MeshesVerzameling = meshesInGltf;

            return(gltfModel);
        }
示例#7
0
 public void SaveCurrentGltf(GltfModel gltfmodel)
 {
     gltfmodel.gltf.SaveGLB(Path.Combine(_env.WebRootPath, "Objects", "CurrentGlb.glb"));
     gltfmodel.gltf.SaveGLTF(Path.Combine(_env.WebRootPath, "Objects", "Current.gltf"));
 }
示例#8
0
        public GltfModel writeToGLtf()
        {
            GltfModel _model = new GltfModel();

            #region "asset"
            _model.Asset.Generator = "3dxmlToGLTF";
            _model.Asset.Version   = "2.0";
            #endregion

            #region "scene" and "scenes"
            // 为 "scene" 添加 0
            _model.Scene = 0;
            // 为 "scenes" 添加 根节点 nodes
            Scene scene = new Scene();
            scene.Name = nameofscene;
            scene.Nodes.Add(0);
            _model.Scenes.Add(scene);
            #endregion

            #region "nodes"  有待添加
            Node node = new Node();
            node.Mesh = 0;
            _model.Nodes.Add(node);
            #endregion

            #region "meshes"
            Mesh      mesh      = new Mesh();
            Primitive primitive = new Primitive();
            var       atts      = new Dictionary <string, int>();
            atts.Add("POSITION", 1);
            //atts.Add("NORMAL", 2);
            primitive.Attributes = atts;
            primitive.Indices    = 0;
            primitive.Material   = 0;
            primitive.Mode       = Mode.Triangles;
            mesh.Primitives.Add(primitive);

            _model.Meshes.Add(mesh);
            #endregion

            #region "materials"
            Material             material             = new Material();
            PbrMetallicRoughness pbrMetallicRoughness = new PbrMetallicRoughness();
            pbrMetallicRoughness.BaseColorFactor = new double[] { 0.1, 0.1, 0.1, 1 };
            pbrMetallicRoughness.MetallicFactor  = 0.0;
            material.PbrMetallicRoughness        = pbrMetallicRoughness;
            material.DoubleSided = true;
            material.Name        = "color";
            material.AlphaMode   = AlphaMode.OPAQUE;
            _model.Materials.Add(material);
            #endregion

            #region "buffers"
            List <byte> bytes = new List <byte>();
            foreach (int e in this.indexes)
            {
                byte[] b = BitConverter.GetBytes(e);
                foreach (byte elementofb in b)
                {
                    bytes.Add(elementofb);
                }
            }
            //若索引为int,顶点位置为float,则不用去判断是否需要添加0字节以达到整除的目的,因为这种情况下必定整除
            foreach (float e in this.positions)
            {
                byte[] b = BitConverter.GetBytes(e);
                foreach (byte elementofb in b)
                {
                    bytes.Add(elementofb);
                }
            }

            byte[] byteflow = bytes.ToArray();

            Arctron.Gltf.Buffer buffer = new Arctron.Gltf.Buffer();
            buffer.ByteLength = byteflow.Length;
            buffer.Uri        = "data:application/octet-stream;base64," + Convert.ToBase64String(byteflow); //byte数组转化为Base64
            _model.Buffers.Add(buffer);
            #endregion

            #region  "bufferViews"
            //索引段
            BufferView bufferView0 = new BufferView();
            bufferView0.Buffer     = 0;
            bufferView0.ByteOffset = 0;
            bufferView0.ByteLength = indexes.Count * sizeof(int);
            bufferView0.Target     = 34963;
            //数据段
            BufferView bufferView1 = new BufferView();
            bufferView1.Buffer     = 0;
            bufferView1.ByteOffset = indexes.Count * sizeof(int);
            bufferView1.ByteLength = positions.Count * sizeof(float);
            bufferView1.Target     = 34962;
            //添加到gltf模型中
            _model.BufferViews.Add(bufferView0);
            _model.BufferViews.Add(bufferView1);
            #endregion

            #region "accessors"
            //索引段
            int index_num  = indexes.Count;
            int vertex_num = positions.Count / 3;

            Accessor accessor0 = new Accessor();
            accessor0.BufferView    = 0;
            accessor0.ByteOffset    = 0;
            accessor0.ComponentType = 5125;
            accessor0.Count         = index_num;
            accessor0.Type          = AccessorType.SCALAR;
            double[] min = new double[1] {
                0
            };
            accessor0.Min = min;
            double[] max = new double[1] {
                vertex_num - 1
            };
            accessor0.Max = max;
            //顶点位置
            Accessor accessor1 = new Accessor();
            accessor1.BufferView    = 1;
            accessor1.ByteOffset    = 0;
            accessor1.ComponentType = 5126;
            accessor1.Count         = vertex_num;
            accessor1.Type          = AccessorType.VEC3;
            accessor1.Min           = accessor1.GetMinVec(positions);
            accessor1.Max           = accessor1.GetMaxVec(positions);
            _model.Accessors.Add(accessor0);
            _model.Accessors.Add(accessor1);
            #endregion

            #region "textures"

            #endregion

            #region "images"

            #endregion

            #region "samplers"

            #endregion

            _model.Clean();



            return(_model);
        }