Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by inkspots · Dec 20, 2011 at 06:47 AM · updateskinnedmeshrendererdeformationmorphdeform

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);

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Dec 20, 2011 at 07:12 AM 1
Share

Could you please improve the formatting in your post? It's kind of difficult to read at the moment.

avatar image doomprodigy · Dec 20, 2011 at 10:32 AM 1
Share

It is the 101010 button on the post if you didn't know (Directed at Question asker)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges