How can I convert these fragColor code snippets from ShaderToy so that they will work in Unity?

笑着哭i 提交于 2019-12-13 03:45:25

问题


I'm following this tutorial: https://www.youtube.com/watch?v=CzORVWFvZ28 to convert some code from ShaderToy to Unity. This is the shader that I'm attempting to convert: https://www.shadertoy.com/view/Ws23WD.

I saw that in the tutorial, he was able to take his fragColor statement from ShaderToy and simply return a color in Unity instead. However, when I tried doing that with the code that I have from ShaderToy, an error about not being able to implicitly convert from float3 to float4 popped up. I saw that my color variable is being declared as a float3 which is what must be causing the issue, but I need some help figuring out how to fix this.

I also noticed that I have an 'a' value with the fragColor variable, in addition to the rgb values; would I use a float4 to take in the (r, g, b, a) values?

fixed4 frag (v2f i) : SV_Target
            {                                
                //float2 uv = float2(fragCoord.x / iResolution.x, fragCoord.y / iResolution.y);
                float2 uv = float2(i.uv);

                uv -= 0.5;
                //uv /= float2(iResolution.y / iResolution.x, 1);

                float3 cam = float3(0, -0.15, -3.5);
                float3 dir = normalize(float3(uv,1));

                float cam_a2 = sin(_Time.y) * pi * 0.1;
                cam.yz = rotate(cam.yz, cam_a2);
                dir.yz = rotate(dir.yz, cam_a2);

                float cam_a = _Time.y * pi * 0.1;
                cam.xz = rotate(cam.xz, cam_a);
                dir.xz = rotate(dir.xz, cam_a);

                float3 color = float3(0.16, 0.12, 0.10);
                float t = 0.00001;
                const int maxSteps = 128;
                for(int i = 0; i < maxSteps; ++i) {
                float3 p = cam + dir * t;
                float d = scene(p);

                if(d < 0.0001 * t) {
                    color = float3(1.0, length(p) * (0.6 + (sin(_Time.y*3.0)+1.0) * 0.5 * 0.4), 0);
                    break;
                }

                t += d;
            }

            //fragColor.rgb = color;
            return color;
            //fragColor.a = 1.0;
        }

来源:https://stackoverflow.com/questions/54543839/how-can-i-convert-these-fragcolor-code-snippets-from-shadertoy-so-that-they-will

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!