MegaDisplaceRT.cs 2.7 KB

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