Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="semantic"></param>
        /// <param name="values"></param>
        /// <param name="vertex"></param>
        /// <param name="vertices"></param>
        private void ProcessInput(Input_Semantic semantic, string source, int set, IOVertex vertex, Vertices vertices, int index, SourceManager srcs, List <IOEnvelope> vertexEnvelopes)
        {
            var values = srcs.GetFloatValue(source, index);

            switch (semantic)
            {
            case Input_Semantic.VERTEX:
                // process per vertex input
                foreach (var vertInput in vertices.Input)
                {
                    ProcessInput(vertInput.Semantic, vertInput.source, 0, vertex, vertices, index, srcs, vertexEnvelopes);
                }

                // load envelopes if availiable
                if (vertexEnvelopes != null && index < vertexEnvelopes.Count)
                {
                    // copy bone weights
                    var en = vertexEnvelopes[index];
                    for (int i = 0; i < en.Weights.Count; i++)
                    {
                        vertex.Envelope.Weights.Add(new IOBoneWeight()
                        {
                            BoneName   = en.Weights[i].BoneName,
                            Weight     = en.Weights[i].Weight,
                            BindMatrix = en.Weights[i].BindMatrix
                        });
                    }

                    // make the bind matrix as being used
                    vertex.Envelope.UseBindMatrix = true;
                }
                break;

            case Input_Semantic.POSITION:
                vertex.Position = new Vector3(
                    values.Length > 0 ? values[0] : 0,
                    values.Length > 1 ? values[1] : 0,
                    values.Length > 2 ? values[2] : 0);
                break;

            case Input_Semantic.NORMAL:
                vertex.Normal = new Vector3(
                    values.Length > 0 ? values[0] : 0,
                    values.Length > 1 ? values[1] : 0,
                    values.Length > 2 ? values[2] : 0);
                break;

            case Input_Semantic.TANGENT:
                vertex.Tangent = new Vector3(
                    values.Length > 0 ? values[0] : 0,
                    values.Length > 1 ? values[1] : 0,
                    values.Length > 2 ? values[2] : 0);
                break;

            case Input_Semantic.BINORMAL:
                vertex.Binormal = new Vector3(
                    values.Length > 0 ? values[0] : 0,
                    values.Length > 1 ? values[1] : 0,
                    values.Length > 2 ? values[2] : 0);
                break;

            case Input_Semantic.TEXCOORD:
                vertex.SetUV(
                    values.Length > 0 ? values[0] : 0,
                    values.Length > 1 ? values[1] : 0,
                    set);
                break;

            case Input_Semantic.COLOR:
                vertex.SetColor(
                    values.Length > 0 ? values[0] : 0,
                    values.Length > 1 ? values[1] : 0,
                    values.Length > 2 ? values[2] : 0,
                    values.Length > 3 ? values[3] : 0,
                    set);
                break;
            }
        }
Exemple #2
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);
        }