- Home /
Script not updating ?
This script is altering a skinnedmeshrenderer based on mesh (morph targets). It is controled by a slider which initiates morph() which can be found at line 84. A visual result of the procedure can be found here:
http://www.youtube.com/watch?v=YU4SsT-e5pA
As you can see the morph is actualy 'overshooting' it's target, initialy the ears should angle about 45 degrees (which is the imported morph target) but it seems that the script doesn't know it's uhm 'bounds' ? after the script/scene restarts i kept the deformed skinnedmesh (i know this can be solved), when entering play mode, the slider ( morph() ) actualy morphs back and does not continue it's path from the previous playmode session. So i suspect that the bounds are only read once per session and not constantly reading info or something. Does anyone have a suggestion ?
/// REMEMBER: When importing assets for use with this script, be sure to set the normal angle to 180 degrees.
/// When importing a mesh Unity automatically splits vertices based on normal creases.
/// This script requires that all meshes have the same number of vertices and that
/// those vertices are laid out in exactly the same way. It won't work if Unity autosplits vertices based on normals.
//Custom definitions to store specialized blend shape data for vertices
class BlendShapeVertex {
var originalIndex : int;
var position : Vector3;
var normal : Vector3;
}
class BlendShape {
//var vertices : BlendShapeVertex[];
var vertices = new Array();
}
var attributes : String[]; //Names for the attributes to be morphed
var sourceMesh : Renderer; //The original mesh
var attributeMeshes : Mesh[]; //The destination meshes for each attribute.
var attributeProgress : float[]; //Current weight for each attribute.
private var blendShapes : BlendShape[]; //Array of BlendShape objects. This will be populated in Awake().
private var workingMesh : Mesh; //Stores mesh data for the working copy of the mesh (so the original mesh doesn't get changed).
function Awake () {
//First, make sure all attributes are assigned.
for (i=0; i<attributeMeshes.length; i++) {
if (attributeMeshes[i] == null) {
Debug.Log("Attribute " + i + " has not been assigned.");
return;
}
}
//Populate the working mesh
var sourceMesh = gameObject.GetComponent(SkinnedMeshRenderer);
workingMesh = sourceMesh.sharedMesh;
//Check attribute meshes to be sure vertex count is the same.
var vertexCount = sourceMesh.sharedMesh.vertexCount;
for (i=0; i<attributeMeshes.Length; i++) {
if (attributeMeshes[i].vertexCount != vertexCount) {
Debug.Log("Mesh " + i + " doesn't have the same number of vertices as the first mesh");
return;
}
}
//Build blend shapes
BuildBlendShapes();
}
//This function populates the various arrays that are used by the script later on.
function BuildBlendShapes () {
blendShapes = new BlendShape[attributes.length];
//For each attribute figure out which vertices are affected, then store their info in the blend shape object.
for (var i=0; i < attributes.length; i++) {
//Populate blendShapes array with new blend shapes
blendShapes[i] = new BlendShape();
for (var j=0; j < workingMesh.vertexCount; j++) {
//If the vertex is affected, populate a blend shape vertex with that info
if (workingMesh.vertices[j] != attributeMeshes[i].vertices[j]) {
//Create a blend shape vertex and populate its data.
var blendShapeVertex = new BlendShapeVertex();
blendShapeVertex.originalIndex = j;
blendShapeVertex.position = attributeMeshes[i].vertices[j] - workingMesh.vertices[j];
blendShapeVertex.normal = attributeMeshes[i].normals[j] - workingMesh.normals[j];
//Add new blend shape vertex to blendShape object.
blendShapes[i].vertices.Push(blendShapeVertex);
}
}
//Convert blendShapes.vertices to builtin array
blendShapes[i].vertices = blendShapes[i].vertices.ToBuiltin(BlendShapeVertex);
}
}
//This is the primary function that controls morphs. It is called from the GUI script every time one of the sliders is updated.
function SetMorph () {
//Set up working data to store mesh offset information.
var morphedVertices : Vector3[] = sourceMesh.sharedMesh.vertices;
var morphedNormals : Vector3[] = sourceMesh.sharedMesh.normals;
//For each attribute...
for (var j=0; j<attributes.length; j++) {
//If the weight of this attribute isn't 0
if (!Mathf.Approximately(attributeProgress[j], 0)) {
//For each vertex in this attribute's blend shape...
for (var i=0; i<blendShapes[j].vertices.length; i++) {
//...adjust the mesh according to the offset value and weight
morphedVertices[blendShapes[j].vertices[i].originalIndex] += blendShapes[j].vertices[i].position * attributeProgress[j];
//Adjust normals as well
morphedNormals[blendShapes[j].vertices[i].originalIndex] += blendShapes[j].vertices[i].normal * attributeProgress[j];
}
}
}
//Update the actual mesh with new vertex and normal information, then recalculate the mesh bounds.
workingMesh.vertices = morphedVertices;
workingMesh.normals = morphedNormals;
workingMesh.RecalculateBounds();
}
//Require that we have a mesh filter component and a mesh renderer component when assigning this script to an object.
@script RequireComponent (SkinnedMeshRenderer);
Could you please improve the formatting in your post? It's kind of difficult to read at the moment.
It is the 101010 button on the post if you didn't know (Directed at Question asker)
Your answer
Follow this Question
Related Questions
How to create deforming soap bubbles in unity? 2 Answers
problem deforming mesh colliders 1 Answer
How are verticies numbered in Unity? 1 Answer
Guide a wire along the touch with deformation. 0 Answers
After updating Unity, SkinnedMeshRenderer requires a mesh with skinning information everywhere 0 Answers