topical media & game development

talk show tell print

lib-unity-tutorial-m2h-2-Assets-Standard-Assets-Water-Sources-WaterSimple.cs / cs



  using UnityEngine;
  
  // Sets up transformation matrices to scale&scroll water waves
  // for the case where graphics card does not support vertex programs.
  
  [ExecuteInEditMode]
  public class WaterSimple : MonoBehaviour
  {
          void Update()
          {
                  if( !renderer )
                          return;
                  Material mat = renderer.sharedMaterial;
                  if( !mat )
                          return;
                          
                  Vector4 waveSpeed = mat.GetVector( "WaveSpeed" );
                  float waveScale = mat.GetFloat( "_WaveScale" );
                  float t = Time.time / 20.0f;
                  
                  Vector4 offset4 = waveSpeed * (t * waveScale);
                  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));
                  mat.SetVector( "_WaveOffset", offsetClamped );
                  
                  Vector3 scale = new Vector3( 1.0f/waveScale, 1.0f/waveScale, 1 );
                  Matrix4x4 scrollMatrix = Matrix4x4.TRS( new Vector3(offsetClamped.x,offsetClamped.y,0), Quaternion.identity, scale );
                  mat.SetMatrix( "_WaveMatrix", scrollMatrix );
                                  
                  scrollMatrix = Matrix4x4.TRS( new Vector3(offsetClamped.z,offsetClamped.w,0), Quaternion.identity, scale * 0.45f );
                  mat.SetMatrix( "_WaveMatrix2", scrollMatrix );
          }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.