smkplus / ShaderMan

Convert ShaderToy to Unity HLSL/CG
https://smkplus.github.io/ShaderMan.io
MIT License
1.39k stars 196 forks source link

Using transformation matrix leads to type mismatch #22

Open alexNecroJack opened 3 years ago

alexNecroJack commented 3 years ago

The error is as described in this post: https://forum.unity.com/threads/using-transformation-matrix-leads-to-type-mismatch.466548/

I met the error trying to convert this shader: https://www.shadertoy.com/view/XlfGRj

The actual code part in which the bug lies was the following. After conversion I was given:

fixed3 dir=fixed3(uv*zoom,1.);
//... ... ...
fixed2x2 rot1=fixed2x2(cos(a1),sin(a1),-sin(a1),cos(a1));
//... ... ...
dir.xz*=rot1;

While the last line from the result as shown above, should be like this in order to work in Unity:

//...
dir.xz = mul(rot1, dir.xz); //dir.xz*=rot1;
smkplus commented 3 years ago

Hey @alexNecroJack I converted your shader to unity

image

Shader "Hidden/Test"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;

            // Star Nest by Pablo Roman Andrioli

            // This content is under the MIT License.

            #define iterations 17
            #define formuparam 0.53

            #define volsteps 20
            #define stepsize 0.1

            #define zoom   0.800
            #define tile   0.850
            #define speed  0.010 

            #define brightness 0.0015
            #define darkmatter 0.300
            #define distfading 0.730
            #define saturation 0.850

            float4 iMouse;

            fixed4 frag (v2f i) : SV_Target
            {

            //get coords and direction
            fixed2 uv=i.uv.xy;
            fixed3 dir=fixed3(uv*zoom,1.);
            float time=_Time.y*speed+.25;

            //mouse rotation
            float a1=.5+iMouse.x;
            float a2=.8+iMouse.y;
            fixed2x2 rot1=fixed2x2(cos(a1),sin(a1),-sin(a1),cos(a1));
            fixed2x2 rot2=fixed2x2(cos(a2),sin(a2),-sin(a2),cos(a2));
            dir.xz = mul(rot1,dir.xz);
            dir.xz = mul(rot2,dir.xz);
            fixed3 from=fixed3(1.,.5,0.5);
            from+=fixed3(time*2.,time,-2.);
            from.xz = mul(rot1,from.xz);
            from.xy = mul(rot2,from.xy);

            //volumetric rendering
            float s=0.1,fade=1.;
            fixed3 v=fixed3(0.,0.,0.);
            for (int r=0; r<volsteps; r++) {
                fixed3 p=from+s*dir*.5;
                p = abs(fixed3(tile,tile,tile)-fmod(p,fixed3(tile*2.,tile*2.,tile*2.))); // tiling fold
                float pa,a=pa=0.;
                for (int i=0; i<iterations; i++) { 
                    p=abs(p)/dot(p,p)-formuparam; // the magic formula
                    a+=abs(length(p)-pa); // absolute sum of average change
                    pa=length(p);
                }
                float dm=max(0.,darkmatter-a*a*.001); //dark matter
                a*=a*a; // add contrast
                if (r>6) fade*=1.-dm; // dark matter, don't render near
                //v+=fixed3(dm,dm*.5,0.);
                v+=fade;
                v+=fixed3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
                fade*=distfading; // distance fading
                s+=stepsize;
            }
            v=lerp(fixed3(length(v),length(v),length(v)),v,saturation); //color adjust
            return fixed4(v*.01,1.);    

            }
            ENDCG
        }
    }
}