Advertisement
Guest User

unity paintvertices.js with explicit mesh types

a guest
Dec 22nd, 2010
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var radius = 1.0;
  2. var pull = 10.0;
  3. private var unappliedMesh : MeshFilter;
  4.  
  5. enum FallOff { Gauss, Linear, Needle }
  6. var fallOff = FallOff.Gauss;
  7.  
  8. static function LinearFalloff (distance : float , inRadius : float) {
  9.     return Mathf.Clamp01(1.0 - distance / inRadius);
  10. }
  11.  
  12. static function GaussFalloff (distance : float , inRadius : float) {
  13.     return Mathf.Clamp01 (Mathf.Pow (360.0, -Mathf.Pow (distance / inRadius, 2.5) - 0.01));
  14. }
  15.  
  16. function NeedleFalloff (dist : float, inRadius : float)
  17. {
  18.     return -(dist*dist) / (inRadius * inRadius) + 1.0;
  19. }
  20.  
  21. function DeformMesh (mesh : Mesh, position : Vector3, power : float, inRadius : float)
  22. {
  23.     var vertices = mesh.vertices;
  24.     var normals = mesh.normals;
  25.     var sqrRadius = inRadius * inRadius;
  26.    
  27.     // Calculate averaged normal of all surrounding vertices   
  28.     var averageNormal = Vector3.zero;
  29.     for (var i=0;i<vertices.length;i++)
  30.     {
  31.         var sqrMagnitude = (vertices[i] - position).sqrMagnitude;
  32.         // Early out if too far away
  33.         if (sqrMagnitude > sqrRadius)
  34.             continue;
  35.  
  36.         var distance = Mathf.Sqrt(sqrMagnitude);
  37.         var falloff = LinearFalloff(distance, inRadius);
  38.         averageNormal += falloff * normals[i];
  39.     }
  40.     averageNormal = averageNormal.normalized;
  41.    
  42.     // Deform vertices along averaged normal
  43.     for (i=0;i<vertices.length;i++)
  44.     {
  45.         sqrMagnitude = (vertices[i] - position).sqrMagnitude;
  46.         // Early out if too far away
  47.         if (sqrMagnitude > sqrRadius)
  48.             continue;
  49.  
  50.         distance = Mathf.Sqrt(sqrMagnitude);
  51.         switch (fallOff)
  52.         {
  53.             case FallOff.Gauss:
  54.                 falloff = GaussFalloff(distance, inRadius);
  55.                 break;
  56.             case FallOff.Needle:
  57.                 falloff = NeedleFalloff(distance, inRadius);
  58.                 break;
  59.             default:
  60.                 falloff = LinearFalloff(distance, inRadius);
  61.                 break;
  62.         }
  63.        
  64.         vertices[i] += averageNormal * falloff * power;
  65.     }
  66.    
  67.     mesh.vertices = vertices;
  68.     mesh.RecalculateNormals();
  69.     mesh.RecalculateBounds();
  70. }
  71.  
  72. function Update () {
  73.  
  74.     // When no button is pressed we update the mesh collider
  75.     if (!Input.GetMouseButton (0))
  76.     {
  77.         // Apply collision mesh when we let go of button
  78.         ApplyMeshCollider();
  79.         return;
  80.     }
  81.        
  82.        
  83.     // Did we hit the surface?
  84.     var hit : RaycastHit;
  85.     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  86.     if (Physics.Raycast (ray, hit))
  87.     {
  88.         var filter : MeshFilter = hit.collider.GetComponent(MeshFilter);
  89.         if (filter)
  90.         {
  91.             // Don't update mesh collider every frame since physX
  92.             // does some heavy processing to optimize the collision mesh.
  93.             // So this is not fast enough for real time updating every frame
  94.             if (filter != unappliedMesh)
  95.             {
  96.                 ApplyMeshCollider();
  97.                 unappliedMesh = filter;
  98.             }
  99.            
  100.             // Deform mesh
  101.             var relativePoint = filter.transform.InverseTransformPoint(hit.point);
  102.             DeformMesh(filter.mesh, relativePoint, pull * Time.deltaTime, radius);
  103.         }
  104.     }
  105. }
  106.  
  107. function ApplyMeshCollider () {
  108.     if (unappliedMesh && unappliedMesh.GetComponent.<MeshCollider>()) {
  109.         unappliedMesh.GetComponent.<MeshCollider>().mesh = unappliedMesh.mesh;
  110.     }
  111.     unappliedMesh = null;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement