// #include "res://addons/rokojori_action_library/Runtime/Shading/Library/Wind.gdshaderinc"

#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Math.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Transform.gdshaderinc"


/*
uniform bool windEnabled = false;
uniform float windOcclusionAmount = 0;
uniform float windStrength = 0;
uniform vec2 windSpeed = vec2(1,1);
uniform float windScale = 0.1;
uniform sampler2D windNoise;
uniform vec2 windNoiseAngleOffset;
uniform vec2 windNoiseStrengthOffset;
uniform float windStart = 0;
uniform float windEnd = 1;
uniform float windWeightCurve:hint_range(0,1) = 0.5f;
uniform float windHeightCompensation :hint_range(0,1) = 0.5f;
uniform float windNormalBending :hint_range(0,1) = 0.1f;
varying float vertexWindAO;

applyWind( 
  TIME, 
  MODEL_MATRIX, 
  VERTEX, 
  NORMAL,
  windAO,
  windOcclusionAmount,
  windStrength,
  windSpeed,
  windScale,
  windNoise,
  windNoiseAngleOffset,
  windNoiseStrengthOffset,
  windStart,
  windEnd,
  windWeightCurve,
  windHeightCompensation,
  windNormalBending
);

*/
void applyWind( 
  float _TIME, 
  mat4 _MODEL_MATRIX, 
  inout vec3 _VERTEX, 
  inout vec3 _NORMAL,
  inout float _windAO,
  float _windOcclusionAmount,
  float _windStrength,
  vec2 _windSpeed,
  float _windScale,
  sampler2D _windNoise,
  vec2 _windNoiseAngleOffset,
  vec2 _windNoiseStrengthOffset,
  float _windStart,
  float _windEnd,
  float _windWeightCurve,
  float _windHeightCompensation,
  float _windNormalBending
 )
{
  float _windAmount = normalizeToRange01( _VERTEX.y, _windStart, _windEnd );
  float rawWindAmount = _windAmount;
  float originalHeight = _VERTEX.y;
  _windAmount = mix( _windAmount, _windAmount * _windAmount, _windWeightCurve );    
  vec3 worldVertex = localToWorld( _VERTEX, _MODEL_MATRIX );
  vec2 _windUV = _TIME * _windSpeed + worldVertex.xz * _windScale;
  float angle = texture( _windNoise, _windUV + _windNoiseAngleOffset).r * PI * 2.0;
  float generalStrength = texture( _windNoise, _windUV + _windNoiseStrengthOffset ).r;
  float strength = generalStrength * _windStrength;
  vec2 circle = onCircle( angle ) * strength;
  _VERTEX = worldToLocal( worldVertex + vec3( circle.x, 0, circle.y ) * _windAmount, _MODEL_MATRIX );
  float minY = min( _VERTEX.y, 0 ); 
  _VERTEX.y = mix( _VERTEX.y, max( minY, _VERTEX.y - strength * _windAmount), _windHeightCompensation * 4.0f );
  _NORMAL = normalize( mix( _NORMAL, vec3( circle.x, -1, circle.y ), generalStrength * _windNormalBending ) );
  _windAO = mix( 1, 0, generalStrength * _windOcclusionAmount);
}