// #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 sampler2D terrainHeightMap;
uniform vec2 terrainCenterXZ = vec2( 0.0, 0.0 );
uniform vec2 terrainSizeXZ = vec2( 100.0, 100.0 );
uniform float terrainMinHeight = 0.0;
uniform float terrainMaxHeight = 100.0;

uniform sampler2D terrainNormalMap;
*/

void addTerrainOffset(   
  mat4 _MODEL_MATRIX, 
  inout vec3 _VERTEX, 
  
  sampler2D terrainHeightMap,
  vec2 terrainCenterXZ,
  vec2 terrainSizeXZ,
  float terrainMinHeight,
  float terrainMaxHeight

)
{
  vec3 worldVertex = localToWorld( _VERTEX, _MODEL_MATRIX );

  vec2 mapOffset = terrainCenterXZ - terrainSizeXZ/2.0;

  vec2 uv = ( worldVertex.xz - mapOffset ) / terrainSizeXZ;

  float height = mix( terrainMinHeight, terrainMaxHeight, texture( terrainHeightMap, uv ).r );
  
  worldVertex.y += height;

  _VERTEX = worldToLocal( worldVertex, _MODEL_MATRIX );

}