I've been learning Unity shader for couple of days and decided to port this oil shader from Shadertoy.
I get the following error:
incorrect number of arguments to numeric-type constructor at line 69 (on glcore)
Here is the relevant part of the shader code:
#define T (_Time/3.+5.)
fixed4 frag(v2f i) : SV_Target
{
float4 fragColor = 0;
float2 fragCoord = i.vertex.xy;
float4 k = fragColor;
float2 p = fragCoord;
float s = 1.;
#define rot(p,a) float2 sc = sin(float2(a,a + 1.6)); p *= float2x2(sc.y,-sc.x,sc);
#define A float3(0,1,157)
#define B {float2 m = frac(p),l = dot(p - m,A.yz) + A.xz,r = lerp(frac(57.*sin(l++)),frac(57.*sin(l)),(m *= m*(3. - m - m)).x); k += lerp(r.x,r.y,m.y) / (s += s); p *= float4(1,1,1,-1);}
p *= log(T) / R.y; // scaling (slow zoom out)
p.x += T; // translation
rot(p, T / 22.); //ERROR HERE
//......the rest of the code below
}
The rot(p, T / 22.);
line is where the error is at. The funny thing is that when I remove or comment-out this line of code, the shader compiles and is working fine. I still want to know why that line can't compile.
What's causing that error and how can I fix it?
EDIT:
As pointed out by helium, float3x3
is a matrix.
Below is the original rot function:
#define rot(p,a) vec2 sc=sin(vec2(a,a+1.6)); p*=mat2(sc.y,-sc.x,sc);
And the ported version:
#define rot(p,a) float2 sc = sin(float2(a,a + 1.6)); p *= float2x2(sc.y,-sc.x,sc);
Notice how I replaced mat2
with float2x2
because that's the equivalent type in HLSL.
I do believe that the problem is from float2x2(sc.y,-sc.x,sc);
. What is the appropriate way to do this and still use the 3
parameters as the original code did?
The built-in value _Time
is a float4
so you have to pick one of the components depending on the time scale you want:
_Time float4 (t/20, t, t*2, t*3), use to animate things inside the shaders.
for example #define T (_Time.y/3.+5.)
.
Another thing is the *
operator for float2x2
represents a component-wise product of 2 matrices. For vector-matrix/matrix-matrix product you should use the mul
function.
So in the rot
function you should have p =mul(p, float2x2(sc.y,-sc.x,sc));
.
For more info on this you can go to: https://en.wikibooks.org/wiki/Cg_Programming/Vector_and_Matrix_Operations.