Stylized Water 2 documentation

Stylized Water 2

Stylized Water 2
Version: 1.6.3
Review

4.5.Scripting #

By large this is a shader asset. Though because aspects revolving around Buoyancy are related to scripting, there are a couple of C# functions in place that can aid development.

Familiarity with C# and OOP programming is assumed. Support cannot be extended to coding help. Some specific functionality and use cases really need to be handled on a project-specific basis with some creative thinking.

If you are using an assembly definition, ensure you add a reference to the sc.stylizedwater2.runtime assembly, otherwise you won’t be able to access the StylizedWater2 namespace and classes.

All these functions have IntelliSense summaries, which can be viewed in your IDE, next to any overload variations. If necessary, each parameter is described there.

StylizedWater2.WaterObject.Find

Anything related to sampling the wave height/normal through the Buoyancy class requires a reference to a Water Object component. This is so the Y-position (water level) and material (wave settings) can be accessed.

This function is a quick way to find a Water Object below or above a given position. It won’t be efficient to rely entirely on this, especially not every frame. Its performance impact scales up by the number of Water Objects in the scene.

StylizedWater2.Buoyancy.FindWaterLevelIntersection

If you imagine the water being a flat plane, this performs a faux-raycast to find the intersection point where a ray shot from a point, in a specific direction meets the water level (height in world-space).

This can be used as a fast board-phase check, for functionality that doesn’t necessarily need wave-specific accuracy.

StylizedWater2.Buoyancy.Raycast

Emulates a physics-style Raycast, under the hood this uses the FindWaterLevelIntersection function, and uses the result for the Buoyancy.SampleWaves function.

This could be used to position effects like bullet impacts on the water surface. Unlike a raycast, it cannot return a boolean if it hits or misses an actual Water Object. Instead you could check if the raycast returns a point that’s below the water level (if so, it likely passed through the water surface).

StylizedWater2.Buoyancy.CanTouchWater

Checks if the position falls between the Water Object’s Y-position, and the maximum possible wave height. In this case, it’s possible for a wave to touch the position at some point in time.

This can be used as a fast broad-phase check, before actually using the more expensive Buoyancy.SampleWaves function.

Floating Origins systems

To offset the water’s world-position coordinates in the shader, you can set the offset value through StylizedWater2.WaterObject.PositionOffset = <a Vector3>.

Network synchronized waves

All the shader effects, including the waves, use the global _TimeParameters shader value set by Unity as a time index. This translates to UnityEngine.Time.time.

If you’re running a networked application, it may be important to ensure the waves are identical for all players (especially when using buoyancy). This is possible by overriding the time index used by the shader.

Through script, pass your time value every frame through StylizedWater2.WaterObject.CustomTime = <a float>. This function will pass the time value to the shader and buoyancy calculations, and keeps them in sync.

To disable this behaviour, pass in a value lower than 0.

Example script

Yes No Suggest edit
Last updated on March 1, 2024
3 of 3 users found this section helpful
Suggest Edit