Yes, you're right. If you're aiming to create an underwater effect where objects or the shader itself needs to portray any level of transparency or blending with the surrounding water, setting the RenderType to "Transparent" instead of "Opaque" is indeed more appropriate. This adjustment is especially crucial for effects that simulate light penetration, semi-transparent objects below water, or any scenario where the underwater objects should not fully obscure objects behind them.
Adjusting the shader to use a transparent rendering mode involves not only changing the render type tag but also adjusting how the shader calculates its final output color, including its alpha value. Below is an updated version of the shader code reflecting these changes:
Shader "Custom/UnderwaterShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_DepthColor ("Depth Color", Color) = (0, 0.4, 0.7, 1)
_DistortionStrength ("Distortion Strength", Float) = 0.1
_Transparency ("Transparency", Range(0, 1)) = 0.5
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
fixed4 _DepthColor;
float _DistortionStrength;
float _Transparency;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
// Basic coloring + fake depth effect
float depth = UNITY_MATRIX_MV[3][3];
c.rgb = lerp(c.rgb, _DepthColor.rgb, saturate(depth / 10.0) * _DepthColor.a);
// Distortion (simple)
IN.uv_MainTex += _Time.y * _DistortionStrength;
fixed4 distortedTex = tex2D(_MainTex, IN.uv_MainTex);
c.rgb = lerp(c.rgb, distortedTex.rgb, _DistortionStrength);
// Adjust transparency
c.a *= _Transparency;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Transparent"
}
In this example, I've added a property called _Transparency that allows you to control the overall transparency of the shader material. I've also adjusted the RenderType and Queue tags to "Transparent" to ensure the rendering engine treats this material accordingly, providing the correct blending and ordering during rendering.
Transparency requires careful handling of rendering order (controlled by the Queue tag) to ensure that transparent objects are rendered in a visually correct manner, especially in complex scenes. Adjusting transparency and other visual effects can dramatically affect the performance and appearance of your underwater scenes, so it's beneficial to test and refine these settings within the context of your specific project.
Remember, working with transparency and blending opens up a vast range of visual possibilities but also introduces challenges with sorting and overlapping objects. Always test your shader in various conditions to ensure it behaves as expected.