I’ve got some code in a pixel shader that modifies the geometry of a water plane to create a simple sine wave wobble. I need to convert that same code into my regular program (Unityscript/Javascript) so that I can calculate the same values in my code easily to sit an object on the water without any collision calculations.
I’ve converted it myself, but run into a small problem. The sine wave oscillates a LOT faster in my javascript code than it does in my shader, which means something is calculated differently, or I’m doing something obvious wrong. When I change the time multiplier from 30 to about 1.5 in my code, it looks right, although this is an arbitrarily chosen value and I don’t know if it’s accurate, or just close by luck.
So is the _Time value in the shader the same as Time.time? The help file just says it’s a way to keep time. It doesn’t tell me what value it is giving. And I assume that the shader language is using radian values the same as the scripting does?
Shader code-
float phase = _Time * 30.0;
float4 wpos = mul( _Object2World, v.vertex);
float offset = (wpos.x + (wpos.z * 0.2)) * 0.5;
wpos.y = sin(phase + offset) * 0.1;
v.vertex = mul(_World2Object, wpos);
In game code-
water_phase = Time.time * 30.0;
water_offset = (block_transform.position.x + (block_transform.position.z * 0.2)) * 0.5;
water_y = Mathf.Sin(water_phase + water_offset) * 0.1;
block_transform.position.y = water_y;