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): Shader "Hidden/EdgeDetectNormalsWill" { Properties { _MainTex ("Base (RGB)", 2D) = "" {} } CGINCLUDE #include "UnityCG.cginc" struct v2f { float4 pos : SV_POSITION; float2 uv[5] : TEXCOORD0; }; struct v2fd { float4 pos : SV_POSITION; float2 uv[2] : TEXCOORD0; }; sampler2D _MainTex; uniform float4 _MainTex_TexelSize; sampler2D _CameraDepthNormalsTexture; sampler2D_float _CameraDepthTexture; uniform half4 _Sensitivity; uniform half4 _BgColor; uniform half _BgFade; uniform half _SampleDistance; uniform float _Exponent; uniform float _Threshold; struct v2flum { float4 pos : SV_POSITION; float2 uv[3] : TEXCOORD0; }; inline half CheckSame (half2 centerNormal, float centerDepth, half4 theSample) { // difference in normals // do not bother decoding normals - there's no need here half2 diff = abs(centerNormal - theSample.xy) * _Sensitivity.y; int isSameNormal = (diff.x + diff.y) * _Sensitivity.y < 0.1; // difference in depth float sampleDepth = DecodeFloatRG (theSample.zw); float zdiff = abs(centerDepth-sampleDepth); // scale the required threshold by the distance int isSameDepth = zdiff * _Sensitivity.x < 0.09 * centerDepth; // return: // 1 - if normals and depth are similar enough // 0 - otherwise return isSameNormal * isSameDepth ? 1.0 : 0.0; } v2f vertThin( appdata_img v ) { v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); float2 uv = v.texcoord.xy; o.uv[0] = uv; #if UNITY_UV_STARTS_AT_TOP if (_MainTex_TexelSize.y < 0) uv.y = 1-uv.y; #endif o.uv[1] = uv; o.uv[4] = uv; // offsets for two additional samples o.uv[2] = uv + float2(-_MainTex_TexelSize.x, -_MainTex_TexelSize.y) * _SampleDistance; o.uv[3] = uv + float2(+_MainTex_TexelSize.x, -_MainTex_TexelSize.y) * _SampleDistance; return o; } half4 fragThin (v2f i) : SV_Target { half4 original = tex2D(_MainTex, i.uv[0]); half4 center = tex2D (_CameraDepthNormalsTexture, i.uv[1]); half4 sample1 = tex2D (_CameraDepthNormalsTexture, i.uv[2]); half4 sample2 = tex2D (_CameraDepthNormalsTexture, i.uv[3]); // encoded normal half2 centerNormal = center.xy; // decoded depth float centerDepth = DecodeFloatRG (center.zw); half edge = 1.0; edge *= CheckSame(centerNormal, centerDepth, sample1); edge *= CheckSame(centerNormal, centerDepth, sample2); return edge * lerp(original, _BgColor, _BgFade); } ENDCG Subshader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vertThin #pragma fragment fragThin ENDCG } } Fallback off } // shader Is my only option to step through this line by line and convert to GLSL by hand? Thanks
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!
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
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!