Files
aibis-dream/Assets/Scripts/FixSystem/HeatMap/HeatTest.shader
T
2024-12-12 16:58:45 +08:00

209 lines
6.9 KiB
GLSL

Shader "Unlit/HeatTest"
{
Properties
{
//_MainTex ("Texture", 2D) = "white" {}
_RampTexture ("_RampTexture", 2D) = "white" {}
_Memorytexture ("_Memorytexture", 2D) = "white" {}
_Eyetexture ("_Eyetexture", 2D) = "white" {}
_UFtexture ("_UFtexture", 2D) = "white" {}
_Emotexture ("_Emotexture", 2D) = "white" {}
_Logictexture ("_Logictexture", 2D) = "white" {}
_Speaktexture ("_Speaktexture", 2D) = "white" {}
_headtexture ("_headtexture", 2D) = "white" {}
_Temtupre("_Temtupre", float) = 0 // 世界空间中的中心
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
Blend SrcAlpha OneMinusSrcAlpha // 设置混合模式为标准透明
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
//float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0; // 用于传递世界空间坐标
};
float3 _Center; // 世界空间中心点,使用 float3
float2 _BoxSize; // 可以调整为 float3,如果需要
//float4 _Time;
sampler2D _RampTexture;
sampler2D _Noise;
uniform float4 _Points[7]; // (x, y) = center z = tempture
uniform float4 _Properties[7]; // (x,y) = boxsize
sampler2D _Memorytexture;
sampler2D _Eyetexture ;
sampler2D _UFtexture;
sampler2D _Emotexture;
sampler2D _Logictexture ;
sampler2D _Speaktexture;
sampler2D _headtexture;
float _Temtupre;
inline float unity_noise_randomValue (float2 uv)
{
return frac(sin(dot(uv, float2(12.9898, 78.233))) * 43758.5453);
}
inline float unity_noise_interpolate (float a, float b, float t)
{
return lerp(a, b, t);
}
inline float unity_valueNoise (float2 uv)
{
float2 i = floor(uv);
float2 f = frac(uv);
f = f * f * (3.0 - 2.0 * f);
float2 c0 = i;
float2 c1 = i + float2(1.0, 0.0);
float2 c2 = i + float2(0.0, 1.0);
float2 c3 = i + float2(1.0, 1.0);
float r0 = unity_noise_randomValue(c0);
float r1 = unity_noise_randomValue(c1);
float r2 = unity_noise_randomValue(c2);
float r3 = unity_noise_randomValue(c3);
float bottom = unity_noise_interpolate(r0, r1, f.x);
float top = unity_noise_interpolate(r2, r3, f.x);
return unity_noise_interpolate(bottom, top, f.y);
}
float Unity_SimpleNoise(float2 UV, float Scale)
{
float noise = 0.0;
float amplitude = 0.5;
float frequency = 1.0;
// 简化循环叠加不同频率的噪波层次
for (int i = 0; i < 3; i++)
{
noise += unity_valueNoise(UV * Scale * frequency) * amplitude;
frequency *= 2.0;
amplitude *= 0.5;
}
return noise;
}
float remap(float x, float a, float b, float c, float d)
{
return c + (x - a) * (d - c) / (b - a);
}
float GetAlphaForTexture(int j, float2 localPos)
{
if (j == 0)
return tex2D(_Memorytexture, localPos).a;
else if (j == 1)
return tex2D(_Eyetexture, localPos).a;
else if (j == 2)
return tex2D(_UFtexture, localPos).a;
else if (j == 3)
return tex2D(_Emotexture, localPos).a;
else if (j == 4)
return tex2D(_Logictexture, localPos).a;
else if (j == 5)
return tex2D(_Speaktexture, localPos).a;
return 0.0;
}
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
// 计算并传递世界空间坐标
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3 worldPos = i.worldPos;
fixed4 col = fixed4(0, 0, 0, 1); // Initial color
bool isFind = false;
float2 pointPos = _Points[6].xy;
float temperature = _Points[6].z;
float2 boxSize = _Properties[6].xy;
float2 localPos = (worldPos.xy - pointPos) / boxSize + 0.5;
float backalpha = tex2D(_headtexture, localPos).a;
backalpha *= (temperature);
backalpha = clamp(backalpha, 0, 1);
backalpha = 1 - backalpha;
//backalpha = 0.7;
for (int j = 0; j < 6; j++)
{
float2 pointPos = _Points[j].xy;
float temperature = _Points[j].z;
float2 boxSize = _Properties[j].xy;
float2 localPos = (worldPos.xy - pointPos) / boxSize + 0.5;
//float alpha = GetAlphaForTexture(j, localPos);
if (localPos.x >= 0.0 && localPos.x <= 1.0 && localPos.y >= 0.0 && localPos.y <= 1.0)
{
isFind = true;
float alpha = GetAlphaForTexture(j, localPos);
alpha *= temperature;
alpha = clamp(alpha, 0, 1);
alpha = 1 - alpha;
alpha = clamp(alpha, 0, backalpha);
fixed4 rampColor = tex2D(_RampTexture, float2(alpha, 0));
col = rampColor;
}
}
// If no point is found, use the default texture (headtexture)
if (!isFind)
{
float2 pointPos = _Points[6].xy;
float temperature = _Points[6].z;
float2 boxSize = _Properties[6].xy;
float2 localPos = (worldPos.xy - pointPos) / boxSize + 0.5;
if (localPos.x >= 0.0 && localPos.x <= 1.0 && localPos.y >= 0.0 && localPos.y <= 1.0)
{
float alpha = tex2D(_headtexture, localPos).a;
alpha *= (temperature - 0.4);
alpha = clamp(alpha, 0, 1);
alpha = 1 - alpha;
//alpha=bl
alpha=backalpha;
fixed4 rampColor = tex2D(_RampTexture, float2(alpha, 0));
col = rampColor;
}
}
col.a = 0.75;
return col;
}
ENDCG
}
}
}