Search Unity

Shader Forge on webgl

Discussion in 'Web' started by spinaljack, Oct 28, 2017.

  1. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    992
    Hi,

    I've got a vertex displacement shader which takes in a height map for terrain which works on desktop but doesn't work on webgl.

    Does anyone have a working vertex displacement shader for webgl?

    Here's the shader:
    Code (csharp):
    1.  
    2. // Shader created with Shader Forge v1.38
    3. // Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
    4. // Note: Manually altering this data may prevent you from opening it in Shader Forge
    5. /*SF_DATA;ver:1.38;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,lico:1,lgpr:1,limd:0,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:False,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,atwp:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:3138,x:32719,y:32712,varname:node_3138,prsc:2|emission-8973-RGB,voffset-109-OUT;n:type:ShaderForge.SFN_Tex2d,id:8973,x:32197,y:32710,ptovrint:False,ptlb:Texture,ptin:_Texture,varname:node_8973,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:555845c57153a9549bc3093bb0addc63,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Tex2d,id:6805,x:31617,y:32956,ptovrint:False,ptlb:Height,ptin:_Height,varname:node_6805,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:9feb21497adb72047b8f47439359390a,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Multiply,id:1189,x:31867,y:32918,varname:node_1189,prsc:2|A-1247-OUT,B-6805-R;n:type:ShaderForge.SFN_ValueProperty,id:1247,x:31617,y:32845,ptovrint:False,ptlb:Height Strength,ptin:_HeightStrength,varname:node_1247,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:4;n:type:ShaderForge.SFN_Slider,id:5781,x:31460,y:33143,ptovrint:False,ptlb:Contrast,ptin:_Contrast,varname:node_5781,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:0.349872,max:1;n:type:ShaderForge.SFN_OneMinus,id:7020,x:31796,y:33061,varname:node_7020,prsc:2|IN-5781-OUT;n:type:ShaderForge.SFN_RemapRangeAdvanced,id:9252,x:32031,y:33171,varname:node_9252,prsc:2|IN-1189-OUT,IMIN-5781-OUT,IMAX-7020-OUT,OMIN-3227-OUT,OMAX-1501-OUT;n:type:ShaderForge.SFN_Vector1,id:3227,x:31735,y:33283,varname:node_3227,prsc:2,v1:0;n:type:ShaderForge.SFN_Vector1,id:1501,x:31735,y:33339,varname:node_1501,prsc:2,v1:1;n:type:ShaderForge.SFN_NormalVector,id:7508,x:32011,y:32936,prsc:2,pt:False;n:type:ShaderForge.SFN_Multiply,id:5630,x:32258,y:33094,varname:node_5630,prsc:2|A-7508-OUT,B-9252-OUT;n:type:ShaderForge.SFN_Multiply,id:109,x:32438,y:33173,varname:node_109,prsc:2|A-2934-R,B-5630-OUT;n:type:ShaderForge.SFN_VertexColor,id:2934,x:32191,y:33272,varname:node_2934,prsc:2;proporder:8973-6805-1247-5781;pass:END;sub:END;*/
    6.  
    7. Shader "Shader Forge/MapSimple" {
    8.     Properties {
    9.         _Texture ("Texture", 2D) = "white" {}
    10.         _Height ("Height", 2D) = "white" {}
    11.         _HeightStrength ("Height Strength", Float ) = 4
    12.         _Contrast ("Contrast", Range(0, 1)) = 0.349872
    13.     }
    14.     SubShader {
    15.         Tags {
    16.             "RenderType"="Opaque"
    17.         }
    18.         Pass {
    19.             Name "FORWARD"
    20.             Tags {
    21.                 "LightMode"="ForwardBase"
    22.             }
    23.            
    24.            
    25.             CGPROGRAM
    26.             #pragma vertex vert
    27.             #pragma fragment frag
    28.             #define UNITY_PASS_FORWARDBASE
    29.             #include "UnityCG.cginc"
    30.             #pragma multi_compile_fwdbase_fullshadows
    31.             #pragma only_renderers d3d9 d3d11 glcore gles
    32.             #pragma target 3.0
    33.             uniform sampler2D _Texture; uniform float4 _Texture_ST;
    34.             uniform sampler2D _Height; uniform float4 _Height_ST;
    35.             uniform float _HeightStrength;
    36.             uniform float _Contrast;
    37.             struct VertexInput {
    38.                 float4 vertex : POSITION;
    39.                 float3 normal : NORMAL;
    40.                 float2 texcoord0 : TEXCOORD0;
    41.                 float4 vertexColor : COLOR;
    42.             };
    43.             struct VertexOutput {
    44.                 float4 pos : SV_POSITION;
    45.                 float2 uv0 : TEXCOORD0;
    46.                 float3 normalDir : TEXCOORD1;
    47.                 float4 vertexColor : COLOR;
    48.             };
    49.             VertexOutput vert (VertexInput v) {
    50.                 VertexOutput o = (VertexOutput)0;
    51.                 o.uv0 = v.texcoord0;
    52.                 o.vertexColor = v.vertexColor;
    53.                 o.normalDir = UnityObjectToWorldNormal(v.normal);
    54.                 float4 _Height_var = tex2Dlod(_Height,float4(TRANSFORM_TEX(o.uv0, _Height),0.0,0));
    55.                 float node_3227 = 0.0;
    56.                 v.vertex.xyz += (o.vertexColor.r*(v.normal*(node_3227 + ( ((_HeightStrength*_Height_var.r) - _Contrast) * (1.0 - node_3227) ) / ((1.0 - _Contrast) - _Contrast))));
    57.                 o.pos = UnityObjectToClipPos( v.vertex );
    58.                 return o;
    59.             }
    60.             float4 frag(VertexOutput i) : COLOR {
    61.                 i.normalDir = normalize(i.normalDir);
    62.                 float3 normalDirection = i.normalDir;
    63. ////// Lighting:
    64. ////// Emissive:
    65.                 float4 _Texture_var = tex2D(_Texture,TRANSFORM_TEX(i.uv0, _Texture));
    66.                 float3 emissive = _Texture_var.rgb;
    67.                 float3 finalColor = emissive;
    68.                 return fixed4(finalColor,1);
    69.             }
    70.             ENDCG
    71.         }
    72.         Pass {
    73.             Name "ShadowCaster"
    74.             Tags {
    75.                 "LightMode"="ShadowCaster"
    76.             }
    77.             Offset 1, 1
    78.             Cull Back
    79.            
    80.             CGPROGRAM
    81.             #pragma vertex vert
    82.             #pragma fragment frag
    83.             #define UNITY_PASS_SHADOWCASTER
    84.             #include "UnityCG.cginc"
    85.             #include "Lighting.cginc"
    86.             #pragma fragmentoption ARB_precision_hint_fastest
    87.             #pragma multi_compile_shadowcaster
    88.             #pragma only_renderers d3d9 d3d11 glcore gles
    89.             #pragma target 3.0
    90.             uniform sampler2D _Height; uniform float4 _Height_ST;
    91.             uniform float _HeightStrength;
    92.             uniform float _Contrast;
    93.             struct VertexInput {
    94.                 float4 vertex : POSITION;
    95.                 float3 normal : NORMAL;
    96.                 float2 texcoord0 : TEXCOORD0;
    97.                 float4 vertexColor : COLOR;
    98.             };
    99.             struct VertexOutput {
    100.                 V2F_SHADOW_CASTER;
    101.                 float2 uv0 : TEXCOORD1;
    102.                 float3 normalDir : TEXCOORD2;
    103.                 float4 vertexColor : COLOR;
    104.             };
    105.             VertexOutput vert (VertexInput v) {
    106.                 VertexOutput o = (VertexOutput)0;
    107.                 o.uv0 = v.texcoord0;
    108.                 o.vertexColor = v.vertexColor;
    109.                 o.normalDir = UnityObjectToWorldNormal(v.normal);
    110.                 float4 _Height_var = tex2Dlod(_Height,float4(TRANSFORM_TEX(o.uv0, _Height),0.0,0));
    111.                 float node_3227 = 0.0;
    112.                 v.vertex.xyz += (o.vertexColor.r*(v.normal*(node_3227 + ( ((_HeightStrength*_Height_var.r) - _Contrast) * (1.0 - node_3227) ) / ((1.0 - _Contrast) - _Contrast))));
    113.                 o.pos = UnityObjectToClipPos( v.vertex );
    114.                 TRANSFER_SHADOW_CASTER(o)
    115.                 return o;
    116.             }
    117.             float4 frag(VertexOutput i) : COLOR {
    118.                 i.normalDir = normalize(i.normalDir);
    119.                 float3 normalDirection = i.normalDir;
    120.                 SHADOW_CASTER_FRAGMENT(i)
    121.             }
    122.             ENDCG
    123.         }
    124.     }
    125.     FallBack "Diffuse"
    126.     CustomEditor "ShaderForgeMaterialInspector"
    127. }
    128.  
     
    Last edited: Oct 30, 2017
  2. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    992
    The console says that Pass 'FORWARD' has no vertex shader but as you can see above it does have a vertex shader. I suspect that something wrong is happening at compile time in unity.

    Console looks like this:
    Code (csharp):
    1.  
    2. You can reduce your startup time if you configure your web server to host .unityweb files using gzip compression.
    3. UnityLoader.js:1 trying binaryen method: native-wasm
    4. UnityLoader.js:1 asynchronously preparing wasm
    5. UnityLoader.js:1 binaryen method succeeded.
    6. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 Initialize engine version: 2017.2.0f3 (46dda1414e51)
    7.  
    8. UnityLoader.js:1 Creating WebGL 2.0 context.
    9. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 Renderer: WebKit WebGL
    10.  
    11. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 Vendor:   WebKit
    12.  
    13. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 Version:  OpenGL ES 3.0 (WebGL 2.0 (OpenGL ES 3.0 Chromium))
    14.  
    15. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 GLES:     3
    16.  
    17. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561  EXT_color_buffer_float GL_EXT_color_buffer_float EXT_disjoint_timer_query_webgl2 GL_EXT_disjoint_timer_query_webgl2 EXT_texture_filter_anisotropic GL_EXT_texture_filter_anisotropic OES_texture_float_linear GL_OES_texture_float_linear WEBGL_compressed_texture_s3tc GL_WEBGL_compressed_texture_s3tc WEBGL_compressed_texture_s3tc_srgb GL_WEBGL_compressed_texture_s3tc_srgb WEBGL_debug_renderer_info GL_WEBGL_debug_renderer_info WEBGL_debug_shaders GL_WEBGL_debug_shaders WEBGL_lose_context GL_WEBGL_lose_context
    18.  
    19. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 OPENGL LOG: Creating OpenGL ES 3.0 graphics device ; Context level  <OpenGL ES 3.0> ; Context handle 1
    20.  
    21. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 WARNING: Shader
    22. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 Unsupported: 'Shader Forge/MapSimple' - Pass 'FORWARD' has no vertex shader
    23.  
    24. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 WARNING: Shader
    25. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 Unsupported: 'Shader Forge/MapSimple' - Pass 'SHADOWCASTER' has no vertex shader
    26.  
    27. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 WARNING: Shader
    28. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 Unsupported: 'Shader Forge/MapSimple' - All passes removed
    29.  
    30. blob:http://localhost:49962/c5fe5b65-6c47-4cba-9479-edb1239b0a8f:7561 UnloadTime: 0.690000 ms
    31.  
    32.  
     
  3. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    992
    In case anyone has the same issue the shader works if you delete this line:

    #pragma only_renderers d3d9 d3d11 glcore gles
     
  4. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    Bit of a necro on my part but yeah, this does seem to solve it. I was tearing my hair out over this, I suspected it was a problem with assetbundles and the shader not being included properly but it's as you say, killing that line allows whatever substitute is in place to render the material properly.

    If anyone else runs into the same problem and finds this thread, now that you've found a solution, take a rest, you've earned it (Unity 2018.4 LTS).