Edwarddv

Vertex Tweaker

Sep 29th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class VertexTweaker : MonoBehaviour
  5. {
  6.     private Mesh thisMesh;
  7.  
  8.     private Vector3[] vertexPositionsWorld;
  9.     private Vector3[] vertexNormalsWorld;
  10.  
  11.     private float distanceToChanger = 100f;
  12.     private Color[] colours;
  13.     private float vertexCount = 0;
  14.  
  15.     public void ClearVertexColours()
  16.     {
  17.         if (thisMesh == null)
  18.             thisMesh = GetComponent<MeshFilter>().mesh;
  19.         colours = new Color[thisMesh.vertexCount];
  20.  
  21.         for (int j = 0; j < thisMesh.vertexCount; j++)
  22.         {
  23.             colours[j] = Color.black;
  24.             //colours[j].a = 0f; // you can do this, but I've just tweaked the shader so A is inverted, A == 1 is no blood
  25.         }
  26.         thisMesh.colors = colours;
  27.     }
  28.     void Awake()
  29.     {
  30.         thisMesh = GetComponent<MeshFilter>().mesh;
  31.         colours = new Color[thisMesh.vertexCount];
  32.  
  33.         ClearVertexColours();
  34.         vertexPositionsWorld = new Vector3[thisMesh.vertexCount];
  35.         vertexNormalsWorld = new Vector3[thisMesh.vertexCount];
  36.         for (int j = 0; j < thisMesh.vertexCount; j++)
  37.         {
  38.             vertexPositionsWorld[j] = transform.TransformPoint(thisMesh.vertices[j]);
  39.             //vertexNormalsWorld[j] = transform.TransformDirection(thisMesh.normals[j]);
  40.  
  41.         }
  42.         vertexNormalsWorld = thisMesh.normals;
  43.         vertexCount = thisMesh.vertexCount;
  44.     }
  45.  
  46.  
  47.     public void ChangeInArea(Color color, Vector3 position, float radius, float strength)
  48.     {
  49.         colours = thisMesh.colors;
  50.         for (int j = 0; j < vertexCount; j++)
  51.         {
  52.             distanceToChanger = (vertexPositionsWorld[j] - position).magnitude;
  53.             if (distanceToChanger < radius)
  54.             {
  55.                 colours[j].r = color.r;
  56.                 colours[j].g = color.g;
  57.                 colours[j].b = color.b;
  58.  
  59.                 colours[j].a -= strength;
  60.                 colours[j].a = Mathf.Clamp01(colours[j].a);
  61.  
  62.             }
  63.  
  64.  
  65.         }
  66.  
  67.         thisMesh.colors = colours;
  68.     }
  69.     public void ChangeInArea(Color color, Vector3 position, float radius, float strength, Vector3 direction)
  70.     {
  71.         colours = thisMesh.colors;
  72.         direction = direction.normalized;
  73.         float facingDot = 0;
  74.         for (int j = 0; j < vertexCount; j++) //iterate through each vertex
  75.         {
  76.             distanceToChanger = (vertexPositionsWorld[j]- position).sqrMagnitude;
  77.             facingDot = Vector3.Dot(direction, vertexNormalsWorld[j]);
  78.             if (distanceToChanger < radius)
  79.             {
  80.                 if (facingDot < 0.5f)
  81.                 {
  82.  
  83.                     colours[j].r = color.r;
  84.                     colours[j].g = color.g;
  85.                     colours[j].b = color.b;
  86.  
  87.                     colours[j].a -= strength;
  88.                     colours[j].a = Mathf.Clamp01(colours[j].a);
  89.                     //uv3s[j].x += strength;
  90.                     //uv3s[j].x = Mathf.Clamp01(uv3s[j].x);
  91.                 }
  92.             }
  93.  
  94.  
  95.         }
  96.         thisMesh.colors = colours; //apply the new colours
  97.     }
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment