Custom Wind Zone
Need custom wind behavior? Creating a custom wind zone is easy! Due to the combined CPU/GPU nature of Zephyr, you will need to create C# and HLSL code to implement your custom wind zones.
C#
To get started, create ta new C# script that derives from ZephyrWindZone.cs. This abstract class contains all of the abstract values that need to be assigned in order to work properly. Rather than implementing all values yourself, go ahead and use this base class as an example:
// Example custom wind zone
using DistantLands.Zephyr;
using UnityEngine;
[AddComponentMenu("Distant Lands/Zephyr/Wind Zones/Custom Wind Zone", 10)]
public class CustomWindZone : ZephyrWindZone
{
// Override the abstract values in the class.
// Sets the order in which this effect is applied. For additive effects make this 1 and multiplicative effects make this 2.
public override int ApplicationOrder => 1;
// Sets the radius of the effect.
public override float Radius => radius;
// Sets the strength of the effect.
public override float Strength => windStrength;
// Defines a unique ID for this wind zone. All custom wind zones need to start after 16.
public override int ID => 17;
// Quick reference to the position of the zone transform.
public override Vector3 Position => zoneTransform.position;
// Quick reference to an axis of effect.
public override Vector3 Direction => zoneTransform.forward;
// Auxiliary output. Can be set to any additional variable that you want to send to the GPU.
public override float AuxOne => 0;
// Auxiliary output. Can be set to any additional variable that you want to send to the GPU.
public override float AuxTwo => 0;
// Auxiliary output. Can be set to any additional variable that you want to send to the GPU.
public override float AuxThree => 0;
// Generic variation frequency controller.
public override float VariationTime => variationFrequency;
// Generic variation magnitude controller.
public override float VariationMagnitude => variationMagnitude;
// Generic variation offset controller for the X axis.
public override float VariationOffsetX => offsetX;
// Generic variation offset controller for the Y axis.
public override float VariationOffsetY => offsetY;
float offsetX;
float offsetY;
public float radius = 15;
public float windStrength = 3;
[Space(10)]
[Range(0, 1)]
public float variationMagnitude = 0.5f;
public float variationFrequency = 50;
public float variationSpeed = 15;
void Awake()
{
}
// Place any logic that needs to update every frame here. ApplyWind may not run every frame!
void Update()
{
}
// Function that applies the wind.
public override void ApplyWind(Vector3 position, ref Vector3 windVector)
{
if (!enabled)
return;
if ((position - Position).sqrMagnitude > (radius * radius))
return;
float influence = 1 - Mathf.Clamp01((position - Position).sqrMagnitude / (radius * radius));
// Replace with your custom wind logic!
// This example showcases the constant force wind zone type:
windVector += influence * influence * Direction * windStrength;
}
// Draws a sphere around where the wind is applied.
public void OnDrawGizmos()
{
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(Position, radius);
}
}HLSL
After creating your C# script and adding your custom logic, create a new HLSL file and convert the logic you set up in C# to HLSL so that the logic can also run on the GPU. All of your wind zone data is passed into the function in the zone struct and can be accessed like this:
This example is ready to be placed into an empty HLSL file:
Once you have created your HLSL, you can include it in the ApplyWindZones.hlsl file and add it to the switch case!
Future Proofing
For the best results editing the package, please ensure that your custom wind zone IDs start at an ID of 17 or higher. Thank you!
Last updated