Search Unity

Shader shows no effect in webgl build

Discussion in 'Shaders' started by lauhsinhung, Oct 15, 2019.

  1. lauhsinhung

    lauhsinhung

    Joined:
    Jan 31, 2019
    Posts:
    1
    Hello, I am having some issues with shader in WebGL. The shader works just fine in Editor but won't show any effect after building it to WebGL (also no pink graphic errors). I also get a bunch of errors in browser console like:
    WARNING: Shader Unsupported: 'Custom/StandardPlaneCulling' - Pass 'Meta' has no vertex shader
    But these shaders work fine.


    I have following shader code:

    Code (CSharp):
    1. Properties
    2.     {
    3.         _RM_Samples ("RM-Samples", Int) = 16
    4.         _TransferTexture("Transfer Texture", 2D) = "defaulttexture" {}
    5.         _DataScale ("Data Scale", Vector) = (0.0, 1.0, 0.0, 0.0)
    6.         _Strength ("Strength", Float) = 1.0
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    11.         LOD 100
    12.  
    13.         ZWrite Off
    14.         ZTest Always
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.         Cull Front
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.  
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             // make fog work
    25.             #pragma multi_compile_fog
    26.  
    27.             #include "UnityCG.cginc"
    28.  
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 UNITY_FOG_COORDS(1)
    37.                 float4 vertex : SV_POSITION;
    38.                 float3 worldPos : TEXCOORD0;
    39.                 float4 screenPos : TEXCOORD1;
    40.             };
    41.  
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = UnityObjectToClipPos(v.vertex);
    46.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
    47.                 o.screenPos = ComputeScreenPos(o.vertex);
    48.                 UNITY_TRANSFER_FOG(o,o.vertex);
    49.                 return o;
    50.             }
    51.  
    52.            
    53.             uniform int _RM_Samples;
    54.             uniform sampler2D _TransferTexture;
    55.             uniform float2 _DataScale;
    56.             uniform float _Strength;
    57.  
    58.             uniform float4 _SensorData[34];
    59.             uniform int _DataSize;
    60.  
    61.             uniform sampler2D _CameraDepthTexture;
    62.  
    63.             float3 _CullPlaneVec;
    64.             float3 _CullPlaneNormal;
    65.             int _CullPlaneOn;
    66.  
    67.             float cullPlaneValue(float3 position){
    68.                 float3 r = position - _CullPlaneVec;
    69.                 float rdotn = dot(r, _CullPlaneNormal);
    70.                 return step(rdotn, 0) * _CullPlaneOn + (1 - _CullPlaneOn);
    71.             }
    72.            
    73.            
    74.             float3 _wingCullPlaneVec;
    75.             float3 _wingCullPlaneNormal;
    76.  
    77.             float wingCullPlaneValue(float3 position){
    78.                 float3 r = position - _wingCullPlaneVec;
    79.                 float rdotn = dot(r, _wingCullPlaneNormal);
    80.                 return step(rdotn, 0);
    81.             }
    82.  
    83.             float2 boxIntersection(in float3 ro, in float3 rd, in float3 rad)
    84.             {
    85.                 float3 m = 1.0 / rd;
    86.                 float3 n = m * ro;
    87.                 float3 k = abs(m) * rad;
    88.                 float3 t1 = -n - k;
    89.                 float3 t2 = -n + k;
    90.  
    91.                 float tN = max(max(t1.x, t1.y), t1.z);
    92.                 float tF = min(min(t2.x, t2.y), t2.z);
    93.  
    94.                 if (tN > tF || tF < 0.0) return float2(-1.0, -1.0); // no intersection
    95.  
    96.                 return float2(tN, tF);
    97.             }
    98.             //
    99.  
    100.             //p in object space
    101.             float SampleValue(float3 p) {
    102.                 float totalValue = 0.0;
    103.                 float denom = 0.0;
    104.  
    105.                 for (int i = 0; i < _DataSize; ++i) {
    106.                     float4 sd = _SensorData[i];
    107.                     float dist = length(p - sd.xyz);
    108.  
    109.                     totalValue += sd.w / (dist * dist);
    110.                     denom += 1.0 / (dist * dist);
    111.                 }
    112.  
    113.                 if (denom==0.0) {
    114.                     return 0.0;
    115.                 }
    116.  
    117.                 return totalValue / denom;
    118.             }
    119.  
    120.             float4 transferFunction(float value) {
    121.                 float tv = (value - _DataScale.x) / (_DataScale.y - _DataScale.x);
    122.  
    123.                 float4 col = tex2D(_TransferTexture, float2(0.5, tv));
    124.                 //float v = (value - 0.5) * 2.0;
    125.                 col.w *= _Strength;
    126.                 return float4(col.xyz * col.w, col.w);
    127.             }
    128.  
    129.  
    130.             float4 rayMarch(float3 ro, float3 rd, float dp) {
    131.                 float3 ro1 = mul(unity_WorldToObject, float4(ro, 1.0));
    132.                 float3 rd1 = mul(unity_WorldToObject, rd);
    133.  
    134.                 float2 t = boxIntersection(ro1, rd1, float3(1, 1, 1) * 0.5);
    135.                 t.x = length(mul(unity_ObjectToWorld, float4(ro1 + rd1 * max(t.x, 0.0), 1.0)) - ro);
    136.                 t.y = length(mul(unity_ObjectToWorld, float4(ro1 + rd1 * t.y, 1.0)) - ro);
    137.                 t.y = min(t.y, dp);
    138.  
    139.                 float4 acc = float4(0.0, 0.0, 0.0, 1.0);
    140.  
    141.                 float totalDelta = (t.y - t.x);
    142.                 float delta = totalDelta / float(_RM_Samples - 1.0);
    143.                
    144.                 float3 p = ro + t.x * rd;
    145.  
    146.                 for (int i = 0; i < _RM_Samples; ++i) {
    147.                     float v = SampleValue(p);
    148.  
    149.                     float4 tf = transferFunction(v) * (cullPlaneValue(p) == 0 ? 0.0 : 1.0);
    150.                     //tf.w *= min(totalDelta*0.1, 1.0);
    151.  
    152.                     float tr = exp(-tf.w * delta);
    153.  
    154.                     acc.xyz += tf.xyz * acc.w * delta;
    155.                     acc.w *= tr;
    156.  
    157.                     p += delta * rd;
    158.                 }
    159.  
    160.                 return float4(acc.xyz, (1.0 - acc.w) * step(t.x, t.y));
    161.             }
    162.  
    163.             fixed4 frag(v2f i) : SV_Target
    164.             {
    165.                 float2 tc = i.screenPos.xy / i.screenPos.w;
    166.                 float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, tc));
    167.                 float eD = LinearEyeDepth(depth);
    168.  
    169.                 float3 ro = _WorldSpaceCameraPos;
    170.                 float3 rd = normalize(i.worldPos - ro);
    171.  
    172.                 float4 col = rayMarch(ro, rd, eD);
    173.                
    174.                 if(wingCullPlaneValue(i.worldPos.xyz)==0){
    175.                     discard;
    176.                 }
    177.  
    178.                 UNITY_APPLY_FOG(i.fogCoord, col);
    179.                 return col;
    180.             }
    181.             ENDCG
    182.         }
    183.     }
    I have tried to change some settings in editor such as including this shader explicitly in Project Settings or switching Graphics to Webgl 1.0(this leads to pink graphic error), etc, but none of them fixed the problem. I truly appreciate your help.