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

use dFdx in glsl 2.0 shader in unity

Discussion in 'Shaders' started by dreamerflyer, Jan 24, 2015.

  1. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You can't.
     
  3. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    WHY? FLASH CAN DO,why unity can not?
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Because.
     
  5. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Maybe explain what your needs are? Your first link is meaningless (did you click it?) and your second link refers to antialiasing?

    If so you will need some shader experience. This package uses it: https://db.tt/pqsUtzuT
     
  7. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    1:the first link is about need turn on this:
    #extension GL_OES_standard_derivatives : enable
    2: Yes i want to make Variance Shadow Mapping (VSM),and use CG ,but not work:
    Code (CSharp):
    1.  
    2.  
    3. #pragma glsl
    4.  
    5. fragment:
    6.     float depth=i.depth.r;
    7.      float dx = dFdx(depth);
    8.  
    9. error:Shader error in 'Hidden/RenderDepth': GLSL vertex shader: 353: ERROR: 'dFdx' : no matching overloaded function found  at line 8

     
  8. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    I test vsm, it seems not work about the ddx when render depth map.What 's wrong ?
    Code (CSharp):
    1. Shader "Hidden/RenderDepth" {
    2.     Properties {
    3.     }
    4.     SubShader {
    5.     Pass {
    6.             Cull Front
    7.            
    8.             CGPROGRAM
    9.        
    10. //#pragma exclude_renderers flash  
    11. #pragma vertex vert
    12. #pragma fragment frag
    13. //#pragma target 3.0
    14. //#pragma profileoption NumTemps=64
    15. #pragma glsl
    16.  
    17. #include "UnityCG.cginc"
    18. //#include GL_OES_standard_derivatives : enable
    19.             struct v2f {
    20.                 float4 pos : POSITION;
    21.                 float4 depth : TEXCOORD0;
    22.             };
    23.  
    24.             v2f vert( appdata_full v )
    25.             {
    26.                 v2f o;
    27.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    28.             //    o.depth=(1,1,1,1);
    29.                 //o.depth.r=(o.pos.z-0.3)/(100+0.3);
    30.                 o.depth = float4( o.pos.z/o.pos.w , 1, 1, 1 );
    31.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    32.                 return o;
    33.             }
    34. //            half4 FracDepthToColor(half depth)
    35. //            {
    36. //                half4 enc =  ( 0f, 255.0f, 62025.0f, 160581375.0f ) * depth ;
    37. //                      
    38. //                    enc =  ( frac( enc.x ), frac( enc.y ), frac( enc.z ), frac( enc.w ) );
    39. //                    half4 yzww =  ( enc.y, enc.y, enc.z, enc.w );
    40. //                    half4 bitgap=( 0f / 255.0f, 0f / 255.0f, 0f / 255.0f, 0.0f );
    41. //                    enc =enc-  yzww* bitgap;
    42. //                    return enc;
    43. //            }
    44.  
    45.             half4 frag( v2f i ) : COLOR
    46.             {
    47.                 //float decodedDepth = DecodeFloatFromRGBA( i.depth );
    48.                 //return half4( EncodeFloatRGBA( i.depth.r ) );
    49.         //    half4 enc =  FracDepthToColor( i.depth.r) ;
    50.  
    51. //              
    52.                 float depth=i.depth.r;
    53. //                  
    54.             //depth = depth * 0.5 + 0.5;  
    55.  
    56. float dx = ddx(depth);
    57. float dy = ddy(depth);
    58. half4 enc= half4(depth,depth*depth + 0.25*dx*dx + 0.25*dy*dy, 0.0, 0);
    59.  
    60.    
    61.         return enc;
    62.             }
    63.  
    64.             ENDCG
    65.         }
    66.     }
    67. }
    68.  
     

    Attached Files:

    • S1.png
      S1.png
      File size:
      23 KB
      Views:
      1,129
    • S2.png
      S2.png
      File size:
      21.9 KB
      Views:
      1,105
  9. TechnoCraft

    TechnoCraft

    Joined:
    Apr 6, 2012
    Posts:
    28
    Filter mode "Point" means no resize filtering. Without antialising there is no edge smothin. Hard "edges" pixels are what I would expect with this settings. Object size in relation to texture size. Lightmaps and normals have special encoding that also causes similar effect (they need to be decoded/unpacked first). But this is not the problem in your case.
     
  10. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    not correct depth..
    Code (CSharp):
    1.  
    2. Shader "Custom/glslRenderDepth" {
    3.  
    4.     Properties {
    5.  
    6.  
    7.         //_MainTex ("Base (RGB)", 2D) = "white" {}
    8.  
    9.           _Color ("Object's Color", Color) = (0,1,0,1)
    10.  
    11.     }
    12.  
    13.     SubShader {
    14.  
    15.     Pass {    
    16.     Tags { "LightMode" = "ForwardBase" } // rendering of object
    17.  
    18.   Cull Front
    19.     //Blend One OneMinusSrcAlpha
    20.          GLSLPROGRAM
    21.  
    22.          // User-specified properties
    23.          #extension GL_OES_standard_derivatives : enable
    24.         varying float depth;
    25.          #ifdef VERTEX
    26.    void main()
    27.  
    28.          {                              
    29.       gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    30.  
    31.             depth=gl_Position.z/gl_Position.w;
    32.  
    33.          }
    34.  
    35.          #endif
    36.  
    37.          #ifdef FRAGMENT
    38.  
    39.      vec4 _Color;
    40.  
    41.      vec4 FracDepthToColor(float depth)
    42.  
    43.             {
    44.  
    45.                 vec4 enc =  vec4( 0.0, 255.0, 62025.0, 160581375.0) * depth ;
    46.  
    47.                         //enc.r=fract( enc.r );
    48.  
    49.                     enc = vec4 ( fract( enc.x ), fract( enc.y ), fract( enc.z ), fract( enc.w ) );
    50.  
    51.                     vec4 yzww =  vec4( enc.y, enc.y, enc.z, enc.w );
    52.  
    53.                     vec4 bitgap=vec4( 0 / 255.0, 0 / 255.0, 0 / 255.0, 0.0 );
    54.  
    55.                     enc =enc-  yzww* bitgap;
    56.  
    57.                     return enc;
    58.  
    59.             }
    60.  
    61.          void main()
    62.          {
    63.              float dx = dFdx(depth);
    64.  
    65.             float dy = dFdy(depth);
    66.  
    67.             _Color= vec4(depth,depth*depth + 0.25*dx*dx + 0.25*dy*dy, 0.0, 0);
    68.  
    69.             //_Color=FracDepthToColor( depth );
    70. //            _Color.r=dx;
    71. //            _Color.g=dy;
    72. //            _Color.b=0;
    73.  
    74.                    gl_FragColor = _Color;
    75.  
    76.          }
    77.  
    78.          #endif
    79.  
    80.          ENDGLSL
    81.  
    82.       }
    83.  
    84.     }
    85.  
    86.     FallBack "Diffuse"
    87.  
    88. }
    89.  
    90.  
     
    Last edited: Feb 3, 2015
  11. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    I think dfdx or ddx can make the smooth when texture filter is "Point",and can do some blur effect ,i am testing that,but it seems not corret..
     
  12. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    any help?I change
    dx=dFdx(depth) to dx=0;
    dy=dFdy(depth) to dy=0;
    it is the same depth effect,that means dFdx and dFdy not work !
     
    Last edited: Feb 3, 2015
  13. TechnoCraft

    TechnoCraft

    Joined:
    Apr 6, 2012
    Posts:
    28
    I attached an example project based on Soft Shadow for Sphere (for details see here). GLSL code works in Unity as long as you recreate the "missing" instructions. One step at a time. But do not expect others to do your homework for you.

    Start Unity.exe with -force-opengl parameter. Example:
    "C:\Program Files (x86)\Unity4610\Editor\Unity.exe" -force-opengl
     

    Attached Files:

  14. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Thanks your example, TechnoCraft~
    I think my codes above maybe has some bug,the keyword for "dFdx" NOT TURN BLUE...
     
  15. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Hi,TechnoCraft,Can you tell me how to use dFdx ? Thanks in advance!
    Attachment is screen shot in pc and android,and the source codes.The shadow seems not correct.
     

    Attached Files:

    Last edited: Feb 10, 2015
  16. TechnoCraft

    TechnoCraft

    Joined:
    Apr 6, 2012
    Posts:
    28
    Based on attached project you are trying to do dynamic (soft) shadows with directional lights (one light to be more precise). You might want to look at more simple examples first and study them. Why are Unity default shadows not acceptable?

    Use Unity default shader language HLSL with #pragma glsl if you need to test cross-compiled GLSL shaders.

    References:
    CG Programming/Unity
    Soft Shadow Mapping
    Cascaded Shadow Maps
     
  17. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    my codes is from this link, but it do not work..
    http://www.derschmale.com/2014/07/24/faster-variance-soft-shadow-mapping-for-varying-penumbra-sizes/
     
    Last edited: Feb 11, 2015