Advertisement
selzero

Manipulate Mesh Script 2

Sep 22nd, 2012
2,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class JingleObject : MonoBehaviour {
  5.     //The original vertices
  6.     private Vector3[] v3BaseVertices;
  7.     //The objects mesh
  8.     private Mesh meshObjectsMesh;
  9.    
  10.     //Initialise class
  11.     void Start () {
  12.         //Get the mesh of this object
  13.         meshObjectsMesh = (Mesh)(gameObject.GetComponent().mesh);
  14.         //Set the original vertices
  15.         v3BaseVertices = meshObjectsMesh.vertices;
  16.     }
  17.  
  18.     // Update is called once per frame
  19.     void Update () {
  20.         //The new vertices
  21.         Vector3[] v3Vertices = new Vector3[v3BaseVertices.Length];
  22.         //Random values to move the axis by and create the jingle
  23.         float fScaleX = Random.value/20 ;
  24.         float fScaleY = Random.value/20 ;
  25.         float fScaleZ = Random.value/20 ;
  26.         //loop through each vertex
  27.         for (var i=0;i<v3Vertices.Length;i++)
  28.         {
  29.             //Get the next vertex
  30.             Vector3 v3Vertex= v3BaseVertices[i];
  31.             //Make a change to each of the coordinates of this vertex
  32.             v3Vertex.x += fScaleX;
  33.             v3Vertex.y += fScaleY;
  34.             v3Vertex.z += fScaleZ;
  35.             //Add the new vertex to our new array
  36.             v3Vertices[i] = v3Vertex;
  37.         }
  38.         //Set the new array as the mesh vertex array
  39.         meshObjectsMesh.vertices = v3Vertices;
  40.         //We can ask the engine to recalculate the normals  
  41.         //of the mesh, the normals are the surfaces that are rendered.              meshObjectsMesh.RecalculateNormals();
  42.  
  43.         //We have to call RecalculateBounds to make sure
  44.         //that the triangles from the vertex relationships
  45.         //are recalculated correctly
  46.         meshObjectsMesh.RecalculateBounds();
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement