Unity cloud rendering effect

need

According to the post-processing results of finite element analysis (stress, etc.), the cloud image effect is reproduced in Unity to the same effect.

Data that needs to be prepared

Export model node data from finite element analysis software (Abaqus, Ansys, etc.)

Implementation principle

Step1 Use C# to convert the data into the color from red to blue according to the data size of the node (equivalent to the model vertex) (using the HSV color model to obtain pure colors), and obtain a Color array of the model vertex; Step2 Pass the color
in The shader performs processing (AppToVert, VertToFrag) and obtains the corresponding color value from the texture map through texture mapping.

Code display

1. C# code.

public class CloudMapRendering
{
    
    
    public static Color[] CalculateModelVerticeColors(float[] physicsDatas)
    {
    
    
        colorDatas = new Color[physicsDatas.Length];
        float[] hueColorH = new float[physicsDatas.Length];
        float max = physicsDatas.GetMax();
        //当有限元网格结点值全为0时,颜色全为蓝
        if (max == 0)
        {
    
    
            for (int i = 0; i < physicsDatas.Length; i++)
            {
    
    
                colorDatas[i] = Color.blue;
            }
            return;
        }
        float min = physicsDatas.GetMin();
        float range = max - min;
        for (int i = 0; i < physicsDatas.Length; i++)
        {
    
    
            hueColorH[i] = 2.0f / 3 * (max - physicsDatas[i]) / range;
            colorDatas[i] = Color.HSVToRGB(hueColorH[i], 1, 1);
        }
        return colorDatas;
    }
}
public static class ArrayExtension
{
    
    
    public static float GetMax(this float[] vs)
    {
    
    
        float max = 0;
        for (int i = 0; i < vs.Length; i++)
        {
    
    
            if (vs[i] > max)
            {
    
    
                max = vs[i];
            }
        }
        return max;
    }
    public static float GetMin(this float[] vs)
    {
    
    
        float min = 0;
        for (int i = 0; i < vs.Length; i++)
        {
    
    
            if (vs[i] < min)
            {
    
    
                min = vs[i];
            }
        }
        return min;
    }
}

Create a new C# script and hang it on the rendering model, call the CalculateModelVerticeColors method above, and assign the calculated value to the colors attribute through the colors attribute in MeshFilter.

2. Shader part.

Shader "Custom/testCloudMapWireframeShader"
{
    
    
    Properties
    {
    
    
		_Texture("Main Tex",2D) = ""{
    
    }
	}
    SubShader
    {
    
    
		Pass
		{
    
    
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			struct a2v {
    
    
				float4 vertex:POSITION;
				fixed4 color : COLOR;
				float4 texcoord:TEXCOORD0;
			};

			struct v2f {
    
    
				float4 position:SV_POSITION;
				float3 color:COLOR0;
				float4 uv:TEXCOORD1;
			};

			sampler2D _Texture;
			float4 _UVposition;
			//由于着色器用RGB进行计算,因此先转换为HSV
			float3 RGB2HSV(float3 c)
			{
    
    
				float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
				float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
				float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));

				float d = q.x - min(q.w, q.y);
				float e = 1.0e-10;
				return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
			}
			//根据HSV颜色值映射到对应的纹理坐标进行纹理采样
			float3 ColorConvert(float3 originalHSVc)
			{
    
    
				return tex2D(_Texture, (1,(1 - originalHSVc.x) * 3) * step(2.0 / 3.0, originalHSVc.x) + (1,originalHSVc.x * 3 / 2) * step(originalHSVc.x, 2.0 / 3.0));
			}
			v2f vert(a2v v)
			{
    
    
				v2f f;
				f.position = UnityObjectToClipPos(v.vertex);
				f.color = v.color;
				f.uv = v.texcoord;
				return f;
			}

			fixed4 frag(v2f f) :SV_Target
			{
    
    
				float3 convertedColor = RGB2HSV(f.color);
				f.color = ColorConvert(convertedColor);
				return fixed4(f.color,1);
			}
			ENDCG
		}
		}
    FallBack "Diffuse"
}

texture map

A common color gradient map (red on the left and blue on the right)
Insert image description here

postscript

This is the first time the author has written an article, and there are many places that are not clearly stated. If you have questions or comments, you are welcome to comment.

Guess you like

Origin blog.csdn.net/flatrrow/article/details/127686081