You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.1 KiB

  1. using UnityEngine;
  2. // Sets up transformation matrices to scale&scroll water waves
  3. // for the case where graphics card does not support vertex programs.
  4. [ExecuteInEditMode]
  5. public class WaterSimple : MonoBehaviour
  6. {
  7. void Update()
  8. {
  9. if( !GetComponent<Renderer>() )
  10. return;
  11. Material mat = GetComponent<Renderer>().sharedMaterial;
  12. if( !mat )
  13. return;
  14. Vector4 waveSpeed = mat.GetVector( "WaveSpeed" );
  15. float waveScale = mat.GetFloat( "_WaveScale" );
  16. float t = Time.time / 20.0f;
  17. Vector4 offset4 = waveSpeed * (t * waveScale);
  18. Vector4 offsetClamped = new Vector4(Mathf.Repeat(offset4.x,1.0f), Mathf.Repeat(offset4.y,1.0f), Mathf.Repeat(offset4.z,1.0f), Mathf.Repeat(offset4.w,1.0f));
  19. mat.SetVector( "_WaveOffset", offsetClamped );
  20. Vector3 scale = new Vector3( 1.0f/waveScale, 1.0f/waveScale, 1 );
  21. Matrix4x4 scrollMatrix = Matrix4x4.TRS( new Vector3(offsetClamped.x,offsetClamped.y,0), Quaternion.identity, scale );
  22. mat.SetMatrix( "_WaveMatrix", scrollMatrix );
  23. scrollMatrix = Matrix4x4.TRS( new Vector3(offsetClamped.z,offsetClamped.w,0), Quaternion.identity, scale * 0.45f );
  24. mat.SetMatrix( "_WaveMatrix2", scrollMatrix );
  25. }
  26. }