MegaDisplaceWebCam.cs 2.7 KB

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