Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Converting a Shadertoy Multipass shader to Unity / HLSL ?

Discussion in 'Shaders' started by glennmarshall, Jul 26, 2016.

  1. glennmarshall

    glennmarshall

    Joined:
    Apr 19, 2016
    Posts:
    6
    As a big Shadertoy fan - I've been enjoying porting these shaders into other platforms such as Processing (www.processing.org) - from here I have lots of control over animation and render output.

    The new multipass feature on Shadertoy however is causing me problems with Processing. Shadertoy uses 16 bit image buffers to render data to, such as this shader,
    https://www.shadertoy.com/view/lsy3R1

    But Processing can only create standard 8 bit image buffers. So I'm now trying to convert these mulitpass shaders into Unity - hoping I can use an 16 bit image buffer.

    I have managed to get basic single pass Shadertoy examples working in Unity using this,
    https://github.com/candycat1992/Shadertoy_Lab

    But now I'm stuck - I'm new to Unity - and to HLSL. Could anyone tell me how to code a Unity shader which can do multipass, i.e. rendering into a 16 bit image first, then a final output pass - exactly like the shader above.

    Thanks!
    Glenn.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,977
  3. glennmarshall

    glennmarshall

    Joined:
    Apr 19, 2016
    Posts:
    6
    Thanks! will look into this... :)
     
  4. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    SKIBMT, 4sascha, Mauri and 3 others like this.
  5. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    OMG. Perfect. I try to help. What is needed?
     
  6. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    Hi,

    could you help me with
    https://www.shadertoy.com/view/4dfXDn

    i corrected

    vec2 rotateCCW(vec2 p, float a)
    {
    mat2 m = mat2(cos(a), sin(a), -sin(a), cos(a));
    return p * m;
    }


    vec2 rotateCW(vec2 p, float a)
    {
    mat2 m = mat2(cos(a), -sin(a), sin(a), cos(a));
    return p * m;
    }

    to....................................


    fixed2 rotateCCW(fixed2 p, fixed a)
    {
    fixed2x2 m = fixed2x2(cos(a), sin(a), -sin(a), cos(a));
    return mul(p,m);
    }

    fixed2 rotateCW(fixed2 p, fixed a)
    {
    fixed2x2 m = fixed2x2(cos(a), -sin(a), sin(a), cos(a));
    return mul(p,m);
    }

    manually and then i get this error

    Output value 'vert' is not completely initialized
    Compiling Vertex program
     
  7. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    Hi I'm glad if you help me to improve this converter
    I will Put ToDo list of needed in my github
     
    Last edited: Nov 21, 2017
  8. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    This is incomplete but I hope help you...
    download this shader:
     

    Attached Files:

  9. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader"ShaderMan/MyShader"{
    4. Properties
    5. {
    6. _iResolution("iResolution", Vector) = (1,1,0,0)
    7. _iMouse("iMouse", Vector) = (0,0,0,0)
    8. }
    9. SubShader{
    10. Pass{
    11. CGPROGRAM
    12. #pragma vertex vert
    13. #pragma fragment frag
    14. #pragma fragmentoption ARB_precision_hint_fastest
    15. #include "UnityCG.cginc"
    16. struct appdata
    17. {
    18.     float4 vertex : POSITION;
    19.     float2 uv : TEXCOORD0;
    20. };
    21.  
    22. uniform fixed3         _iResolution;     // viewport resolution in pixels
    23. uniform fixed4     fragColor;
    24. uniform fixed      iChannelTime[4];// channel playback time (in seconds)
    25. uniform fixed3     iChannelResolution[4];// channel resolution (in pixels)
    26. uniform fixed4     _iMouse;// mouse pixel coords. xy: current (if MLB down), zw: click
    27. uniform fixed4     iDate;// (year, month, day, time in seconds)
    28. uniform fixed      iSampleRate;// sound sample rate (i.e., 44100)
    29.  
    30.  
    31.  
    32. struct v2f
    33. {
    34.     float2 uv : TEXCOORD0;
    35.     float4 vertex : SV_POSITION;
    36.     float4 screenCoord : TEXCOORD1;
    37. };
    38.  
    39. v2f vert(appdata v)
    40. {
    41.     v2f o;
    42.     o.vertex = UnityObjectToClipPos(v.vertex);
    43.     o.uv = v.uv;
    44.     o.screenCoord.xy = ComputeScreenPos(o.vertex);
    45.     return o;
    46. }
    47.  
    48.  
    49. fixed smoothMerge(fixed d1, fixed d2, fixed k)
    50. {
    51.     fixed h = clamp(0.5 + 0.5*(d2 - d1)/k, 0.0, 1.0);
    52.     return lerp(d2, d1, h) - k * h * (1.0-h);
    53. }
    54.  
    55.  
    56. fixed merge(fixed d1, fixed d2)
    57. {
    58.     return min(d1, d2);
    59. }
    60.  
    61.  
    62. fixed mergeExclude(fixed d1, fixed d2)
    63. {
    64.     return min(max(-d1, d2), max(-d2, d1));
    65. }
    66.  
    67.  
    68. fixed substract(fixed d1, fixed d2)
    69. {
    70.     return max(-d1, d2);
    71. }
    72.  
    73.  
    74. fixed intersect(fixed d1, fixed d2)
    75. {
    76.     return max(d1, d2);
    77. }
    78.  
    79.  
    80.  
    81.  
    82.  
    83. fixed2 rotateCCW(fixed2 p, fixed a)
    84. {
    85.     fixed2x2 m = fixed2x2(cos(a), sin(a), -sin(a), cos(a));
    86.     return mul(p,m);  
    87. }
    88.  
    89.  
    90. fixed2 rotateCW(fixed2 p, fixed a)
    91. {
    92.     fixed2x2 m = fixed2x2(cos(a), -sin(a), sin(a), cos(a));
    93.     return mul(p,m);
    94. }
    95.  
    96.  
    97. fixed2 translate(fixed2 p, fixed2 t)
    98. {
    99.     return p - t;
    100. }
    101.  
    102.  
    103.  
    104.  
    105.  
    106. fixed pie(fixed2 p, fixed angle)
    107. {
    108.     angle = radians(angle) / 2.0;
    109.     fixed2 n = fixed2(cos(angle), sin(angle));
    110.     return abs(p).x * n.x + p.y*n.y;
    111. }
    112.  
    113.  
    114. fixed circleDist(fixed2 p, fixed radius)
    115. {
    116.     return length(p) - radius;
    117. }
    118.  
    119.  
    120. fixed triangleDist(fixed2 p, fixed radius)
    121. {
    122.     return max(    abs(p).x * 0.866025 +
    123.                    p.y * 0.5, -p.y)
    124.                 -radius * 0.5;
    125. }
    126.  
    127.  
    128. fixed triangleDist(fixed2 p, fixed width, fixed height)
    129. {
    130.     fixed2 n = normalize(fixed2(height, width / 2.0));
    131.     return max(    abs(p).x*n.x + p.y*n.y - (height*n.y), -p.y);
    132. }
    133.  
    134.  
    135. fixed semiCircleDist(fixed2 p, fixed radius, fixed angle, fixed width)
    136. {
    137.     width /= 2.0;
    138.     radius -= width;
    139.     return substract(pie(p, angle),
    140.                      abs(circleDist(p, radius)) - width);
    141. }
    142.  
    143.  
    144. fixed boxDist(fixed2 p, fixed2 size, fixed radius)
    145. {
    146.     size -= fixed2(radius,radius);
    147.     fixed2 d = abs(p) - size;
    148.       return min(max(d.x, d.y), 0.0) + length(max(d, 0.0)) - radius;
    149. }
    150.  
    151.  
    152. fixed lineDist(fixed2 p, fixed2 start, fixed2 end, fixed width)
    153. {
    154.     fixed2 dir = start - end;
    155.     fixed lngth = length(dir);
    156.     dir /= lngth;
    157.     fixed2 proj = max(0.0, min(lngth, dot((start - p), dir))) * dir;
    158.     return length( (start - p) - proj ) - (width / 2.0);
    159. }
    160.  
    161.  
    162.  
    163.  
    164. fixed fillMask(fixed dist)
    165. {
    166.     return clamp(-dist, 0.0, 1.0);
    167. }
    168.  
    169.  
    170. fixed innerBorderMask(fixed dist, fixed width)
    171. {
    172.     //dist += 1.0;
    173.     fixed alpha1 = clamp(dist + width, 0.0, 1.0);
    174.     fixed alpha2 = clamp(dist, 0.0, 1.0);
    175.     return alpha1 - alpha2;
    176. }
    177.  
    178.  
    179. fixed outerBorderMask(fixed dist, fixed width)
    180. {
    181.     //dist += 1.0;
    182.     fixed alpha1 = clamp(dist, 0.0, 1.0);
    183.     fixed alpha2 = clamp(dist - width, 0.0, 1.0);
    184.     return alpha1 - alpha2;
    185. }
    186.  
    187.  
    188.  
    189.  
    190. fixed sceneDist(fixed2 p)
    191. {
    192.     fixed c = circleDist(        translate(p, fixed2(100, 250)), 40.0);
    193.     fixed b1 =  boxDist(        translate(p, fixed2(200, 250)), fixed2(40, 40),     0.0);
    194.     fixed b2 =  boxDist(        translate(p, fixed2(300, 250)), fixed2(40, 40),     10.0);
    195.     fixed l = lineDist(            p,              fixed2(370, 220),  fixed2(430, 280),    10.0);
    196.     fixed t1 = triangleDist(    translate(p, fixed2(500, 210)), 80.0,             80.0);
    197.     fixed t2 = triangleDist(    rotateCW(translate(p, fixed2(600, 250)), _Time.y), 40.0);
    198.    
    199.     fixed m =     merge(c, b1);
    200.     m =         merge(m, b2);
    201.     m =         merge(m, l);
    202.     m =         merge(m, t1);
    203.     m =         merge(m, t2);
    204.    
    205.     fixed b3 = boxDist(        translate(p, fixed2(100, sin(_Time.y * 3.0 + 1.0) * 40.0 + 100.0)),
    206.                                fixed2(40, 15),     0.0);
    207.     fixed c2 = circleDist(    translate(p, fixed2(100, 100)),    30.0);
    208.     fixed s = substract(b3, c2);
    209.    
    210.     fixed b4 = boxDist(        translate(p, fixed2(200, sin(_Time.y * 3.0 + 2.0) * 40.0 + 100.0)),
    211.                                fixed2(40, 15),     0.0);
    212.     fixed c3 = circleDist(    translate(p, fixed2(200, 100)),     30.0);
    213.     fixed i = intersect(b4, c3);
    214.    
    215.     fixed b5 = boxDist(        translate(p, fixed2(300, sin(_Time.y * 3.0 + 3.0) * 40.0 + 100.0)),
    216.                                fixed2(40, 15),     0.0);
    217.     fixed c4 = circleDist(    translate(p, fixed2(300, 100)),     30.0);
    218.     fixed a = merge(b5, c4);
    219.    
    220.     fixed b6 = boxDist(        translate(p, fixed2(400, 100)),    fixed2(40, 15),     0.0);
    221.     fixed c5 = circleDist(    translate(p, fixed2(400, 100)),     30.0);
    222.     fixed sm = smoothMerge(b6, c5, 10.0);
    223.    
    224.     fixed sc = semiCircleDist(translate(p, fixed2(500,100)), 40.0, 90.0, 10.0);
    225.    
    226.     fixed b7 = boxDist(        translate(p, fixed2(600, sin(_Time.y * 3.0 + 3.0) * 40.0 + 100.0)),
    227.                                fixed2(40, 15),     0.0);
    228.     fixed c6 = circleDist(    translate(p, fixed2(600, 100)),     30.0);
    229.     fixed e = mergeExclude(b7, c6);
    230.    
    231.     m = merge(m, s);
    232.     m = merge(m, i);
    233.     m = merge(m, a);
    234.     m = merge(m, sm);
    235.     m = merge(m, sc);
    236.     m = merge(m, e);
    237.    
    238.     return m;
    239. }
    240.  
    241.  
    242. fixed sceneSmooth(fixed2 p, fixed r)
    243. {
    244.     fixed accum = sceneDist(p);
    245.     accum += sceneDist(p + fixed2(0.0, r));
    246.     accum += sceneDist(p + fixed2(0.0, -r));
    247.     accum += sceneDist(p + fixed2(r, 0.0));
    248.     accum += sceneDist(p + fixed2(-r, 0.0));
    249.     return accum / 5.0;
    250. }
    251.  
    252.  
    253.  
    254.  
    255.  
    256. fixed shadow(fixed2 p, fixed2 pos, fixed radius)
    257. {
    258.     fixed2 dir = normalize(pos - p);
    259.     fixed dl = length(p - pos);
    260.    
    261.     // fracion of light visible, starts at one radius (second fixed added in the end);
    262.     fixed lf = radius * dl;
    263.    
    264.     // distance traveled
    265.     fixed dt = 0.01;
    266.  
    267.     for (int i = 0; i < 64; ++i)
    268.     {              
    269.         // distance to scene at current position
    270.         fixed sd = sceneDist(p + dir * dt);
    271.  
    272.         // early out when this ray is guaranteed to be full shadow
    273.         if (sd < -radius)
    274.             return 0.0;
    275.        
    276.         // width of cone-overlap at light
    277.         // 0 in center, so 50% overlap: add one radius outside of loop to get total coverage
    278.         // should be '(sd / dt) * dl', but '*dl' outside of loop
    279.         lf = min(lf, sd / dt);
    280.        
    281.         // move ahead
    282.         dt += max(1.0, abs(sd));
    283.         if (dt > dl) break;
    284.     }
    285.  
    286.     // multiply by dl to get the real projected overlap (moved out of loop)
    287.     // add one radius, before between -radius and + radius
    288.     // normalize to 1 ( / 2*radius)
    289.     lf = clamp((lf*dl + radius) / (2.0 * radius), 0.0, 1.0);
    290.     lf = smoothstep(0.0, 1.0, lf);
    291.     return lf;
    292. }
    293.  
    294.  
    295.  
    296. fixed4 drawLight(fixed2 p, fixed2 pos, fixed4 color, fixed dist, fixed range, fixed radius)
    297. {
    298.     // distance to light
    299.     fixed ld = length(p - pos);
    300.    
    301.     // out of range
    302.     if (ld > range) return fixed4(0.0,0.0,0.0,0.0);
    303.    
    304.     // shadow and falloff
    305.     fixed shad = shadow(p, pos, radius);
    306.     fixed fall = (range - ld)/range;
    307.     fall *= fall;
    308.     fixed source = fillMask(circleDist(p - pos, radius));
    309.     return (shad * fall + source) * color;
    310. }
    311.  
    312.  
    313. fixed luminance(fixed4 col)
    314. {
    315.     return 0.2126 * col.r + 0.7152 * col.g + 0.0722 * col.b;
    316. }
    317.  
    318.  
    319. void setLuminance(inout fixed4 col, fixed lum)
    320. {
    321.     lum /= luminance(col);
    322.     col *= lum;
    323. }
    324.  
    325.  
    326. fixed AO(fixed2 p, fixed dist, fixed radius, fixed intensity)
    327. {
    328.     fixed a = clamp(dist / radius, 0.0, 1.0) - 1.0;
    329.     return 1.0 - (pow(abs(a), 5.0) + 1.0) * intensity + (1.0 - intensity);
    330.     return smoothstep(0.0, 1.0, dist / radius);
    331. }
    332.  
    333.  
    334.  
    335.  
    336. fixed4 frag(v2f i) : SV_Target{
    337.  
    338. {
    339.     fixed2 p = i.uv.xy * _iResolution.xy + fixed2(0.5,0.5);
    340.     //fixed2 p = i.uv.xy * 512.0 + fixed2(0.5,0.5);
    341.     fixed2 c = _iResolution.xy / 2.0;
    342.  
    343.     //fixed dist = sceneSmooth(p, 5.0);
    344.     fixed dist = sceneDist(p);
    345.    
    346.     fixed2 light1Pos = _iMouse.xy;
    347.     //fixed2 light1Pos =  fixed4(0,0,0,0);
    348.     fixed4 light1Col = fixed4(0.75, 1.0, 0.5, 1.0);
    349.     setLuminance(light1Col, 0.4);
    350.    
    351.     fixed2 light2Pos = fixed2(_iResolution.x * (sin(_Time.y + 3.1415) + 1.2) / 2.4, 175.0);
    352.     fixed4 light2Col = fixed4(1.0, 0.75, 0.5, 1.0);
    353.     setLuminance(light2Col, 0.5);
    354.    
    355.     fixed2 light3Pos = fixed2(_iResolution.x * (sin(_Time.y) + 1.2) / 2.4, 340.0);
    356.     fixed4 light3Col = fixed4(0.5, 0.75, 1.0, 1.0);
    357.     setLuminance(light3Col, 0.6);
    358.    
    359.     // gradient
    360.     fixed4 col = fixed4(0.5, 0.5, 0.5, 1.0) * (1.0 - length(c - p)/_iResolution.x);
    361.     // grid
    362.     col *= clamp(min(fmod(p.y, 10.0), fmod(p.x, 10.0)), 0.9, 1.0);
    363.     // ambient occlusion
    364.     col *= AO(p, sceneSmooth(p, 10.0), 40.0, 0.4);
    365.     //col *= 1.0-AO(p, sceneDist(p), 40.0, 1.0);
    366.     // light
    367.     col += drawLight(p, light1Pos, light1Col, dist, 150.0, 6.0);
    368.     col += drawLight(p, light2Pos, light2Col, dist, 200.0, 8.0);
    369.     col += drawLight(p, light3Pos, light3Col, dist, 300.0, 12.0);
    370.     // shape fill
    371.     col = lerp(col, fixed4(0.5, 0.5, 0.5, 1.0), fillMask(dist));
    372.     // shape outline
    373.     col = lerp(col, fixed4(0.4, 0.4, 0.4, 1.0), innerBorderMask(dist, 1.5));
    374.  
    375.     return  clamp(col, 0.0, 1.0);
    376. }
    377.  
    378. }ENDCG
    379. }
    380. }
    381. }
    382.  
    383.  
    Code (CSharp):
    1.  
     
  10. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class RaycastInfoToShader : MonoBehaviour
    7. {
    8.     void Update ()
    9.     {
    10.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    11.         RaycastHit hit;
    12.         if (Physics.Raycast(ray, out hit)) {
    13.             Vector4 iResolution = GetComponent<Renderer>().material.GetVector ("_iResolution");
    14.  
    15.             Vector4 iMouse = new Vector4(hit.textureCoord.x * iResolution.x, hit.textureCoord.y * iResolution.y, 0, 0);
    16.  
    17.             GetComponent<Renderer> ().material.SetVector ("_iMouse", iMouse);
    18.         }
    19.     }
    20. }
    21.  
     
    Last edited: Nov 23, 2017
  11. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    Thanks.
    This is the working version. ShaderMan/MyShader
    Use this and the RaycastInfoToShader.cs on the plane for mouse movement.
     
    Last edited: Nov 22, 2017
  12. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShaderToy : MonoBehaviour
    5. {
    6.     public float speed = 1;
    7.  
    8.     public int horizontalResolution = 320;
    9.     public int verticalResolution = 240;
    10.  
    11.     public Material Material = null;
    12.  
    13.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    14.     {
    15.         Material.SetFloat("_Speed", speed);
    16.         //Graphics.Blit(source, destination, Material);
    17.  
    18.         RenderTexture scaled = RenderTexture.GetTemporary(horizontalResolution, verticalResolution);
    19.         Graphics.Blit(source, scaled, Material);
    20.         Graphics.Blit(scaled, destination);
    21.         RenderTexture.ReleaseTemporary(scaled);
    22.     }
    23. }  
     
    Last edited: Nov 23, 2017
  13. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    You could also hang ShaderToy.cs to the camera and put any of your materials in.
    Then it renders your shadertoy shaders to the camera.
     
  14. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    next working on the fft sound input
     
  15. DigitalEmotions

    DigitalEmotions

    Joined:
    Feb 18, 2014
    Posts:
    6
  16. DigitalEmotions

    DigitalEmotions

    Joined:
    Feb 18, 2014
    Posts:
    6
    hello again :)

    there is bug in matrix multiplication conversion ... after conversion we have a*m but must be mul(a,m)
     
  17. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    Hello,

    i think you cannot replace mod with fmod
    in CodeFix.cs 241
    Input = Regex.Replace(Input, "mod", "fmod");

    for positive numbers is ok,
    for negative numbers not

    fmod (HLSL) will output a positive number
    mod (GLSL) will output a negative number

    you can define a macro
    #define ModFix(x, y) (x - y * floor(x / y))

    found it here after some hours of wondering why
    https://forum.unity.com/threads/center-uv-co-ordinates-glsl-cg-hlsl.452233/
     
    Last edited: Dec 16, 2017
  18. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    Hey,

    the fft sound is nearly done.
    I have some problems to have correct output for this great shader..

    https://www.shadertoy.com/view/Msl3Rr

    Here s i my fix so far.
    Probably you have an idea.

    Works so far in an XRAY Variant.)



    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2. //https://www.shadertoy.com/view/Msl3Rr
    3. Shader"ShaderMan/Cubescape"
    4. {
    5.     Properties
    6.     {
    7.         _MainTex("_MainTex", 2D) = "white"{}
    8.         _SecondTex("SecondTex", 2D) = "white"{}
    9.         _iMouse("iMouse", Vector) = (0,0,0,0)
    10.         _iResolution("iResolution", Vector) = (1,1,0,0)
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #pragma fragmentoption ARB_precision_hint_fastest
    21.             #include "UnityCG.cginc"
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             uniform fixed4     fragColor;
    29.             uniform fixed      iChannelTime[4];            // channel playback time (in seconds)
    30.             uniform fixed3     iChannelResolution[4];    // channel resolution (in pixels)
    31.             uniform fixed4     iMouse;                    // mouse pixel coords. xy: current (if MLB down), zw: click
    32.             uniform fixed4     iDate;                    // (year, month, day, time in seconds)
    33.             uniform fixed      iSampleRate;                // sound sample rate (i.e., 44100)
    34.             sampler2D _MainTex;
    35.             sampler2D _SecondTex;
    36.  
    37.             float4 _iMouse;
    38.             uniform fixed3         _iResolution;            // viewport resolution in pixels
    39.  
    40.             struct v2f
    41.             {
    42.                 float2 uv : TEXCOORD0;
    43.                 float4 vertex : SV_POSITION;
    44.                 float4 screenCoord : TEXCOORD1;
    45.             };
    46.  
    47.             v2f vert(appdata v)
    48.             {
    49.                 v2f o;
    50.                 o.vertex = UnityObjectToClipPos(v.vertex);
    51.                 o.uv = v.uv;
    52.                 o.screenCoord.xy = ComputeScreenPos(o.vertex);
    53.                 return o;
    54.             }
    55.             // Created by inigo quilez - iq/2013
    56.             // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
    57.  
    58.             //---------------------------------
    59.  
    60.             #define ANTIALIAS 1
    61.  
    62.             fixed hash( fixed n ) { return frac(sin(n)*13.5453123); }
    63.  
    64.             fixed maxcomp( in fixed3 v ) { return max( max( v.x, v.y ), v.z ); }
    65.  
    66.             fixed dbox( fixed3 p, fixed3 b, fixed r )
    67.             {
    68.                 return length(max(abs(p)-b,0.0))-r;
    69.             }
    70.  
    71.             fixed4 texcube( sampler2D sam, in fixed3 p, in fixed3 n )
    72.             {
    73.                 fixed4 x = tex2D( sam, p.yz );
    74.                 fixed4 y = tex2D( sam, p.zx );
    75.                 fixed4 z = tex2D( sam, p.yx );
    76.                 fixed3 a = abs(n);
    77.                 return (x*a.x + y*a.y + z*a.z) / (a.x + a.y + a.z);
    78.             }
    79.  
    80.             //---------------------------------
    81.  
    82.             fixed freqs[4];
    83.  
    84.             fixed3 mapH( in fixed2 pos )
    85.             {
    86.                 fixed2 fpos = frac( pos );
    87.                 fixed2 ipos = floor( pos );
    88.  
    89.                 fixed f = 0.0;  
    90.                 fixed id = hash( ipos.x + ipos.y*57.0 );
    91.                 //mul?
    92.                 f += freqs[0] * clamp(1.0 - abs(id-0.20)/0.30, 0.0, 1.0 );
    93.                 f += freqs[1] * clamp(1.0 - abs(id-0.40)/0.30, 0.0, 1.0 );
    94.                 f += freqs[2] * clamp(1.0 - abs(id-0.60)/0.30, 0.0, 1.0 );
    95.                 f += freqs[3] * clamp(1.0 - abs(id-0.80)/0.30, 0.0, 1.0 );
    96.  
    97.                 f = pow( clamp( f, 0.0, 1.0 ), 2.0 );
    98.                 fixed h = 2.5*f;
    99.  
    100.                 return fixed3( h, id, f );
    101.             }
    102.  
    103.             fixed3 map( in fixed3 pos )
    104.             {
    105.                 fixed2  p = frac( pos.xz );
    106.                 fixed3  m = mapH( pos.xz );
    107.                 fixed d = dbox( fixed3(p.x-0.5,pos.y-0.5*m.x,p.y-0.5), fixed3(0.3,m.x*0.5,0.3), 0.1 );
    108.                 return fixed3( d, m.yz );
    109.             }
    110.  
    111.             const fixed surface = 0.001;
    112.  
    113.             fixed3 trace( fixed3 ro, in fixed3 rd, in fixed tmin, in fixed tmax )
    114.             {
    115.                 ro += tmin*rd;
    116.  
    117.                 fixed2 pos = floor(ro.xz);
    118.                 fixed3 rdi = 1.0/rd;
    119.                 fixed3 rda = abs(rdi);
    120.                 fixed2 rds = sign(rd.xz);
    121.                 fixed2 dis = (pos-ro.xz+ 0.5 + rds*0.5) * rdi.xz;
    122.  
    123.                 fixed3 res = fixed3( -1.0 , -1.0 , -1.0 );
    124.  
    125.                 // traverse regular grid (in 2D)
    126.                 fixed2 mm = fixed2(0.0,0.0);
    127.                 [unroll(100)]
    128.             for( int i=0; i<28; i++ )
    129.                 {
    130.                     fixed3 cub = mapH( pos );
    131.  
    132.                     #if 1
    133.                         fixed2 pr = pos+0.5-ro.xz;
    134.                         fixed2 mini = (pr-0.5*rds)*rdi.xz;
    135.                         fixed s = max( mini.x, mini.y );
    136.                         if( (tmin+s)>tmax ) break;
    137.                     #endif
    138.      
    139.      
    140.                     // intersect box
    141.                     fixed3  ce = fixed3( pos.x+0.5, 0.5*cub.x, pos.y+0.5 );
    142.                     fixed3  rb = fixed3(0.3,cub.x*0.5,0.3);
    143.                     fixed3  ra = rb + 0.12;
    144.                     fixed3  rc = ro - ce;
    145.                     fixed tN = maxcomp( -rdi*rc - rda*ra );
    146.                     fixed tF = maxcomp( -rdi*rc + rda*ra );
    147.                     if( tN < tF )//&& tF > 0.0 )
    148.                     {
    149.                         // raymarch
    150.                         fixed s = tN;
    151.                         fixed h = 1.0;
    152.                         [unroll(100)]
    153.                         for( int j=0; j<24; j++ )
    154.                         {
    155.                             h = dbox( rc+s*rd, rb, 0.1 );
    156.                             s += h;
    157.                             if( s>tF ) break;
    158.                         }
    159.  
    160.                         if( h < (surface*s*2.0) )
    161.                         {
    162.                             res = fixed3( s, cub.yz );
    163.                             break;
    164.                         }
    165.          
    166.                     }
    167.  
    168.                     // step to next cell      
    169.                     mm = step( dis.xy, dis.yx );
    170.                     dis += mm*rda.xz;
    171.                     pos += mm*rds;
    172.                 }
    173.  
    174.                 res.x += tmin;
    175.  
    176.                 return res;
    177.             }
    178.  
    179.  
    180.             fixed softshadow( in fixed3 ro, in fixed3 rd, in fixed mint, in fixed maxt, in fixed k )
    181.             {
    182.                 fixed res = 1.0;
    183.                 fixed t = mint;
    184.                 [unroll(100)]
    185.                 for( int i=0; i<50; i++ )
    186.                 {
    187.                     fixed h = map( ro + rd*t ).x;
    188.                     res = min( res, k*h/t );
    189.                     t += clamp( h, 0.05, 0.2 );
    190.                     if( res<0.001 || t>maxt ) break;
    191.                 }
    192.                 return clamp( res, 0.0, 1.0 );
    193.             }
    194.  
    195.             fixed3 calcNormal( in fixed3 pos, in fixed t )
    196.             {
    197.                 fixed2 e = fixed2(1.0,-1.0)*surface*t;
    198.                 return normalize( e.xyy*map( pos + e.xyy ).x +
    199.                                   e.yyx*map( pos + e.yyx ).x +
    200.                                   e.yxy*map( pos + e.yxy ).x +
    201.                                   e.xxx*map( pos + e.xxx ).x );
    202.             }
    203.  
    204.             const fixed3 light1 = fixed3(  0.70, 0.52, -0.45 );
    205.             const fixed3 light2 = fixed3( -0.71, 0.000,  0.71 );
    206.             const fixed3 lpos = fixed3(0.0,0.0,0.0) + 6.0 * fixed3(  0.70, 0.52, -0.45 );
    207.             //const fixed3 lpos = fixed3(0.0,0.0,0.0) + 6.0 * light1;
    208.  
    209.             fixed2 boundingVlume( fixed2 tminmax, in fixed3 ro, in fixed3 rd )
    210.             {
    211.                 fixed bp = 2.7;
    212.                 fixed tp = (bp-ro.y)/rd.y;
    213.                 if( tp>0.0 )
    214.                 {
    215.                     if( ro.y>bp ) tminmax.x = max( tminmax.x, tp );
    216.                     else          tminmax.y = min( tminmax.y, tp );
    217.                 }
    218.                 bp = 0.0;
    219.                 tp = (bp-ro.y)/rd.y;
    220.                 if( tp>0.0 )
    221.                 {
    222.                     if( ro.y>bp ) tminmax.y = min( tminmax.y, tp );
    223.                 }
    224.                 return tminmax;
    225.             }
    226.  
    227.             fixed3 doLighting( in fixed3 col, in fixed ks,
    228.                              in fixed3 pos, in fixed3 nor, in fixed3 rd )
    229.             {
    230.                 fixed3  ldif = lpos - pos;
    231.                 fixed llen = length( ldif );
    232.                 ldif /= llen;
    233.                 fixed con = dot( light1,ldif);
    234.                 fixed occ = lerp( clamp( pos.y/4.0, 0.0, 1.0 ), 1.0, max(0.0,nor.y) );
    235.                 fixed2 sminmax = fixed2(0.01, 5.0);
    236.  
    237.                 fixed sha = softshadow( pos, ldif, sminmax.x, sminmax.y, 32.0 );;
    238.      
    239.                 fixed bb = smoothstep( 0.5, 0.8, con );
    240.                 fixed lkey = clamp( dot(nor,ldif), 0.0, 1.0 );
    241.                 fixed3  lkat = fixed3(1.0,1.0,1.0);
    242.                       lkat *= fixed3(bb*bb*0.6+0.4*bb,bb*0.5+0.5*bb*bb,bb).zyx;
    243.                       lkat /= 1.0+0.25*llen*llen;      
    244.                       lkat *= 30.0;
    245.                       lkat *= sha;
    246.                 fixed lbac = clamp( 0.1 + 0.9*dot( light2, nor ), 0.0, 1.0 );
    247.                       lbac *= smoothstep( 0.0, 0.8, con );
    248.                       lbac /= 1.0+0.2*llen*llen;      
    249.                       lbac *= 4.0;
    250.                 fixed lamb = 1.0 - 0.5*nor.y;
    251.                       lamb *= 1.0-smoothstep( 10.0, 25.0, length(pos.xz) );
    252.                       lamb *= 0.25 + 0.75*smoothstep( 0.0, 0.8, con );
    253.                       lamb *= 0.25;
    254.      
    255.                 fixed3 lin  = 1.0*fixed3(0.20,0.05,0.02)*lamb*occ;
    256.                      lin += 1.0*fixed3(1.60,0.70,0.30)*lkey*lkat*(0.5+0.5*occ);
    257.                      lin += 1.0*fixed3(0.70,0.20,0.08)*lbac*occ;
    258.                      lin *= fixed3(1.3,1.1,1.0);
    259.  
    260.                 col = col*lin;
    261.  
    262.                 fixed3 spe = ks*lkey*lkat*(0.5+0.5*occ)*5.0*
    263.                            pow( clamp(dot(normalize(ldif-rd), nor),0.0,1.0), 4.0 ) *
    264.                            (0.04+0.96*pow(clamp(dot(nor,-rd),0.0,1.0),5.0));
    265.  
    266.                 col += (0.5+0.5*ks)*0.5*spe*fixed3(1.0,0.9,0.7);
    267.  
    268.                 return col;
    269.             }
    270.  
    271.             fixed3x3 setLookAt( in fixed3 ro, in fixed3 ta, fixed cr )
    272.             {
    273.                 fixed3  cw = normalize(ta-ro);
    274.                 fixed3  cp = fixed3(sin(cr), cos(cr),0.0);
    275.                 fixed3  cu = normalize( cross(cw,cp) );
    276.                 fixed3  cv = normalize( cross(cu,cw) );
    277.                 return fixed3x3( cu, cv, cw );
    278.             }
    279.  
    280.             fixed3 render( in fixed3 ro, in fixed3 rd )
    281.             {
    282.                 fixed3 col = fixed3( 0.0 , 0.0 , 0.0 );
    283.  
    284.                 fixed2 tminmax = fixed2(0.0, 40.0 );
    285.  
    286.                 tminmax = boundingVlume( tminmax, ro, rd );
    287.  
    288.                 // raytrace
    289.                 fixed3 res = trace( ro, rd, tminmax.x, tminmax.y );
    290.                 if( res.y > -0.5 )
    291.                 {
    292.                     fixed t = res.x;
    293.                     fixed3 pos = ro + t*rd;
    294.                     fixed3 nor = calcNormal( pos, t );
    295.  
    296.                     // material  
    297.                     col = 0.5 + 0.5*cos( 6.2831*res.y + fixed3(0.0, 0.4, 0.8) );
    298.                     fixed3 ff = texcube( _SecondTex, 0.1*fixed3(pos.x,4.0*res.z-pos.y,pos.z), nor ).xyz;
    299.                     col *= ff.x;
    300.  
    301.                     // lighting
    302.                     col = doLighting( col, ff.x, pos, nor, rd );
    303.                     col *= 1.0 - smoothstep( 20.0, 40.0, t );
    304.                 }
    305.                 return col;
    306.             }
    307.  
    308.  
    309.  
    310.             fixed4 frag(v2f i) : SV_Target{
    311.  
    312.             {
    313.                 freqs[0] = tex2D( _MainTex, fixed2( 0.01, 0.25 ) ).x;
    314.                 freqs[1] = tex2D( _MainTex, fixed2( 0.07, 0.25 ) ).x;
    315.                 freqs[2] = tex2D( _MainTex, fixed2( 0.15, 0.25 ) ).x;
    316.                 freqs[3] = tex2D( _MainTex, fixed2( 0.30, 0.25 ) ).x;
    317.                 //-----------
    318.                 //fixed time = 5.0 + 0.2*_Time.y + 20.0*_iMouse.x/1;
    319.                 fixed time = 5.0 + 0.2*_Time.y + 20.0*_iMouse.x / _iResolution.x;
    320.  
    321.                 fixed2 _i_uv = i.uv;
    322.  
    323.                 fixed3 tot = fixed3(0.0,0.0,0.0);
    324.  
    325.                 //#ifdef ANTIALIAS
    326.                 //[unroll(2)]
    327.                 //for( int j=0; j<ANTIALIAS; j++ )
    328.                 //[unroll(2)]
    329.                 //for( int i=0; i<ANTIALIAS; i++ )
    330.                 //{
    331.                         //fixed2 off = fixed2(fixed(i),fixed(j))/fixed(ANTIALIAS);
    332.                     //#else
    333.                         fixed2 off = fixed2(0.0,0.0);
    334.                     //#endif
    335.                  
    336.                         fixed2 xy = (-_iResolution.xy + 2.0 * (_i_uv)) / _iResolution.y;
    337.                         //fixed2 xy = (-1 + 2.0 * (_i_uv)) / 1;
    338.                         //fixed2 xy = (-1 + 2.0 * (_i_uv + off)) / 1;
    339.                         //fixed2 xy = (-1+2.0*(i.uv)) / 1;
    340.                         //fixed2 xy = (-1+2.0*(i.uv+off)) / 1;
    341.  
    342.                         // camera  
    343.                         fixed3 ro = fixed3( 8.5*cos(0.2+.33*time), 5.0+2.0*cos(0.1*time), 8.5*sin(0.1+0.37*time) );
    344.                         fixed3 ta = fixed3( -2.5+3.0*cos(1.2+.41*time), 0.0, 2.0+3.0*sin(2.0+0.38*time) );
    345.                         fixed roll = 0.2*sin(0.1*time);
    346.  
    347.                         // camera tx
    348.                         fixed3x3 ca = setLookAt( ro, ta, roll );
    349.                         //fixed3 rd = normalize( ca * fixed3(xy.xy,1.75) );
    350.                         //fixed3 rd = normalize( mul(ca, fixed3(xy.xy,1.75)) );
    351.                         fixed3 rd = normalize(mul(fixed3(xy.xy, 1.75), ca));
    352.  
    353.  
    354.      
    355.                         fixed3 col = render( ro, rd );
    356.                         col = pow( col, fixed3(0.4545,0.4545,0.4545) );
    357.                         col = pow( col, fixed3(0.8,0.95,1.0) );
    358.                         col = clamp(col,0.0,1.0);
    359.                         tot += col;
    360.      
    361.                     //#ifdef ANTIALIAS
    362.                 //}
    363.                 //tot /= fixed(ANTIALIAS*ANTIALIAS);
    364.                 //#endif  
    365.  
    366.                 // vigneting
    367.                 //fixed2 q = i.uv.xy/ _iResolution.xy;
    368.                 fixed2 q = _i_uv.xy / _iResolution.xy;
    369.                 tot *= 0.2 + 0.8*pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.1 );
    370.  
    371.                 return  fixed4( tot, 1.0 );
    372.             }
    373.  
    374.  
    375.             }ENDCG
    376.         }
    377.     }
    378. }
    379.  
    380.  
     
  19. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    Here is example of converted ShaderToy multi-pass shader (self-accumulated buffer) to Unity, using compute shader and two render textures:



    Source code:
    Code (CSharp):
    1. //Simplified version of shader, which full version was created by Florian Berger: https://www.shadertoy.com/view/MsGSRd
    2. //Translated from ShaderToy to Unity by Przemyslaw Zaworski, 18.12.2017, https://github.com/przemyslawzaworski/Unity3D-CG-programming
    3. //Additional support: Seyed Morteza Kamaly, https://github.com/smkplus
    4. //To simulate self-accumulated buffer, I use two render textures which are swapped between every render frame.
    5. //Usage: apply fluid_dynamics.cs script to gameobject, select fluid_dynamics.compute and any material with _MainTex free slot,
    6. //for example built-in Unlit/Texture.
    7.  
    8. #pragma kernel CSMain
    9.  
    10. Texture2D<float4> reader;
    11. RWTexture2D<float4> writer;
    12. SamplerState _LinearClamp;
    13.  
    14. [numthreads(8,8,1)]
    15. void CSMain (uint2 id : SV_DispatchThreadID)
    16. {
    17.     float2 f = float2(id.x,id.y);  //fragCoord
    18.     float2 b = float2(0.31,0.95);
    19.     float2 iResolution=float2(1024,1024); //texture resolution
    20.     float2 v = float2(0.0,0.0);
    21.     float4 c = float4(0,0,0,1);   //initial color
    22.     for(int l=0;l<20;l++)
    23.     {
    24.         if ( dot(b,b) > pow(iResolution.y,2.0) ) break;
    25.         float2 p = b;
    26.         for(int i=0;i<5;i++)
    27.         {        
    28.             float2 pos = f+p;
    29.             float rot=0.0;
    30.             for(int i=0;i<5;i++)
    31.             {
    32.                 rot+=dot(reader.SampleLevel(_LinearClamp,float2(frac((pos+p)/iResolution.xy)),0).xy-float2(0.5,0.5),mul(float2(1,-1),p.yx));
    33.                 p=float2(0.31*p.x+0.95*p.y,-0.95*p.x+0.31*p.y);
    34.             }
    35.             v+=p.yx* rot/5.0/dot(b,b);    
    36.             p=float2(0.31*p.x+0.95*p.y,-0.95*p.x+0.31*p.y);
    37.         }
    38.         b*=2.0;
    39.     }  
    40.     float4 color=reader.SampleLevel(_LinearClamp,frac((f+v*float2(-2,2))/iResolution.xy),0);
    41.     float2 s=(f.xy/iResolution.xy)*2.0-float2(1.0,1.0);
    42.     color.xy += (0.01*s.xy / (dot(s,s)/0.1+0.3));
    43.     c = color;
    44.     writer[id]=c;
    45. }
    Code (CSharp):
    1. //Load fluid_dynamics.compute into Unity 3D engine.
    2. using UnityEngine;
    3.  
    4. public class fluid_dynamics : MonoBehaviour
    5. {
    6.     public ComputeShader compute_shader;
    7.     RenderTexture A;
    8.     RenderTexture B;
    9.     public Material material;  
    10.     int handle_main;
    11.  
    12.     void Start()
    13.     {
    14.         A = new RenderTexture(1024,1024,0);
    15.         A.enableRandomWrite = true;
    16.         A.Create();  
    17.         B = new RenderTexture(1024,1024,0);
    18.         B.enableRandomWrite = true;
    19.         B.Create();  
    20.         handle_main = compute_shader.FindKernel("CSMain");
    21.     }
    22.        
    23.     void Update()
    24.     {
    25.         compute_shader.SetTexture(handle_main, "reader", A);  
    26.         compute_shader.SetTexture(handle_main, "writer", B);
    27.         compute_shader.Dispatch(handle_main, A.width / 8, A.height / 8, 1);
    28.         compute_shader.SetTexture(handle_main, "reader", B);  
    29.         compute_shader.SetTexture(handle_main, "writer", A);
    30.         compute_shader.Dispatch(handle_main, B.width / 8, B.height / 8, 1);
    31.         material.mainTexture = B;
    32.     }
    33. }
     
  20. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    Amazing!!!
    works fine!
    but some platform don't support compute shaders :(
    have you any idea?
     
  21. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    45
    Great. Love.

    I finished
    mouse input and
    sound fft input from audio clip
    for shader men.

    The generic Multipass Buffer is finished soon.
    When you can generate some buffer tabs in your converter window it should run similar to shadertoy.

    I try to branch/fork your project on github to integrate it. I am new there. Need some days to figure it out. Do you have some tips?

    What are you working on?
    We should avoid doing things twice.)
     
    Last edited: Dec 18, 2017
    Seyed_Morteza_Kamaly likes this.
  22. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    ShaderToy multi-pass shader (self-accumulated buffer) to Unity without compute shader:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class fluid_dynamics : MonoBehaviour
    4. {
    5.     RenderTexture A;
    6.     RenderTexture B;
    7.     public Material material;  
    8.  
    9.     void Start()
    10.     {
    11.         A = new RenderTexture(1024,1024,0);
    12.         A.Create();  
    13.         B = new RenderTexture(1024,1024,0);
    14.         B.Create();  
    15.     }
    16.        
    17.     void Update()
    18.     {
    19.         material.SetTexture("MainTex", A);
    20.         Graphics.Blit(A,B,material);
    21.         material.SetTexture("MainTex", B);
    22.         Graphics.Blit(B,A,material);
    23.     }
    24. }
    Code (CSharp):
    1. //original source: https://www.shadertoy.com/view/MsGSRd
    2. //translated from GLSL to CG by P.Z.
    3. Shader "Fluid dynamics"
    4. {
    5.     Properties
    6.     {
    7.         MainTex ("Texture", 2D) = "black" {}
    8.     }
    9.     Subshader
    10.     {  
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.             #pragma vertex vertex_shader
    15.             #pragma fragment pixel_shader
    16.             #pragma target 3.0
    17.  
    18.             struct type
    19.             {
    20.                 float4 vertex : SV_POSITION;
    21.                 float2 uv : TEXCOORD0;
    22.             };
    23.                
    24.             sampler2D MainTex;
    25.            
    26.             type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
    27.             {
    28.                 type vs;
    29.                 vs.vertex = UnityObjectToClipPos (vertex);
    30.                 vs.uv = uv;
    31.                 return vs;
    32.             }
    33.  
    34.             float4 pixel_shader (type ps) : SV_TARGET
    35.             {
    36.                 float2 iResolution=float2(1024,1024); //texture resolution
    37.                 float2 f = ps.uv*iResolution;  //fragCoord
    38.                 float2 b = float2(0.31,0.95);
    39.                 float2 v = float2(0.0,0.0);
    40.                 float4 c = float4(0,0,0,1);   //initial color
    41.                 for(int l=0;l<20;l++)
    42.                 {
    43.                     if ( dot(b,b) > pow(iResolution.y,2.0) ) break;
    44.                     float2 p = b;
    45.                     for(int i=0;i<5;i++)
    46.                     {        
    47.                         float2 pos = f+p;
    48.                         float rot=0.0;
    49.                         for(int i=0;i<5;i++)
    50.                         {
    51.                             rot+=dot(tex2Dlod(MainTex,float4(frac((pos+p)/iResolution.xy),0,0)  ).xy-float2(0.5,0.5),mul(float2(1,-1),p.yx));
    52.                             p=float2(0.31*p.x+0.95*p.y,-0.95*p.x+0.31*p.y);
    53.                         }
    54.                         v+=p.yx* rot/5.0/dot(b,b);    
    55.                         p=float2(0.31*p.x+0.95*p.y,-0.95*p.x+0.31*p.y);
    56.                     }
    57.                     b*=2.0;
    58.                 }  
    59.                 float4 color=tex2Dlod(MainTex,float4(frac((f+v*float2(-2,2))/iResolution.xy),0,0));
    60.                 float2 s=(f.xy/iResolution.xy)*2.0-float2(1.0,1.0);
    61.                 color.xy += (0.01*s.xy / (dot(s,s)/0.1+0.3));
    62.                 c = color;
    63.                 return c;
    64.             }
    65.             ENDCG
    66.         }
    67.     }
    68. }
     
    4sascha likes this.
  23. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    Usage: apply script to gameobject ( quad ), then material with shader to script and to the quad mesh renderer.
     
    4sascha and Seyed_Morteza_Kamaly like this.
  24. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    pretty cool!
    you've solved the big issue
    I would add multipass feature to ShaderMan :)
     
  25. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    Shaderman simply find pattern and replace equivalent(glsl to hlsl)
    https://alastaira.wordpress.com/2015/08/07/unity-shadertoys-a-k-a-converting-glsl-shaders-to-cghlsl/

    I use regular expressions for finding some patterns
    https://en.wikipedia.org/wiki/Regular_expression

    I'd be happy if you help me :)
    (currently I'm working on raymarching)
     
  26. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    Another simple example (use with fluid_dynamics.cs):

    Code (CSharp):
    1. //original source: https://www.shadertoy.com/view/4sc3DB
    2. //translated from GLSL to CG by Przemyslaw Zaworski
    3. Shader "Very simple feedback smoke"
    4. {
    5.     Properties
    6.     {
    7.         MainTex ("Texture", 2D) = "black" {}
    8.     }
    9.     Subshader
    10.     {  
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.             #pragma vertex vertex_shader
    15.             #pragma fragment pixel_shader
    16.             #pragma target 3.0
    17.  
    18.             struct structure
    19.             {
    20.                 float4 vertex : SV_POSITION;
    21.                 float2 uv : TEXCOORD0;
    22.             };
    23.                
    24.             sampler2D MainTex;
    25.            
    26.             structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
    27.             {
    28.                 structure vs;
    29.                 vs.vertex = UnityObjectToClipPos (vertex);
    30.                 vs.uv = uv;
    31.                 return vs;
    32.             }
    33.  
    34.             float4 pixel_shader (structure ps) : SV_TARGET
    35.             {
    36.                 float2 iResolution = float2(1024,1024);
    37.                 float2 uv = ps.uv.xy;
    38.                 if (length(float2(sin(_Time.g)*.75, cos(_Time.g*1.1294)*.75)-(uv*2.-1.)) < .06) return float4(1.,0.,1.,1.);  
    39.                 float4 c = tex2Dlod(MainTex, float4(uv,0,0))*5.;  
    40.                 float2 odr = 1./iResolution.xy;  
    41.                 float4 cLeft = tex2Dlod(MainTex, float4(uv-float2(odr.x,0.),0,0));
    42.                 float4 cRight = tex2Dlod(MainTex, float4(uv+float2(odr.x,0.),0,0));
    43.                 float4 cUp = tex2Dlod(MainTex, float4(uv-float2(0.,odr.y),0,0));
    44.                 float4 cDown = tex2Dlod(MainTex, float4(uv+float2(0.,odr.y),0,0));
    45.                 c += cLeft.wyzx*(abs(cos(_Time.g+uv.x*32.234+cRight.w*32.234))+1.);
    46.                 c += cRight.zxyw*(abs(cos(uv.x*32.234+cLeft.z*32.34+_Time.g*1.36))+1.);
    47.                 c += cUp*(abs(cos(_Time.g*2.12+uv.y*32.1432+cDown.y*32.24))+1.);
    48.                 c += cDown.wzyx*(abs(cos(uv.y*32.345+cUp.x*32.234))+1.);      
    49.                 return max(c/11.6-.0001, 0.);
    50.             }
    51.             ENDCG
    52.         }
    53.     }
    54. }
     
    4sascha likes this.
  27. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171


    Progressive path tracing (smallpt.compute and smallpt.cs from my repository).
     
  28. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171



    ShaderToy's seascape and atmospheric scattering blended together with Unity mesh game objects.
    Need to fix water reflections.
     
  29. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    392
  30. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    Motion Blur without Render Textures (I only use single RWStructuredBuffer for both store and accumulate data).
    :)


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class structured_buffer : MonoBehaviour
    4. {
    5.     public ComputeShader shader;
    6.     public Material material;
    7.     public int resolution = 1024;
    8.     ComputeBuffer A;
    9.    
    10.     void Start ()
    11.     {
    12.         A = new ComputeBuffer(resolution*resolution, sizeof(float)*4, ComputeBufferType.Default);
    13.         shader.SetBuffer(0, "A", A);
    14.         material.SetBuffer("A",A);
    15.     }
    16.    
    17.     void Update ()
    18.     {
    19.         material.SetInt("resolution",resolution);
    20.         shader.SetFloat("time",Time.time);
    21.         shader.SetInt("resolution",resolution);
    22.         shader.Dispatch(0, resolution / 16, resolution / 16, 1);  
    23.     }
    24.    
    25.     void OnDestroy()
    26.     {
    27.         A.Release();
    28.     }  
    29. }
    Code (CSharp):
    1. #pragma kernel CSMain
    2.  
    3. RWStructuredBuffer<float4> A;
    4. float time;
    5. int resolution;
    6.  
    7. float2x2 rotationX( float x)
    8. {
    9.     return float2x2(cos(x),sin(x),-sin(x),cos(x));
    10. }
    11.  
    12. float2x2 rotationY( float y)
    13. {
    14.     return float2x2(cos(y),-sin(y),sin(y),cos(y));
    15. }
    16.  
    17. float map (float3 p)
    18. {
    19.     p.yz=mul(rotationX(time),p.yz);
    20.     p.xz=mul(rotationY(time),p.xz);
    21.     p = abs(p); p = max(p, p.yzx);
    22.     return max(max(p.x, p.y) - 1.0, 0.8 - min(p.x, min(p.y, p.z)));
    23. }
    24.  
    25. float3 raymarch (float3 ro, float3 rd)
    26. {
    27.     float t = 0.0;
    28.     for (int i = 0; i < 32; ++i)
    29.     {
    30.         float3 p = ro + rd * t;
    31.         t += map(p);
    32.     }
    33.     return float3(1.0,0.8,0.0) / (1.0+t*t*0.1) * 0.5;
    34. }
    35.  
    36. [numthreads(16,16,1)]
    37. void CSMain (uint2 id : SV_DispatchThreadID)
    38. {
    39.     float scale = 0.9;   //motion blur scale
    40.     float2 uv = (2.0*float2(id.x,id.y)-float2(resolution,resolution))/float(resolution);
    41.     float3 ro = float3(0.0, 0.0, -5.0);   //virtual camera position
    42.     float3 rd = normalize(float3(uv, 2.0));   //ray direction
    43.     float3 total = raymarch(ro, rd);   //trace in current frame
    44.     total += A[id.y*resolution+id.x].rgb * scale;   //accumulate data
    45.     A[id.y*resolution+id.x] = float4(total, 1.0);   //set final color
    46. }
    Code (CSharp):
    1. Shader "Structured Buffer"
    2. {
    3.     SubShader
    4.     {
    5.         Pass
    6.         {
    7.             CGPROGRAM
    8.             #pragma vertex vertex_shader
    9.             #pragma fragment pixel_shader
    10.             #pragma target 5.0            
    11.  
    12.             uniform StructuredBuffer<float4> A;
    13.             uniform int resolution;
    14.  
    15.             struct SHADERDATA
    16.             {
    17.                 float4 vertex : SV_POSITION;
    18.                 float2 uv : TEXCOORD0;
    19.             };
    20.  
    21.             SHADERDATA vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
    22.             {
    23.                 SHADERDATA vs;
    24.                 vs.vertex = UnityObjectToClipPos(vertex);
    25.                 vs.uv = uv;
    26.                 return vs;
    27.             }
    28.  
    29.             float4 pixel_shader (SHADERDATA ps) : SV_Target
    30.             {
    31.                 int x = int(round(ps.uv.x*resolution));
    32.                 int y = int(round(ps.uv.y*resolution));
    33.                 return A[y*resolution+x];
    34.             }
    35.  
    36.             ENDCG
    37.         }
    38.     }
    39. }
     
    Seyed_Morteza_Kamaly likes this.
  31. Razor_J

    Razor_J

    Joined:
    Apr 6, 2016
    Posts:
    3
    Hi, I am trying to use your Shadertoy to Unity plugin and keep getting broken shader results on everything I have tried converting except for "fire" example which worked. I tried over 20 of them and simple ones too. Seems that ones you have done in example folder won't convert either (except for "fire"). What am I missing? I have tried in Unity 2017.3.1p3 (64-bit) and 5.6.4 with same results. Any suggestions/fixes? Thanks.
     
  32. Razor_J

    Razor_J

    Joined:
    Apr 6, 2016
    Posts:
    3
    Ummm... this (above) was with Shaderman's utility, am I in the right area? Seems like this is Candycat's forum? (I will try this one as well but would appreciate help on above if anyone knows either way).
     
  33. Razor_J

    Razor_J

    Joined:
    Apr 6, 2016
    Posts:
    3
    (Actually it was "flame" not "fire")
     
  34. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    Hi dear friend , sorry my converter just used to basic convert(changes the equivalent keywords) from shadertoy to Unity , this is mean converted shader need to fix...
    Unfortunately I didn't have the time to fixing problems but any way I will help you to convert your Intended shader.

    Also I made a tutorial that show you how you can convert Shadertoy to Unity

     
    Last edited: Jun 1, 2018
    vahab2 likes this.
  35. Pointcloud

    Pointcloud

    Joined:
    Nov 24, 2014
    Posts:
    33
  36. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    Ready to use. Create material with shader , and add to quad or plane etc.

    Code (CSharp):
    1.  
    2. // Original from: https://www.shadertoy.com/view/MdfGRr
    3. Shader "Juliabulb"
    4. {
    5.     SubShader
    6.     {
    7.         Pass
    8.         {
    9.             CGPROGRAM
    10.             #pragma vertex vertex_shader
    11.             #pragma fragment pixel_shader
    12.  
    13.             typedef vector <float, 2> vec2;
    14.             typedef vector <float, 3> vec3;
    15.             typedef vector <float, 4> vec4;
    16.             #define mix lerp
    17.             #define iTime _Time.g
    18.          
    19.             vec2 isphere( in vec4 sph, in vec3 ro, in vec3 rd )
    20.             {
    21.                 vec3 oc = ro - sph.xyz;
    22.                 float b = dot(oc,rd);
    23.                 float c = dot(oc,oc) - sph.w*sph.w;
    24.                 float h = b*b - c;
    25.                 if( h<0.0 ) return vec2(-1.0,-1.0);
    26.                 h = sqrt( h );
    27.                 return -b + vec2(-h,h);
    28.             }
    29.  
    30.             float map( in vec3 p, in vec3 c, out vec4 resColor )
    31.             {
    32.                 vec3 z = p;
    33.                 float m = dot(z,z);
    34.                 vec4 trap = vec4(abs(z),m);
    35.                 float dz = 1.0;
    36.                 for( int i=0; i<4; i++ )
    37.                 {
    38.                     dz = 8.0*pow(m,3.5)*dz;
    39.                     float r = length(z);
    40.                     float b = 8.0*acos( clamp(z.y/r, -1.0, 1.0));
    41.                     float a = 8.0*atan2( z.x, z.z );
    42.                     z = c + pow(r,8.0) * vec3( sin(b)*sin(a), cos(b), sin(b)*cos(a) );
    43.                     trap = min( trap, vec4(abs(z),m) );
    44.                     m = dot(z,z);
    45.                     if( m > 2.0 ) break;
    46.                 }
    47.                 resColor = trap;
    48.                 return 0.25*log(m)*sqrt(m)/dz;
    49.             }
    50.  
    51.             float intersect( in vec3 ro, in vec3 rd, out vec4 rescol, float fov, vec3 c )
    52.             {
    53.                 float res = -1.0;
    54.                 rescol = 0.0;
    55.                 vec2 dis = isphere( vec4( 0.0, 0.0, 0.0, 1.25 ), ro, rd );
    56.                 if( dis.y<0.0 ) return -1.0;
    57.                 dis.x = max( dis.x, 0.0 );
    58.                 vec4 trap;
    59.                 float fovfactor = 1.0/sqrt(1.0+fov*fov);
    60.                 float t = dis.x;
    61.                 for( int i=0; i<128; i++  )
    62.                 {
    63.                     vec3 p = ro + rd*t;
    64.                     float surface = clamp( 0.0015*t*fovfactor, 0.0001, 0.1 );
    65.                     float dt = map( p, c, trap );
    66.                     if( t>dis.y || dt<surface ) break;
    67.                     t += dt;
    68.                 }
    69.                 if( t<dis.y )
    70.                 {
    71.                     rescol = trap;
    72.                     res = t;
    73.                 }
    74.                 return res;
    75.             }
    76.  
    77.             float softshadow( in vec3 ro, in vec3 rd, float mint, float k, vec3 c )
    78.             {
    79.                 float res = 1.0;
    80.                 float t = mint;
    81.                 for( int i=0; i<80; i++ )
    82.                 {
    83.                     vec4 kk;
    84.                     float h = map(ro + rd*t, c, kk);
    85.                     res = min( res, k*h/t );
    86.                     if( res<0.001 ) break;
    87.                     t += clamp( h, 0.002, 0.1 );
    88.                 }
    89.                 return clamp( res, 0.0, 1.0 );
    90.             }
    91.  
    92.             vec3 calcNormal( in vec3 pos, in float t, in float fovfactor, vec3 c )
    93.             {
    94.                 vec4 tmp;
    95.                 float surface = clamp( 0.3 * 0.0015*t*fovfactor, 0.0001, 0.1 );
    96.                 vec2 eps = vec2( surface, 0.0 );
    97.                 return normalize( vec3(
    98.                     map(pos+eps.xyy,c,tmp) - map(pos-eps.xyy,c,tmp),
    99.                     map(pos+eps.yxy,c,tmp) - map(pos-eps.yxy,c,tmp),
    100.                     map(pos+eps.yyx,c,tmp) - map(pos-eps.yyx,c,tmp) ) );
    101.             }
    102.          
    103.             void vertex_shader (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0)
    104.             {
    105.                 vertex = UnityObjectToClipPos(vertex);
    106.             }
    107.          
    108.             float4 pixel_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) : SV_TARGET
    109.             {
    110.                 vec2 p = 2.0*float2(2.0*uv.xy-1.0);
    111.                 float time = iTime*.15;
    112.                 vec3 light1 = vec3(  0.577, 0.577, -0.577 );
    113.                 vec3 light2 = vec3( -0.707, 0.000,  0.707 );
    114.                 float r = 1.3+0.1*cos(.29*time);
    115.                 vec3  ro = vec3( r*cos(.33*time), 0.8*r*sin(.37*time), r*sin(.31*time) );
    116.                 vec3  ta = vec3(0.0,0.1,0.0);
    117.                 float cr = 0.5*cos(0.1*time);
    118.                 float fov = 1.5;
    119.                 vec3 cw = normalize(ta-ro);
    120.                 vec3 cp = vec3(sin(cr), cos(cr),0.0);
    121.                 vec3 cu = normalize(cross(cw,cp));
    122.                 vec3 cv = normalize(cross(cu,cw));
    123.                 vec3 rd = normalize( p.x*cu + p.y*cv + fov*cw );
    124.                 vec3 cc = vec3( 0.9*cos(3.9+1.2*time)-.3, 0.8*cos(2.5+1.1*time), 0.8*cos(3.4+1.3*time) );
    125.                 if( length(cc)<0.50 ) cc=0.50*normalize(cc);
    126.                 if( length(cc)>0.95 ) cc=0.95*normalize(cc);
    127.                 vec3 col;
    128.                 vec4 tra;
    129.                 float t = intersect( ro, rd, tra, fov, cc );
    130.                 if( t<0.0 )
    131.                 {
    132.                     col = 1.3*vec3(0.8,.95,1.0)*(0.7+0.3*rd.y);
    133.                     col += vec3(0.8,0.7,0.5)*pow( clamp(dot(rd,light1),0.0,1.0), 32.0 );
    134.                 }
    135.                 else
    136.                 {
    137.                     vec3 pos = ro + t*rd;
    138.                     vec3 nor = calcNormal( pos, t, fov, cc );
    139.                     vec3 hal = normalize( light1-rd);
    140.                     vec3 ref = reflect( rd, nor );  
    141.                     col = vec3(1.0,1.0,1.0)*0.3;
    142.                     col = mix( col, vec3(0.7,0.3,0.3), sqrt(tra.x) );
    143.                     col = mix( col, vec3(1.0,0.5,0.2), sqrt(tra.y) );
    144.                     col = mix( col, vec3(1.0,1.0,1.0), tra.z );
    145.                     col *= 0.4;  
    146.                     float dif1 = clamp( dot( light1, nor ), 0.0, 1.0 );
    147.                     float dif2 = clamp( 0.5 + 0.5*dot( light2, nor ), 0.0, 1.0 );
    148.                     float occ = clamp(1.2*tra.w-0.6,0.0,1.0);
    149.                     float sha = softshadow( pos,light1, 0.0001, 32.0, cc );
    150.                     float fre = 0.04 + 0.96*pow( clamp(1.0-dot(-rd,nor),0.0,1.0), 5.0 );
    151.                     float spe = pow( clamp(dot(nor,hal),0.0,1.0), 12.0 ) * dif1 * fre*8.0;  
    152.                     vec3 lin  = 1.0*vec3(0.15,0.20,0.23)*(0.6+0.4*nor.y)*(0.1+0.9*occ);
    153.                         lin += 4.0*vec3(1.00,0.90,0.60)*dif1*sha;
    154.                         lin += 4.0*vec3(0.14,0.14,0.14)*dif2*occ;
    155.                         lin += 2.0*vec3(1.00,1.00,1.00)*spe*sha * occ;
    156.                         lin += 0.3*vec3(0.20,0.30,0.40)*(0.02+0.98*occ);
    157.                     col *= lin;
    158.                     col += spe*1.0*occ*sha;
    159.                 }
    160.                 col = sqrt( col );
    161.                 return vec4( col, 1.0 );
    162.             }
    163.             ENDCG
    164.         }
    165.     }
    166. }
     
    Last edited: Oct 15, 2018
  37. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
  38. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    Another multipass shader:

     
  39. Gaidzin

    Gaidzin

    Joined:
    Nov 14, 2012
    Posts:
    11
    ShaderMan don't working in Unity 2018.3
     
  40. Fdo_decea

    Fdo_decea

    Joined:
    Jul 29, 2017
    Posts:
    7
  41. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    @Mizontec
    yeah because I have another Idea

    I want to make online converter that convert shadertoy to all engine like Unity,Game Maker,Godot , Playcanvas , Unreal Engine and .....

    I'm working on it in my free time

    https://smkplus.github.io/ShaderMan.io/

     
  42. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68

    It's better that you understand concept of shader because your shader have many calculations and when you cenvert it will kill performance

    your shader has two part

    SDF + ImageEffect

    Before converting SDF you should know concept of Raymarching

    and Image effect used Buffer to storing previous frame
    Equivalent of Buffer Pass in Unity is RenderTexure

    for example I could make this with Unity Particles
    as you can see I Render Particles and store it in RenderTexture then I blur previous frame



    https://github.com/smkplus/RainDrop



    (Iran - Qom - Jamkaran)

    ToDo : I will distort Grab pass by above Normal

    useful link:

    https://gamedev.stackexchange.com/questions/152496/how-can-i-make-natural-rain-drops-on-screen
     
    Last edited: Mar 2, 2019
  43. Madhanraj

    Madhanraj

    Joined:
    Feb 14, 2018
    Posts:
    3
    Hi,

    ShaderMan!

    I am getting the below error in Unity after conversion. This is the link (https://www.shadertoy.com/view/3sXSWs), I have used for the below error. But I have faced this error for a couple of links too. I'm stuck here. Please help me to move forward.
    upload_2019-3-15_12-19-49.png

    Getting convert in this link properly, but not working inside Unity.
    https://smkplus.github.io/ShaderMan.io/

    Thanks
    Madhan
     
    Last edited: Mar 15, 2019
  44. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    171
    @Madhanraj

    In GLSL (shading language used by ShaderToy) vec3(0.55735), in HLSL (shading language used by Unity) is 0.55735.xxx or float3(0.55735,0.55735,0.55735)

    Code:

    Code (CSharp):
    1. Shader "Bulbs"
    2. {
    3.     SubShader
    4.     {
    5.         Pass
    6.         {
    7.             CGPROGRAM
    8.             #pragma vertex VSMain
    9.             #pragma fragment PSMain
    10.             #define SIZE 12.
    11.             #define BGCOLOR float3(.0, .0, .0)
    12.  
    13.             float3 hueShift(in float3 col, in float shift)
    14.             {
    15.                 float3 P = 0.55735.xxx * dot(0.55735.xxx, col);
    16.                 float3 U = col - P;
    17.                 float3 V = cross(0.55735.xxx, U);  
    18.                 col = U * cos(shift * 6.2832) + V * sin(shift * 6.2832) + P;
    19.                 return col;
    20.             }
    21.  
    22.             float sdCircle(in float2 p)
    23.             {
    24.                 return length(p - .5);
    25.             }
    26.  
    27.             float radialMask(in float2 p)
    28.             {
    29.                 return float2(sdCircle(p), atan2(p.y, p.x)).x * 4.;
    30.             }
    31.  
    32.             float sphereMask(in float2 p, in float r, in float hard)
    33.             {
    34.                 float dist = sdCircle(p);
    35.                 return 1. - smoothstep(r - (r * hard), r + (r * hard), dist);
    36.             }
    37.        
    38.             void VSMain (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0)
    39.             {
    40.                 vertex = UnityObjectToClipPos(vertex);
    41.             }
    42.        
    43.             void PSMain (float4 vertex:POSITION,float2 uv:TEXCOORD0, out float4 fragColor:SV_TARGET)
    44.             {
    45.                 float2 ouv = uv;
    46.                 uv = frac(uv * SIZE);
    47.                 ouv = floor(ouv * SIZE) / SIZE;  
    48.                 float spMask = sphereMask(uv, .5, .05);
    49.                 float3 color = spMask.xxx;
    50.                 float flow = abs(sin(1. - radialMask(ouv) + _Time.g * 4.));
    51.                 flow = pow(flow, 2.);
    52.                 float3 rainbow = hueShift(float3(1., 0., 0.), flow);
    53.                 float flow2 = abs(sin(1. - radialMask(ouv) + _Time.g * 1.5));
    54.                 flow2 = pow(flow2, 3.);
    55.                 color *= rainbow * flow2;
    56.                 color = lerp(BGCOLOR, color, spMask);
    57.                 fragColor = float4(color, 1.);
    58.             }
    59.             ENDCG
    60.         }
    61.     }
    62. }
    upload_2019-3-15_9-14-11.png
     
  45. Madhanraj

    Madhanraj

    Joined:
    Feb 14, 2018
    Posts:
    3
    @Przemyslaw_Zaworski

    I appreciate your explanation! May i know why direct conversion is not working inside Unity SHADERMAN tool?

    Thank you
    Madhan
     
  46. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    68
    It worked fine for me I convert your shader by ShaderMan
    shaders in shadertoy created by different people so finding algorithm to convert them is hard
    so shaderman don't work always correctly.I created shaderman to help converting shader.



    Code (CSharp):
    1.  
    2.     Shader "ShaderMan/Blob"
    3.     {
    4.     Properties{
    5.  
    6.     }
    7.     SubShader
    8.     {
    9.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    10.     Pass
    11.     {
    12.     ZWrite Off
    13.     Blend SrcAlpha OneMinusSrcAlpha
    14.     CGPROGRAM
    15.     #pragma vertex vert
    16.     #pragma fragment frag
    17.     #include "UnityCG.cginc"
    18.          
    19.  
    20.  
    21.     float4 vec4(float x,float y,float z,float w){return float4(x,y,z,w);}
    22.     float4 vec4(float x){return float4(x,x,x,x);}
    23.     float4 vec4(float2 x,float2 y){return float4(float2(x.x,x.y),float2(y.x,y.y));}
    24.     float4 vec4(float3 x,float y){return float4(float3(x.x,x.y,x.z),y);}
    25.  
    26.  
    27.     float3 vec3(float x,float y,float z){return float3(x,y,z);}
    28.     float3 vec3(float x){return float3(x,x,x);}
    29.     float3 vec3(float2 x,float y){return float3(float2(x.x,x.y),y);}
    30.  
    31.     float2 vec2(float x,float y){return float2(x,y);}
    32.     float2 vec2(float x){return float2(x,x);}
    33.  
    34.     float vec(float x){return float(x);}
    35.  
    36.  
    37.  
    38.     struct VertexInput {
    39.     float4 vertex : POSITION;
    40.     float2 uv:TEXCOORD0;
    41.     float4 tangent : TANGENT;
    42.     float3 normal : NORMAL;
    43.     //VertexInput
    44.     };
    45.     struct VertexOutput {
    46.     float4 pos : SV_POSITION;
    47.     float2 uv:TEXCOORD0;
    48.     //VertexOutput
    49.     };
    50.  
    51.  
    52.     VertexOutput vert (VertexInput v)
    53.     {
    54.     VertexOutput o;
    55.     o.pos = UnityObjectToClipPos (v.vertex);
    56.     o.uv = v.uv;
    57.     //VertexFactory
    58.     return o;
    59.     }
    60.  
    61.     #define SIZE 12.
    62. #define BGCOLOR vec3(.0, .0, .0)
    63.  
    64. // from mAlk's: https://www.shadertoy.com/view/MsjXRt
    65. float3 hueShift(in float3 col, in float shift) {
    66.     float3 P = vec3(0.55735) * dot(vec3(0.55735), col);
    67.     float3 U = col - P;
    68.     float3 V = cross(vec3(0.55735), U);  
    69.     col = U * cos(shift * 6.2832) + V * sin(shift * 6.2832) + P;
    70.     return col;
    71. }
    72.  
    73. float sdCircle(in float2 p) {
    74.     return length(p - .5);
    75. }
    76.  
    77. float radialMask(in float2 p) {
    78.     return vec2(sdCircle(p), atan2( p.x,p.y)).x * 4.;
    79. }
    80.  
    81. float sphereMask(in float2 p, in float r, in float hard) {
    82.     float dist = sdCircle(p);
    83.     return 1. - smoothstep(r - (r * hard), r + (r * hard), dist);
    84. }
    85.  
    86.  
    87.  
    88.  
    89.  
    90.     fixed4 frag(VertexOutput vertex_output) : SV_Target
    91.     {
    92.  
    93.     float2 uv = vertex_output.uv / 1;
    94.     float2 ouv = uv;
    95.  
    96.     uv = frac(uv * SIZE);
    97.     ouv = floor(ouv * SIZE) / SIZE;
    98.  
    99.     float spMask = sphereMask(uv, .5, .05);
    100.  
    101.     float3 color = vec3(spMask);
    102.  
    103.     float flow = abs(sin(1. - radialMask(ouv) + _Time.y * 4.));
    104.     flow = pow(flow, 2.);
    105.     float3 rainbow = hueShift(vec3(1., 0., 0.), flow);
    106.  
    107.     float flow2 = abs(sin(1. - radialMask(ouv) + _Time.y * 1.5));
    108.     flow2 = pow(flow2, 3.);
    109.  
    110.     color *= rainbow * flow2;
    111.  
    112.     color = lerp(BGCOLOR, color, spMask);
    113.  
    114.     return vec4(color, 1.);
    115.  
    116.     }
    117.     ENDCG
    118.     }
    119.   }
    120.   }
    121.  
     
  47. Madhanraj

    Madhanraj

    Joined:
    Feb 14, 2018
    Posts:
    3
    Thank you!