Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class VertexTweaker : MonoBehaviour
- {
- private Mesh thisMesh;
- private Vector3[] vertexPositionsWorld;
- private Vector3[] vertexNormalsWorld;
- private float distanceToChanger = 100f;
- private Color[] colours;
- private float vertexCount = 0;
- public void ClearVertexColours()
- {
- if (thisMesh == null)
- thisMesh = GetComponent<MeshFilter>().mesh;
- colours = new Color[thisMesh.vertexCount];
- for (int j = 0; j < thisMesh.vertexCount; j++)
- {
- colours[j] = Color.black;
- //colours[j].a = 0f; // you can do this, but I've just tweaked the shader so A is inverted, A == 1 is no blood
- }
- thisMesh.colors = colours;
- }
- void Awake()
- {
- thisMesh = GetComponent<MeshFilter>().mesh;
- colours = new Color[thisMesh.vertexCount];
- ClearVertexColours();
- vertexPositionsWorld = new Vector3[thisMesh.vertexCount];
- vertexNormalsWorld = new Vector3[thisMesh.vertexCount];
- for (int j = 0; j < thisMesh.vertexCount; j++)
- {
- vertexPositionsWorld[j] = transform.TransformPoint(thisMesh.vertices[j]);
- //vertexNormalsWorld[j] = transform.TransformDirection(thisMesh.normals[j]);
- }
- vertexNormalsWorld = thisMesh.normals;
- vertexCount = thisMesh.vertexCount;
- }
- public void ChangeInArea(Color color, Vector3 position, float radius, float strength)
- {
- colours = thisMesh.colors;
- for (int j = 0; j < vertexCount; j++)
- {
- distanceToChanger = (vertexPositionsWorld[j] - position).magnitude;
- if (distanceToChanger < radius)
- {
- colours[j].r = color.r;
- colours[j].g = color.g;
- colours[j].b = color.b;
- colours[j].a -= strength;
- colours[j].a = Mathf.Clamp01(colours[j].a);
- }
- }
- thisMesh.colors = colours;
- }
- public void ChangeInArea(Color color, Vector3 position, float radius, float strength, Vector3 direction)
- {
- colours = thisMesh.colors;
- direction = direction.normalized;
- float facingDot = 0;
- for (int j = 0; j < vertexCount; j++) //iterate through each vertex
- {
- distanceToChanger = (vertexPositionsWorld[j]- position).sqrMagnitude;
- facingDot = Vector3.Dot(direction, vertexNormalsWorld[j]);
- if (distanceToChanger < radius)
- {
- if (facingDot < 0.5f)
- {
- colours[j].r = color.r;
- colours[j].g = color.g;
- colours[j].b = color.b;
- colours[j].a -= strength;
- colours[j].a = Mathf.Clamp01(colours[j].a);
- //uv3s[j].x += strength;
- //uv3s[j].x = Mathf.Clamp01(uv3s[j].x);
- }
- }
- }
- thisMesh.colors = colours; //apply the new colours
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment