Search Unity

Converting an glsl shader to hlsl (Issue with buffer pass)

Discussion in 'Shaders' started by TEEBQNE, Mar 24, 2019.

  1. TEEBQNE

    TEEBQNE

    Joined:
    Jan 25, 2017
    Posts:
    10
    I have a shader in shader toy which looks like this:

    Code (CSharp):
    1. //Image
    2. void mainImage( out vec4 fragColor, in vec2 fragCoord )
    3. {
    4.     vec2 uv = fragCoord/iResolution.xy;
    5.     uv = 2.0 * (uv - .5);
    6.     if (iFrame > 0) {
    7.         vec3 fin = texture(iChannel0, vec2(0, 0)).xyz;
    8.         uv = fin.z * uv;
    9.         uv = uv + fin.xy;
    10.     }
    11.       vec2 c;
    12.     c.x = 0.3;
    13.     c.y = 0.5;
    14.     float R = 3.0, L, ITERS = 100.0;
    15.     for(float i = 0.0; i < ITERS; i++)
    16.     {
    17.         float buffer = uv.x;
    18.         uv.x = uv.x * uv.x - uv.y * uv.y;
    19.         uv.y = 2.0 * buffer * uv.y;
    20.         uv.x = uv.x + c.x;
    21.         uv.y = uv.y + c.y;
    22.         if (sqrt(uv.x * uv.x + uv.y * uv.y) > R)
    23.             break;
    24.         L += 1.0;
    25.     }
    26.     fragColor = vec4(L/ITERS, 0.0, 0.0, 1.0);
    27. }
    28.  
    29. // buffer A
    30. bool readKey(int value) {
    31.     float keyVal = texture(iChannel0, vec2((float(value)+0.5)/256.0, 0.25)).x;
    32.     return (keyVal > 0.5) ? true: false;
    33. }
    34. void mainImage( out vec4 fragColor, in vec2 fragCoord )
    35. {  
    36.     float zoom = 2.0;
    37.     vec2 uv = fragCoord / iResolution.xy;
    38.     uv = zoom * uv;
    39.     if (iFrame > 0) {
    40.         vec3 fin = texture(iChannel1, vec2(0, 0)).xyz;
    41.         uv = fin.xy;
    42.         zoom = fin.z;
    43.     }
    44.     if (readKey(65))
    45.         uv.x -= 0.025 * zoom;
    46.     if (readKey(68))
    47.         uv.x += 0.025 * zoom;
    48.     if (readKey(87))
    49.         uv.y += 0.025 * zoom;
    50.     if (readKey(83))
    51.         uv.y -= 0.025 * zoom;  
    52.     if (readKey(69))
    53.         zoom = zoom - 0.025 * zoom;
    54.     if (readKey(81))
    55.        zoom = zoom + 0.025 * zoom;
    56.     fragColor = vec4(uv.x, uv.y, zoom, 1.0);
    57. }
    I transcribed the image portion to glsl which works, which looks like:

    Code (CSharp):
    1.  
    2. Shader "Shaders/Julia"
    3.     {
    4.  
    5.     Properties{
    6.     _MainTex ("MainTex", 2D) = "white" {}
    7.    
    8.     _Iterations ("Iterations", int) = 0
    9.    
    10.     _Zoom ("Zoom", float) = 2.0
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    16.  
    17.     Pass
    18.     {
    19.     ZWrite Off
    20.     Blend SrcAlpha OneMinusSrcAlpha
    21.  
    22.     CGPROGRAM
    23.     #pragma vertex vert
    24.     #pragma fragment frag
    25.     #include "UnityCG.cginc"
    26.  
    27.     struct VertexInput {
    28.     fixed4 vertex : POSITION;
    29.     fixed2 uv:TEXCOORD0;
    30.     fixed4 tangent : TANGENT;
    31.     fixed3 normal : NORMAL;
    32.     //VertexInput
    33.     };
    34.  
    35.  
    36.     struct VertexOutput {
    37.     fixed4 pos : SV_POSITION;
    38.     fixed2 uv:TEXCOORD0;
    39.     //VertexOutput
    40.     };
    41.  
    42.     //Variables
    43. sampler2D _MainTex;
    44. int _Iterations;
    45. float _Zoom;
    46.  
    47.    
    48.  
    49.  
    50.  
    51.     VertexOutput vert (VertexInput v)
    52.     {
    53.     VertexOutput o;
    54.     o.pos = UnityObjectToClipPos (v.vertex);
    55.     o.uv = v.uv;
    56.     //VertexFactory
    57.     return o;
    58.     }
    59.     fixed4 frag(VertexOutput i) : SV_Target
    60.     {
    61.    
    62.     fixed2 uv = i.uv/1;
    63.     uv = 2.0 * (uv - .5);
    64.     fixed2 c;
    65.     c.x = 0.3;
    66.     c.y = 0.5;
    67.     float R = 3.0, L, ITERS = _Iterations * 10;
    68.     for(float i = 0.0; i < ITERS; i++)
    69.     {
    70.         float buffer = uv.x;
    71.         uv.x = uv.x * uv.x - uv.y * uv.y;
    72.         uv.y = 2.0 * buffer * uv.y;
    73.         uv.x = uv.x + c.x;
    74.         uv.y = uv.y + c.y;
    75.         if (sqrt(uv.x * uv.x + uv.y * uv.y) > R)
    76.             break;
    77.         L += 1.0;
    78.     }  
    79.    return fixed4(L/ITERS, 0.0, 0.0, 1.0);
    80.    
    81.     }
    82.     ENDCG
    83.   }
    84. }
    85. }
    86.  
    I was hoping to get the same effect with zooming / panning around the shader as I have in shader toy, but I am not sure how to use a buffer along with the image shader in hlsl / Unity. Is there a way to move around the shader similar to how i'm doing with the wasd keys, then zoom inward/outward with the qe keys?

    Here is the direct link to the shader toy if you would like to see what I mean: https://www.shadertoy.com/view/4lKBRR. Thank you.
     
  2. TEEBQNE

    TEEBQNE

    Joined:
    Jan 25, 2017
    Posts:
    10
    Instead of doing this I just made an attribute that was of float4. Passed in the x/y position and z/w scale. Assigned it when reading in the fragCoord (uv) by doing _Area.xy + (uv manipulation for shader) * _Area.wz where _Area is that float4. Does not answer the question but I suppose I do not need multiple buffers to achieve what I was trying to do.