Search Unity

How to create HeatMap in Unity

Discussion in 'AR/VR (XR) Discussion' started by RazerZero, Aug 4, 2016.

  1. RazerZero

    RazerZero

    Joined:
    Jun 1, 2016
    Posts:
    3
  2. ibps13

    ibps13

    Joined:
    Oct 6, 2012
    Posts:
    113
    Hi,

    Tried same tutorial from AlanZucconi on unity 5.4.1f1 and not work... Same question anyone found a solution for that ?

    Thanks
     
    KC1302 likes this.
  3. KC1302

    KC1302

    Joined:
    Jan 11, 2014
    Posts:
    94
    Can confirm doesn't work with 5.4.1f1
     
  4. NickAtUnity

    NickAtUnity

    Unity Technologies

    Joined:
    Sep 13, 2016
    Posts:
    84
    There are just a couple small changes you can make to get his sample to work in 5.4.1f1.

    Heatmap.shader

    Go into the shader and change _Points and _Properties to both be float4 arrays:

    Code (CSharp):
    1. uniform float4 _Points[100];        // (x, y, z) = position
    2. uniform float4 _Properties[100];    // x = radius, y = intensity
    Heatmap.cs

    Here you need to change the positions array to be a Vector4 array and merge the properties into another Vector4 array. Then you can set both arrays easily. Here's the full code for that.

    Code (CSharp):
    1. // Alan Zucconi
    2. // www.alanzucconi.com
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Heatmap : MonoBehaviour
    7. {
    8.     public Vector4[] positions;
    9.     public Vector4[] properties;
    10.  
    11.     public Material material;
    12.  
    13.     public int count = 50;
    14.  
    15.     void Start ()
    16.     {
    17.         positions = new Vector4[count];
    18.         properties = new Vector4[count];
    19.  
    20.         for (int i = 0; i < positions.Length; i++)
    21.         {
    22.             positions[i] = new Vector4(Random.Range(-0.4f, +0.4f), Random.Range(-0.4f, +0.4f), 0, 0);
    23.             properties[i] = new Vector4(Random.Range(0f, 0.25f), Random.Range(-0.25f, 1f), 0, 0);
    24.         }
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         for (int i = 0; i < positions.Length; i++)
    30.             positions[i] += new Vector4(Random.Range(-0.1f,+0.1f), Random.Range(-0.1f, +0.1f), 0, 0) * Time.deltaTime;
    31.  
    32.         material.SetInt("_Points_Length", count);
    33.         material.SetVectorArray("_Points", positions);
    34.         material.SetVectorArray("_Properties", properties);
    35.     }
    36. }
    Once I put those change into place I started seeing output that looked like what his blog post showed:

    heatmap.png

    There is some room to optimize this a little bit if you wanted. Since his example always had Z=0 for the positions and only used two properties, you could combine the arrays into a single float4/Vector4 where XY are the 2D position, Z is the radius, and W is the intensity. But I'll leave that up to the reader to implement. ;)

    Hope that helps!
     
  5. unity_b8lc95t4QaLiXQ

    unity_b8lc95t4QaLiXQ

    Joined:
    Apr 5, 2018
    Posts:
    1
    Hello Guys. Thank you for clarifying this. I have used his tutorial for my project. I am working on FOVE Eye tracking HMD and would like to generate heatmaps by looking at an area. but its not working. I am new to Unity and C #. Please if someone could check my code and respond. I will really appreciate your help.
    Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class EyesTrack : MonoBehaviour {

    public FoveInterface fove;
    Vector4[] pos;
    Vector4[] properties;
    public float[] radiuses;
    public float[] intensities;

    public Material material;

    // Use this for initialization
    void Start () {
    int obj1 = (int) GetComponent ().bounds.size.magnitude;
    print (“magniture”+ obj1);
    pos = new Vector4[obj1];
    radiuses = new float[obj1];
    intensities= new float[obj1];
    properties = new Vector4[obj1];
    }
    void Update () {
    // position of the object
    material.SetInt(“_Points_Length”, pos.Length);
    FoveInterfaceBase.EyeRays eyeRay = fove.GetGazeRays ();
    Ray rays = new Ray ();
    rays = eyeRay.left;
    RaycastHit hit;
    if (Physics.Raycast (rays, out hit, Mathf.Infinity))
    {
    for (int i = 0; i < pos.Length; i++)
    {
    pos = (Vector4)hit.point* Time.deltaTime;
    properties = new Vector4 (radiuses , intensities , 0, 0);
    print ("pos at the update " + pos );

    }
    print ("hit points" + hit.point);

    }
    material.SetVectorArray("_Points", pos);
    material.SetVectorArray("_Properties", properties);
    }
    }
     
    hasantastan likes this.
  6. mars_up

    mars_up

    Joined:
    Apr 25, 2017
    Posts:
    2
    i'm working on a similar project and i need some help. does someone has tips to create heatmaps with fove?
     
    hasantastan likes this.
  7. aislingkearney

    aislingkearney

    Joined:
    Jul 19, 2018
    Posts:
    1
    I'm also working on an eye tracking project with the FOVE - did you get your heatmap working by any chance and if so could you provide me with some tips?. Thanks.
     
  8. saverio1993br

    saverio1993br

    Joined:
    Nov 6, 2018
    Posts:
    1
    Hi, maybe my question is stupid but what material do I have to use for the public variable material ?
     
  9. fraeri

    fraeri

    Joined:
    Nov 8, 2018
    Posts:
    64
    Can I apply this on the surface of an Gameobject? I have points on the gameobject and want to turn it into a heatmap
     
  10. orkungo

    orkungo

    Joined:
    May 3, 2014
    Posts:
    62
    Is there anyone tried with 2017.4.30.f1?
     
  11. Bshsf_9527

    Bshsf_9527

    Joined:
    Sep 6, 2017
    Posts:
    43
    yes , the shader and scirpt in this page can work fine with high level Unity version ,event Untiy 2019 .
    see the article first floor mentioned and with the update suggestion by NickAtUnity
     
  12. prasetion

    prasetion

    Joined:
    Apr 3, 2014
    Posts:
    28
    wondering if any answer for this
     
  13. tknippenberg

    tknippenberg

    Joined:
    Feb 15, 2017
    Posts:
    13
    Can confirm the package from the original link still works in 2022.3.13f1, after applying the changes from comment #4 above.
     
    arfish likes this.