当前位置:网站首页>Method of establishing unity thermodynamic diagram

Method of establishing unity thermodynamic diagram

2022-06-24 14:09:00 Xiaosheng yunmu

Unity Method of establishing thermodynamic diagram

Realization effect


Project file download

The basic idea

The method of generating thermal maps by assigning colors to mesh vertices has been written before . There is one big drawback , Requires a large number of grid points to support , Improved GPU Rendering pressure . So I changed my mind , Directly create and modify the color of picture pixels , In this way, a mesh with only four vertices can be used to generate thermal maps .

Before calculation fbs→97.5

After calculation fbs→96.5

Basically unaffected .

Problems and Solutions

  1. Conversion of world coordinates and pixel coordinates ;

  2. Initialization of pixels and weighted values of pixels ;

  3. Configuration of the relationship between the color of the color block and the influence range ;

  4. Adjustment of resolution and performance .

Conversion of world coordinates and pixel coordinates

Here are two points of attention , The first is the pivot point of the grid , Two is uv The axis of coordinates . The origin of the grid is specified by the model art , But when the world coordinates are converted to pixel coordinates , The origin needs to correspond to , So I want to change the origin of the mesh . There are two ways to change the origin ,1. Let the art modify , Change the origin of the mesh to uv Origin of coordinate system ;2. Add a parent object to the mesh , The position of the parent object is in the uv Coordinate origin . As shown in the figure below

After the origin is aligned , It is only necessary to ask for a proportion .

            Vector3 modelSize = gameObject.GetComponent<Collider>().bounds.size;
            float MapRatio = heatMapBase.Resolution / modelSize.x;
            //heatMapBase.Resolution Is the defined picture pixel resolution .
            //MapRatio Is the scale of the transformation between picture coordinates and world coordinates 

Initialization of pixels and weighted values of pixels

Before running, the weight of all pixels should be reset to zero . The weighted reference points will affect the surrounding pixels , The scope of influence is related to its own weight , You can also introduce a variable here DisRatio, Used to change the influence range of the weight . When the pixel is weighted , Only values can override small values .

 // Each pixel is initialized 
            List<Vector2> my_Pixels = new List<Vector2>();
            List<float> my_Values = new List<float>();
            for (int y = 0; y < texture.height; y++)
            {
                for (int x = 0; x < texture.width; x++)
                {

                    Vector2 pixel = new Vector2(x, y);
                    my_Pixels.Add(pixel);
                    my_Values.Add(0);
                }
            }
            int allLength = my_Pixels.Count;// So the number of pixels 
            int pointLength = heatMapInfos.Count;// Number of points of interest in the heat map 
            int colorLength = heatMapBase.HeatMapInfos.Count;// Color block grade 
            // Weighted value per pixel 
            for (int p = 0; p < pointLength; p++)
            {
                for (int a = 0; a < allLength; a++)
                {
                    float my_Distance = Vector2.Distance(heatMapInfos[p].Pixel* MapRatio, my_Pixels[a]);

                    float my_MaxDis = heatMapBase.DisRatio * heatMapInfos[p].Amount;

                    if (my_Distance < my_MaxDis)
                    {

                        float value = (1 - (Mathf.Pow(my_Distance, 2) / Mathf.Pow(my_MaxDis, 2))) * heatMapInfos[p].Amount;
                        if (value > my_Values[a])
                        {
                            my_Values[a] = value;
                        }
                    }
                }
            }

Configuration of the relationship between the color of the color block and the influence range

Color selection of color block , A weight , The image resolution and influence range can be dynamically configured . A template is added here to control . For the convenience of pixel color assignment , Specify the weight from large to small .

    public class HeatMapBase : ScriptableObject
    {
        public float DisRatio;// Distance ratio 
        public float Resolution;// The resolution of the 
        public List<HeatMapInfo> HeatMapInfos;
   
        [Serializable]
        public class HeatMapInfo
        {
            public float MaxAmount;// Maximum weight 
            public Color Color;// Color 
        }
    }

Adjustment of resolution and performance

At the beginning of writing , Consider dynamically changing the resolution , The larger the mesh, the greater the resolution . But this undoubtedly increases the consumption of performance . So consider writing the resolution into the configuration table . Instead, a wide range of jagged images appear, as shown in the following figure . I've introduced color Interpolation operation of , Solve the problem perfectly .

No interpolation

Additive interpolation

  // Each pixel is assigned a color 
            for (int i = 0; i < allLength; i++)
            {
                for (int j = 0; j < colorLength; j++)
                {
                    float my_CurMaxAmount = heatMapBase.HeatMapInfos[j].MaxAmount;
                  
                    if (my_Values[i] >= my_CurMaxAmount)
                    {
                        // The color of the current block 
                        Color my_CurColor = heatMapBase.HeatMapInfos[j].Color;

                        if (j != 0)
                        {

                            float my_UpDiffValue = heatMapBase.HeatMapInfos[j - 1].MaxAmount - my_CurMaxAmount;
                            Color my_UpColor = heatMapBase.HeatMapInfos[j - 1].Color;

                            float t = (my_Values[i] - my_CurMaxAmount) / my_UpDiffValue;
                            texture.SetPixel((int)my_Pixels[i].x, (int)my_Pixels[i].y, Color.Lerp(my_CurColor, my_UpColor, t));
                            break;
                        }
                        else
                        {
                            texture.SetPixel((int)my_Pixels[i].x, (int)my_Pixels[i].y, my_CurColor);
                            break;
                        }
                    }
                }
            }
原网站

版权声明
本文为[Xiaosheng yunmu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241142500498.html