Search Unity

Export GLSL Shader

Discussion in 'Shaders' started by wxxhrt, Jan 6, 2016.

  1. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    Hi,

    I'm rebuilding a simple Unity game in Babylon.js. In Unity I use the Edge Detect Normals shader in Triangle Depth Normals mode. I'm trying to get this shader into GLSL as that's what Babylon.js uses. Is it possible to output the Unity shader as GLSL?

    I don't fully understand shaders but read that Unity shaders get compiled to GLSL when building for android- is there anyway to access them inside an .apk?

    I also read that Unity shaders are written in CG/HLSL- is this correct? I tried to use Nvidia's CGC to compile a shader into GLSL as detailed here but no luck, https://www.opengl.org/discussion_boards/showthread.php/174728-Compiling-CG-to-GLSL .

    I've ripped out everything I don't need from the Edge Detect Normals shader- leaving only code that relates to the Triangle Depth Normals mode:

    Code (CSharp):
    1.  
    2. Shader "Hidden/EdgeDetectNormalsWill" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "" {}
    5.     }
    6.  
    7.     CGINCLUDE
    8.    
    9.     #include "UnityCG.cginc"
    10.    
    11.     struct v2f {
    12.         float4 pos : SV_POSITION;
    13.         float2 uv[5] : TEXCOORD0;
    14.     };
    15.    
    16.     struct v2fd {
    17.         float4 pos : SV_POSITION;
    18.         float2 uv[2] : TEXCOORD0;
    19.     };
    20.  
    21.     sampler2D _MainTex;
    22.     uniform float4 _MainTex_TexelSize;
    23.  
    24.     sampler2D _CameraDepthNormalsTexture;
    25.     sampler2D_float _CameraDepthTexture;
    26.  
    27.     uniform half4 _Sensitivity;
    28.     uniform half4 _BgColor;
    29.     uniform half _BgFade;
    30.     uniform half _SampleDistance;
    31.     uniform float _Exponent;
    32.  
    33.     uniform float _Threshold;
    34.  
    35.     struct v2flum {
    36.         float4 pos : SV_POSITION;
    37.         float2 uv[3] : TEXCOORD0;
    38.     };
    39.  
    40.  
    41.  
    42.  
    43.     inline half CheckSame (half2 centerNormal, float centerDepth, half4 theSample)
    44.     {
    45.         // difference in normals
    46.         // do not bother decoding normals - there's no need here
    47.         half2 diff = abs(centerNormal - theSample.xy) * _Sensitivity.y;
    48.         int isSameNormal = (diff.x + diff.y) * _Sensitivity.y < 0.1;
    49.         // difference in depth
    50.         float sampleDepth = DecodeFloatRG (theSample.zw);
    51.         float zdiff = abs(centerDepth-sampleDepth);
    52.         // scale the required threshold by the distance
    53.         int isSameDepth = zdiff * _Sensitivity.x < 0.09 * centerDepth;
    54.    
    55.         // return:
    56.         // 1 - if normals and depth are similar enough
    57.         // 0 - otherwise
    58.        
    59.         return isSameNormal * isSameDepth ? 1.0 : 0.0;
    60.     }  
    61.        
    62.  
    63.    
    64.     v2f vertThin( appdata_img v )
    65.     {
    66.         v2f o;
    67.         o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    68.        
    69.         float2 uv = v.texcoord.xy;
    70.         o.uv[0] = uv;
    71.        
    72.         #if UNITY_UV_STARTS_AT_TOP
    73.         if (_MainTex_TexelSize.y < 0)
    74.             uv.y = 1-uv.y;
    75.         #endif
    76.        
    77.         o.uv[1] = uv;
    78.         o.uv[4] = uv;
    79.                
    80.         // offsets for two additional samples
    81.         o.uv[2] = uv + float2(-_MainTex_TexelSize.x, -_MainTex_TexelSize.y) * _SampleDistance;
    82.         o.uv[3] = uv + float2(+_MainTex_TexelSize.x, -_MainTex_TexelSize.y) * _SampleDistance;
    83.        
    84.         return o;
    85.     }    
    86.  
    87.    
    88.     half4 fragThin (v2f i) : SV_Target
    89.     {
    90.         half4 original = tex2D(_MainTex, i.uv[0]);
    91.        
    92.         half4 center = tex2D (_CameraDepthNormalsTexture, i.uv[1]);
    93.         half4 sample1 = tex2D (_CameraDepthNormalsTexture, i.uv[2]);
    94.         half4 sample2 = tex2D (_CameraDepthNormalsTexture, i.uv[3]);
    95.        
    96.         // encoded normal
    97.         half2 centerNormal = center.xy;
    98.         // decoded depth
    99.         float centerDepth = DecodeFloatRG (center.zw);
    100.        
    101.         half edge = 1.0;
    102.        
    103.         edge *= CheckSame(centerNormal, centerDepth, sample1);
    104.         edge *= CheckSame(centerNormal, centerDepth, sample2);
    105.            
    106.         return edge * lerp(original, _BgColor, _BgFade);
    107.     }
    108.    
    109.     ENDCG
    110.    
    111. Subshader {
    112. Pass {
    113.       ZTest Always Cull Off ZWrite Off
    114.  
    115.       CGPROGRAM
    116.       #pragma vertex vertThin
    117.       #pragma fragment fragThin
    118.       ENDCG
    119.   }
    120. }
    121.  
    122. Fallback off
    123.    
    124. } // shader
    125.  
    Is my only option to step through this line by line and convert to GLSL by hand?

    Thanks
     
  2. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    This is possible, but it's not as easy as you'd think, use the compile and show code option in Unity for Open GL 2.0, it will give you the code, but you have to convert the attributes to Babylonjs ones!
     
  3. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    Thanks Jonny, I ended up using threejs as Baboylonjs was giving me problems but will look into to doing this when I next have some spare time
     
  4. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Interesting, I found Babylonjs to be better than threejs, mainly because of more documentation and better support, maybe I felt threejs was not really progressing much... But, that said Babylonjs did have a couple of bugs, although the dev team do seem to jump on them pretty quick if you report them.

    Good luck anyway, either way it's not as easy as Unity!