private string GetRelativeNodePath(int nodeTargetId, int nodeSourceId) { var targetNode = this.Nodes[nodeTargetId]; var sourceNode = this.Nodes[nodeSourceId]; return(EntityPathHelper.RelativePathFromEntityPath(targetNode.NodePath, sourceNode.NodePath)); }
private void FillNode(int nodeId, Entity[] nodeEntities, bool setMaterials) { var node = this.Nodes[nodeId]; var entityNode = nodeEntities[nodeId]; if (node.Mesh >= 0) { var meshContent = this.Meshes[node.Mesh]; // Gets all materials bool isSkinnedMesh = false; HashSet <int> materialIndices = new HashSet <int>(); foreach (var mesh in meshContent.MeshParts) { materialIndices.Add(mesh.MaterialIndex); if (mesh is SkinnedMesh) { isSkinnedMesh = true; } } // Add Material components foreach (var materialIndex in materialIndices) { string materialPath = null; var materialName = this.Materials[materialIndex]; if (setMaterials) { string assetDir = Path.GetDirectoryName(this.AssetPath); materialPath = Path.Combine(assetDir, "Materials", materialName + ".wmat").Replace('\\', '/'); } entityNode.AddComponent(new MaterialComponent() { MaterialPath = materialPath, AsignedTo = materialName }); } // Add the file mesh entityNode.AddComponent(new FileMesh() { ModelPath = this.AssetPath, ModelMeshName = meshContent.Name }); if (!isSkinnedMesh) { // Normal mesh entityNode.AddComponent(new MeshRenderer()); } else { // Skinned mesh var skinnedMeshRenderer = new SkinnedMeshRenderer(); // Sets morph weights skinnedMeshRenderer.MorphTargetWeights = node.MorphTargetWeights ?? meshContent.MorphTargetWeights ?? null; // Skin if (node.Skin >= 0) { var skin = this.Skins[node.Skin]; var rootJointPath = this.GetRelativeNodePath(skin.RootJoint, nodeId); var joints = new string[skin.Joints.Length]; for (int i = 0; i < joints.Length; i++) { var jointNode = this.Nodes[skin.Joints[i].NodeId]; var jointEntity = nodeEntities[skin.Joints[i].NodeId]; string jointNodePath = EntityPathHelper.RelativePathFromEntityPath(jointNode.NodePath, node.NodePath); joints[i] = this.GetRelativeNodePath(skin.Joints[i].NodeId, nodeId); } skinnedMeshRenderer.RootJointPath = rootJointPath; skinnedMeshRenderer.JointPaths = joints; } entityNode.AddComponent(skinnedMeshRenderer); } } }