/// <summary> /// MeshFilter + MeshRenderer もしくは SkinnedMeshRenderer を構築する /// </summary> public static Renderer CreateRenderer(VrmLib.Node node, GameObject go, ModelMap map) { var mesh = node.MeshGroup.Meshes[0]; Renderer renderer = null; var hasBlendShape = mesh.MorphTargets.Any(); if (node.MeshGroup.Skin != null || hasBlendShape) { var skinnedMeshRenderer = go.AddComponent <SkinnedMeshRenderer>(); renderer = skinnedMeshRenderer; skinnedMeshRenderer.sharedMesh = map.Meshes[node.MeshGroup]; if (node.MeshGroup.Skin != null) { skinnedMeshRenderer.bones = node.MeshGroup.Skin.Joints.Select(x => map.Nodes[x].transform).ToArray(); if (node.MeshGroup.Skin.Root != null) { skinnedMeshRenderer.rootBone = map.Nodes[node.MeshGroup.Skin.Root].transform; } } } else { var meshFilter = go.AddComponent <MeshFilter>(); renderer = go.AddComponent <MeshRenderer>(); meshFilter.sharedMesh = map.Meshes[node.MeshGroup]; } var materials = mesh.Submeshes.Select(x => map.Materials[x.Material]).ToArray(); renderer.sharedMaterials = materials; return(renderer); }
private static void CreateNodes( Transform parentTransform, VrmLib.Node parentNode, Dictionary <GameObject, VrmLib.Node> nodes) { // parentNode.SetMatrix(parentTransform.localToWorldMatrix.ToNumericsMatrix4x4(), false); parentNode.LocalTranslation = parentTransform.localPosition.ToNumericsVector3(); parentNode.LocalRotation = parentTransform.localRotation.ToNumericsQuaternion(); parentNode.LocalScaling = parentTransform.localScale.ToNumericsVector3(); nodes.Add(parentTransform.gameObject, parentNode); foreach (Transform child in parentTransform) { var childNode = new VrmLib.Node(child.gameObject.name); CreateNodes(child, childNode, nodes); parentNode.Add(childNode); } }
/// <summary> /// ヒエラルキーを再帰的に構築する /// <summary> public static void CreateNodes(VrmLib.Node node, GameObject parent, Dictionary <VrmLib.Node, GameObject> nodes) { GameObject go = new GameObject(node.Name); go.transform.SetPositionAndRotation(node.Translation.ToUnityVector3(), node.Rotation.ToUnityQuaternion()); nodes.Add(node, go); if (parent != null) { go.transform.SetParent(parent.transform); } if (node.Children.Count > 0) { for (int n = 0; n < node.Children.Count; n++) { CreateNodes(node.Children[n], go, nodes); } } }