Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="n"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public IOMesh LoadGeometryFromID(Node n, string id, List <IOEnvelope> vertexEnvelopes = null)
        {
            // sanitize
            id = ColladaHelper.SanitizeID(id);

            // find geometry by id
            var geom = _collada.Library_Geometries.Geometry.First(e => e.ID == id);

            // not found
            if (geom == null)
            {
                return(null);
            }

            // create new mesh
            IOMesh mesh = new IOMesh()
            {
                Name = n.Name
            };

            // create source manager helper
            SourceManager srcs = new SourceManager();

            if (geom.Mesh.Source != null)
            {
                foreach (var src in geom.Mesh.Source)
                {
                    srcs.AddSource(src);
                }
            }


            // load geomtry meshes
            if (geom.Mesh.Triangles != null)
            {
                foreach (var tri in geom.Mesh.Triangles)
                {
                    var stride = tri.Input.Max(e => e.Offset) + 1;
                    var poly   = new IOPolygon()
                    {
                        PrimitiveType = IOPrimitive.TRIANGLE,
                        MaterialName  = tri.Material
                    };

                    var p = tri.P.GetValues();

                    for (int i = 0; i < tri.Count * 3; i++)
                    {
                        IOVertex vertex = new IOVertex();

                        for (int j = 0; j < tri.Input.Length; j++)
                        {
                            var input = tri.Input[j];

                            var index = p[i * stride + input.Offset];

                            ProcessInput(input.Semantic, input.source, input.Set, vertex, geom.Mesh.Vertices, index, srcs, vertexEnvelopes);
                        }

                        poly.Indicies.Add(mesh.Vertices.Count);
                        mesh.Vertices.Add(vertex);
                    }

                    mesh.Polygons.Add(poly);
                }
            }

            //TODO: collada trifan

            //TODO: collada  tristrip

            //TODO: collada linestrip

            //TODO: collada polylist

            //TODO: collada polygon

            return(mesh);
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IOMaterial LoadMaterial(Material mat)
        {
            var effectURL = mat.Instance_Effect?.URL;

            if (effectURL == null)
            {
                return(null);
            }

            var effect = _collada.Library_Effects.Effect.ToList().Find(e => e.ID == ColladaHelper.SanitizeID(effectURL));

            IOMaterial material = new IOMaterial()
            {
                Name  = mat.ID,
                Label = mat.Name
            };

            if (effect != null && effect.Profile_COMMON != null && effect.Profile_COMMON.Length > 0)
            {
                var prof = effect.Profile_COMMON[0];

                var phong   = prof.Technique.Phong;
                var blinn   = prof.Technique.Blinn;
                var lambert = prof.Technique.Lambert;

                if (phong != null)
                {
                    if (phong.Transparency != null)
                    {
                        material.Alpha = phong.Transparency.Float.Value;
                    }

                    if (phong.Shininess != null)
                    {
                        material.Shininess = phong.Shininess.Float.Value;
                    }

                    if (phong.Diffuse != null)
                    {
                        if (ReadEffectColorType(prof, phong.Diffuse, out Vector4 color, out IOTexture texture))
                        {
                            material.DiffuseColor = color;
                        }

                        if (texture != null)
                        {
                            material.DiffuseMap = texture;
                        }
                    }

                    if (phong.Ambient != null)
                    {
                        if (ReadEffectColorType(prof, phong.Ambient, out Vector4 color, out IOTexture texture))
                        {
                            material.AmbientColor = color;
                        }

                        if (texture != null)
                        {
                            material.AmbientMap = texture;
                        }
                    }

                    if (phong.Specular != null)
                    {
                        if (ReadEffectColorType(prof, phong.Specular, out Vector4 color, out IOTexture texture))
                        {
                            material.SpecularColor = color;
                        }

                        if (texture != null)
                        {
                            material.SpecularMap = texture;
                        }
                    }

                    if (phong.Reflective != null)
                    {
                        if (ReadEffectColorType(prof, phong.Reflective, out Vector4 color, out IOTexture texture))
                        {
                            material.ReflectiveColor = color;
                        }

                        if (texture != null)
                        {
                            material.ReflectiveMap = texture;
                        }
                    }
                }


                if (lambert != null)
                {
                    if (lambert.Transparency != null)
                    {
                        material.Alpha = lambert.Transparency.Float.Value;
                    }

                    if (lambert.Diffuse != null)
                    {
                        if (ReadEffectColorType(prof, lambert.Diffuse, out Vector4 color, out IOTexture texture))
                        {
                            material.DiffuseColor = color;
                        }

                        if (texture != null)
                        {
                            material.DiffuseMap = texture;
                        }
                    }

                    if (lambert.Ambient != null)
                    {
                        if (ReadEffectColorType(prof, lambert.Ambient, out Vector4 color, out IOTexture texture))
                        {
                            material.AmbientColor = color;
                        }

                        if (texture != null)
                        {
                            material.AmbientMap = texture;
                        }
                    }

                    if (lambert.Reflective != null)
                    {
                        if (ReadEffectColorType(prof, lambert.Reflective, out Vector4 color, out IOTexture texture))
                        {
                            material.ReflectiveColor = color;
                        }

                        if (texture != null)
                        {
                            material.ReflectiveMap = texture;
                        }
                    }
                }


                if (blinn != null)
                {
                    if (blinn.Transparency != null)
                    {
                        material.Alpha = blinn.Transparency.Float.Value;
                    }

                    if (blinn.Shininess != null)
                    {
                        material.Shininess = blinn.Shininess.Float.Value;
                    }

                    if (blinn.Diffuse != null)
                    {
                        if (ReadEffectColorType(prof, blinn.Diffuse, out Vector4 color, out IOTexture texture))
                        {
                            material.DiffuseColor = color;
                        }

                        if (texture != null)
                        {
                            material.DiffuseMap = texture;
                        }
                    }

                    if (blinn.Ambient != null)
                    {
                        if (ReadEffectColorType(prof, blinn.Ambient, out Vector4 color, out IOTexture texture))
                        {
                            material.AmbientColor = color;
                        }

                        if (texture != null)
                        {
                            material.AmbientMap = texture;
                        }
                    }

                    if (blinn.Specular != null)
                    {
                        if (ReadEffectColorType(prof, blinn.Specular, out Vector4 color, out IOTexture texture))
                        {
                            material.SpecularColor = color;
                        }

                        if (texture != null)
                        {
                            material.SpecularMap = texture;
                        }
                    }

                    if (blinn.Reflective != null)
                    {
                        if (ReadEffectColorType(prof, blinn.Reflective, out Vector4 color, out IOTexture texture))
                        {
                            material.ReflectiveColor = color;
                        }

                        if (texture != null)
                        {
                            material.ReflectiveMap = texture;
                        }
                    }
                }
            }

            return(material);
        }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="n"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public IOMesh LoadGeometryControllerFromID(Node n, string id)
        {
            // sanitize
            id = ColladaHelper.SanitizeID(id);

            // find geometry by id
            var con = _collada.Library_Controllers.Controller.FirstOrDefault(e => e.ID == id);

            // not found
            if (con == null)
            {
                return(null);
            }

            // load controllers

            SourceManager srcs = new SourceManager();

            foreach (var src in con.Skin.Source)
            {
                srcs.AddSource(src);
            }

            var v           = con.Skin.Vertex_Weights.V.GetValues();
            var counts      = con.Skin.Vertex_Weights.VCount.GetValues();
            var vi          = 0;
            var vertexIndex = 0;

            List <IOEnvelope> envelopes = new List <IOEnvelope>();

            for (int i = 0; i < con.Skin.Vertex_Weights.Count; i++)
            {
                var en = new IOEnvelope();

                var stride = con.Skin.Vertex_Weights.Input.Length;

                for (int j = 0; j < counts[i]; j++)
                {
                    IOBoneWeight bw = new IOBoneWeight();
                    foreach (var input in con.Skin.Vertex_Weights.Input)
                    {
                        var index = v[vi + input.Offset + j * stride];
                        switch (input.Semantic)
                        {
                        case Input_Semantic.JOINT:
                            foreach (var jointInput in con.Skin.Joints.Input)
                            {
                                switch (jointInput.Semantic)
                                {
                                case Input_Semantic.JOINT:
                                    bw.BoneName = srcs.GetNameValue(jointInput.source, index)[0];
                                    break;

                                case Input_Semantic.INV_BIND_MATRIX:
                                    var m = srcs.GetFloatValue(jointInput.source, index);
                                    var t = new Matrix4x4(
                                        m[0], m[4], m[8], m[12],
                                        m[1], m[5], m[9], m[13],
                                        m[2], m[6], m[10], m[14],
                                        m[3], m[7], m[11], m[15]);
                                    bw.BindMatrix = t;
                                    break;
                                }
                            }
                            break;

                        case Input_Semantic.WEIGHT:
                            bw.Weight = srcs.GetFloatValue(input.source, index)[0];
                            break;
                        }
                    }
                    en.Weights.Add(bw);
                }

                envelopes.Add(en);

                vi += counts[i] * stride;
                vertexIndex++;
            }

            // load geometry
            var geom = string.IsNullOrEmpty(con.Skin.sourceid) ? LoadGeometryFromID(n, con.Skin.sID, envelopes) : LoadGeometryFromID(n, con.Skin.sourceid, envelopes);


            // bind shape
            if (con.Skin.Bind_Shape_Matrix != null)
            {
                var m = con.Skin.Bind_Shape_Matrix.GetValues();
                var t = new Matrix4x4(
                    m[0], m[4], m[8], m[12],
                    m[1], m[5], m[9], m[13],
                    m[2], m[6], m[10], m[14],
                    m[3], m[7], m[11], m[15]);
                geom.TransformVertices(t);
            }

            return(geom);
        }