public static BlendShapeFrame Create(float weight, List<Vector2> vertices) { BlendShapeFrame frame = new BlendShapeFrame(); frame.vertices = vertices; frame.weight = weight; return frame; }
void CreateBlendShapeCache(SpriteMeshData spriteMeshData) { DestroyBlendShapeCache(""); List <BlendShapeFrame> frameClones = new List <BlendShapeFrame>(); foreach (BlendShape blendShape in spriteMeshData.blendshapes) { frameClones.Clear(); foreach (BlendShapeFrame frame in blendShape.frames) { BlendShapeFrame frameClone = ScriptableObject.CreateInstance <BlendShapeFrame>(); frameClone.hideFlags = HideFlags.DontSave; EditorUtility.CopySerialized(frame, frameClone); frameClones.Add(frameClone); } BlendShape blendShapeClone = CreateBlendshape(blendShape.name); blendShapeClone.frames = frameClones.ToArray(); } }
public static BlendShapeFrame CreateBlendShapeFrame(BlendShape blendshape, float weight, Vector3[] vertices) { BlendShapeFrame l_blendshapeFrame = null; if (blendshape && vertices != null) { l_blendshapeFrame = BlendShapeFrame.Create(weight, vertices); l_blendshapeFrame.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(l_blendshapeFrame, blendshape); List <BlendShapeFrame> l_blendshapeFrames = new List <BlendShapeFrame>(blendshape.frames); l_blendshapeFrames.Add(l_blendshapeFrame); l_blendshapeFrames.Sort((a, b) => { return(a.weight.CompareTo(b.weight)); }); blendshape.frames = l_blendshapeFrames.ToArray(); EditorUtility.SetDirty(blendshape); EditorUtility.SetDirty(l_blendshapeFrame); } return(l_blendshapeFrame); }
public void SetBlendShapeFrameWeight(BlendShapeFrame blendShapeFrame, float weight, string undoName) { if (selectedBlendshape && blendShapeFrame && weight >= 1f && selectedBlendshape.frames.Contains(blendShapeFrame)) { RegisterObjectUndo(selectedBlendshape, undoName); RegisterObjectUndo(blendShapeFrame, undoName); BlendShapeFrame other = null; foreach (BlendShapeFrame f in selectedBlendshape.frames) { if (f != blendShapeFrame && f.weight == weight) { other = f; break; } } if (other) { DeleteBlendShapeFrame(selectedBlendshape, other, undoName); } blendShapeFrame.weight = weight; SortBlendshapeFrames(selectedBlendshape); m_DirtyVertices = true; isDirty = true; } }
public BlendShapeFrame CreateBlendShapeFrame(BlendShape blendshape, float weight, string undoName) { BlendShapeFrame frame = null; if (blendshape && weight >= 1f) { frame = BlendShapeFrame.Create(weight, ToVertices(GetTexVertices()).ToArray()); RegisterCreatedObjectUndo(frame, undoName); RegisterObjectUndo(blendshape, undoName); List <BlendShapeFrame> frames = new List <BlendShapeFrame>(blendshape.frames); frames.Add(frame); blendshape.frames = frames.ToArray(); SortBlendshapeFrames(blendshape); m_DirtyVertices = true; isDirty = true; } return(frame); }
public static BlendShapeFrame Create(float weight, Vector3[] vertices) { BlendShapeFrame frame = ScriptableObject.CreateInstance <BlendShapeFrame>(); frame.vertices = vertices; frame.weight = weight; return(frame); }
public static BlendShapeFrameDopeElement Create(BlendShapeFrame frame) { BlendShapeFrameDopeElement element = null; if (frame) { element = new BlendShapeFrameDopeElement(); element.blendShapeFrame = frame; } return(element); }
public void DeleteBlendShapeFrame(BlendShape blendShape, BlendShapeFrame blendShapeFrame, string undoName = "") { if (blendShape && blendShape.frames.Contains(blendShapeFrame)) { RegisterObjectUndo(blendShape, undoName); List <BlendShapeFrame> frames = blendShape.frames.ToList(); frames.Remove(blendShapeFrame); blendShape.frames = frames.ToArray(); DestroyObjectImmediate(blendShapeFrame); m_DirtyVertices = true; isDirty = true; } }
BlendShapeFrame GetBlendShapeFrame() { BlendShapeFrame frame = null; if (selectedBlendshape) { foreach (BlendShapeFrame f in selectedBlendshape.frames) { if (f && Mathf.Approximately(f.weight, blendShapeWeight)) { frame = f; break; } } } return(frame); }
public static void RebuildBlendShapes(SpriteMesh spriteMesh, Mesh mesh) { if (!mesh) { return; } if (!spriteMesh) { return; } BlendShape[] blendShapes = null; SpriteMeshData spriteMeshData = LoadSpriteMeshData(spriteMesh); if (spriteMeshData) { blendShapes = spriteMeshData.blendshapes; } if (spriteMesh.sharedMesh.vertexCount != mesh.vertexCount) { return; } if (blendShapes != null) { #if !(UNITY_5_0 || UNITY_5_1 || UNITY_5_2) List <string> blendShapeNames = new List <string>(); mesh.ClearBlendShapes(); Vector3[] from = mesh.vertices; for (int i = 0; i < blendShapes.Length; i++) { BlendShape blendshape = blendShapes[i]; if (blendshape) { string blendShapeName = blendshape.name; if (blendShapeNames.Contains(blendShapeName)) { Debug.LogWarning("Found repeated BlendShape name '" + blendShapeName + "' in SpriteMesh: " + spriteMesh.name); } else { blendShapeNames.Add(blendShapeName); for (int j = 0; j < blendshape.frames.Length; j++) { BlendShapeFrame l_blendshapeFrame = blendshape.frames[j]; if (l_blendshapeFrame && from.Length == l_blendshapeFrame.vertices.Length) { Vector3[] deltaVertices = GetDeltaVertices(from, l_blendshapeFrame.vertices); mesh.AddBlendShapeFrame(blendShapeName, l_blendshapeFrame.weight, deltaVertices, null, null); } } } } } mesh.UploadMeshData(false); EditorUtility.SetDirty(mesh); #endif } }
void OnFrameChanged(BlendShapeFrame blendShapeFrame, float weight) { spriteMeshCache.SetBlendShapeFrameWeight(blendShapeFrame, weight, "Set weight"); }
List <Vector2> GetBlendshapePositions(BlendShape blendshape, float weight) { weight = Mathf.Clamp(weight, 0f, weight); List <Vector2> result = new List <Vector2>(m_TexVertices.Count); if (blendshape) { BlendShapeFrame prevFrame = null; BlendShapeFrame nextFrame = null; foreach (BlendShapeFrame frame in blendshape.frames) { if (frame && frame.weight < weight) { prevFrame = frame; } else if (frame && nextFrame == null) { nextFrame = frame; break; } } Vector3[] prevFrameVertices = null; Vector3[] nextFrameVertices = null; float prevWeight = 0f; float nextWeight = 0f; if (prevFrame) { prevFrameVertices = prevFrame.vertices; prevWeight = prevFrame.weight; } else { prevFrameVertices = ToVertices(m_TexVertices).ToArray(); } if (nextFrame) { nextFrameVertices = nextFrame.vertices; nextWeight = nextFrame.weight; } else if (prevFrameVertices != null) { nextFrameVertices = prevFrameVertices; nextWeight = prevWeight; } if (prevFrameVertices != null && nextFrameVertices != null && prevFrameVertices.Length == nextFrameVertices.Length) { int count = prevFrameVertices.Length; float pixelsPerUnit = SpriteMeshUtils.GetSpritePixelsPerUnit(spriteMesh.sprite); float t = 0f; float weightDelta = (nextWeight - prevWeight); if (weightDelta > 0f) { t = (weight - prevWeight) / weightDelta; } for (int i = 0; i < count; ++i) { Vector3 v = Vector3.Lerp(prevFrameVertices[i], nextFrameVertices[i], t); result.Add(SpriteMeshUtils.VertexToTexCoord(spriteMesh, pivotPoint, v, pixelsPerUnit)); } } } return(result); }