Пример #1
0
        public static void ExportCollada(CmpFile cmp, ResourceManager resources, string output)
        {
            //Build tree
            InputModel        rootModel    = null;
            List <InputModel> parentModels = new List <InputModel>();

            foreach (var p in cmp.Parts)
            {
                if (p.Construct == null)
                {
                    rootModel = new InputModel()
                    {
                        Transform = Matrix4x4.Identity,
                        Model     = p.Model,
                        Con       = "Root"
                    };
                    break;
                }
            }

            parentModels.Add(rootModel);
            var q = new Queue <Part>(cmp.Parts);
            int infiniteDetect = 0;

            while (q.Count > 0)
            {
                var part = q.Dequeue();
                if (part.Construct == null)
                {
                    continue;
                }

                bool enqueue = true;
                foreach (var mdl in parentModels)
                {
                    if (part.Construct.ParentName == mdl.Con)
                    {
                        var child = new InputModel()
                        {
                            Transform = part.Construct.Rotation * Matrix4x4.CreateTranslation(part.Construct.Origin),
                            Model     = part.Model,
                            Con       = part.Construct.ChildName
                        };
                        mdl.Children.Add(child);
                        parentModels.Add(child);
                        enqueue = false;
                        break;
                    }
                }

                if (enqueue)
                {
                    q.Enqueue(part);
                }
                infiniteDetect++;
                if (infiniteDetect > 200000000)
                {
                    throw new Exception("Infinite cmp loop detected");
                }
            }

            //Build collada
            var dae    = NewCollada();
            var efx    = new CL.library_effects();
            var mats   = new CL.library_materials();
            var geos   = new CL.library_geometries();
            var scenes = new CL.library_visual_scenes();
            var vscene = new CL.visual_scene();

            vscene.name                     = vscene.id = "main-scene";
            scenes.visual_scene             = new CL.visual_scene[] { vscene };
            dae.scene                       = new CL.COLLADAScene();
            dae.scene.instance_visual_scene = new CL.InstanceWithExtra()
            {
                url = "#main-scene"
            };
            var glist   = new List <CL.geometry>();
            var mlist   = new List <string>();
            var matlist = new List <ExportMaterial>();

            BuildModel(resources, rootModel, glist, mlist, matlist);
            geos.geometry = glist.ToArray();
            mats.material = mlist.Select((x) => new CL.material()
            {
                name            = x,
                id              = x + "-material",
                instance_effect = new CL.instance_effect()
                {
                    url = "#" + x + "-effect"
                }
            }).ToArray();
            efx.effect = matlist.Select((x) => new CL.effect()
            {
                id    = x.Name + "-effect",
                Items = new[]
                {
                    new CL.effectFx_profile_abstractProfile_COMMON()
                    {
                        technique = new CL.effectFx_profile_abstractProfile_COMMONTechnique()
                        {
                            id   = "common",
                            sid  = "common",
                            Item = new CL.effectFx_profile_abstractProfile_COMMONTechniquePhong()
                            {
                                ambient             = ColladaColor("ambient", Color4.Black),
                                emission            = ColladaColor("emmision", Color4.Black),
                                diffuse             = ColladaColor("diffuse", x.Dc),
                                specular            = ColladaColor("specular", new Color4(0.25f, 0.25f, 0.25f, 1f)),
                                shininess           = ColladaFloat("shininess", 50),
                                index_of_refraction = ColladaFloat("index_of_refraction", 1)
                            }
                        }
                    }
                }
            }).ToArray();
            var rootNodes = new List <CL.node>();

            BuildNodes(rootModel, rootNodes);
            vscene.node = rootNodes.ToArray();
            dae.Items   = new object[] { efx, mats, geos, scenes };
            using (var stream = File.Create(output))
                _xml.Serialize(stream, dae);
        }
Пример #2
0
        public static void ExportCollada(ModelFile mdl, ResourceManager resources, string output)
        {
            var dae    = NewCollada();
            var mats   = new CL.library_materials();
            var efx    = new CL.library_effects();
            var geos   = new CL.library_geometries();
            var scenes = new CL.library_visual_scenes();
            var vscene = new CL.visual_scene();

            vscene.name                     = vscene.id = "main-scene";
            scenes.visual_scene             = new CL.visual_scene[] { vscene };
            dae.scene                       = new CL.COLLADAScene();
            dae.scene.instance_visual_scene = new CL.InstanceWithExtra()
            {
                url = "#main-scene"
            };
            var exported = ProcessModel(mdl, resources);

            geos.geometry = exported.Geometries.ToArray();
            mats.material = exported.Materials.Select((x) => new CL.material()
            {
                name            = x.Name,
                id              = x.Name + "-material",
                instance_effect = new CL.instance_effect()
                {
                    url = "#" + x.Name + "-effect"
                }
            }).ToArray();
            efx.effect = exported.Materials.Select((x) => new CL.effect()
            {
                id    = x.Name + "-effect",
                Items = new[]
                {
                    new CL.effectFx_profile_abstractProfile_COMMON()
                    {
                        technique = new CL.effectFx_profile_abstractProfile_COMMONTechnique()
                        {
                            id   = "common",
                            sid  = "common",
                            Item = new CL.effectFx_profile_abstractProfile_COMMONTechniquePhong()
                            {
                                ambient             = ColladaColor("ambient", Color4.Black),
                                emission            = ColladaColor("emmision", Color4.Black),
                                diffuse             = ColladaColor("diffuse", x.Dc),
                                specular            = ColladaColor("specular", new Color4(0.25f, 0.25f, 0.25f, 1f)),
                                shininess           = ColladaFloat("shininess", 50),
                                index_of_refraction = ColladaFloat("index_of_refraction", 1)
                            }
                        }
                    }
                }
            }).ToArray();
            var nodes = new List <CL.node>();

            for (int i = 0; i < exported.Geometries.Count; i++)
            {
                nodes.Add(exported.GetNode(i, Matrix4x4.Identity, mdl.Path));
            }

            vscene.node = nodes.ToArray();
            dae.Items   = new object[] { efx, mats, geos, scenes };
            using (var stream = File.Create(output))
                _xml.Serialize(stream, dae);
        }