public static Bone2DWeights CalculateBoneWeights(Transform owner, Bone[] bones, Mesh mesh) { if (bones != null) { Bone2DWeights boneWeights = new Bone2DWeights(); boneWeights.weights = new Bone2DWeight[] { }; int vIndex = 0; foreach (Vector3 v in mesh.vertices) { Bone closest = FindClosestBone(v, bones); if (closest != null) { boneWeights.SetWeight(vIndex++, closest.name, Array.IndexOf(bones, closest), 1f); } } BoneWeight[] unitweights = boneWeights.GetUnityBoneWeights(); mesh.boneWeights = unitweights; Transform[] bonesArr = bones.Select(b => b.transform).ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bonesArr.Length]; for (int i = 0; i < bonesArr.Length; i++) { bindPoses[i] = bonesArr[i].worldToLocalMatrix * owner.localToWorldMatrix; } mesh.bindposes = bindPoses; return boneWeights; } return null; }
public void BuildMesh() { if (PrefabUtility.GetPrefabType(gameObject) == PrefabType.None && sprite != null && root != null) { SkinnedMeshRenderer skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>(); CleanUp(); Mesh mesh = SpriteMesh.CreateSpriteMeshPoly(sprite); mesh.name = "Generated Mesh"; Bone[] bones = root.GetComponentsInChildren<Bone>(); boneWeights = SpriteMesh.CalculateBoneWeights(transform, bones, mesh); skinnedMeshRenderer.rootBone = root.transform; skinnedMeshRenderer.bones = bones.Select(b => b.transform).ToArray(); skinnedMeshRenderer.sharedMesh = mesh; if (material != null) { skinnedMeshRenderer.sharedMaterial = material; } else { Material mat = new Material(Shader.Find("Sprites/Default")); mat.mainTexture = sprite.texture; skinnedMeshRenderer.sharedMaterial = mat; } AssetDatabase.SaveAssets(); } }
public void BuildMesh() { if (PrefabUtility.GetPrefabType(gameObject) == PrefabType.None && sprite != null && root != null) { SkinnedMeshRenderer skinnedMeshRenderer = GetComponent <SkinnedMeshRenderer>(); CleanUp(); Mesh mesh = SpriteMesh.CreateSpriteMeshPoly(sprite); mesh.name = "Generated Mesh"; Bone[] bones = root.GetComponentsInChildren <Bone>(); boneWeights = SpriteMesh.CalculateBoneWeights(transform, bones, mesh); skinnedMeshRenderer.rootBone = root.transform; skinnedMeshRenderer.bones = bones.Select(b => b.transform).ToArray(); skinnedMeshRenderer.sharedMesh = mesh; if (material != null) { skinnedMeshRenderer.sharedMaterial = material; } else { Material mat = new Material(Shader.Find("Sprites/Default")); mat.mainTexture = sprite.texture; skinnedMeshRenderer.sharedMaterial = mat; } AssetDatabase.SaveAssets(); } }
public void CalculateBoneWeights(Bone[] bones) { if (MeshFilter.sharedMesh == null) { Debug.Log("No Shared Mesh."); return; } Mesh mesh = new Mesh(); mesh.name = "Generated Mesh"; mesh.vertices = MeshFilter.sharedMesh.vertices; mesh.triangles = MeshFilter.sharedMesh.triangles; mesh.normals = MeshFilter.sharedMesh.normals; mesh.uv = MeshFilter.sharedMesh.uv; mesh.uv2 = MeshFilter.sharedMesh.uv2; mesh.bounds = MeshFilter.sharedMesh.bounds; if (bones != null && mesh != null) { boneWeights = new Bone2DWeights(); boneWeights.weights = new Bone2DWeight[] { }; int index = 0; foreach (Bone bone in bones) { int i = 0; foreach (Vector3 v in mesh.vertices) { float influence = bone.GetInfluence(v + transform.position); boneWeights.SetWeight(i, bone.name, index, influence); i++; } index++; } BoneWeight[] unitweights = boneWeights.GetUnityBoneWeights(); mesh.boneWeights = unitweights; Transform[] bonesArr = bones.Select(b => b.transform).ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bonesArr.Length]; for (int i = 0; i < bonesArr.Length; i++) { bindPoses[i] = bonesArr[i].worldToLocalMatrix * transform.localToWorldMatrix; } mesh.bindposes = bindPoses; var renderer = GetComponent <SkinnedMeshRenderer>(); if (renderer.sharedMesh != null && !AssetDatabase.Contains(renderer.sharedMesh.GetInstanceID())) { Object.DestroyImmediate(renderer.sharedMesh); } renderer.bones = bonesArr; renderer.sharedMesh = mesh; } }
public static Bone2DWeights CalculateBoneWeights(Transform owner, Bone[] bones, Mesh mesh) { if (bones != null) { Bone2DWeights boneWeights = new Bone2DWeights(); boneWeights.weights = new Bone2DWeight[] { }; int vIndex = 0; foreach (Vector3 v in mesh.vertices) { Bone closest = FindClosestBone(v, bones); if (closest != null) { boneWeights.SetWeight(vIndex++, closest.name, Array.IndexOf(bones, closest), 1f); } } BoneWeight[] unitweights = boneWeights.GetUnityBoneWeights(); mesh.boneWeights = unitweights; Transform[] bonesArr = bones.Select(b => b.transform).ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bonesArr.Length]; for (int i = 0; i < bonesArr.Length; i++) { bindPoses[i] = bonesArr[i].worldToLocalMatrix * owner.localToWorldMatrix; } mesh.bindposes = bindPoses; return(boneWeights); } return(null); }
public void CalculateBoneWeights(Bone[] bones, bool weightToParent) { if(MeshFilter.sharedMesh == null) { Debug.Log("No Shared Mesh."); return; } Mesh mesh = new Mesh(); mesh.name = "Generated Mesh"; mesh.vertices = MeshFilter.sharedMesh.vertices; mesh.triangles = MeshFilter.sharedMesh.triangles; mesh.normals = MeshFilter.sharedMesh.normals; mesh.uv = MeshFilter.sharedMesh.uv; mesh.uv2 = MeshFilter.sharedMesh.uv2; mesh.bounds = MeshFilter.sharedMesh.bounds; if (bones != null) { boneWeights = new Bone2DWeights(); boneWeights.weights = new Bone2DWeight[] { }; int index = 0; foreach (Bone bone in bones) { int i=0; foreach (Vector3 v in mesh.vertices) { float influence; if (!weightToParent || bone.transform != transform.parent) { influence = bone.GetInfluence(v + transform.position); } else { influence = 1.0f; } boneWeights.SetWeight(i, bone.name, index, influence); i++; } index++; } BoneWeight[] unitweights = boneWeights.GetUnityBoneWeights(); mesh.boneWeights = unitweights; Transform[] bonesArr = bones.Select(b => b.transform).ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bonesArr.Length]; for (int i = 0; i < bonesArr.Length; i++) { bindPoses[i] = bonesArr[i].worldToLocalMatrix * transform.localToWorldMatrix; } mesh.bindposes = bindPoses; var skinRenderer = GetComponent<SkinnedMeshRenderer>(); if (skinRenderer.sharedMesh != null && !AssetDatabase.Contains(skinRenderer.sharedMesh.GetInstanceID())) Object.DestroyImmediate(skinRenderer.sharedMesh); skinRenderer.bones = bonesArr; skinRenderer.sharedMesh = mesh; } }
// Calculate the bone weights of this Skin2D and optionally weight to the parent bone public void CalculateBoneWeights(Bone[] bones, bool weightToParent) { if (!lockBoneWeights) { if (meshFilter.sharedMesh == null) { Debug.Log("No Shared Mesh."); return; } // Create a new mesh and copy over the sharedMesh from the MeshFilter Mesh mesh = meshFilter.sharedMesh.Clone(); mesh.name = meshFilter.sharedMesh.name; mesh.name = mesh.name.Replace(".Mesh", ".SkinnedMesh"); if (bones != null && mesh != null) { boneWeights = new Bone2DWeights(); boneWeights.weights = new Bone2DWeight[] { }; int index = 0; foreach (Bone bone in bones) { int i = 0; // Save a reference to this bone if it is active or not bool boneActive = bone.gameObject.activeSelf; // Activate the bone so we can weight it properly bone.gameObject.SetActive(true); foreach (Vector3 v in mesh.vertices) { float influence; // If we are not weighting to the parent bone and the bone has a parent // Get the influence of the bone weight from the vertex and transform positions if (!weightToParent || weightToParent && bone.transform != transform.parent) { influence = bone.GetInfluence(v + transform.position); } else { // If we are weighting to the parent bone, then make it fully influenced by the bone influence = 1.0f; } boneWeights.SetWeight(i, bone.name, index, influence); i++; } index++; // Set the bone back to its initial active state bone.gameObject.SetActive(boneActive); } BoneWeight[] unitweights = boneWeights.GetUnityBoneWeights(); mesh.boneWeights = unitweights; Transform[] bonesArr = bones.Select(b => b.transform).ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bonesArr.Length]; for (int i = 0; i < bonesArr.Length; i++) { bindPoses[i] = bonesArr[i].worldToLocalMatrix * transform.localToWorldMatrix; } mesh.bindposes = bindPoses; skinnedMeshRenderer.bones = bonesArr; // Get the Skeleton of this Skin2D if (skeleton == null) { Debug.LogError("No Skeleton for this Skin2D: " + name); return; } // Use the skeleton name to find the SkinnedMesh of this Skin2D DirectoryInfo meshSkelDir = new DirectoryInfo("Assets/Meshes/SkinnedMeshes/" + skeleton.gameObject.name); // Create the directory if it does not exist if (Directory.Exists(meshSkelDir.FullName) == false) { Directory.CreateDirectory(meshSkelDir.FullName); } string path = "Assets/Meshes/SkinnedMeshes/" + skeleton.gameObject.name + "/" + mesh.name + ".asset"; // Create or replace the mesh asset CreateOrReplaceAsset(mesh, path); AssetDatabase.Refresh(); Mesh generatedMesh = AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)) as Mesh; // Ensure it has bindPoses and weights generatedMesh.boneWeights = unitweights; generatedMesh.bindposes = bindPoses; // Set the reference and sharedMesh to the generated mesh skinnedMeshRenderer.sharedMesh = generatedMesh; referenceMesh = generatedMesh; EditorUtility.SetDirty(skinnedMeshRenderer.gameObject); AssetDatabase.SaveAssets(); } else { Debug.Log("No bones or mesh for this Skin2D: " + name); } } }
public void CalculateBoneWeights(Bone[] bones, bool weightToParent) { if (MeshFilter.sharedMesh == null) { Debug.Log("No Shared Mesh."); return; } Mesh mesh = new Mesh(); mesh.name = "Generated Mesh"; mesh.vertices = MeshFilter.sharedMesh.vertices; mesh.triangles = MeshFilter.sharedMesh.triangles; mesh.normals = MeshFilter.sharedMesh.normals; mesh.uv = MeshFilter.sharedMesh.uv; mesh.uv2 = MeshFilter.sharedMesh.uv2; mesh.bounds = MeshFilter.sharedMesh.bounds; if (bones != null && mesh != null) { boneWeights = new Bone2DWeights(); boneWeights.weights = new Bone2DWeight[] { }; int index = 0; foreach (Bone bone in bones) { int i = 0; bool boneActive = bone.gameObject.activeSelf; bone.gameObject.SetActive(true); foreach (Vector3 v in mesh.vertices) { float influence; if (!weightToParent || weightToParent && bone.transform != transform.parent) { influence = bone.GetInfluence(v + transform.position); } else { influence = 1.0f; } boneWeights.SetWeight(i, bone.name, index, influence); i++; } index++; bone.gameObject.SetActive(boneActive); } BoneWeight[] unitweights = boneWeights.GetUnityBoneWeights(); mesh.boneWeights = unitweights; Transform[] bonesArr = bones.Select(b => b.transform).ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bonesArr.Length]; for (int i = 0; i < bonesArr.Length; i++) { bindPoses[i] = bonesArr[i].worldToLocalMatrix * transform.localToWorldMatrix; } mesh.bindposes = bindPoses; var skinRenderer = GetComponent <SkinnedMeshRenderer>(); if (skinRenderer.sharedMesh != null && !AssetDatabase.Contains(skinRenderer.sharedMesh.GetInstanceID())) { Object.DestroyImmediate(skinRenderer.sharedMesh); } skinRenderer.bones = bonesArr; skinRenderer.sharedMesh = mesh; EditorUtility.SetDirty(skinRenderer.gameObject); if (PrefabUtility.GetPrefabType(skinRenderer.gameObject) != PrefabType.None) { AssetDatabase.SaveAssets(); } } }
public void CalculateBoneWeights(Bone[] bones, bool weightToParent) { if (!lockBoneWeights) { if (meshFilter.sharedMesh == null) { Debug.Log("No Shared Mesh."); return; } Mesh mesh = new Mesh(); mesh.name = meshFilter.sharedMesh.name; mesh.name = mesh.name.Replace(".Mesh", ".SkinnedMesh"); mesh.vertices = meshFilter.sharedMesh.vertices; mesh.triangles = meshFilter.sharedMesh.triangles; mesh.normals = meshFilter.sharedMesh.normals; mesh.uv = meshFilter.sharedMesh.uv; mesh.uv2 = meshFilter.sharedMesh.uv2; mesh.bounds = meshFilter.sharedMesh.bounds; if (bones != null && mesh != null) { boneWeights = new Bone2DWeights(); boneWeights.weights = new Bone2DWeight[] { }; int index = 0; foreach (Bone bone in bones) { int i = 0; bool boneActive = bone.gameObject.activeSelf; bone.gameObject.SetActive(true); foreach (Vector3 v in mesh.vertices) { float influence; if (!weightToParent || weightToParent && bone.transform != transform.parent) { influence = bone.GetInfluence(v + transform.position); } else { influence = 1.0f; } boneWeights.SetWeight(i, bone.name, index, influence); i++; } index++; bone.gameObject.SetActive(boneActive); } BoneWeight[] unitweights = boneWeights.GetUnityBoneWeights(); mesh.boneWeights = unitweights; Transform[] bonesArr = bones.Select(b => b.transform).ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bonesArr.Length]; for (int i = 0; i < bonesArr.Length; i++) { bindPoses[i] = bonesArr[i].worldToLocalMatrix * transform.localToWorldMatrix; } mesh.bindposes = bindPoses; skinnedMeshRenderer.bones = bonesArr; Skeleton[] skeletons = transform.root.gameObject.GetComponentsInChildren <Skeleton>(true); Skeleton skeleton = null; foreach (Skeleton s in skeletons) { if (transform.IsChildOf(s.transform)) { skeleton = s; } } DirectoryInfo meshSkelDir = new DirectoryInfo("Assets/Meshes/SkinnedMeshes/" + skeleton.gameObject.name); if (Directory.Exists(meshSkelDir.FullName) == false) { Directory.CreateDirectory(meshSkelDir.FullName); } string path = "Assets/Meshes/SkinnedMeshes/" + skeleton.gameObject.name + "/" + mesh.name + ".asset"; CreateOrReplaceAsset(mesh, path); AssetDatabase.Refresh(); Mesh generatedMesh = AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)) as Mesh; // Ensure it has bindPoses and weights generatedMesh.boneWeights = unitweights; generatedMesh.bindposes = bindPoses; skinnedMeshRenderer.sharedMesh = generatedMesh; EditorUtility.SetDirty(skinnedMeshRenderer.gameObject); AssetDatabase.SaveAssets(); } } }
public void CalculateBoneWeights(Bone[] bones, bool weightToParent) { if (!lockBoneWeights) { if(meshFilter.sharedMesh == null) { Debug.Log("No Shared Mesh."); return; } Mesh mesh = new Mesh(); mesh.name = meshFilter.sharedMesh.name; mesh.name = mesh.name.Replace(".Mesh", ".SkinnedMesh"); mesh.vertices = meshFilter.sharedMesh.vertices; mesh.triangles = meshFilter.sharedMesh.triangles; mesh.normals = meshFilter.sharedMesh.normals; mesh.uv = meshFilter.sharedMesh.uv; mesh.uv2 = meshFilter.sharedMesh.uv2; mesh.bounds = meshFilter.sharedMesh.bounds; if (bones != null && mesh != null) { boneWeights = new Bone2DWeights(); boneWeights.weights = new Bone2DWeight[] { }; int index = 0; foreach (Bone bone in bones) { int i=0; bool boneActive = bone.gameObject.activeSelf; bone.gameObject.SetActive(true); foreach (Vector3 v in mesh.vertices) { float influence; if (!weightToParent || weightToParent && bone.transform != transform.parent) { influence = bone.GetInfluence(v + transform.position); } else { influence = 1.0f; } boneWeights.SetWeight(i, bone.name, index, influence); i++; } index++; bone.gameObject.SetActive(boneActive); } BoneWeight[] unitweights = boneWeights.GetUnityBoneWeights(); mesh.boneWeights = unitweights; Transform[] bonesArr = bones.Select(b => b.transform).ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bonesArr.Length]; for (int i = 0; i < bonesArr.Length; i++) { bindPoses[i] = bonesArr[i].worldToLocalMatrix * transform.localToWorldMatrix; } mesh.bindposes = bindPoses; skinnedMeshRenderer.bones = bonesArr; Skeleton[] skeletons = transform.root.gameObject.GetComponentsInChildren<Skeleton>(true); Skeleton skeleton = null; foreach (Skeleton s in skeletons) { if (transform.IsChildOf(s.transform)) { skeleton = s; } } DirectoryInfo meshSkelDir = new DirectoryInfo("Assets/Meshes/SkinnedMeshes/" + skeleton.gameObject.name); if (Directory.Exists(meshSkelDir.FullName) == false) { Directory.CreateDirectory(meshSkelDir.FullName); } string path = "Assets/Meshes/SkinnedMeshes/" + skeleton.gameObject.name + "/" + mesh.name + ".asset"; CreateOrReplaceAsset (mesh, path); AssetDatabase.Refresh(); Mesh generatedMesh = AssetDatabase.LoadAssetAtPath (path, typeof(Mesh)) as Mesh; // Ensure it has bindPoses and weights generatedMesh.boneWeights = unitweights; generatedMesh.bindposes = bindPoses; skinnedMeshRenderer.sharedMesh = generatedMesh; EditorUtility.SetDirty(skinnedMeshRenderer.gameObject); AssetDatabase.SaveAssets(); } } }