移动端水面Shader实现:简单易懂的代码示例
以下是一个移动端可以使用的水面shader的示例代码:
Shader "Custom/WaterShader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_NormalMap ("Normal Map", 2D) = "bump" {}
_WaveSpeed ("Wave Speed", Range(0, 1)) = 0.5
_WaveStrength ("Wave Strength", Range(0, 1)) = 0.1
}
SubShader {
Tags {'Queue'='Transparent' 'RenderType'='Transparent'}
LOD 100
CGPROGRAM
#pragma surface surf Lambert alpha
sampler2D _MainTex;
sampler2D _NormalMap;
float _WaveSpeed;
float _WaveStrength;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutputAlpha o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Alpha = c.a;
// Calculate wave displacement
float2 waveOffset = IN.uv_MainTex * _WaveSpeed;
float2 waveDisplacement = tex2D (_NormalMap, waveOffset).rg * _WaveStrength;
// Apply wave displacement to the vertex position
float3 vertexPos = UnityObjectToClipPos(IN.uv_MainTex.xy);
vertexPos.x += waveDisplacement.x;
vertexPos.z += waveDisplacement.y;
o.Alpha *= smoothstep(0.98, 1, vertexPos.y); // Fade out the waves near the horizon
// Set the final vertex position
o.Alpha *= vertexPos.y; // Adjust the transparency based on height
o.Alpha *= 1 - length(waveDisplacement); // Fade out the waves at the edges
// Set the final color
o.Albedo = c.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
注意:上述代码是一个简单的水面shader示例,仅实现了基本的水波效果,可能不够真实。你可以根据需要进行调整和优化。
原文地址: https://www.cveoy.top/t/topic/o4es 著作权归作者所有。请勿转载和采集!