MegaPush.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. public enum MegaNormType
  3. {
  4. Normals = 0,
  5. Vertices,
  6. Average,
  7. }
  8. [AddComponentMenu("Modifiers/Push")]
  9. public class MegaPush : MegaModifier
  10. {
  11. public float amount = 0.0f;
  12. public MegaNormType method = MegaNormType.Normals;
  13. Vector3[] normals;
  14. public override string ModName() { return "Push"; }
  15. public override string GetHelpURL() { return "?page_id=282"; }
  16. public override Vector3 Map(int i, Vector3 p)
  17. {
  18. if ( i >= 0 )
  19. p += normals[i] * amount;
  20. return p;
  21. }
  22. void CalcNormals(Mesh mesh)
  23. {
  24. if ( mesh != null )
  25. {
  26. switch ( method )
  27. {
  28. case MegaNormType.Normals:
  29. normals = mesh.normals;
  30. break;
  31. case MegaNormType.Vertices:
  32. normals = new Vector3[mesh.normals.Length];
  33. for ( int i = 0; i < mesh.vertexCount; i++ )
  34. normals[i] = Vector3.Normalize(mesh.vertices[i]);
  35. break;
  36. case MegaNormType.Average:
  37. normals = mesh.normals;
  38. for ( int i = 0; i < mesh.vertexCount; i++ )
  39. {
  40. for ( int j = 0; j < mesh.vertexCount; j++ )
  41. {
  42. if ( mesh.vertices[i] == mesh.vertices[j] )
  43. {
  44. normals[i] = (normals[i] + normals[j]) / 2.0f;
  45. normals[j] = (normals[i] + normals[j]) / 2.0f;
  46. }
  47. }
  48. }
  49. break;
  50. }
  51. }
  52. }
  53. public override void ModStart(MegaModifiers mc)
  54. {
  55. CalcNormals(mc.mesh);
  56. }
  57. public override bool ModLateUpdate(MegaModContext mc)
  58. {
  59. return Prepare(mc);
  60. }
  61. public override bool Prepare(MegaModContext mc)
  62. {
  63. if ( normals != null )
  64. return true;
  65. return false;
  66. }
  67. void Reset()
  68. {
  69. Renderer rend = GetComponent<Renderer>();
  70. if ( rend != null )
  71. {
  72. Mesh ms = MegaUtils.GetSharedMesh(gameObject);
  73. if ( ms != null )
  74. {
  75. CalcNormals(ms);
  76. Bounds b = ms.bounds;
  77. Offset = -b.center;
  78. bbox.min = b.center - b.extents;
  79. bbox.max = b.center + b.extents;
  80. }
  81. }
  82. }
  83. }