Example #1
0
        public DAE ToDAE(int Model)
        {
            if (Data.Models.Length == 0 || Data.Models.Length <= Model)
            {
                return(null);
            }
            var m = Data.Models[Model];
            var o = new DAE();

            o.Content.scene = new DAE.COLLADA._scene();
            o.Content.scene.instance_visual_scene     = new DAE.InstanceWithExtra();
            o.Content.scene.instance_visual_scene.url = "#ID1";
            o.Content.library_visual_scenes           = new DAE.library_visual_scenes();
            var scene    = new DAE.visual_scene("ID1");
            var rootnode = new DAE.node(m.Name);

            scene.node.Add(rootnode);
            o.Content.library_visual_scenes.visual_scene.Add(scene);
            o.Content.library_geometries = new DAE.library_geometries();
            o.Content.library_materials  = new DAE.library_materials();
            o.Content.library_effects    = new DAE.library_effects();
            if (Data.Textures != null && Data.Textures.Length > 0)
            {
                o.Content.library_images = new DAE.library_images();
            }
            int id = 2;
            int i  = 0;

            foreach (var matt in m.Materials)
            {
                var mat2 = new DAE.material();
                mat2.id   = "ID" + id++;
                mat2.name = matt.Name;
                var eff = new DAE.effect();
                eff.id = "ID" + id++;
                mat2.instance_effect                         = new DAE.instance_effect();
                mat2.instance_effect.url                     = "#" + eff.id;
                eff.profile_COMMON                           = new DAE.profile_COMMON();
                eff.profile_COMMON.technique                 = new DAE.profile_COMMON._technique();
                eff.profile_COMMON.technique.sid             = "COMMON";
                eff.profile_COMMON.technique.lambert         = new DAE.profile_COMMON._technique._lambert();
                eff.profile_COMMON.technique.lambert.diffuse = new DAE.common_color_or_texture_type();
                if (matt.Tex0 != null && matt.Tex0.TextureObject is ReferenceTexture)
                {
                    string texid = "ID" + id++;
                    o.Content.library_images.image.Add(new DAE.image()
                    {
                        id = texid, init_from = "Tex/" + ((ReferenceTexture)matt.Tex0.TextureObject).LinkedTextureName + ".png"
                    });

                    var param1 = new DAE.common_newparam_type()
                    {
                        sid = "ID" + id++, choice = new DAE.fx_surface_common()
                        {
                            type = DAE.fx_surface_type_enum._2D
                        }
                    };
                    ((DAE.fx_surface_common)param1.choice).init_from.Add(new DAE.fx_surface_init_from_common()
                    {
                        content = texid
                    });
                    eff.profile_COMMON.newparam = new List <DAE.common_newparam_type>();
                    eff.profile_COMMON.newparam.Add(param1);
                    eff.profile_COMMON.newparam.Add(new DAE.common_newparam_type()
                    {
                        sid = "ID" + id++, choice = new DAE.fx_sampler2D_common()
                        {
                            source = param1.sid
                        }
                    });
                    eff.profile_COMMON.technique.lambert.diffuse.texture = new DAE.common_color_or_texture_type._texture()
                    {
                        texture = eff.profile_COMMON.newparam[1].sid, texcoord = "UVSET0"
                    };
                }
                else
                {
                    eff.profile_COMMON.technique.lambert.diffuse.color         = new DAE.common_color_or_texture_type._color();
                    eff.profile_COMMON.technique.lambert.diffuse.color.content = Color.White;
                }
                o.Content.library_materials.material.Add(mat2);
                o.Content.library_effects.effect.Add(eff);
            }

            int ff = 0;

            //foreach (CMDL m in Models)
            {
                foreach (var vv in m.Shapes)
                {
                    var geometry = new DAE.geometry();
                    geometry.id = "ID" + id++;

                    Polygon p   = vv.GetVertexData(m);
                    var     mat = m.Materials[m.Meshes[ff].MaterialIndex];

                    geometry.mesh          = new DAE.mesh();
                    geometry.mesh.vertices = new DAE.vertices()
                    {
                        id = "ID" + id++
                    };
                    if (p.Vertex != null)
                    {
                        var src = new DAE.source()
                        {
                            id = "ID" + id++
                        };
                        src.float_array = new DAE.source._float_array()
                        {
                            id = "ID" + id++, count = (uint)p.Vertex.Length * 3
                        };
                        foreach (var v in p.Vertex)
                        {
                            src.float_array.content.Add(v.X);
                            src.float_array.content.Add(v.Y);
                            src.float_array.content.Add(v.Z);
                        }
                        src.technique_common                 = new DAE.source._technique_common();
                        src.technique_common.accessor        = new DAE.accessor();
                        src.technique_common.accessor.count  = (uint)p.Vertex.Length;
                        src.technique_common.accessor.source = "#" + src.float_array.id;
                        src.technique_common.accessor.stride = 3;
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "X", type = "float"
                        });
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "Y", type = "float"
                        });
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "Z", type = "float"
                        });
                        geometry.mesh.source.Add(src);
                        geometry.mesh.vertices.input.Add(new DAE.InputLocal()
                        {
                            semantic = "POSITION", source = "#" + src.id
                        });
                    }
                    if (p.Normals != null)
                    {
                        var src = new DAE.source()
                        {
                            id = "ID" + id++
                        };
                        src.float_array = new DAE.source._float_array()
                        {
                            id = "ID" + id++, count = (uint)p.Normals.Length * 3
                        };
                        foreach (var v in p.Normals)
                        {
                            src.float_array.content.Add(v.X);
                            src.float_array.content.Add(v.Y);
                            src.float_array.content.Add(v.Z);
                        }
                        src.technique_common                 = new DAE.source._technique_common();
                        src.technique_common.accessor        = new DAE.accessor();
                        src.technique_common.accessor.count  = (uint)p.Normals.Length;
                        src.technique_common.accessor.source = "#" + src.float_array.id;
                        src.technique_common.accessor.stride = 3;
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "X", type = "float"
                        });
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "Y", type = "float"
                        });
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "Z", type = "float"
                        });
                        geometry.mesh.source.Add(src);
                        geometry.mesh.vertices.input.Add(new DAE.InputLocal()
                        {
                            semantic = "NORMAL", source = "#" + src.id
                        });
                    }
                    DAE.source texcoordsrc = null;
                    if (p.TexCoords != null)
                    {
                        if (mat.NrActiveTextureCoordiators > 0 && mat.TextureCoordiators[0].MappingMethod == 0)
                        {
                            Vector2[] texc;
                            if (mat.TextureCoordiators[0].SourceCoordinate == 0)
                            {
                                texc = p.TexCoords.ToArray();
                            }
                            else if (mat.TextureCoordiators[0].SourceCoordinate == 1)
                            {
                                texc = p.TexCoords2.ToArray();
                            }
                            else
                            {
                                texc = p.TexCoords3.ToArray();
                            }

                            var src = texcoordsrc = new DAE.source()
                            {
                                id = "ID" + id++
                            };
                            src.float_array = new DAE.source._float_array()
                            {
                                id = "ID" + id++, count = (uint)texc.Length * 2
                            };
                            foreach (var v in texc)
                            {
                                Vector2 result = v * new Matrix34(mat.TextureCoordiators[0].Matrix);
                                src.float_array.content.Add(result.X);
                                src.float_array.content.Add(result.Y);
                            }
                            src.technique_common                 = new DAE.source._technique_common();
                            src.technique_common.accessor        = new DAE.accessor();
                            src.technique_common.accessor.count  = (uint)p.TexCoords.Length;
                            src.technique_common.accessor.source = "#" + src.float_array.id;
                            src.technique_common.accessor.stride = 2;
                            src.technique_common.accessor.param.Add(new DAE.param()
                            {
                                name = "S", type = "float"
                            });
                            src.technique_common.accessor.param.Add(new DAE.param()
                            {
                                name = "T", type = "float"
                            });
                            geometry.mesh.source.Add(src);
                        }
                    }
                    DAE.source colorsrc = null;
                    if (p.Colors != null)
                    {
                        var src = colorsrc = new DAE.source()
                        {
                            id = "ID" + id++
                        };
                        src.float_array = new DAE.source._float_array()
                        {
                            id = "ID" + id++, count = (uint)p.Colors.Length * 4
                        };
                        foreach (var v in p.Colors)
                        {
                            src.float_array.content.Add(v.R / 255f);
                            src.float_array.content.Add(v.G / 255f);
                            src.float_array.content.Add(v.B / 255f);
                            src.float_array.content.Add(v.A / 255f);
                        }
                        src.technique_common                 = new DAE.source._technique_common();
                        src.technique_common.accessor        = new DAE.accessor();
                        src.technique_common.accessor.count  = (uint)p.Colors.Length;
                        src.technique_common.accessor.source = "#" + src.float_array.id;
                        src.technique_common.accessor.stride = 4;
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "R", type = "float"
                        });
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "G", type = "float"
                        });
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "B", type = "float"
                        });
                        src.technique_common.accessor.param.Add(new DAE.param()
                        {
                            name = "A", type = "float"
                        });
                        geometry.mesh.source.Add(src);
                    }

                    foreach (var q in vv.PrimitiveSets[0].Primitives[0].IndexStreams)
                    {
                        Vector3[] defs = q.GetFaceData();

                        var tri = new DAE.triangles()
                        {
                            count = (uint)defs.Length, material = mat.Name
                        };
                        uint offs = 0;
                        tri.input.Add(new DAE.InputLocalOffset()
                        {
                            offset = offs++, semantic = "VERTEX", source = "#" + geometry.mesh.vertices.id
                        });
                        if (texcoordsrc != null)
                        {
                            tri.input.Add(new DAE.InputLocalOffset()
                            {
                                offset = offs++, semantic = "TEXCOORD", source = "#" + texcoordsrc.id
                            });
                        }
                        if (colorsrc != null)
                        {
                            tri.input.Add(new DAE.InputLocalOffset()
                            {
                                offset = offs++, semantic = "COLOR", source = "#" + colorsrc.id, set = 0
                            });
                        }
                        tri.p.Add(new DAE.p());
                        foreach (Vector3 d in defs)
                        {
                            tri.p[0].content.Add((ulong)d.X);
                            if (texcoordsrc != null)
                            {
                                tri.p[0].content.Add((ulong)d.X);
                            }
                            if (colorsrc != null)
                            {
                                tri.p[0].content.Add((ulong)d.X);
                            }
                            tri.p[0].content.Add((ulong)d.Y);
                            if (texcoordsrc != null)
                            {
                                tri.p[0].content.Add((ulong)d.Y);
                            }
                            if (colorsrc != null)
                            {
                                tri.p[0].content.Add((ulong)d.Y);
                            }
                            tri.p[0].content.Add((ulong)d.Z);
                            if (texcoordsrc != null)
                            {
                                tri.p[0].content.Add((ulong)d.Z);
                            }
                            if (colorsrc != null)
                            {
                                tri.p[0].content.Add((ulong)d.Z);
                            }
                        }
                        geometry.mesh.triangles.Add(tri);
                    }
                    o.Content.library_geometries.geometry.Add(geometry);
                    var instgem = new DAE.instance_geometry()
                    {
                        url = "#" + geometry.id
                    };
                    instgem.bind_material = new DAE.bind_material();
                    instgem.bind_material.technique_common = new DAE.bind_material._technique_common();
                    var instmat = new DAE.instance_material();
                    instmat.symbol = mat.Name;
                    instmat.target = "#" + o.Content.library_materials.material[(int)m.Meshes[ff].MaterialIndex].id;
                    instmat.bind_vertex_input.Add(new DAE.instance_material._bind_vertex_input()
                    {
                        semantic = "UVSET0", input_semantic = "TEXCOORD", input_set = 0
                    });
                    instgem.bind_material.technique_common.instance_material.Add(instmat);
                    rootnode.instance_geometry.Add(instgem);
                    ff++;
                }
            }
            return(o);
        }
Example #2
0
        public DAE ToDAE(CGFX Resource)
        {
            var m = this;
            var o = new DAE();
            o.Content.scene = new DAE.COLLADA._scene();
            o.Content.scene.instance_visual_scene = new DAE.InstanceWithExtra();
            o.Content.scene.instance_visual_scene.url = "#ID1";
            o.Content.library_visual_scenes = new DAE.library_visual_scenes();
            var scene = new DAE.visual_scene("ID1");
            var rootnode = new DAE.node(m.Name);
            scene.node.Add(rootnode);
            o.Content.library_visual_scenes.visual_scene.Add(scene);
            o.Content.library_geometries = new DAE.library_geometries();
            o.Content.library_materials = new DAE.library_materials();
            o.Content.library_effects = new DAE.library_effects();
            if (Resource.Data.Textures != null && Resource.Data.Textures.Length > 0) o.Content.library_images = new DAE.library_images();
            int id = 2;
            int i = 0;
            foreach (var matt in m.Materials)
            {
                var mat2 = new DAE.material();
                mat2.id = "ID" + id++;
                mat2.name = matt.Name;
                var eff = new DAE.effect();
                eff.id = "ID" + id++;
                mat2.instance_effect = new DAE.instance_effect();
                mat2.instance_effect.url = "#" + eff.id;
                eff.profile_COMMON = new DAE.profile_COMMON();
                eff.profile_COMMON.technique = new DAE.profile_COMMON._technique();
                eff.profile_COMMON.technique.sid = "COMMON";
                eff.profile_COMMON.technique.lambert = new DAE.profile_COMMON._technique._lambert();
                eff.profile_COMMON.technique.lambert.diffuse = new DAE.common_color_or_texture_type();
                if (matt.Tex0 != null && matt.Tex0.TextureObject is ReferenceTexture)
                {
                    string texid = "ID" + id++;
                    o.Content.library_images.image.Add(new DAE.image() { id = texid, init_from = "Tex/" + ((ReferenceTexture)matt.Tex0.TextureObject).LinkedTextureName + ".png" });

                    var param1 = new DAE.common_newparam_type() { sid = "ID" + id++, choice = new DAE.fx_surface_common() { type = DAE.fx_surface_type_enum._2D } };
                    ((DAE.fx_surface_common)param1.choice).init_from.Add(new DAE.fx_surface_init_from_common() { content = texid });
                    eff.profile_COMMON.newparam = new List<DAE.common_newparam_type>();
                    eff.profile_COMMON.newparam.Add(param1);
                    eff.profile_COMMON.newparam.Add(new DAE.common_newparam_type() { sid = "ID" + id++, choice = new DAE.fx_sampler2D_common() { source = param1.sid } });
                    eff.profile_COMMON.technique.lambert.diffuse.texture = new DAE.common_color_or_texture_type._texture() { texture = eff.profile_COMMON.newparam[1].sid, texcoord = "UVSET0" };
                }
                else
                {
                    eff.profile_COMMON.technique.lambert.diffuse.color = new DAE.common_color_or_texture_type._color();
                    eff.profile_COMMON.technique.lambert.diffuse.color.content = Color.White;
                }
                o.Content.library_materials.material.Add(mat2);
                o.Content.library_effects.effect.Add(eff);
            }

            int ff = 0;
            //foreach (CMDL m in Models)
            {
                foreach (var vv in m.Shapes)
                {
                    var geometry = new DAE.geometry();
                    geometry.id = "ID" + id++;

                    Polygon p = vv.GetVertexData(m);
                    var mat = m.Materials[m.Meshes[ff].MaterialIndex];

                    geometry.mesh = new DAE.mesh();
                    geometry.mesh.vertices = new DAE.vertices() { id = "ID" + id++ };
                    if (p.Vertex != null)
                    {
                        var src = new DAE.source() { id = "ID" + id++ };
                        src.float_array = new DAE.source._float_array() { id = "ID" + id++, count = (uint)p.Vertex.Length * 3 };
                        foreach (var v in p.Vertex)
                        {
                            src.float_array.content.Add(v.X);
                            src.float_array.content.Add(v.Y);
                            src.float_array.content.Add(v.Z);
                        }
                        src.technique_common = new DAE.source._technique_common();
                        src.technique_common.accessor = new DAE.accessor();
                        src.technique_common.accessor.count = (uint)p.Vertex.Length;
                        src.technique_common.accessor.source = "#" + src.float_array.id;
                        src.technique_common.accessor.stride = 3;
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "X", type = "float" });
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "Y", type = "float" });
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "Z", type = "float" });
                        geometry.mesh.source.Add(src);
                        geometry.mesh.vertices.input.Add(new DAE.InputLocal() { semantic = "POSITION", source = "#" + src.id });
                    }
                    if (p.Normals != null)
                    {
                        var src = new DAE.source() { id = "ID" + id++ };
                        src.float_array = new DAE.source._float_array() { id = "ID" + id++, count = (uint)p.Normals.Length * 3 };
                        foreach (var v in p.Normals)
                        {
                            src.float_array.content.Add(v.X);
                            src.float_array.content.Add(v.Y);
                            src.float_array.content.Add(v.Z);
                        }
                        src.technique_common = new DAE.source._technique_common();
                        src.technique_common.accessor = new DAE.accessor();
                        src.technique_common.accessor.count = (uint)p.Normals.Length;
                        src.technique_common.accessor.source = "#" + src.float_array.id;
                        src.technique_common.accessor.stride = 3;
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "X", type = "float" });
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "Y", type = "float" });
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "Z", type = "float" });
                        geometry.mesh.source.Add(src);
                        geometry.mesh.vertices.input.Add(new DAE.InputLocal() { semantic = "NORMAL", source = "#" + src.id });
                    }
                    DAE.source texcoordsrc = null;
                    if (p.TexCoords != null)
                    {
                        if (mat.NrActiveTextureCoordiators > 0 && mat.TextureCoordiators[0].MappingMethod == 0)
                        {
                            Vector2[] texc;
                            if (mat.TextureCoordiators[0].SourceCoordinate == 0)
                            {
                                texc = p.TexCoords.ToArray();
                            }
                            else if (mat.TextureCoordiators[0].SourceCoordinate == 1)
                            {
                                texc = p.TexCoords2.ToArray();
                            }
                            else
                            {
                                texc = p.TexCoords3.ToArray();
                            }

                            var src = texcoordsrc = new DAE.source() { id = "ID" + id++ };
                            src.float_array = new DAE.source._float_array() { id = "ID" + id++, count = (uint)texc.Length * 2 };
                            foreach (var v in texc)
                            {
                                Vector2 result = v * new Matrix34(mat.TextureCoordiators[0].Matrix);
                                src.float_array.content.Add(result.X);
                                src.float_array.content.Add(result.Y);
                            }
                            src.technique_common = new DAE.source._technique_common();
                            src.technique_common.accessor = new DAE.accessor();
                            src.technique_common.accessor.count = (uint)p.TexCoords.Length;
                            src.technique_common.accessor.source = "#" + src.float_array.id;
                            src.technique_common.accessor.stride = 2;
                            src.technique_common.accessor.param.Add(new DAE.param() { name = "S", type = "float" });
                            src.technique_common.accessor.param.Add(new DAE.param() { name = "T", type = "float" });
                            geometry.mesh.source.Add(src);
                        }
                    }
                    DAE.source colorsrc = null;
                    if (p.Colors != null)
                    {
                        var src = colorsrc = new DAE.source() { id = "ID" + id++ };
                        src.float_array = new DAE.source._float_array() { id = "ID" + id++, count = (uint)p.Colors.Length * 4 };
                        foreach (var v in p.Colors)
                        {
                            src.float_array.content.Add(v.R / 255f);
                            src.float_array.content.Add(v.G / 255f);
                            src.float_array.content.Add(v.B / 255f);
                            src.float_array.content.Add(v.A / 255f);
                        }
                        src.technique_common = new DAE.source._technique_common();
                        src.technique_common.accessor = new DAE.accessor();
                        src.technique_common.accessor.count = (uint)p.Colors.Length;
                        src.technique_common.accessor.source = "#" + src.float_array.id;
                        src.technique_common.accessor.stride = 4;
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "R", type = "float" });
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "G", type = "float" });
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "B", type = "float" });
                        src.technique_common.accessor.param.Add(new DAE.param() { name = "A", type = "float" });
                        geometry.mesh.source.Add(src);
                    }

                    foreach (var q in vv.PrimitiveSets[0].Primitives[0].IndexStreams)
                    {
                        Vector3[] defs = q.GetFaceData();

                        var tri = new DAE.triangles() { count = (uint)defs.Length, material = mat.Name };
                        uint offs = 0;
                        tri.input.Add(new DAE.InputLocalOffset() { offset = offs++, semantic = "VERTEX", source = "#" + geometry.mesh.vertices.id });
                        if (texcoordsrc != null) tri.input.Add(new DAE.InputLocalOffset() { offset = offs++, semantic = "TEXCOORD", source = "#" + texcoordsrc.id });
                        if (colorsrc != null) tri.input.Add(new DAE.InputLocalOffset() { offset = offs++, semantic = "COLOR", source = "#" + colorsrc.id, set = 0 });
                        tri.p.Add(new DAE.p());
                        foreach (Vector3 d in defs)
                        {
                            tri.p[0].content.Add((ulong)d.X);
                            if (texcoordsrc != null) tri.p[0].content.Add((ulong)d.X);
                            if (colorsrc != null) tri.p[0].content.Add((ulong)d.X);
                            tri.p[0].content.Add((ulong)d.Y);
                            if (texcoordsrc != null) tri.p[0].content.Add((ulong)d.Y);
                            if (colorsrc != null) tri.p[0].content.Add((ulong)d.Y);
                            tri.p[0].content.Add((ulong)d.Z);
                            if (texcoordsrc != null) tri.p[0].content.Add((ulong)d.Z);
                            if (colorsrc != null) tri.p[0].content.Add((ulong)d.Z);
                        }
                        geometry.mesh.triangles.Add(tri);
                    }
                    o.Content.library_geometries.geometry.Add(geometry);
                    var instgem = new DAE.instance_geometry() { url = "#" + geometry.id };
                    instgem.bind_material = new DAE.bind_material();
                    instgem.bind_material.technique_common = new DAE.bind_material._technique_common();
                    var instmat = new DAE.instance_material();
                    instmat.symbol = mat.Name;
                    instmat.target = "#" + o.Content.library_materials.material[(int)m.Meshes[ff].MaterialIndex].id;
                    instmat.bind_vertex_input.Add(new DAE.instance_material._bind_vertex_input() { semantic = "UVSET0", input_semantic = "TEXCOORD", input_set = 0 });
                    instgem.bind_material.technique_common.instance_material.Add(instmat);
                    rootnode.instance_geometry.Add(instgem);
                    ff++;
                }
            }
            return o;
        }