Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Heatmap shader not working on WebGL 1.0

Discussion in 'Shaders' started by arisdev82, Jun 11, 2019.

  1. arisdev82

    arisdev82

    Joined:
    Feb 27, 2019
    Posts:
    21
    Hi there!

    I have found a heatmap shader and changing some things it works in the editor and in WebGL 2.0. Unfortunattely it doesn't work on WebGL 1.0, not sure why (shaders are not my speciallity :\).

    If any one can help me i'll really apreccitate it. Thanks in advance!


    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    3.  
    4. // Alan Zucconi
    5. // www.alanzucconi.com
    6. Shader "Hidden/Heatmap" {
    7.     Properties{
    8.         _HeatTex("Texture", 2D) = "white" {}
    9.     }
    10.         SubShader{
    11.         Tags{ "Queue" = "Transparent" }
    12.         Blend SrcAlpha OneMinusSrcAlpha // Alpha blend
    13.  
    14.         Pass{
    15.         CGPROGRAM
    16.              #pragma vertex vert
    17.              #pragma fragment frag
    18.  
    19.       struct vertInput {
    20.           float4 pos : POSITION;
    21.       };
    22.  
    23.       struct vertOutput {
    24.           float4 pos : POSITION;
    25.           fixed3 worldPos : TEXCOORD1;
    26.       };
    27.  
    28.       vertOutput vert(vertInput input) {
    29.           vertOutput o;
    30.           o.pos = UnityObjectToClipPos(input.pos);
    31.           o.worldPos = mul(unity_ObjectToWorld, input.pos).xyz;
    32.           return o;
    33.       }
    34.  
    35.       uniform int _Points_Length = 0;
    36.       uniform float4 _Points[100];        // (x, y, z) = position
    37.       uniform float4 _Properties[100];    // x = radius, y = intensity
    38.  
    39.       sampler2D _HeatTex;
    40.  
    41.       half4 frag(vertOutput output) : COLOR{
    42.           // Loops over all the points
    43.           half h = 0;
    44.       for (int i = 0; i < _Points_Length; i++)
    45.       {
    46.           // Calculates the contribution of each point
    47.           half di = distance(output.worldPos, _Points[i].xyz);
    48.  
    49.           half ri = _Properties[i].x;
    50.           half hi = 1 - saturate(di / ri);
    51.  
    52.           h += hi * _Properties[i].y;
    53.       }
    54.  
    55.       // Converts (0-1) according to the heat texture
    56.       //h = saturate(h);
    57.       half4 color = tex2D(_HeatTex, fixed2(h, 0.5));
    58.       return color;
    59.       }
    60.           ENDCG
    61.       }
    62.     }
    63.         Fallback "Diffuse"
    64. }

    ps.-

    Editor

    HeatMap_Editor_A.PNG


    Chrome

    HeatMap_chrome.PNG
     
    Last edited: Jun 11, 2019
  2. arisdev82

    arisdev82

    Joined:
    Feb 27, 2019
    Posts:
    21
    I have tried to change the loop using a const. Nothing, even removing the loop itself.

    In Chrome debug console says: "Note: Creation of internal variant of shader 'Own/Heatmap' failed."

    No reasson to clarify why it fails, not even if enabling all debug available T_T
     
  3. arisdev82

    arisdev82

    Joined:
    Feb 27, 2019
    Posts:
    21
    "Solved".

    Seems to be a memory limit in WebGL 1.0, limiting points to 20 it works. Curious.
     
  4. arisdev82

    arisdev82

    Joined:
    Feb 27, 2019
    Posts:
    21
    The problem is somehow in the max array length, so i have subdivided points and properties in different arrays A, B, C, etc, to use them depending on the index of each object.

    f.e. if A ,B and C have a length of 15 each and I have 45 objects with their own temperatures then from 0-14 goes to A, 15-29 to B and 30-44 to C.

    It's a little S***ty but works :p