This article's documentation is for anything that uses the Source engine. Click here for more information.

Water (shader): Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Added Caveats for Water related Fog Parameters)
(Cleared up a "Confirm:",added context to $Scale, $Abovewater and $underwateroverlay)
Line 21: Line 21:
{{note|In games before {{As}}, there is no {{ent|$basetexture}}. The surface is a combination of fog color, animated bump map, reflection and refraction.<br>{{tip|If a BaseTexture is necessary in earlier games, and real-time reflections are not necessary, then the [[Refract]] shader could be used instead.}}}}
{{note|In games before {{As}}, there is no {{ent|$basetexture}}. The surface is a combination of fog color, animated bump map, reflection and refraction.<br>{{tip|If a BaseTexture is necessary in earlier games, and real-time reflections are not necessary, then the [[Refract]] shader could be used instead.}}}}


{{MatParam|$abovewater|bool|Whether this material is used for above or below the water’s surface.}}
{{MatParam|$abovewater|bool|Whether this material is used for above or below the water’s surface.<br>
{{note|Not setting this in the VMT will automatically force it to 1. If not defined, a warning will appear saying <code>Need to set $abovewater for material x</code>}}}}
{{MatParam|$bottommaterial|material|Required parameter. This is the material ('''not''' texture) to use when underneath the water’s surface. The bottom material must have <code>$reflecttexture</code>, <code>$abovewater</code> and <code>$envmap</code> disabled, but can otherwise do whatever it wants.}}
{{MatParam|$bottommaterial|material|Required parameter. This is the material ('''not''' texture) to use when underneath the water’s surface. The bottom material must have <code>$reflecttexture</code>, <code>$abovewater</code> and <code>$envmap</code> disabled, but can otherwise do whatever it wants.}}
{{MatParam|$underwateroverlay|material|Applies a refracting screen overlay when the camera is underwater. Generally used with <code>effects\water_warp01</code>. Requires <code>$abovewater</code> to be 0.|since={{src07}}}}
{{MatParam|$underwateroverlay|material|Applies a refracting screen overlay when the camera is underwater. Generally used with <code>effects\water_warp01</code>. Requires <code>$abovewater</code> to be 0.<br>
{{note|This is technically not a parameter on the water shader, meaning it can be abused for other unintended purposes! The overlay may still be applied even when the underwater material is told not to render ( {{ent|$no_draw}})}}|since={{src07}}}}
{{MatParam|$bumpmap|texture|dx8=1}}
{{MatParam|$bumpmap|texture|dx8=1}}
{{MatParam|$normalmap|texture|A [[Du/dv map]] for DirectX 8 rendering (<code>$bumpmap</code>), and a [[bump map]] for DirectX 9 and above (<code>$normalmap</code>). These should be animated unless the water is to be perfectly still or you're using a flow map, which makes an animated bump map redundant.
{{MatParam|$normalmap|texture|A [[Du/dv map]] for DirectX 8 rendering (<code>$bumpmap</code>), and a [[bump map]] for DirectX 9 and above (<code>$normalmap</code>). These should be animated unless the water is to be perfectly still or you're using a flow map, which makes an animated bump map redundant.
Line 31: Line 33:
{{MatParam|$bumptransform|matrix|Transforms the bump map texture.
{{MatParam|$bumptransform|matrix|Transforms the bump map texture.
{{VMT UVtransform}}}}
{{VMT UVtransform}}}}
{{MatParam|$scale|vector2|{{todo|What does this do?}}}}
{{MatParam|$scale|vector2|Unused Parameter.-Likely a precursor of $RefractAmount and $ReflectAmount.<br>
{{MatParam|$flashlighttint|float|{{confirm|How much projected textures should tint the water?}}|since={{l4d2}}}}
In {{src13}} and {{as}}'s Shadercode it will bundle up $Reflect/$RefractAmount into a single float4 to save some instructions, like so:<br>
<code>"[$ReflectAmount, $ReflectAmount, $RefractAmount, $RefractAmount]"</code> This is pretty much $Scale but with two more components.<br>
The constant register is referred to as 'g_ReflectRefractScale' as well. The only thing the shader does with it now is set a default value of "[1 1]".}}
{{MatParam|$flashlighttint|float|Tints the results of projected textures on the water. The flashlight mainly affects the brigthness of the fog in the water. A comment in {{as}}'s Shadercode explains :<br>
"The flashlight on the water surface is basically the diffuse flashlight * waterfogcolor * g_flFlashlightTint.
g_flFlashlightTint is tweakable in cases where the water fog color is really dark and the flashlight doesn't show up, etc."|since={{l4d2}}}}
{{MatParam|$waterdepth|float|{{todo|What does this do exactly? Seems like it's related to $waterblendfactor, Found in liquids/nuke_water.vmt. Found in 2006/2007 code, does it do anything there?}}|since={{csgo}}}}
{{MatParam|$waterdepth|float|{{todo|What does this do exactly? Seems like it's related to $waterblendfactor, Found in liquids/nuke_water.vmt. Found in 2006/2007 code, does it do anything there?}}|since={{csgo}}}}
{{MatParam|$depth_feather|int|{{todo|What does this do exactly? Found in liquids/nuke_water.vmt.}}|since={{csgo}}}}
{{MatParam|$depth_feather|int|{{todo|What does this do exactly? Found in liquids/nuke_water.vmt.}}|since={{csgo}}}}

Revision as of 15:35, 28 September 2023

English (en)Français (fr)中文 (zh)Translate (Translate)
Water reflecting entities in real-time in Episode Two.

Water is a Pixel shader available in all Source Source games. It creates water that realistically reflects and refracts the world, and that can flow throughout a map.

Tip.pngTip:Water does not need to use the Water shader! If a less realistic effect is desired, or the water volume is oddly shaped, other shaders can be used, such as LightmappedGeneric or Refract. The method for creating the materials is the same.

Special behaviour

Expensive and cheap
Water materials fall into two groups: expensive and cheap. Expensive water reflects and refracts the world in real-time, while cheap water uses an $envmap and does not refract. Users can disable expensive water rendering through their video options, so always place env_cubemap entities throughout a map.
Above and below
Water actually requires two materials: one for above the surface and one for below. These materials are independent of each other, but for proper effect need to correlate in most areas.
Flow Mapping
Water can flow in arbitrary directions. It's very tricky to create a flow map by hand however, and the tool Valve used internally hasn't been released.

Shader parameters

Textures

Note.pngNote:In games before Alien Swarm, there is no $basetexture. The surface is a combination of fog color, animated bump map, reflection and refraction.
Tip.pngTip:If a BaseTexture is necessary in earlier games, and real-time reflections are not necessary, then the Refract shader could be used instead.

$abovewater $bottommaterial $underwateroverlay $bumpmap $normalmap $dudvframe $bumpframe $bumptransform $scale $flashlighttint $waterdepth $depth_feather

Fog

$fogenable $fogcolor

$fogstart $fogend $lightmapwaterfog

Reflection

$reflecttexture $reflectamount $reflecttint $envmap $envmapframe $forceenvmap $forcecheap $forceexpensive $cheapwaterstartdistance $cheapwaterenddistance $reflectentities $reflectonlymarkedentities $reflectskyboxonly $reflect2dskybox $reflect3dskybox $reflectblendfactor $nofresnel $forcefresnel $basereflectance $maxreflectance

Refraction

$refract $refracttexture $refractamount $refracttint $blurrefract $pseudotranslucent $waterblendfactor

Size/angle of $scroll1 and $scroll2

Flowing water

$scroll1 "[<normal X> <normal Y>]" (in all games since Source 2006)
$scroll2 "[<normal X> <normal Y>]" (in all games since Source 2006)
If $scroll1 is defined and X is not zero, two more instances of the normal map will be drawn in such a way that they are merged together. The first layer is 7x larger and rotated 45°; the second is 2x larger and rotated 90°. The parameters specify the speed and direction that the extra layers will move. (Valve's materials usually contain a third number, but it doesn't have any apparent effect and is most likely obsolete.)

Flow maps

A flow map texture.
The flow map texture in-engine, shown with $flow_debug 1.
The result in-engine.

$flowmap $flow_normaluvscale $flow_worlduvscale $flow_uvscrolldistance $flow_timeintervalinseconds $flow_timescale $flow_bumpstrength $flow_noise_texture $flow_noise_scale $flow_debug

Basetexture Flow

$basetexture $color_flow_uvscale $color_flow_timescale $color_flow_timeintervalinseconds $color_flow_uvscrolldistance $color_flow_lerpexp $color_flow_displacebynormalstrength

Authoring a flow map

Directions of water flow depend on the red and green channels of the flow map.

Overall, trying to create a flow map manually is a nightmare. Painting the flow map, fitting it into the world, and painting foam, is extremely difficult.

But if you do want to give it a try, flow direction is read from the red and green channels of the flow map. 50% tone means no movement.

The texture scale of any faces using a water texture with a flow map appear to have a direct correlation on the flow map. The fewer water faces or planes you use the easier the process of authoring a custom flow map will be (selecting all of your water faces and using 'treat as one' when using the fit scaling may also be an option)

Red channel
X axis
Green channel
Y axis
Alpha channel
Controls $basetexture blending from Portal 2 onwards.
There are now tools to assist in painting flowmaps.
Flow field editor from algoholic.eu : http://algoholic.eu/another-flow-field-editor-update/ [dead link]
Flowmap generator (paid): http://www.superpositiongames.com/products/flowmap-generator
Creating flowmaps using Houdini (UDK) : https://www.dropbox.com/s/ii2x077vj64lyhl/Water%20Flow%20For%20UDK.pdf [dead link]

Other

%compilewater $surfaceprop %tooltexture

WaterLOD proxy
This connects the water_lod_control entity in a level to the water’s internal parameters. This must be declared in the material for the LOD mechanisms to work properly.

See also