public static void FFDCreationMode(Vector3 mousePos) { string newCtrlName = "FFD_CTRL"; string newCtrlGRPName = "FFD_CTRL_GRP"; if (Puppet2D_Editor.FFDGameObject) { newCtrlName = Puppet2D_Editor.FFDGameObject.name + "_Ctrl"; newCtrlGRPName = Puppet2D_Editor.FFDGameObject.name + "_Ctrl_GRP"; } GameObject newCtrl = new GameObject(Puppet2D_BoneCreation.GetUniqueBoneName(newCtrlName)); GameObject newCtrlGRP = new GameObject(Puppet2D_BoneCreation.GetUniqueBoneName(newCtrlGRPName)); newCtrl.transform.parent = newCtrlGRP.transform; Undo.RegisterCreatedObjectUndo(newCtrl, "Created newCtrl"); Undo.RegisterCreatedObjectUndo(newCtrlGRP, "Created newCtrlGRP"); Undo.RecordObject(ffdStoreData, "Adding FFD Control"); ffdStoreData.FFDCtrls.Add(newCtrl.transform); Puppet2D_FFDLineDisplay ffdline = newCtrl.AddComponent <Puppet2D_FFDLineDisplay>(); if (ffdStoreData.FFDCtrls.Count > 1) { if (ffdStoreData.FFDPathNumber.Count > 0) { if (ffdStoreData.FFDCtrls.Count - 1 > ffdStoreData.FFDPathNumber[ffdStoreData.FFDPathNumber.Count - 1]) { ffdline.target = ffdStoreData.FFDCtrls[ffdStoreData.FFDCtrls.Count - 2]; } } else { ffdline.target = ffdStoreData.FFDCtrls[ffdStoreData.FFDCtrls.Count - 2]; } } newCtrlGRP.transform.position = new Vector3(mousePos.x, mousePos.y, 0); SpriteRenderer spriteRenderer = newCtrl.AddComponent <SpriteRenderer>(); spriteRenderer.sortingLayerName = Puppet2D_Editor._controlSortingLayer; string path = (Puppet2D_Editor._puppet2DPath + "/Textures/GUI/ffdBone.psd"); Sprite sprite = AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)) as Sprite; spriteRenderer.sprite = sprite; spriteRenderer.sortingLayerName = Puppet2D_Editor._controlSortingLayer; }
public void TraverseHierarchy(Transform root) { foreach (Transform child in root) { GameObject Go = child.gameObject; SpriteRenderer spriteRenderer = Go.transform.GetComponent <SpriteRenderer>(); if (spriteRenderer && spriteRenderer.sprite) { if (spriteRenderer.sprite.name.Contains("Control")) { _Controls.Add(spriteRenderer); } else if (spriteRenderer.sprite.name.Contains("ffd")) { _FFDControls.Add(spriteRenderer); } } Puppet2D_Bone boneRender = Go.transform.GetComponent <Puppet2D_Bone>(); if (boneRender) { _Bones.Add(boneRender); } Puppet2D_ParentControl newParentCtrl = Go.transform.GetComponent <Puppet2D_ParentControl>(); if (newParentCtrl) { _ParentControls.Add(newParentCtrl); } Puppet2D_IKHandle newIKCtrl = Go.transform.GetComponent <Puppet2D_IKHandle>(); if (newIKCtrl) { _Ikhandles.Add(newIKCtrl); } Puppet2D_FFDLineDisplay ffdCtrl = Go.transform.GetComponent <Puppet2D_FFDLineDisplay>(); if (ffdCtrl) { _ffdControls.Add(ffdCtrl); } Puppet2D_SplineControl splineCtrl = Go.transform.GetComponent <Puppet2D_SplineControl>(); if (splineCtrl) { _SplineControls.Add(splineCtrl); } TraverseHierarchy(child); } }
void CombineAllMeshes() { Vector3 originalScale = transform.localScale; Quaternion originalRot = transform.rotation; Vector3 originalPos = transform.position; transform.localScale = Vector3.one; transform.rotation = Quaternion.identity; transform.position = Vector3.zero; SkinnedMeshRenderer[] smRenderers = GetComponentsInChildren <SkinnedMeshRenderer>(); List <Transform> bones = new List <Transform>(); List <BoneWeight> boneWeights = new List <BoneWeight>(); List <CombineInstance> combineInstances = new List <CombineInstance>(); List <Texture2D> textures = new List <Texture2D>(); Material currentMaterial = null; int numSubs = 0; var smRenderersDict = new Dictionary <SkinnedMeshRenderer, float>(smRenderers.Length); bool updateWhenOffscreen = false; foreach (SkinnedMeshRenderer smr in smRenderers) { smRenderersDict.Add(smr, smr.transform.position.z); updateWhenOffscreen = smr.updateWhenOffscreen; } var items = from pair in smRenderersDict orderby pair.Key.sortingOrder ascending select pair; items = from pair in items orderby pair.Value descending select pair; foreach (KeyValuePair <SkinnedMeshRenderer, float> pair in items) { //Debug.Log(pair.Key.name + " " + pair.Value); numSubs += pair.Key.sharedMesh.subMeshCount; } int[] meshIndex = new int[numSubs]; int boneOffset = 0; int s = 0; foreach (KeyValuePair <SkinnedMeshRenderer, float> pair in items) { SkinnedMeshRenderer smr = pair.Key; if (currentMaterial == null) { currentMaterial = smr.sharedMaterial; } else if (currentMaterial.mainTexture && smr.sharedMaterial.mainTexture && currentMaterial.mainTexture != smr.sharedMaterial.mainTexture) { continue; } bool ffdMesh = false; foreach (Transform boneToCheck in smr.bones) { Puppet2D_FFDLineDisplay ffdLine = boneToCheck.GetComponent <Puppet2D_FFDLineDisplay>(); if (ffdLine && ffdLine.outputSkinnedMesh != smr) { ffdMesh = true; } } if (ffdMesh) { continue; } BoneWeight[] meshBoneweight = smr.sharedMesh.boneWeights; foreach (BoneWeight bw in meshBoneweight) { BoneWeight bWeight = bw; bWeight.boneIndex0 += boneOffset; bWeight.boneIndex1 += boneOffset; bWeight.boneIndex2 += boneOffset; bWeight.boneIndex3 += boneOffset; boneWeights.Add(bWeight); } boneOffset += smr.bones.Length; Transform[] meshBones = smr.bones; foreach (Transform bone in meshBones) { bones.Add(bone); } if (smr.material.mainTexture != null) { textures.Add(smr.GetComponent <Renderer>().material.mainTexture as Texture2D); } CombineInstance ci = new CombineInstance(); ci.mesh = smr.sharedMesh; meshIndex[s] = ci.mesh.vertexCount; ci.transform = smr.transform.localToWorldMatrix; combineInstances.Add(ci); Object.Destroy(smr.gameObject); s++; } List <Matrix4x4> bindposes = new List <Matrix4x4>(); for (int b = 0; b < bones.Count; b++) { if (bones[b].GetComponent <Puppet2D_FFDLineDisplay>()) { Vector3 boneparentPos = bones[b].transform.parent.parent.position; Quaternion boneparentRot = bones[b].transform.parent.parent.rotation; bones[b].transform.parent.parent.position = Vector3.zero; bones[b].transform.parent.parent.rotation = Quaternion.identity; bindposes.Add(bones[b].worldToLocalMatrix * transform.worldToLocalMatrix); bones[b].transform.parent.parent.position = boneparentPos; bones[b].transform.parent.parent.rotation = boneparentRot; } else { bindposes.Add(bones[b].worldToLocalMatrix * transform.worldToLocalMatrix); } } SkinnedMeshRenderer r = gameObject.AddComponent <SkinnedMeshRenderer>(); r.updateWhenOffscreen = updateWhenOffscreen; r.sharedMesh = new Mesh(); r.sharedMesh.CombineMeshes(combineInstances.ToArray(), true, true); Material combinedMat; if (currentMaterial != null) { combinedMat = currentMaterial; } else { combinedMat = new Material(Shader.Find("Unlit/Transparent")); } combinedMat.mainTexture = textures[0]; r.sharedMesh.uv = r.sharedMesh.uv; r.sharedMaterial = combinedMat; r.bones = bones.ToArray(); r.sharedMesh.boneWeights = boneWeights.ToArray(); r.sharedMesh.bindposes = bindposes.ToArray(); r.sharedMesh.RecalculateBounds(); transform.localScale = originalScale; transform.rotation = originalRot; transform.position = originalPos; }
public static void FFDFinishCreation() { if (ffdStoreData == null) { return; } Puppet2D_Editor.FFDCreation = false; CloseFFDPath(); Texture spriteTexture = null; //GameObject FFDControlsGrp = new GameObject(Puppet2D_BoneCreation.GetUniqueBoneName("FFD_Ctrls_GRP")); if (Puppet2D_Editor.FFDGameObject && Puppet2D_Editor.FFDGameObject.GetComponent <SpriteRenderer>() && Puppet2D_Editor.FFDGameObject.GetComponent <SpriteRenderer>().sprite) { spriteTexture = Puppet2D_Editor.FFDGameObject.GetComponent <SpriteRenderer>().sprite.texture; foreach (Transform FFDCtrl in ffdStoreData.FFDCtrls) { FFDCtrl.transform.position = Puppet2D_Editor.FFDGameObject.transform.InverseTransformPoint(FFDCtrl.transform.position); } FFDControlsGrp.transform.position = Puppet2D_Editor.FFDGameObject.transform.position; FFDControlsGrp.transform.rotation = Puppet2D_Editor.FFDGameObject.transform.rotation; FFDControlsGrp.transform.localScale = Puppet2D_Editor.FFDGameObject.transform.localScale; //Puppet2D_Editor.FFDGameObject.transform.position = Vector3.zero; //Puppet2D_Editor.FFDGameObject.transform.rotation = Quaternion.identity; //Puppet2D_Editor.FFDGameObject.transform.localScale = Vector3.one; } if (ffdStoreData.FFDCtrls.Count < 3) { //Undo.DestroyObjectImmediate(ffdStoreData); return; } Puppet2D_CreatePolygonFromSprite polyFromSprite = ScriptableObject.CreateInstance("Puppet2D_CreatePolygonFromSprite") as Puppet2D_CreatePolygonFromSprite; List <Vector3> verts = new List <Vector3>(); for (int i = 0; i < ffdStoreData.FFDCtrls.Count(); i++) { if (ffdStoreData.FFDCtrls[i]) { verts.Add(new Vector3(ffdStoreData.FFDCtrls[i].position.x, ffdStoreData.FFDCtrls[i].position.y, 0)); } else { // Debug.LogWarning("A FFD control point has been removed, no mesh created"); // Undo.DestroyObjectImmediate(ffdStoreData); // return; } } GameObject newMesh; if (ffdStoreData.FFDPathNumber.Count > 0 && verts.Count > 2) { if (Puppet2D_Editor.FFDGameObject == null) { Puppet2D_Editor.FFDGameObject = new GameObject(); Undo.RegisterCreatedObjectUndo(Puppet2D_Editor.FFDGameObject, "newGameObject"); } Puppet2D_Editor._numberBonesToSkinToIndex = 0; string sortingLayer = ""; int sortingOrder = 0; if (Puppet2D_Editor.FFDGameObject.GetComponent <Renderer>()) { sortingLayer = Puppet2D_Editor.FFDGameObject.GetComponent <Renderer>().sortingLayerName; sortingOrder = Puppet2D_Editor.FFDGameObject.GetComponent <Renderer>().sortingOrder; } newMesh = polyFromSprite.MakeFromVerts(true, verts.ToArray(), ffdStoreData.FFDPathNumber, Puppet2D_Editor.FFDGameObject); if (Puppet2D_Editor.FFDGameObject.GetComponent <Renderer>()) { newMesh.GetComponent <Renderer>().sortingLayerName = sortingLayer; newMesh.GetComponent <Renderer>().sortingOrder = sortingOrder; } Puppet2D_Editor._numberBonesToSkinToIndex = 1; } else { //Undo.DestroyObjectImmediate(ffdStoreData); return; } Undo.DestroyObjectImmediate(polyFromSprite); if (Puppet2D_Editor.FFDGameObject) { if (spriteTexture != null) { newMesh.GetComponent <Renderer>().sharedMaterial.mainTexture = spriteTexture; } else { newMesh.GetComponent <Renderer>().sharedMaterial = new Material(Shader.Find("Unlit/Texture")); } newMesh.name = Puppet2D_Editor.FFDGameObject.name; ffdStoreData.FFDCtrls.Add(newMesh.transform); Undo.DestroyObjectImmediate(Puppet2D_Editor.FFDGameObject); } GameObject globalCtrl = Puppet2D_CreateControls.CreateGlobalControl(); Undo.SetTransformParent(FFDControlsGrp.transform, globalCtrl.transform, "parentToGlobal"); Undo.SetTransformParent(newMesh.transform, globalCtrl.transform, "parentToGlobal"); List <Object> newObjs = new List <Object>(); foreach (Transform tr in ffdStoreData.FFDCtrls) { if (tr) { newObjs.Add(tr.gameObject); } } Selection.objects = newObjs.ToArray(); Puppet2D_Editor._numberBonesToSkinToIndex = 1; //Undo.RecordObjects (newObjs.ToArray(), "recordingStuff"); //Undo.RegisterCompleteObjectUndo (newObjs.ToArray(), "recordingStuff"); Puppet2D_Skinning.BindSmoothSkin(1); for (int i = 0; i < ffdStoreData.FFDCtrls.Count - 1; i++) { //Debug.Log(ffdStoreData.FFDCtrls[i]); if (ffdStoreData.FFDCtrls[i]) { Puppet2D_FFDLineDisplay ffdLine = ffdStoreData.FFDCtrls[i].GetComponent <Puppet2D_FFDLineDisplay>(); ffdLine.outputSkinnedMesh = newMesh.GetComponent <SkinnedMeshRenderer>(); for (int j = 0; j < ffdLine.outputSkinnedMesh.sharedMesh.vertices.Length; j++) { Vector3 vert = ffdLine.outputSkinnedMesh.sharedMesh.vertices[j]; if (Vector3.Distance(vert, ffdStoreData.FFDCtrls[i].transform.position) < .001f) { ffdStoreData.FFDCtrls[i].GetComponent <Puppet2D_FFDLineDisplay>().vertNumber = j; } } Undo.SetTransformParent(ffdStoreData.FFDCtrls[i].parent.transform, FFDControlsGrp.transform, "parentFFDControls"); ffdStoreData.FFDCtrls[i].transform.localPosition = Vector3.zero; } } FFDControlsGrp.transform.position = new Vector3(FFDControlsGrp.transform.position.x, FFDControlsGrp.transform.position.y, ffdStoreData.OriginalSpritePosition.z); Selection.activeGameObject = ffdStoreData.FFDCtrls[ffdStoreData.FFDCtrls.Count - 1].gameObject; ffdStoreData.FFDCtrls.RemoveAt(ffdStoreData.FFDCtrls.Count - 1); Undo.RegisterCompleteObjectUndo(ffdStoreData, "changinEditable"); ffdStoreData.Editable = false; // Undo.DestroyObjectImmediate(ffdStoreData); if (globalCtrl.GetComponent <Puppet2D_GlobalControl>().AutoRefresh) { globalCtrl.GetComponent <Puppet2D_GlobalControl>().Init(); } }