Пример #1
0
        static void LoadNodeRekursiv(Assimp.Node Node, Scene _Scene)
        {
            if (Node.HasMeshes)
            {
                for (int i = 0; i < Node.MeshIndices.Count; i++)
                {
                    _Scene.Meshes[Node.MeshIndices[i]].Transformation = AssimpConv.ConvertTransform(Node.Transform);
                }
            }

            for (int i = 0; i < Node.Children.Count; i++)
            {
                LoadNodeRekursiv(Node.Children[i], _Scene);
            }
        }
Пример #2
0
        /// <summary>
        /// Build a transformation matrix from rotation, scaling and translation components.
        /// The transformation order is scaling, rotation, translation (left to right).
        /// </summary>
        /// <param name="presentRotation"></param>
        /// <param name="presentScaling"></param>
        /// <param name="presentPosition"></param>
        /// <param name="outMatrix"></param>
        private static void BuildTransform(ref Quaternion presentRotation, ref Vector3D presentScaling,
                                           ref Vector3D presentPosition, out Matrix outMatrix)
        {
            // build a transformation matrix from it
            var mat = new Matrix4x4(presentRotation.GetMatrix());

            mat.A1 *= presentScaling.X;
            mat.B1 *= presentScaling.X;
            mat.C1 *= presentScaling.X;
            mat.A2 *= presentScaling.Y;
            mat.B2 *= presentScaling.Y;
            mat.C2 *= presentScaling.Y;
            mat.A3 *= presentScaling.Z;
            mat.B3 *= presentScaling.Z;
            mat.C3 *= presentScaling.Z;
            mat.A4  = presentPosition.X;
            mat.B4  = presentPosition.Y;
            mat.C4  = presentPosition.Z;

            outMatrix = AssimpConv.ConvertTransform(mat);
        }
Пример #3
0
        private NodeState CreateNodeTree(Assimp.Node rootNode, NodeState parent)
        {
            var outNode = new NodeState {
                LocalTransform = AssimpConv.ConvertTransform(rootNode.Transform)
            };

            outNode.Parent = parent;

            // calculate transforms
            outNode.GlobalTransform = parent != null ? parent.GlobalTransform * outNode.LocalTransform : outNode.LocalTransform;

            // populate by-name map to quickly map nodes to their state
            _nodeStateByName[rootNode.Name] = outNode;

            // find the index of the animation track affecting this node, if any
            outNode.ChannelIndex = -1;
            if (ActiveAnimation != -1)
            {
                var channels = _scene._Scene.Animations[ActiveAnimation].NodeAnimationChannels;
                for (int i = 0; i < channels.Count; ++i)
                {
                    if (channels[i].NodeName != rootNode.Name)
                    {
                        continue;
                    }
                    outNode.ChannelIndex = i;
                    break;
                }
            }

            outNode.Children = new NodeState[rootNode.ChildCount];

            // recursively add up children
            for (int i = 0; i < rootNode.ChildCount; ++i)
            {
                outNode.Children[i] = CreateNodeTree(rootNode.Children[i], outNode);
            }

            return(outNode);
        }
Пример #4
0
        /// <summary>
        /// internal.
        /// </summary>
        internal static Scene ConvertFromAssimp(Assimp.Scene _Scene)
        {
            Scene Result = new Scene();

            LoadTextures(_Scene);
            Result.CompileEnable = false;
            for (int i = 0; i < _Scene.Meshes.Count; i++)
            {
                D3DMesh _M = new D3DMesh();
                _M.CompileEnable = false;
                Assimp.Mesh M        = _Scene.Meshes[i];
                List <int>  _Indices = new List <int>(M.Faces.Count * 4);
                if (M.Faces.Count > 0)
                {
                    for (int j = 0; j < M.Faces.Count; j++)
                    {
                        if (M.Faces[j].Indices.Count > 4)
                        {
                            _Indices.Add(M.Faces[j].Indices[0]);
                            _Indices.Add(M.Faces[j].Indices[1]);
                            _Indices.Add(M.Faces[j].Indices[4]);
                            _Indices.Add(M.Faces[j].Indices[4]);
                            _Indices.Add(M.Faces[j].Indices[1]);
                            _Indices.Add(M.Faces[j].Indices[2]);
                            _Indices.Add(M.Faces[j].Indices[4]);
                            _Indices.Add(M.Faces[j].Indices[2]);
                            _Indices.Add(M.Faces[j].Indices[3]);
                        }
                        else
                        if (M.Faces[j].Indices.Count > 3)
                        {
                            _Indices.Add(M.Faces[j].Indices[0]);
                            _Indices.Add(M.Faces[j].Indices[1]);
                            _Indices.Add(M.Faces[j].Indices[3]);
                            _Indices.Add(M.Faces[j].Indices[3]);
                            _Indices.Add(M.Faces[j].Indices[1]);
                            _Indices.Add(M.Faces[j].Indices[2]);
                        }
                        else
                        {
                            _Indices.Add(M.Faces[j].Indices[0]);
                            _Indices.Add(M.Faces[j].Indices[1]);
                            _Indices.Add(M.Faces[j].Indices[2]);
                        }
                    }
                    _M.Indices = _Indices.ToArray();
                }

                Result.Meshes.Add(_M);
                //----------------Indices--------------------

                int[] Indices = M.GetIndices();

                _M.Material = AssimpConv.ConvertMaterial(_Scene.Materials[M.MaterialIndex]);
                _M.Texture  = LoadTexture(_Scene.Materials[M.MaterialIndex]);
                List <Vector3D> Vertices = M.Vertices;
                _M.Position = new xyzf[Vertices.Count];
                for (int j = 0; j < Vertices.Count; j++)
                {
                    Vector3D V = Vertices[j];
                    _M.Position[j] = new xyzf(V.X, V.Y, V.Z);
                }
                List <Vector3D> Normals = M.Normals;
                _M.Normals = new xyzf[Normals.Count];

                for (int j = 0; j < Normals.Count; j++)
                {
                    Vector3D V = Normals[j];
                    _M.Normals[j] = new xyzf(V.X, V.Y, V.Z).normalized();
                }
                if (M.TextureCoordinateChannelCount > 0)
                {
                    _M.TextureCoords = new xyf[M.TextureCoordinateChannels[0].Count];
                    for (int j = 0; j < M.TextureCoordinateChannels[0].Count; j++)
                    {
                        Vector3D V = M.TextureCoordinateChannels[0][j];
                        _M.TextureCoords[j] = new xyf(V.X, V.Y);
                    }
                }


                _M.Bones = new List <Bone>();
                for (int k = 0; k < M.BoneCount; k++)
                {
                    Assimp.Bone    B  = M.Bones[k];
                    VertexWeight[] VW = new VertexWeight[B.VertexWeights.Count];
                    for (int g = 0; g < VW.Length; g++)
                    {
                        VW[g] = new VertexWeight(B.VertexWeights[g].VertexID, B.VertexWeights[g].Weight);
                    }
                    Bone _B = new Bone(B.Name, AssimpConv.ConvertTransform(B.OffsetMatrix), VW);
                    _M.Bones.Add(_B);
                }
            }

            LoadNode(_Scene, Result);
            MoveTranslucentAtEnd(Result);
            Result._Scene        = _Scene;
            Result.SceneAnimator = new SceneAnimator(Result);
            return(Result);
        }