/Assets/Scripts/InGameVertexPaint.cs

https://bitbucket.org/lostfictions/vertexpainting · C# · 101 lines · 64 code · 24 blank · 13 comment · 14 complexity · d00223b86e1d92e9befe4ba634c8a8ee MD5 · raw file

  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using Debug = UnityEngine.Debug;
  5. using Object = UnityEngine.Object;
  6. using Random = UnityEngine.Random;
  7. public class InGameVertexPaint : MonoBehaviour
  8. {
  9. public Color32 color;
  10. [Range(1f, 100f)]
  11. public float strength = 2f;
  12. //if true, checks if shader has "VertexPaint" tag set.
  13. public bool checkVertexPaintable = true;
  14. public bool drawDebugInfo = true;
  15. public Material autoConvertFrom;
  16. public Material autoConvertTo;
  17. void Start()
  18. {
  19. }
  20. void Update()
  21. {
  22. //TODO: should start checking within selection on mouse down, not
  23. //just checking continuously
  24. // if(!Input.GetMouseButton(0))
  25. // return;
  26. Ray ray = camera.ScreenPointToRay(Input.mousePosition);
  27. RaycastHit hit;
  28. if(!Physics.Raycast(ray, out hit))
  29. return;
  30. //TODO: at this point check cached transform
  31. // var mf = hit.transform.GetComponent<MeshFilter>();
  32. // if(!ren
  33. // || !mf
  34. // || !mf.mesh)
  35. // return;
  36. Renderer ren = hit.transform.renderer;
  37. MeshCollider mc = hit.collider as MeshCollider;
  38. if(mc == null
  39. || mc.sharedMesh == null
  40. || !ren.sharedMaterial)
  41. return;
  42. if(ren.sharedMaterial == autoConvertFrom)
  43. {
  44. ren.sharedMaterial = autoConvertTo;
  45. //HACK
  46. // mc.sharedMesh.colors32 = new Color32[mc.sharedMesh.vertices.Length];
  47. }
  48. else if(checkVertexPaintable && ren.sharedMaterial.GetTag("VertexPaint", false, "") == "")
  49. return;
  50. Mesh mesh = mc.sharedMesh;
  51. int[] triangles = mesh.triangles;
  52. var colors = mesh.colors32 ?? new Color32[mesh.vertices.Length];
  53. int i0 = triangles[hit.triangleIndex * 3 + 0];
  54. int i1 = triangles[hit.triangleIndex * 3 + 1];
  55. int i2 = triangles[hit.triangleIndex * 3 + 2];
  56. Vector3 bary = hit.barycentricCoordinate;
  57. colors[i0] = Color32.Lerp(colors[i0], color, bary.x * strength / 100f);
  58. colors[i1] = Color32.Lerp(colors[i1], color, bary.y * strength / 100f);
  59. colors[i2] = Color32.Lerp(colors[i2], color, bary.z * strength / 100f);
  60. mesh.colors32 = colors;
  61. if(drawDebugInfo)
  62. {
  63. var vertices = mesh.vertices;
  64. Transform hitTransform = hit.transform;
  65. Vector3 p0 = hitTransform.TransformPoint(vertices[i0]);
  66. Vector3 p1 = hitTransform.TransformPoint(vertices[i1]);
  67. Vector3 p2 = hitTransform.TransformPoint(vertices[i2]);
  68. Debug.DrawLine(p0, p1, Color.red, 0, false);
  69. Debug.DrawLine(p1, p2, Color.red, 0, false);
  70. Debug.DrawLine(p2, p0, Color.red, 0, false);
  71. if(Input.GetKeyDown("f"))
  72. {
  73. Debug.Log(hit.triangleIndex + "/" + triangles.Length / 3 + " (" + vertices.Length + " verts)");
  74. }
  75. }
  76. }
  77. }