MegaDisplace.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using UnityEngine;
  2. public enum MegaChannel
  3. {
  4. Red = 0,
  5. Green,
  6. Blue,
  7. Alpha,
  8. }
  9. // Could calc normals for say cylinder mapping etc like in Push
  10. [AddComponentMenu("Modifiers/Displace")]
  11. public class MegaDisplace : MegaModifier
  12. {
  13. public Texture2D map;
  14. public float amount = 0.0f;
  15. public Vector2 offset = Vector2.zero;
  16. public float vertical = 0.0f;
  17. public Vector2 scale = Vector2.one;
  18. public MegaChannel channel = MegaChannel.Red;
  19. public bool CentLum = true;
  20. public float CentVal = 0.5f;
  21. public float Decay = 0.0f;
  22. [HideInInspector]
  23. public Vector2[] uvs;
  24. [HideInInspector]
  25. public Vector3[] normals;
  26. public override string ModName() { return "Displace"; }
  27. public override string GetHelpURL() { return "?page_id=168"; }
  28. public override MegaModChannel ChannelsReq() { return MegaModChannel.Verts | MegaModChannel.UV; }
  29. public override MegaModChannel ChannelsChanged() { return MegaModChannel.Verts; }
  30. [ContextMenu("Init")]
  31. public virtual void Init()
  32. {
  33. MegaModifyObject mod = (MegaModifyObject)GetComponent<MegaModifyObject>();
  34. uvs = mod.cachedMesh.uv;
  35. normals = mod.cachedMesh.normals;
  36. }
  37. public override void MeshChanged()
  38. {
  39. Init();
  40. }
  41. public override Vector3 Map(int i, Vector3 p)
  42. {
  43. p = tm.MultiplyPoint3x4(p);
  44. if ( i >= 0 )
  45. {
  46. Vector2 uv = Vector2.Scale(uvs[i] + offset, scale);
  47. Color col = map.GetPixelBilinear(uv.x, uv.y); //uvs[i].x, uvs[i].y);
  48. float str = amount;
  49. if ( Decay != 0.0f )
  50. str *= (float)Mathf.Exp(-Decay * p.magnitude);
  51. if ( CentLum )
  52. str *= (col[(int)channel] + CentVal);
  53. else
  54. str *= (col[(int)channel]);
  55. float of = col[(int)channel] * str;
  56. p.x += (normals[i].x * of) + (normals[i].x * vertical);
  57. p.y += (normals[i].y * of) + (normals[i].y * vertical);
  58. p.z += (normals[i].z * of) + (normals[i].z * vertical);
  59. }
  60. return invtm.MultiplyPoint3x4(p);
  61. }
  62. public override void Modify(MegaModifiers mc)
  63. {
  64. for ( int i = 0; i < verts.Length; i++ )
  65. sverts[i] = Map(i, verts[i]);
  66. }
  67. public override bool ModLateUpdate(MegaModContext mc)
  68. {
  69. return Prepare(mc);
  70. }
  71. public override bool Prepare(MegaModContext mc)
  72. {
  73. if ( uvs == null || uvs.Length == 0 )
  74. uvs = mc.mod.mesh.uv;
  75. if ( normals == null || normals.Length == 0 )
  76. {
  77. MegaModifyObject mobj = (MegaModifyObject)GetComponent<MegaModifyObject>();
  78. if ( mobj )
  79. normals = mobj.cachedMesh.normals;
  80. else
  81. normals = mc.mod.mesh.normals;
  82. }
  83. if ( uvs.Length == 0 )
  84. return false;
  85. if ( normals.Length == 0 )
  86. return false;
  87. if ( map == null )
  88. return false;
  89. return true;
  90. }
  91. }