private void BuildSkeletonNodes(Node node, List <string> boneNames, STSkeleton skeleton, ref Matrix4x4 rootTransform) { Matrix4x4 trafo = node.Transform; Matrix4x4 world = trafo * rootTransform; Matrix4 worldTK = AssimpHelper.TKMatrix(world); string Name = node.Name; string ParentArmatureName = node.Parent != null ? node.Parent.Name : ""; if (DaeHelper.IDMapToName.ContainsKey(node.Name)) { Name = DaeHelper.IDMapToName[node.Name]; } if (ParentArmatureName != string.Empty && DaeHelper.IDMapToName.ContainsKey(ParentArmatureName)) { ParentArmatureName = DaeHelper.IDMapToName[ParentArmatureName]; } bool IsBone = boneNames.Contains(Name) && !boneNames.Contains(ParentArmatureName) || Name.Contains("Skl_Root") || Name.Contains("nw4f_root") || Name.Contains("skl_root") || Name.Contains("all_root") || Name.Contains("_root") || Name.Contains("Root"); if (DaeHelper.VisualSceneNodeTypes.ContainsKey(Name)) { if (DaeHelper.VisualSceneNodeTypes[Name] == "JOINT") { IsBone = true; } } //Root set saved by this tool //Get our root manually as it's a child to this bool IsRootSkeleton = Name == "skeleton_root"; short SmoothIndex = 0; short RigidIndex = -1; //Loop through all the bones. If the parent is not in the bone list, then it's Parent is the root if (IsBone) { var idenity = Matrix4x4.Identity; CreateByNode(node, skeleton, ParentArmatureName, SmoothIndex, RigidIndex, true, ref rootTransform); } else if (IsRootSkeleton && node.HasChildren) { var idenity = Matrix4x4.Identity; CreateByNode(node.Children[0], skeleton, ParentArmatureName, SmoothIndex, RigidIndex, true, ref world); } else { if (node.HasChildren) { foreach (Node child in node.Children) { BuildSkeletonNodes(child, boneNames, skeleton, ref world); } } } }
private void BuildNode(Node parent, ref Matrix4x4 rootTransform) { Matrix4x4 world = rootTransform; Matrix4 worldTK = Matrix4.Identity; if (UseTransformMatrix) { Matrix4x4 trafo = parent.Transform; world = trafo * rootTransform; worldTK = AssimpHelper.TKMatrix(world); } if (parent.MeshCount > 0) { STConsole.WriteLine($"Use Transform Matrix {UseTransformMatrix}"); STConsole.WriteLine($"Transform node {parent.Name}"); STConsole.WriteLine($"Translation {worldTK.ExtractTranslation()}"); STConsole.WriteLine($"Rotation {worldTK.ExtractRotation()}"); STConsole.WriteLine($"Scale {worldTK.ExtractScale()}"); } foreach (int index in parent.MeshIndices) { objects.Add(CreateGenericObject(parent, scene.Meshes[index], index, worldTK)); } foreach (Node child in parent.Children) { BuildNode(child, ref world); } }
private void SaveSkeleton(STSkeleton skeleton, Node parentNode) { Node root = new Node("skeleton_root"); parentNode.Children.Add(root); Console.WriteLine($"bones {skeleton.bones.Count}"); if (skeleton.bones.Count > 0) { foreach (var bone in skeleton.bones) { //Get each root bone and find children if (bone.parentIndex == -1) { Node boneNode = new Node(bone.Text); boneNode.Transform = AssimpHelper.GetBoneMatrix(bone); root.Children.Add(boneNode); foreach (STBone child in bone.GetChildren()) { SaveBones(boneNode, child, skeleton); } } } } }
public void LoadScene() { objects.Clear(); materials.Clear(); BoneNames.Clear(); skeleton = new STSkeleton(); processNode(); if (scene.RootNode != null) { var rootTransform = scene.RootNode.Transform; Matrix4 transformMat = AssimpHelper.TKMatrix(rootTransform); var scale = transformMat.ExtractScale(); var rotation = transformMat.ExtractRotation(); var position = transformMat.ExtractTranslation(); STConsole.WriteLine($"-".Repeat(30)); STConsole.WriteLine($"rootTransform {transformMat}"); STConsole.WriteLine($"scale {scale}"); STConsole.WriteLine($"rotation {rotation}"); STConsole.WriteLine($"position {position}"); STConsole.WriteLine($"-".Repeat(30)); var SklRoot = GetSklRoot(scene.RootNode, BoneNames); if (SklRoot != null) { BuildSkeletonNodes(SklRoot, BoneNames, skeleton, ref rootTransform); } else { BuildSkeletonNodes(scene.RootNode, BoneNames, skeleton, ref rootTransform); } skeleton.update(); skeleton.reset(); } if (scene.HasMaterials) { foreach (Material mat in scene.Materials) { materials.Add(CreateGenericMaterial(mat)); } } foreach (Assimp.Animation animation in scene.Animations) { } foreach (var tex in scene.Textures) { } }
private void SaveBones(Node parentBone, STBone bone, STSkeleton skeleton) { Node boneNode = new Node(bone.Text); parentBone.Children.Add(boneNode); boneNode.Transform = AssimpHelper.GetBoneMatrix(bone); foreach (STBone child in bone.GetChildren()) { SaveBones(boneNode, child, skeleton); } }
private Matrix4 FromAssimpMatrix(Matrix4x4 mat) { Vector3D scaling; Vector3D tranlation; Assimp.Quaternion rot; mat.Decompose(out scaling, out rot, out tranlation); Console.WriteLine($"rotQ " + rot); Matrix4 positionMat = Matrix4.CreateTranslation(AssimpHelper.FromVector(tranlation)); Matrix4 rotQ = Matrix4.CreateFromQuaternion(AssimpHelper.TKQuaternion(rot)); Matrix4 scaleMat = Matrix4.CreateScale(AssimpHelper.FromVector(scaling)); Matrix4 matrixFinal = scaleMat * rotQ * positionMat; return(matrixFinal); }
private void SaveSkeleton(STSkeleton skeleton, Node parentNode) { Node root = new Node("skeleton_root"); parentNode.Children.Add(root); if (skeleton.bones.Count > 0) { Node boneNode = new Node(skeleton.bones[0].Text); boneNode.Transform = AssimpHelper.GetBoneMatrix(skeleton.bones[0]); root.Children.Add(boneNode); foreach (STBone child in skeleton.bones[0].GetChildren()) { SaveBones(boneNode, child, skeleton); } } }
public List <Vertex> GetVertices(Mesh msh, Matrix4 transform, STGenericObject STobj) { Matrix4 NormalsTransform = Matrix4.CreateFromQuaternion(transform.ExtractRotation()); List <Vertex> vertices = new List <Vertex>(); for (int v = 0; v < msh.VertexCount; v++) { Vertex vert = new Vertex(); if (msh.HasVertices) { vert.pos = Vector3.TransformPosition(AssimpHelper.FromVector(msh.Vertices[v]), transform); } if (msh.HasNormals) { vert.nrm = Vector3.TransformNormal(AssimpHelper.FromVector(msh.Normals[v]), NormalsTransform); } if (msh.HasTextureCoords(0)) { vert.uv0 = new Vector2(msh.TextureCoordinateChannels[0][v].X, msh.TextureCoordinateChannels[0][v].Y); } if (msh.HasTextureCoords(1)) { vert.uv1 = new Vector2(msh.TextureCoordinateChannels[1][v].X, msh.TextureCoordinateChannels[1][v].Y); } if (msh.HasTextureCoords(2)) { vert.uv2 = new Vector2(msh.TextureCoordinateChannels[2][v].X, msh.TextureCoordinateChannels[2][v].Y); } if (msh.HasTangentBasis) { vert.tan = new Vector4(msh.Tangents[v].X, msh.Tangents[v].Y, msh.Tangents[v].Z, 1); } if (msh.HasVertexColors(0) && !isDae) { vert.col = new Vector4(msh.VertexColorChannels[0][v].R, msh.VertexColorChannels[0][v].G, msh.VertexColorChannels[0][v].B, msh.VertexColorChannels[0][v].A); } if (msh.HasVertexColors(1) && !isDae) { vert.col2 = new Vector4(msh.VertexColorChannels[1][v].R, msh.VertexColorChannels[1][v].G, msh.VertexColorChannels[1][v].B, msh.VertexColorChannels[1][v].A); } if (msh.HasTangentBasis) { vert.bitan = new Vector4(msh.BiTangents[v].X, msh.BiTangents[v].Y, msh.BiTangents[v].Z, 1); } vertices.Add(vert); Console.WriteLine($"{msh.Name} COLOR { vert.col}"); } if (msh.HasBones && msh.BoneCount > 1) { for (int i = 0; i < msh.BoneCount; i++) { Bone bn = msh.Bones[i]; if (bn.HasVertexWeights) { foreach (VertexWeight w in bn.VertexWeights) { if (DaeHelper.IDMapToName.ContainsKey(bn.Name)) { bn.Name = DaeHelper.IDMapToName[bn.Name]; } vertices[w.VertexID].boneWeights.Add(w.Weight); vertices[w.VertexID].boneNames.Add(bn.Name); if (!BoneNames.Contains(bn.Name)) { BoneNames.Add(bn.Name); } } } } } return(vertices); }
private void CreateByNode(Node node, STSkeleton skeleton, string ParentArmatureName, short SmoothIndex, short RigidIndex, bool IsRoot, ref Assimp.Matrix4x4 rootTransform) { Matrix4x4 trafo = node.Transform; Matrix4x4 world = trafo * rootTransform; var transformMat = AssimpHelper.TKMatrix(world); int matchedBoneIndex = skeleton.bones.FindIndex(item => item.Name == node.Name); if (matchedBoneIndex < 0) { tempBoneNodes.Add(node); STBone bone = new STBone(); bone.skeletonParent = skeleton; bone.RotationType = STBone.BoneRotationType.Euler; skeleton.bones.Add(bone); if (DaeHelper.IDMapToName.ContainsKey(node.Name)) { bone.Text = DaeHelper.IDMapToName[node.Name]; } else { bone.Text = node.Name; } bone.SmoothMatrixIndex = (short)skeleton.bones.IndexOf(bone); bone.RigidMatrixIndex = -1; //Todo calculate these if (IsRoot) { bone.parentIndex = -1; if (RotateSkeleton) { transformMat = AssimpHelper.TKMatrix(world * Matrix4x4.FromRotationX(MathHelper.DegreesToRadians(RotateSkeletonAmount))); } else { transformMat = AssimpHelper.TKMatrix(world); } } else { if (tempBoneNodes.Contains(node.Parent)) { bone.parentIndex = tempBoneNodes.IndexOf(node.Parent); } } var scale = transformMat.ExtractScale(); var rotation = transformMat.ExtractRotation(); var position = transformMat.ExtractTranslation(); STConsole.WriteLine($"-".Repeat(30)); STConsole.WriteLine($"Processing Bone {bone.Text}"); STConsole.WriteLine($"scale {scale}"); STConsole.WriteLine($"rotation {rotation}"); STConsole.WriteLine($"position {position}"); STConsole.WriteLine($"-".Repeat(30)); bone.FromTransform(transformMat); } else { STConsole.WriteLine($"Duplicate node name found for bone {node.Name}!", Color.Red); } var identity = Matrix4x4.Identity; foreach (Node child in node.Children) { CreateByNode(child, skeleton, ParentArmatureName, SmoothIndex, RigidIndex, false, ref identity); } }