Guest User

BlendShapeController.cs

a guest
Dec 6th, 2014
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BlendShapeController : MonoBehaviour
  5. {
  6.     public int blendShape;
  7.     public float weight;
  8.     public int blendShape2;
  9.     public float weight2;
  10.     int blendShapeCount;
  11.     int currBlendShape = 0;
  12.     SkinnedMeshRenderer skinnedMeshRenderer;
  13.     Mesh skinnedMesh;
  14.     float blendVal = 0f;
  15.     bool first_update = true;
  16.    
  17.     void Awake ()
  18.     {
  19.         skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer> ();
  20.         skinnedMesh = GetComponent<SkinnedMeshRenderer> ().sharedMesh;
  21.     }
  22.    
  23.     void Start ()
  24.     {
  25.         blendShapeCount = skinnedMesh.blendShapeCount;
  26.  
  27.         //initialize any blendshapes for this model here
  28.         skinnedMeshRenderer.SetBlendShapeWeight (blendShape, weight);
  29.         skinnedMeshRenderer.SetBlendShapeWeight (blendShape2, weight2);
  30.  
  31.         currBlendShape = 0;
  32.         blendVal = skinnedMeshRenderer.GetBlendShapeWeight(currBlendShape);
  33.     }
  34.    
  35.     void Update ()
  36.     {
  37.         if(first_update)
  38.         {
  39.             Start ();
  40.             first_update = false;
  41.         }
  42.  
  43.         //reset all blend shapes if select is pressed
  44.         if (Input.GetKeyDown (KeyCode.Joystick1Button6) || (Input.GetKeyDown(KeyCode.P)))
  45.         {
  46.             currBlendShape = blendShapeCount - 1;
  47.             blendVal = 0f;
  48.  
  49.             do
  50.             {
  51.                 skinnedMeshRenderer.SetBlendShapeWeight (currBlendShape, blendVal);
  52.                 currBlendShape--;
  53.             }
  54.             while( currBlendShape > 0 );
  55.  
  56.             Start ();
  57.         }
  58.  
  59.         //change the current shape
  60.         if (Input.GetKeyDown(KeyCode.Joystick1Button2) || Input.GetKeyDown(KeyCode.Comma))
  61.         {
  62.             if(currBlendShape <= 0)
  63.                 currBlendShape = blendShapeCount - 1;
  64.             else
  65.                 currBlendShape--;
  66.  
  67.             blendVal = skinnedMeshRenderer.GetBlendShapeWeight(currBlendShape);
  68.         }
  69.         else if (Input.GetKeyDown(KeyCode.Joystick1Button1) || Input.GetKeyDown(KeyCode.Period))
  70.         {
  71.             if(currBlendShape + 1 >= blendShapeCount)
  72.                 currBlendShape = 0;
  73.             else
  74.                 currBlendShape++;
  75.  
  76.             blendVal = skinnedMeshRenderer.GetBlendShapeWeight(currBlendShape);
  77.         }
  78.  
  79.         //modify current shape by trigger values
  80.         float value = Input.GetAxis("Triggers") * -1;
  81.  
  82.         if( Input.GetKey(KeyCode.Semicolon) )
  83.         {
  84.             value = 1f;
  85.         }
  86.         else if( Input.GetKey(KeyCode.Quote) )
  87.         {
  88.             value = -1f;
  89.         }
  90.  
  91.         if (blendVal > 0f && value < 0f)
  92.         {
  93.             //restore dick head if size increases from 0
  94.             if(currBlendShape == blendShape)
  95.             {
  96.                 if( blendVal >= 100f )
  97.                 {
  98.                     skinnedMeshRenderer.SetBlendShapeWeight (blendShape2, weight2);
  99.                 }
  100.             }
  101.  
  102.             blendVal += value;
  103.         }
  104.         else if (blendVal < 100f && value > 0f)
  105.         {
  106.             blendVal += value;
  107.         }
  108.  
  109.         skinnedMeshRenderer.SetBlendShapeWeight (currBlendShape, blendVal);
  110.  
  111.         //hide dick head if size reduced to 0
  112.         if(currBlendShape == blendShape)
  113.         {
  114.             if( blendVal >= 100f && skinnedMeshRenderer.GetBlendShapeWeight(blendShape2) > 0f )
  115.             {
  116.                 skinnedMeshRenderer.SetBlendShapeWeight (blendShape2, 0f);
  117.             }
  118.         }
  119.     }
  120. }
RAW Paste Data