Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Converting ShaderToy to Unity aka (Converting GLSL shaders to Cg/HLSL)

Discussion in 'Shaders' started by markashburner, Mar 17, 2018.

  1. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    I am trying to convert this shader from ShaderToy to Unity.

    I found an awesome plugin called ShaderMan which helps out a lot over here:

    https://github.com/smkplus/ShaderMan

    I am trying to convert this shader here from ShaderToy:

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

    Using ShaderMan I managed to get this code:

    Code (CSharp):
    1. Shader "ShaderMan/Terrain"
    2.     {
    3.  
    4.     Properties{
    5.     _MainTex ("MainTex", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    11.  
    12.     Pass
    13.     {
    14.     ZWrite Off
    15.     Blend SrcAlpha OneMinusSrcAlpha
    16.  
    17.     CGPROGRAM
    18.     #pragma vertex vert
    19.     #pragma fragment frag
    20.     #include "UnityCG.cginc"
    21.  
    22.     struct VertexInput {
    23.     fixed4 vertex : POSITION;
    24.     fixed2 uv:TEXCOORD0;
    25.     fixed4 tangent : TANGENT;
    26.     fixed3 normal : NORMAL;
    27.     //VertexInput
    28.     };
    29.  
    30.  
    31.     struct VertexOutput {
    32.     fixed4 pos : SV_POSITION;
    33.     fixed2 uv:TEXCOORD0;
    34.     //VertexOutput
    35.     };
    36.  
    37.     //Variables
    38. sampler2D _SecondTex;
    39. sampler2D _MainTex;
    40.  
    41.  
    42. // One simple way to avoid tex2D tile repetition, at the cost of 4 times the amount of
    43. // tex2D lookups (still much better than https://www.shadertoy.com/view/4tsGzf)
    44. //
    45. // More info: http://www.iquilezles.org/www/articles/tex2Drepetition/tex2Drepetition.htm
    46.  
    47. #define USEHASH
    48.  
    49. fixed4 hash4( fixed2 p ) { return frac(sin(fixed4( 1.0+dot(p,fixed2(37.0,17.0)),
    50.                                               2.0+dot(p,fixed2(11.0,47.0)),
    51.                                               3.0+dot(p,fixed2(41.0,29.0)),
    52.                                               4.0+dot(p,fixed2(23.0,31.0))))*103.0); }
    53.  
    54. fixed4 tex2DNoTile( sampler2D samp, in fixed2 uv )
    55. {
    56.     fixed2 iuv = floor( uv );
    57.     fixed2 fuv = frac( uv );
    58.  
    59. #ifdef USEHASH  
    60.     // generate per-tile transform (needs GL_NEAREST_MIPMAP_LINEARto work right)
    61.     fixed4 ofa = tex2D( _SecondTex, (iuv + fixed2(0.5,0.5))/256.0 );
    62.     fixed4 ofb = tex2D( _SecondTex, (iuv + fixed2(1.5,0.5))/256.0 );
    63.     fixed4 ofc = tex2D( _SecondTex, (iuv + fixed2(0.5,1.5))/256.0 );
    64.     fixed4 ofd = tex2D( _SecondTex, (iuv + fixed2(1.5,1.5))/256.0 );
    65. #else
    66.     // generate per-tile transform
    67.     fixed4 ofa = hash4( iuv + fixed2(0.0,0.0) );
    68.     fixed4 ofb = hash4( iuv + fixed2(1.0,0.0) );
    69.     fixed4 ofc = hash4( iuv + fixed2(0.0,1.0) );
    70.     fixed4 ofd = hash4( iuv + fixed2(1.0,1.0) );
    71. #endif
    72.  
    73.     fixed2 ddx = dFdx( uv );
    74.     fixed2 ddy = dFdy( uv );
    75.  
    76.     // transform per-tile uvs
    77.     ofa.zw = sign(ofa.zw-0.5);
    78.     ofb.zw = sign(ofb.zw-0.5);
    79.     ofc.zw = sign(ofc.zw-0.5);
    80.     ofd.zw = sign(ofd.zw-0.5);
    81.  
    82.     // uv's, and derivarives (for correct mipmapping)
    83.     fixed2 uva = uv*ofa.zw + ofa.xy; fixed2 ddxa = ddx*ofa.zw; fixed2 ddya = ddy*ofa.zw;
    84.     fixed2 uvb = uv*ofb.zw + ofb.xy; fixed2 ddxb = ddx*ofb.zw; fixed2 ddyb = ddy*ofb.zw;
    85.     fixed2 uvc = uv*ofc.zw + ofc.xy; fixed2 ddxc = ddx*ofc.zw; fixed2 ddyc = ddy*ofc.zw;
    86.     fixed2 uvd = uv*ofd.zw + ofd.xy; fixed2 ddxd = ddx*ofd.zw; fixed2 ddyd = ddy*ofd.zw;
    87.      
    88.     // fetch and blend
    89.     fixed2 b = smoothstep(0.25,0.75,fuv);
    90.  
    91.     return lerp( lerp( tex2DGrad( samp, uva, ddxa, ddya ),
    92.                      tex2DGrad( samp, uvb, ddxb, ddyb ), b.x ),
    93.                 lerp( tex2DGrad( samp, uvc, ddxc, ddyc ),
    94.                      tex2DGrad( samp, uvd, ddxd, ddyd ), b.x), b.y );
    95. }
    96.  
    97.  
    98.  
    99.  
    100.  
    101.     VertexOutput vert (VertexInput v)
    102.     {
    103.     VertexOutput o;
    104.     o.pos = UnityObjectToClipPos (v.vertex);
    105.     o.uv = v.uv;
    106.     //VertexFactory
    107.     return o;
    108.     }
    109.     fixed4 frag(VertexOutput i) : SV_Target
    110.     {
    111.  
    112.     fixed2 uv = i.uv / 1;
    113.  
    114.     fixed f = smoothstep( 0.4, 0.6, sin(_Time.y    ) );
    115.     fixed s = smoothstep( 0.4, 0.6, sin(_Time.y*0.5) );
    116.      
    117.     uv = (4.0 + 16.0*s)*uv + _Time.y*0.1;
    118.      
    119.     fixed3 cola = tex2DNoTile( _MainTex, uv ).xyz;
    120.     fixed3 colb = tex2D( _MainTex, uv ).xyz;
    121.  
    122.     fixed3 col = lerp( cola, colb, f );
    123.  
    124.     return fixed4( col, 1.0 );
    125.  
    126.     }
    127.     ENDCG
    128.     }
    129.   }
    130. }
    131.  
    132.  
    However it comes up with this error here:

    Shader error in 'ShaderMan/Terrain': undeclared identifier 'dFdx' at line 78 (on d3d11)


    It has something to do with these lines of code here:

    Code (CSharp):
    1.  fixed2 ddx = dFdx( uv );
    2.     fixed2 ddy = dFdy( uv );
    Would someone help me solve this issue?

    Thanks
     
    Last edited: Mar 17, 2018
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    That's because it's trying to use the GLSL screenspace derivative calls still. Just change those two lines to:


    Code (CSharp):
    1. fixed2 ddx = ddx( uv );
    2. fixed2 ddy = ddy( uv );
    Though you might also have to change the variable name it's assigning to as well, since they might conflict with the ddy/ddx method names.

    edit: I pushed a fix for that tool you linked, so whenever they accept the pull request then there will be a new version to download and use that won't have that issue (or just click pull requests and download my fork).
     
    Last edited: Mar 17, 2018
    sharkapps likes this.
  3. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Yes I figured that out! Thanks for the pull request as well.

    There was another issue I solved and that is the tex2DGrad should be tex2Dgrad instead:

    Code (CSharp):
    1. return lerp( lerp( tex2Dgrad ( samp, uva, ddxa, ddya ),
    2.                      tex2Dgrad( samp, uvb, ddxb, ddyb ), b.x ),
    3.                 lerp( tex2Dgrad ( samp, uvc, ddxc, ddyc ),
    4.                      tex2Dgrad ( samp, uvd, ddxd, ddyd ), b.x), b.y );
    5. }
    The shader now works however I am not getting the same effect as the one from ShaderToy

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

    If you want to test it out yourself, here is the fixed code:

    Code (CSharp):
    1. Shader "ShaderMan/Terrain"
    2.     {
    3.  
    4.     Properties{
    5.     _MainTex ("MainTex", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    11.  
    12.     Pass
    13.     {
    14.     ZWrite Off
    15.     Blend SrcAlpha OneMinusSrcAlpha
    16.  
    17.     CGPROGRAM
    18.     #pragma vertex vert
    19.     #pragma fragment frag
    20.     #include "UnityCG.cginc"
    21.  
    22.     struct VertexInput {
    23.     fixed4 vertex : POSITION;
    24.     fixed2 uv:TEXCOORD0;
    25.     fixed4 tangent : TANGENT;
    26.     fixed3 normal : NORMAL;
    27.     //VertexInput
    28.     };
    29.  
    30.  
    31.     struct VertexOutput {
    32.     fixed4 pos : SV_POSITION;
    33.     fixed2 uv:TEXCOORD0;
    34.     //VertexOutput
    35.     };
    36.  
    37.     //Variables
    38. sampler2D _SecondTex;
    39. sampler2D _MainTex;
    40.  
    41.     // The MIT License
    42. // Copyright © 2015 Inigo Quilez
    43. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, fmodify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    44.  
    45.  
    46. // One simple way to avoid tex2D tile repetition, at the cost of 4 times the amount of
    47. // tex2D lookups (still much better than https://www.shadertoy.com/view/4tsGzf)
    48. //
    49. // More info: http://www.iquilezles.org/www/articles/tex2Drepetition/tex2Drepetition.htm
    50.  
    51. #define USEHASH
    52.  
    53. fixed4 hash4( fixed2 p ) { return frac(sin(fixed4( 1.0+dot(p,fixed2(37.0,17.0)),
    54.                                               2.0+dot(p,fixed2(11.0,47.0)),
    55.                                               3.0+dot(p,fixed2(41.0,29.0)),
    56.                                               4.0+dot(p,fixed2(23.0,31.0))))*103.0); }
    57.  
    58. fixed4 tex2DNoTile( sampler2D samp, in fixed2 uv )
    59. {
    60.     fixed2 iuv = floor( uv );
    61.     fixed2 fuv = frac( uv );
    62.    
    63.  
    64. #ifdef USEHASH  
    65.     // generate per-tile transform (needs GL_NEAREST_MIPMAP_LINEARto work right)
    66.     fixed4 ofa = tex2D( _SecondTex, (iuv + fixed2(0.5,0.5))/256.0 );
    67.     fixed4 ofb = tex2D( _SecondTex, (iuv + fixed2(1.5,0.5))/256.0 );
    68.     fixed4 ofc = tex2D( _SecondTex, (iuv + fixed2(0.5,1.5))/256.0 );
    69.     fixed4 ofd = tex2D( _SecondTex, (iuv + fixed2(1.5,1.5))/256.0 );
    70. #else
    71.     // generate per-tile transform
    72.     fixed4 ofa = hash4( iuv + fixed2(0.0,0.0) );
    73.     fixed4 ofb = hash4( iuv + fixed2(1.0,0.0) );
    74.     fixed4 ofc = hash4( iuv + fixed2(0.0,1.0) );
    75.     fixed4 ofd = hash4( iuv + fixed2(1.0,1.0) );
    76. #endif
    77.    
    78.     fixed2 _ddx = ddx( uv );
    79.     fixed2 _ddy = ddy( uv );
    80.  
    81.     // transform per-tile uvs
    82.     ofa.zw = sign(ofa.zw-0.5);
    83.     ofb.zw = sign(ofb.zw-0.5);
    84.     ofc.zw = sign(ofc.zw-0.5);
    85.     ofd.zw = sign(ofd.zw-0.5);
    86.    
    87.     // uv's, and derivarives (for correct mipmapping)
    88.     fixed2 uva = uv*ofa.zw + ofa.xy; fixed2 ddxa = _ddx*ofa.zw; fixed2 ddya = _ddy*ofa.zw;
    89.     fixed2 uvb = uv*ofb.zw + ofb.xy; fixed2 ddxb = _ddx*ofb.zw; fixed2 ddyb = _ddy*ofb.zw;
    90.     fixed2 uvc = uv*ofc.zw + ofc.xy; fixed2 ddxc = _ddx*ofc.zw; fixed2 ddyc = _ddy*ofc.zw;
    91.     fixed2 uvd = uv*ofd.zw + ofd.xy; fixed2 ddxd = _ddx*ofd.zw; fixed2 ddyd = _ddy*ofd.zw;
    92.        
    93.     // fetch and blend
    94.     fixed2 b = smoothstep(0.25,0.75,fuv);
    95.    
    96.     return lerp( lerp( tex2Dgrad ( samp, uva, ddxa, ddya ),
    97.                      tex2Dgrad( samp, uvb, ddxb, ddyb ), b.x ),
    98.                 lerp( tex2Dgrad ( samp, uvc, ddxc, ddyc ),
    99.                      tex2Dgrad ( samp, uvd, ddxd, ddyd ), b.x), b.y );
    100. }
    101.  
    102.  
    103.  
    104.  
    105.  
    106.     VertexOutput vert (VertexInput v)
    107.     {
    108.     VertexOutput o;
    109.     o.pos = UnityObjectToClipPos (v.vertex);
    110.     o.uv = v.uv;
    111.     //VertexFactory
    112.     return o;
    113.     }
    114.     fixed4 frag(VertexOutput i) : SV_Target
    115.     {
    116.    
    117.     fixed2 uv = i.uv / 1;
    118.    
    119.     fixed f = smoothstep( 0.4, 0.6, sin(_Time.y    ) );
    120.     fixed s = smoothstep( 0.4, 0.6, sin(_Time.y*0.5) );
    121.        
    122.     uv = (4.0 + 16.0*s)*uv + _Time.y*0.1;
    123.        
    124.     fixed3 cola = tex2DNoTile( _MainTex, uv ).xyz;
    125.     fixed3 colb = tex2D( _MainTex, uv ).xyz;
    126.    
    127.     fixed3 col = lerp( cola, colb, f );
    128.    
    129.     return fixed4( col, 1.0 );
    130.  
    131.     }
    132.     ENDCG
    133.     }
    134.   }
    135. }
    136.  
    137.  
    As you can see the effect isn't quite the same as the one from ShaderToy.
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Thanks, committed that change as well.

    The effect seems the same for me. Did you enter play mode? Editor window doesn't update at a consistent rate, so time based shader effects will be kinda wonky unless you are in play mode.
     
  5. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Yes in play mode, it only rotates the textures on its x and y axis which still causes tiled repetition.

    there doesn't seem to be any difference between tex2Dgrad and tex2D.

    This is a screenshot of when the tiles are rotated, as you can see there is still tiled repetition.
     

    Attached Files:

  6. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    It's because they are using a second texture in that shader which I guess the converted didn't make shaderlab for.
    Add:
    _SecondTex ("Variation Noise", 2D) = "white" {}

    to your "Properties" section at the top. And then assign the Channel1 noise texture they have assigned on that website.
     
  7. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Yup I did that. Still doesn't seem to make a difference?

    Does it have something to do with the Vertex Output?

    Code (CSharp):
    1. fixed4 frag(VertexOutput i) : SV_Target
    2.     {
    3.  
    4.     fixed2 uv = i.uv / 1;
    5.  
    6.     fixed f = smoothstep( 0.4, 0.6, sin(_Time.y    ) );
    7.     fixed s = smoothstep( 0.4, 0.6, sin(_Time.y*0.5) );
    8.      
    9.     uv = (4.0 + 16.0*s)*uv + _Time.y*0.1;
    10.      
    11.     fixed3 cola = tex2DNoTile( _MainTex, uv ).xyz;
    12.     fixed3 colb = tex2D( _MainTex, uv ).xyz;
    13.  
    14.     fixed3 col = lerp( cola, colb, f );
    15.  
    16.     return fixed4( col, 1.0 );
    17.  
    18.     }
     

    Attached Files:

  8. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    No, that's proper, that's how it is in the shadertoy page.

     
  9. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    That's weird it works for you but not for me.

    Do you mind posting your code? Thanks
     
  10. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Here, I also updated it with an alternative to the missing GL_NEAREST_MIPMAP_LINEARto that Unity doesn't have implemented for HLSL/CG. So the hash method won't have the line artifacts between tiles.


    Code (CSharp):
    1. Shader "ShaderMan/Terrain"
    2. {
    3.     Properties{
    4.         _MainTex("MainTex", 2D) = "white" {}
    5.         _SecondTex("Variation Noise", 2D) = "white" {}
    6.     }
    7.  
    8.         SubShader
    9.     {
    10.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    11.  
    12.     Pass
    13.     {
    14.     ZWrite Off
    15.     Blend SrcAlpha OneMinusSrcAlpha
    16.  
    17.     CGPROGRAM
    18.     #pragma vertex vert
    19.     #pragma fragment frag
    20.     #include "UnityCG.cginc"
    21.  
    22.     struct VertexInput {
    23.     fixed4 vertex : POSITION;
    24.     fixed2 uv : TEXCOORD0;
    25.     fixed4 tangent : TANGENT;
    26.     fixed3 normal : NORMAL;
    27.     //VertexInput
    28.     };
    29.  
    30.     struct VertexOutput {
    31.     fixed4 pos : SV_POSITION;
    32.     fixed2 uv : TEXCOORD0;
    33.     //VertexOutput
    34.     };
    35.  
    36.     //Variables
    37.     sampler2D _SecondTex;
    38.     float4 _SecondTex_TexelSize;
    39.     sampler2D _MainTex;
    40.  
    41.     // The MIT License
    42. // Copyright © 2015 Inigo Quilez
    43. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, fmodify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    44.  
    45. // One simple way to avoid tex2D tile repetition, at the cost of 4 times the amount of
    46. // tex2D lookups (still much better than https://www.shadertoy.com/view/4tsGzf)
    47. //
    48. // More info: http://www.iquilezles.org/www/articles/tex2Drepetition/tex2Drepetition.htm
    49.  
    50. #define USEHASH
    51.  
    52.     fixed4 hash4(fixed2 p)
    53.     {
    54.         return frac(sin(fixed4(
    55.                         1.0 + dot(p,fixed2(37.0,17.0)),
    56.                         2.0 + dot(p,fixed2(11.0,47.0)),
    57.                         3.0 + dot(p,fixed2(41.0,29.0)),
    58.                         4.0 + dot(p,fixed2(23.0,31.0))))*103.0);
    59.     }
    60.  
    61.     float map(float a, float b, float r, float s, float value)
    62.     {
    63.         if (a == b) { return (value <= a) ? r : s; }
    64.  
    65.         float ratio = (value - a) / (b - a);
    66.         return r + (s - r) * ratio;
    67.     }
    68.  
    69.     float mapAndClamp(float a, float b, float s, float t, float value)
    70.     {
    71.         return clamp(map(a, b, s, t, value), s, t);
    72.     }
    73.  
    74.     float4 pointSampleTex2D(sampler2D tex, float2 uv, float4 st)
    75.     {
    76.         float2 snappedUV = ((float2)((int2)(uv * st.zw + float2(1, 1))) - float2(0.5, 0.5)) * st.xy;
    77.         return tex2Dlod(tex, float4(snappedUV.x, snappedUV.y, 0, 0));
    78.     }
    79.     //HLSL compatible adaption of GL_NEAREST_MIPMAP_LINEARto by
    80.     // https://forum.unity.com/threads/filter-mode-point-and-linear-filter-on-a-texture-based-on-the-distance.462621/#post-3009740
    81.     float4 crispMipMapTex2D(sampler2D tex, float2 uv, float4 texelSize)
    82.     {
    83.         float2 _ddx = ddx(uv);
    84.         float2 _ddy = ddy(uv);
    85.         float lod = max(_ddx * texelSize.zw.x, _ddy *  texelSize.zw.x);
    86.         // float lod = sqrt(pow(_ddx * texelSize.zw.x, 2) + pow(_ddy *  texelSize.zw.x, 2));
    87.         float t = mapAndClamp(0.5, 1, 0, 1, lod);
    88.         return lerp(pointSampleTex2D(tex, uv, texelSize), tex2D(tex, uv, _ddx, _ddy), t);
    89.     }
    90.  
    91.     fixed4 tex2DNoTile(sampler2D samp, in fixed2 uv)
    92.     {
    93.         fixed2 iuv = floor(uv);
    94.         fixed2 fuv = frac(uv);
    95.         fixed2 _ddx = ddx(uv);
    96.         fixed2 _ddy = ddy(uv);
    97.  
    98.     #ifdef USEHASH
    99.         // generate per-tile transform (needs GL_NEAREST_MIPMAP_LINEARto work right)
    100.         fixed4 ofa = crispMipMapTex2D(_SecondTex, (iuv + fixed2(0.5,0.5)) / 256.0, _SecondTex_TexelSize);
    101.         fixed4 ofb = crispMipMapTex2D(_SecondTex, (iuv + fixed2(1.5,0.5)) / 256.0, _SecondTex_TexelSize);
    102.         fixed4 ofc = crispMipMapTex2D(_SecondTex, (iuv + fixed2(0.5,1.5)) / 256.0, _SecondTex_TexelSize);
    103.         fixed4 ofd = crispMipMapTex2D(_SecondTex, (iuv + fixed2(1.5,1.5)) / 256.0, _SecondTex_TexelSize);
    104.     #else
    105.         // generate per-tile transform
    106.         fixed4 ofa = hash4(iuv + fixed2(0.0,0.0));
    107.         fixed4 ofb = hash4(iuv + fixed2(1.0,0.0));
    108.         fixed4 ofc = hash4(iuv + fixed2(0.0,1.0));
    109.         fixed4 ofd = hash4(iuv + fixed2(1.0,1.0));
    110.     #endif
    111.  
    112.         // transform per-tile uvs
    113.         ofa.zw = sign(ofa.zw - 0.5);
    114.         ofb.zw = sign(ofb.zw - 0.5);
    115.         ofc.zw = sign(ofc.zw - 0.5);
    116.         ofd.zw = sign(ofd.zw - 0.5);
    117.  
    118.         // uv's, and derivarives (for correct mipmapping)
    119.         fixed2 uva = uv * ofa.zw + ofa.xy; fixed2 ddxa = _ddx * ofa.zw; fixed2 ddya = _ddy * ofa.zw;
    120.         fixed2 uvb = uv * ofb.zw + ofb.xy; fixed2 ddxb = _ddx * ofb.zw; fixed2 ddyb = _ddy * ofb.zw;
    121.         fixed2 uvc = uv * ofc.zw + ofc.xy; fixed2 ddxc = _ddx * ofc.zw; fixed2 ddyc = _ddy * ofc.zw;
    122.         fixed2 uvd = uv * ofd.zw + ofd.xy; fixed2 ddxd = _ddx * ofd.zw; fixed2 ddyd = _ddy * ofd.zw;
    123.  
    124.         // fetch and blend
    125.         fixed2 b = smoothstep(0.25,0.75,fuv);
    126.  
    127.         return lerp(lerp(tex2Dgrad(samp, uva, ddxa, ddya),
    128.                         tex2Dgrad(samp, uvb, ddxb, ddyb), b.x),
    129.                     lerp(tex2Dgrad(samp, uvc, ddxc, ddyc),
    130.                         tex2Dgrad(samp, uvd, ddxd, ddyd), b.x), b.y);
    131.     }
    132.  
    133.     VertexOutput vert(VertexInput v)
    134.     {
    135.         VertexOutput o;
    136.         o.pos = UnityObjectToClipPos(v.vertex);
    137.         o.uv = v.uv;
    138.         //VertexFactory
    139.         return o;
    140.     }
    141.     fixed4 frag(VertexOutput i) : SV_Target
    142.     {
    143.         fixed2 uv = i.uv / 1;
    144.  
    145.         fixed f = smoothstep(0.4, 0.6, sin(_Time.y));
    146.         fixed s = smoothstep(0.4, 0.6, sin(_Time.y*0.5));
    147.  
    148.         uv = (4.0 + 16.0*s)*uv + _Time.y*0.1;
    149.  
    150.         fixed3 cola = tex2DNoTile(_MainTex, uv).xyz;
    151.         fixed3 colb = tex2D(_MainTex, uv).xyz;
    152.  
    153.         fixed3 col = lerp(cola, colb, f);
    154.  
    155.         return fixed4(col, 1.0);
    156.     }
    157.     ENDCG
    158.     }
    159.     }
    160. }
     
  11. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Thanks Invertex that's awesome!

    i realized that the noise file I had was what was causing it to look tiled. After changing my shader script to yours and using the noise file you uploaded, it works pretty awesome now!

    Do you think this shader would work well with terrains? Especially a spherical terrain?

    Obviously just have to get rid of the time functions and the panning but I am hoping to integrate it with a terrain shader to get rid of tiled textures. This seems like an awesome solution.

    I will post screenshots once I have it set up correctly.

    Thanks again man!

    You should check out all those shaders on ShaderToy...there are some amazing shaders on that site!

    ShaderMan is the man ;)
     
  12. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Oh and just to let you know, I also converted this shader as well:

    https://www.shadertoy.com/view/4tsGzf

    Which works perfectly and looks just as good as well.

    Here is the code for it.

    Code (CSharp):
    1. Shader "ShaderMan/Terrain2"
    2.     {
    3.  
    4.     Properties{
    5.     _MainTex ("MainTex", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    11.  
    12.     Pass
    13.     {
    14.     ZWrite Off
    15.     Blend SrcAlpha OneMinusSrcAlpha
    16.  
    17.     CGPROGRAM
    18.     #pragma vertex vert
    19.     #pragma fragment frag
    20.     #include "UnityCG.cginc"
    21.  
    22.     struct VertexInput {
    23.     fixed4 vertex : POSITION;
    24.     fixed2 uv:TEXCOORD0;
    25.     fixed4 tangent : TANGENT;
    26.     fixed3 normal : NORMAL;
    27.     //VertexInput
    28.     };
    29.  
    30.  
    31.     struct VertexOutput {
    32.     fixed4 pos : SV_POSITION;
    33.     fixed2 uv:TEXCOORD0;
    34.     //VertexOutput
    35.     };
    36.  
    37.     //Variables
    38. sampler2D _MainTex;
    39.  
    40.     // The MIT License
    41. // Copyright © 2015 Inigo Quilez
    42. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, fmodify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    43.  
    44.  
    45. // One way to avoid tex2D tile repetition one using one small tex2D to cover a huge area.
    46. // Based on Voronoise (https://www.shadertoy.com/view/Xd23Dh), a random offset is applied to
    47. // the tex2D UVs per Voronoi cell. Distance to the cell is used to smooth the transitions
    48. // between cells.
    49.  
    50. // More info here: http://www.iquilezles.org/www/articles/tex2Drepetition/tex2Drepetition.htm
    51.  
    52.  
    53. fixed4 hash4( fixed2 p ) { return frac(sin(fixed4( 1.0+dot(p,fixed2(37.0,17.0)),
    54.                                               2.0+dot(p,fixed2(11.0,47.0)),
    55.                                               3.0+dot(p,fixed2(41.0,29.0)),
    56.                                               4.0+dot(p,fixed2(23.0,31.0))))*103.0); }
    57.  
    58.  
    59. fixed3 tex2DNoTile( sampler2D samp, in fixed2 uv, fixed v )
    60. {
    61.     fixed2 p = floor( uv );
    62.     fixed2 f = frac( uv );
    63.  
    64.     // derivatives (for correct mipmapping)
    65.     fixed2 _ddx = ddx( uv );
    66.     fixed2 _ddy = ddy( uv );
    67.  
    68.     fixed3 va = fixed3(0.0,0.0,0.0);
    69.     fixed w1 = 0.01;
    70.     fixed w2 = 0.01;
    71.     [unroll(100)]
    72. for( int j=-1; j<=1; j++ )
    73.     [unroll(100)]
    74. for( int i=-1; i<=1; i++ )
    75.     {
    76.         fixed2 g = fixed2( fixed(i),fixed(j) );
    77.         fixed4 o = hash4( p + g );
    78.         fixed2 r = g - f + o.xy;
    79.         fixed d = dot(r,r);
    80.         fixed w = exp(-5.0*d );
    81.         fixed3 c = tex2D( samp, uv + v*o.zw, _ddx, _ddy ).xyz;
    82.         va += w*c;
    83.         w1 += w;
    84.         w2 += w*w;
    85.     }
    86.  
    87.     // normal averaging --> lowers contrasts
    88.     //return va/w1;
    89.  
    90.     // contrast preserving average
    91.     fixed mean = 0;// tex2DGrad( samp, uv, ddx*16.0, ddy*16.0 ).x;
    92.     fixed3 res = mean + (va-w1*mean)/sqrt(w2);
    93.     return lerp( va/w1, res, v );
    94. }
    95.  
    96.  
    97.  
    98.  
    99.  
    100.     VertexOutput vert (VertexInput v)
    101.     {
    102.     VertexOutput o;
    103.     o.pos = UnityObjectToClipPos (v.vertex);
    104.     o.uv = v.uv;
    105.     //VertexFactory
    106.     return o;
    107.     }
    108.     fixed4 frag(VertexOutput i) : SV_Target
    109.     {
    110.  
    111.     fixed2 uv = i.uv / 1;
    112.  
    113.     fixed f = smoothstep( 0.4, 0.6, sin(_Time.y    ) );
    114.     fixed s = smoothstep( 0.4, 0.6, sin(_Time.y*0.5) );
    115.  
    116.     fixed3 col = tex2DNoTile( _MainTex, (4.0 + 4.0*s)*uv + _Time.y*0.1, f ).xyz;
    117.  
    118.     return fixed4( col, 1.0 );
    119.  
    120.     }
    121.     ENDCG
    122.     }
    123.   }
    124. }
     
  13. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Haha yes I know ShaderToy well. It was how I started out learning to write shaders in Unity, by taking interesting ones and researching how I could implement it in Unity's CG shaders. It's a great way to learn about all the different syntaxes in a meaningful way.

    Glad it's working for ya, and yeah that shader seems a little less process intensive.
     
  14. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Invertex I am fairly a newb at shading.

    Would you be able to tell me how to get rid of the smoothstep and time variables as well as the panning and switching from tiled and none tiled texture?

    Code (CSharp):
    1. Shader "ShaderMan/PlanetSurface"
    2.     {
    3.  
    4.     Properties{
    5.     _MainTex ("MainTex", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    11.  
    12.     Pass
    13.     {
    14.     ZWrite Off
    15.     Blend SrcAlpha OneMinusSrcAlpha
    16.  
    17.     CGPROGRAM
    18.     #pragma  vertex vert
    19.     #pragma fragment frag
    20.     #include "UnityCG.cginc"
    21.  
    22.     struct VertexInput {
    23.     fixed4 vertex : POSITION;
    24.     fixed2 uv:TEXCOORD0;
    25.     fixed4 tangent : TANGENT;
    26.     fixed3 normal : NORMAL;
    27.     //VertexInput
    28.     };
    29.  
    30.  
    31.     struct VertexOutput {
    32.     fixed4 pos : SV_POSITION;
    33.     fixed2 uv:TEXCOORD0;
    34.     //VertexOutput
    35.     };
    36.  
    37.     //Variables
    38. sampler2D _MainTex;
    39.  
    40.  
    41.  
    42.  
    43. // One way to avoid tex2D tile repetition one using one small tex2D to cover a huge area.
    44. // Based on Voronoise (https://www.shadertoy.com/view/Xd23Dh), a random offset is applied to
    45. // the tex2D UVs per Voronoi cell. Distance to the cell is used to smooth the transitions
    46. // between cells.
    47.  
    48. // More info here: http://www.iquilezles.org/www/articles/tex2Drepetition/tex2Drepetition.htm
    49.  
    50.  
    51. fixed4 hash4( fixed2 p ) { return frac(sin(fixed4( 1.0+dot(p,fixed2(37.0,17.0)),
    52.                                               2.0+dot(p,fixed2(11.0,47.0)),
    53.                                               3.0+dot(p,fixed2(41.0,29.0)),
    54.                                               4.0+dot(p,fixed2(23.0,31.0))))*103.0); }
    55.  
    56.  
    57. fixed3 tex2DNoTile( sampler2D samp, in fixed2 uv, fixed v )
    58. {
    59.     fixed2 p = floor( uv );
    60.     fixed2 f = frac( uv );
    61.    
    62.     // derivatives (for correct mipmapping)
    63.     fixed2 _ddx = ddx( uv );
    64.     fixed2 _ddy = ddy( uv );
    65.    
    66.     fixed3 va = fixed3(0.0,0.0,0.0);
    67.     fixed w1 = 0.0;
    68.     fixed w2 = 0.0;
    69.     [unroll(100)]
    70. for( int j=-1; j<=1; j++ )
    71.     [unroll(100)]
    72. for( int i=-1; i<=1; i++ )
    73.     {
    74.         fixed2 g = fixed2( fixed(i),fixed(j) );
    75.         fixed4 o = hash4( p + g );
    76.         fixed2 r = g - f + o.xy;
    77.         fixed d = dot(r,r);
    78.         fixed w = exp(-5.0*d );
    79.         fixed3 c = tex2D( samp, uv + v*o.zw, _ddx, _ddy ).xyz;
    80.         va += w*c;
    81.         w1 += w;
    82.         w2 += w*w;
    83.     }
    84.    
    85.     // normal averaging --> lowers contrasts
    86.     //return va/w1;
    87.  
    88.     // contrast preserving average
    89.     fixed mean = 0;// tex2DGrad( samp, uv, ddx*16.0, ddy*16.0 ).x;
    90.     fixed3 res = mean + (va-w1*mean)/sqrt(w2);
    91.     return lerp( va/w1, res, v );
    92. }
    93.  
    94.  
    95.  
    96.  
    97.  
    98.     VertexOutput vert (VertexInput v)
    99.     {
    100.     VertexOutput o;
    101.     o.pos = UnityObjectToClipPos (v.vertex);
    102.     o.uv = v.uv;
    103.     //VertexFactory
    104.     return o;
    105.     }
    106.     fixed4 frag(VertexOutput i) : SV_Target
    107.     {
    108.    
    109.     fixed2 uv = i.uv /1;
    110.    
    111.     fixed f = smoothstep( 0.0, 0.0, sin(_Time.y    ) );
    112.     fixed s = smoothstep( 0.0, 0.0, sin(_Time.y*0.0) );
    113.  
    114.     fixed3 col = tex2DNoTile( _MainTex, (5.0 + 5.0*s)*uv + _Time.y*0.0, f ).xyz;
    115.    
    116.     return fixed4( col, 1.0 );
    117.  
    118.     }
    119.     ENDCG
    120.     }
    121.   }
    122. }
     
  15. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    It's as simple as removing the _Time references. But once you do that, f and s will always be zero, so those are useless too, so you get rid of those also and any references to them, and you're good to go. Here is the shader, with an extra parameter to control the pattern shifting.


    Code (CSharp):
    1. Shader "ShaderMan/PlanetSurface"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("MainTex", 2D) = "white" {}
    6.         _FractalDivergance("Fractal Divergance", Range(0, 4)) = 1.0
    7.     }
    8.     SubShader
    9.     {
    10.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    11.     Pass
    12.     {
    13.     ZWrite Off
    14.     Blend SrcAlpha OneMinusSrcAlpha
    15.     CGPROGRAM
    16.     #pragma  vertex vert
    17.     #pragma fragment frag
    18.     #include "UnityCG.cginc"
    19.     struct VertexInput {
    20.     fixed4 vertex : POSITION;
    21.     fixed2 uv:TEXCOORD0;
    22.     fixed4 tangent : TANGENT;
    23.     fixed3 normal : NORMAL;
    24.     //VertexInput
    25.     };
    26.     struct VertexOutput {
    27.     fixed4 pos : SV_POSITION;
    28.     fixed2 uv:TEXCOORD0;
    29.     //VertexOutput
    30.     };
    31.     //Variables
    32.     sampler2D _MainTex;
    33.     float _FractalDivergance;
    34.     // One way to avoid tex2D tile repetition one using one small tex2D to cover a huge area.
    35.     // Based on Voronoise (https://www.shadertoy.com/view/Xd23Dh), a random offset is applied to
    36.     // the tex2D UVs per Voronoi cell. Distance to the cell is used to smooth the transitions
    37.     // between cells.
    38.     // More info here: http://www.iquilezles.org/www/articles/tex2Drepetition/tex2Drepetition.htm
    39.     fixed4 hash4( fixed2 p ) { return frac(sin(fixed4( 1.0+dot(p,fixed2(37.0,17.0)),
    40.                                                   2.0+dot(p,fixed2(11.0,47.0)),
    41.                                                   3.0+dot(p,fixed2(41.0,29.0)),
    42.                                                   4.0+dot(p,fixed2(23.0,31.0)))) * _FractalDivergance); }
    43.     fixed3 tex2DNoTile( sampler2D samp, in fixed2 uv, fixed v )
    44.     {
    45.         fixed2 p = floor( uv );
    46.         fixed2 f = frac( uv );
    47.  
    48.         // derivatives (for correct mipmapping)
    49.         fixed2 _ddx = ddx( uv );
    50.         fixed2 _ddy = ddy( uv );
    51.  
    52.         fixed3 va = fixed3(0.0,0.0,0.0);
    53.         fixed w1 = 0.0;
    54.         fixed w2 = 0.0;
    55.  
    56.         [unroll(3)]
    57.         for (int j = -1; j <= 1; j++)
    58.         {
    59.             [unroll(3)]
    60.             for (int i = -1; i <= 1; i++)
    61.             {
    62.                 fixed2 g = fixed2(fixed(i), fixed(j));
    63.                 fixed4 o = hash4(p + g);
    64.                 fixed2 r = g - f + o.xy;
    65.                 fixed d = dot(r, r);
    66.                 fixed w = exp(-5.0*d);
    67.                 fixed3 c = tex2D(samp, uv + v * o.zw, _ddx, _ddy).xyz;
    68.                 va += w * c;
    69.                 w1 += w;
    70.                 w2 += w * w;
    71.             }
    72.         }
    73.         // normal averaging --> lowers contrasts
    74.         //return va/w1;
    75.         // contrast preserving average
    76.         fixed mean = 0;// tex2DGrad( samp, uv, ddx*16.0, ddy*16.0 ).x;
    77.         fixed3 res = mean + (va-w1*mean)/sqrt(w2);
    78.         return lerp( va/w1, res, v );
    79.     }
    80.  
    81.     VertexOutput vert (VertexInput v)
    82.     {
    83.         VertexOutput o;
    84.         o.pos = UnityObjectToClipPos (v.vertex);
    85.         o.uv = v.uv;
    86.  
    87.         return o;
    88.     }
    89.     fixed4 frag(VertexOutput i) : SV_Target
    90.     {
    91.         fixed2 uv = i.uv /1;
    92.  
    93.         fixed f = 1;
    94.         fixed s = 1;
    95.         fixed3 col = tex2DNoTile( _MainTex, 10 * uv, 1).xyz;
    96.  
    97.         return fixed4( col, 1.0 );
    98.     }
    99.     ENDCG
    100.     }
    101.   }
    102. }
     
    markashburner likes this.
  16. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    That's awesome Invertex!

    How would I combine this with a surface shader, so I can use it for terrain?
     
  17. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    This may not be relevant to you, but I have discovered that ddx and ddy do not work on GLES (Android) but or Vulkan (in which many things do not seem to work).
    It works fine on DX11/PC.
    Unity converts the programs back to GLSL while building, I wonder why ddy and ddx are not converted to dFdy, dFdx.
    I tried GLSLPROGRAM rather than CGPROGRAM but this seems to cause many other problems.
     
    Last edited: Jan 3, 2019
  18. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    I don't believe derivatives are enabled by default on GLES versions prior to 3.0 for mobile. As for using GLSLPROGRAM instead, that would require you to write your shader code in GLSL style, otherwise yeah you'll get a lot of errors.

    You can try just setting
    #pragma target 3.0
    at the start of your CGPROGRAM to enforce a shader model level that supports derivatives. This of course will mean any hardware that doesn't support them won't render entirely properly or at all, but such phones would be quite old at this point.
     
  19. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    We require #pragma target 3.0 as it is for textures. Adding that did not seem to work.
    I tried 3.5 and that did not work
    Same for 4.0
    The symptoms are odd, everything looks unlit, but there are some white triangles and any pixels less than about 8m from the camera are dark as if in shadow (we don't have shadows enabled).

    Thanks for the suggestion though!
     
  20. Feartheway

    Feartheway

    Joined:
    Dec 12, 2017
    Posts:
    92
    I am also trying to convert a shadertoy into unity. I am completely lost. Is this the right place to post?

    here is the shader
    https://www.shadertoy.com/view/MsB3WR

    i am really just after a still water shader for lakes and rivers much like prodaytime.

    I get this error

    Shader error in 'ShaderMan/mistymountain1': type mismatch at line 90 (on d3d11)
    Compiling Vertex program
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_RGBM_ENCODING
     
  21. Feartheway

    Feartheway

    Joined:
    Dec 12, 2017
    Posts:
    92
    Code (CSharp):
    1.  
    2. Shader "ShaderMan/mistymountain1"
    3.     {
    4.  
    5.     Properties{
    6.     _MainTex ("MainTex", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    12.  
    13.     Pass
    14.     {
    15.     ZWrite Off
    16.     Blend SrcAlpha OneMinusSrcAlpha
    17.  
    18.     CGPROGRAM
    19.     #pragma vertex vert
    20.     #pragma fragment frag
    21.     #include "UnityCG.cginc"
    22.  
    23.     struct VertexInput {
    24.     fixed4 vertex : POSITION;
    25.     fixed2 uv:TEXCOORD0;
    26.     fixed4 tangent : TANGENT;
    27.     fixed3 normal : NORMAL;
    28.     //VertexInput
    29.     };
    30.  
    31.  
    32.     struct VertexOutput {
    33.     fixed4 pos : SV_POSITION;
    34.     fixed2 uv:TEXCOORD0;
    35.     //VertexOutput
    36.     };
    37.  
    38.     //Variables
    39. float4 _iMouse;
    40. sampler2D _ThirdTex;
    41. sampler2D _SecondTex;
    42. sampler2D _MainTex;
    43.  
    44.     // Misty Lake. Created by Reinder Nijhoff 2013
    45. // Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    46. // @reindernijhoff
    47. //
    48. // https://www.shadertoy.com/view/MsB3WR
    49. //
    50.  
    51. #define BUMPFACTOR 0.1
    52. #define EPSILON 0.1
    53. #define BUMPDISTANCE 60.
    54.  
    55. #define time (_Time.y+285.)
    56.  
    57. // Noise functions by inigo quilez
    58.  
    59. fixed noise( const in fixed2 x ) {
    60.     fixed2 p = floor(x);
    61.     fixed2 f = frac(x);
    62.     f = f*f*(3.0-2.0*f);
    63.    
    64.     fixed2 uv = (p.xy) + f.xy;
    65.     return tex2Dlod( _MainTex,float4( (uv+ 0.5)/256.0, 0.0 ,0)).x;
    66. }
    67.  
    68. fixed noise( const in fixed3 x ) {
    69.     fixed3 p = floor(x);
    70.     fixed3 f = frac(x);
    71.     f = f*f*(3.0-2.0*f);
    72.    
    73.     fixed2 uv = (p.xy+fixed2(37.0,17.0)*p.z) + f.xy;
    74.     fixed2 rg = tex2Dlod( _MainTex,float4( (uv+ 0.5)/256.0, 0.0 ,0)).yx;
    75.     return lerp( rg.x, rg.y, f.z );
    76. }
    77.  
    78. fixed2x2 rot(const in fixed a) {
    79.     return fixed2x2(cos(a),sin(a),-sin(a),cos(a));  
    80. }
    81.  
    82. const fixed2x2 m2 = fixed2x2( 0.60, -0.80, 0.80, 0.60 );
    83.  
    84. const fixed3x3 m3 = fixed3x3( 0.00,  0.80,  0.60,
    85.                      -0.80,  0.36, -0.48,
    86.                      -0.60, -0.48,  0.64 );
    87.  
    88. fixed fbm( in fixed3 p ) {
    89.     fixed f = 0.0;
    90.     f += 0.5000*noise( p ); p = m3*p*2.02;
    91.     f += 0.2500*noise( p ); p = m3*p*2.03;
    92.     f += 0.1250*noise( p ); p = m3*p*2.01;
    93.     f += 0.0625*noise( p );
    94.     return f/0.9375;
    95. }
    96.  
    97. fixed hash( in fixed n ) {
    98.     return frac(sin(n)*43758.5453);
    99. }
    100.  
    101. // intersection functions
    102.  
    103. bool intersectPlane(const in fixed3 ro, const in fixed3 rd, const in fixed height, inout fixed dist) {  
    104.     if (rd.y==0.0) {
    105.         return false;
    106.     }
    107.        
    108.     fixed d = -(ro.y - height)/rd.y;
    109.     d = min(100000.0, d);
    110.     if( d > 0. && d < dist ) {
    111.         dist = d;
    112.         return true;
    113.     } else {
    114.         return false;
    115.     }
    116. }
    117.  
    118. // light direction
    119.  
    120. fixed3 lig = normalize(fixed3( 0.3,0.5, 0.6));
    121.  
    122. fixed3 bgColor( const in fixed3 rd ) {
    123.     fixed sun = clamp( dot(lig,rd), 0.0, 1.0 );
    124.     fixed3 col = fixed3(0.5, 0.52, 0.55) - rd.y*0.2*fixed3(1.0,0.8,1.0) + 0.15*0.75;
    125.     col += fixed3(1.0,.6,0.1)*pow( sun, 8.0 );
    126.     col *= 0.95;
    127.     return col;
    128. }
    129.  
    130. // coulds functions by inigo quilez
    131.  
    132. #define CLOUDSCALE (500./(64.*0.03))
    133.  
    134. fixed cloudMap( const in fixed3 p, const in fixed ani ) {
    135.     fixed3 r = p/CLOUDSCALE;
    136.  
    137.     fixed den = -1.8+cos(r.y*5.-4.3);
    138.        
    139.     fixed f;
    140.     fixed3 q = 2.5*r*fixed3(0.75,1.0,0.75)  + fixed3(1.0,1.0,15.0)*ani*0.15;
    141.     f  = 0.50000*noise( q ); q = q*2.02 - fixed3(-1.0,1.0,-1.0)*ani*0.15;
    142.     f += 0.25000*noise( q ); q = q*2.03 + fixed3(1.0,-1.0,1.0)*ani*0.15;
    143.     f += 0.12500*noise( q ); q = q*2.01 - fixed3(1.0,1.0,-1.0)*ani*0.15;
    144.     f += 0.06250*noise( q ); q = q*2.02 + fixed3(1.0,1.0,1.0)*ani*0.15;
    145.     f += 0.03125*noise( q );
    146.    
    147.     return 0.065*clamp( den + 4.4*f, 0.0, 1.0 );
    148. }
    149.  
    150. fixed3 raymarchClouds( const in fixed3 ro, const in fixed3 rd, const in fixed3 bgc, const in fixed3 fgc, const in fixed startdist, const in fixed maxdist, const in fixed ani ) {
    151.     // dithering  
    152.     fixed t = startdist+CLOUDSCALE*0.02*hash(rd.x+35.6987221*rd.y+time);//0.1*tex2D( _MainTex, i.uv/iChannelResolution[0].x ).x;
    153.    
    154.     // raymarch  
    155.     fixed4 sum = fixed3( 0.0 , 0.0 , 0.0 , 0.0 );
    156.     [unroll(100)]
    157. for( int i=0; i<64; i++ ) {
    158.         if( sum.a > 0.99 || t > maxdist ) continue;
    159.        
    160.         fixed3 pos = ro + t*rd;
    161.         fixed a = cloudMap( pos, ani );
    162.  
    163.         // lighting  
    164.         fixed dif = clamp(0.1 + 0.8*(a - cloudMap( pos + lig*0.15*CLOUDSCALE, ani )), 0., 0.5);
    165.         fixed4 col = fixed3( (1.+dif, (1.+dif, (1.+dif, (1.+dif)*fgc, a );
    166.         // fog      
    167.     //    col.xyz = lerp( col.xyz, fgc, 1.0-exp(-0.0000005*t*t) );
    168.        
    169.         col.rgb *= col.a;
    170.         sum = sum + col*(1.0 - sum.a);  
    171.  
    172.         // advance ray with LOD
    173.         t += (0.03*CLOUDSCALE)+t*0.012;
    174.     }
    175.  
    176.     // blend with background  
    177.     sum.xyz = lerp( bgc, sum.xyz/(sum.w+0.0001), sum.w );
    178.    
    179.     return clamp( sum.xyz, 0.0, 1.0 );
    180. }
    181.  
    182. // terrain functions
    183. fixed terrainMap( const in fixed3 p ) {
    184.     return (tex2Dlod( _SecondTex,float4( (-p.zx*m2)*0.000046, 0. ).x*600.) * smoothstep( 820., 1000., length(p.xz) ) - 2. + noise(p.xz*0.5,0))*15.;
    185. }
    186.  
    187. fixed3 raymarchTerrain( const in fixed3 ro, const in fixed3 rd, const in fixed3 bgc, const in fixed startdist, inout fixed dist ) {
    188.     fixed t = startdist;
    189.  
    190.     // raymarch  
    191.     fixed4 sum = fixed3( 0.0 , 0.0 , 0.0 , 0.0 );
    192.     bool hit = false;
    193.     fixed3 col = bgc;
    194.    
    195.     [unroll(100)]
    196. for( int i=0; i<80; i++ ) {
    197.         if( hit ) break;
    198.        
    199.         t += 8. + t/300.;
    200.         fixed3 pos = ro + t*rd;
    201.        
    202.         if( pos.y < terrainMap(pos) ) {
    203.             hit = true;
    204.         }      
    205.     }
    206.     if( hit ) {
    207.         // binary search for hit      
    208.         fixed dt = 4.+t/400.;
    209.         t -= dt;
    210.        
    211.         fixed3 pos = ro + t*rd;  
    212.         t += (0.5 - step( pos.y , terrainMap(pos) )) * dt;      
    213.         [unroll(100)]
    214. for( int j=0; j<2; j++ ) {
    215.             pos = ro + t*rd;
    216.             dt *= 0.5;
    217.             t += (0.5 - step( pos.y , terrainMap(pos) )) * dt;
    218.         }
    219.         pos = ro + t*rd;
    220.        
    221.         fixed3 dx = fixed3( 100.*EPSILON, 0., 0. );
    222.         fixed3 dz = fixed3( 0., 0., 100.*EPSILON );
    223.        
    224.         fixed3 normal = fixed3( 0., 0., 0. );
    225.         normal.x = (terrainMap(pos + dx) - terrainMap(pos-dx) ) / (200. * EPSILON);
    226.         normal.z = (terrainMap(pos + dz) - terrainMap(pos-dz) ) / (200. * EPSILON);
    227.         normal.y = 1.;
    228.         normal = normalize( normal );      
    229.  
    230.         col = fixed3(0.2,0.2,0.2) + 0.7*tex2D( _ThirdTex, pos.xz * 0.01 ).xyz *
    231.                    fixed3(1.,.9,0.6);
    232.        
    233.         fixed veg = 0.3*fbm(pos*0.2)+normal.y;
    234.                    
    235.         if( veg > 0.75 ) {
    236.             col = fixed3( 0.45, 0.6, 0.3 )*(0.5+0.5*fbm(pos*0.5))*0.6;
    237.         } else
    238.         if( veg > 0.66 ) {
    239.             col = col*0.6+fixed3( 0.4, 0.5, 0.3 )*(0.5+0.5*fbm(pos*0.25))*0.3;
    240.         }
    241.         col *= fixed3(0.5, 0.52, 0.65)*fixed3(1.,.9,0.8);
    242.        
    243.         fixed3 brdf = col;
    244.        
    245.         fixed diff = clamp( dot( normal, -lig ), 0., 1.);
    246.        
    247.         col = brdf*diff*fixed3(1.0,.6,0.1);
    248.         col += brdf*clamp( dot( normal, lig ), 0., 1.)*fixed3(0.8,.6,0.5)*0.8;
    249.         col += brdf*clamp( dot( normal, fixed3(0.,1.,0.) ), 0., 1.)*fixed3(0.8,.8,1.)*0.2;
    250.        
    251.         dist = t;
    252.         t -= pos.y*3.5;
    253.         col = lerp( col, bgc, 1.0-exp(-0.0000005*t*t) );
    254.        
    255.     }
    256.     return col;
    257. }
    258.  
    259. fixed waterMap( fixed2 pos ) {
    260.     fixed2 posm = pos * m2;
    261.    
    262.     return abs( fbm( fixed3( 8.*posm, time ))-0.5 )* 0.1;
    263. }
    264.  
    265.  
    266.  
    267.  
    268.  
    269.     VertexOutput vert (VertexInput v)
    270.     {
    271.     VertexOutput o;
    272.     o.pos = UnityObjectToClipPos (v.vertex);
    273.     o.uv = v.uv;
    274.     //VertexFactory
    275.     return o;
    276.     }
    277.     fixed4 frag(VertexOutput i) : SV_Target
    278.     {
    279.    
    280.     fixed2 q = i.uv / 1;
    281.     fixed2 p = -1.0 + 2.0*q;
    282.     p.x *= 1/ 1;
    283.    
    284.     // camera parameters
    285.     fixed3 ro = fixed3(0.0, 0.5, 0.0);
    286.     fixed3 ta = fixed3(0.0, 0.45,1.0);
    287.     if (_iMouse.z>=1.) {
    288.         ta.xz *= rot( (_iMouse.x/1-.5)*7. );
    289.    
    290.     }
    291.     ENDCG
    292.     }
    293.   }
    294. }
    295.  
    296.  
     
  22. Entretoize

    Entretoize

    Joined:
    Feb 27, 2015
    Posts:
    57
    Does someone knows how to use one of this shader in shader graph ? I tried to convert the code to work with a custom node but I don't know the language enough, it seems to be hlsl