Transferring equation from shader to public class

hey, if you have some time pls help. how do I adjust the equation for GetWaveHeight? The equation I’m trying to use is from a shader, so everythings broken for some reason. idk why, i just started c# a few days ago.

public class WaveManager1 : MonoBehaviour
{
    public static WaveManager1 instance;

    public float amplitude = 1f;
    public float length = 2f;
    public float speed = 1f;
    public float offset = 0f;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Debug.Log("Instance already exists, destroying object!");
            Destroy(this);
        }   
    }

    private void Update()
    {
        offset += Time.deltaTime * speed;
    }

    public float GetWaveHeight(float _x)
    {
        return amplitude * Mathf.Sin(_x / length + offset);
    }
}

Shader "Custom/Waves"
{
    Properties
    {
        _WaveA ("Wave A (dir, steepness, wavelength)", Vector) = (1,0,0.5,10)
        _WaveB ("Wave B", Vector) = (0,1,0.25,20)
        _WaveC ("Wave C", Vector) = (1,1,0.15,10)
      
    }

    SubShader
    {
        float4 _WaveA, _WaveB, _WaveC;

      
            float3 GerstnerWave (
            float4 wave, float3 p, inout float3 tangent, inout float3 binormal)
            {
            float steepness = wave.z;
            float wavelength = wave.w;
            float k = 2 * UNITY_PI / wavelength;
            float c = sqrt(9.8 / k);
            float2 d = normalize(wave.xy);
            float f = k * (dot(d, p.xz) - c * _Time.y);
            float a = steepness / k;

            tangent += float3(
                -d.x * d.x * (steepness * sin(f)),
                d.x * (steepness * cos(f)),
                -d.x * d.y * (steepness * sin(f))
            );
            binormal += float3(
                -d.x * d.y * (steepness * sin(f)),
                d.y * (steepness * cos(f)),
                -d.y * d.y * (steepness * sin(f))
            );
            return float3(
                d.x * (a * cos(f)),
                a * sin(f),
                d.y * (a * cos(f))
            );
        }

thanks

Hey and welcome to the CPU side of things.

Can you clarify your question a bit? I’m not 100% sure what you’re looking for, and I don’t see much correlation between the C# script and the shader other than the fact that they both deal with “waves”. What kind of “adjustment” are you looking for?

1 Like

yo thanks:)

hm how I can describe it is the wavemanager creates an invisible wave, and the shader manipulates vertices on the gpu? so its cosmetic? by adjusting the formula in wavemanger, objects can float on top of the invisible wave and match the visual one.