

#ifndef FOLIAGE_WIND_FIELD_INCLUDED
#define FOLIAGE_WIND_FIELD_INCLUDED

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"

//uniform float4	  _global_wind_list[1024];
TEXTURE2D(_global_wind_texture);	SAMPLER(sampler_global_wind_texture);
uniform float _global_wind_cur_frame;
uniform float _global_wind_scale;

float3 GetGlobalWindValue(float3 worldPos)
{
	//half maxDistance = 128.0;	// 8m->64 grass, 1024 / 64 = 16 * 8 = 128;
	//half distance = fmod(worldPos.x, maxDistance);
	// now is (-maxDistance, maxDistance), next to (0, 2.0 * maxDistance)
	//distance += maxDistance;
	// to (0, maxDistance)
	//distance = fmod(distance, maxDistance);
	//float delayTime = worldPos.x *  0.0078125; /* 1.0 / maxDistance = 0.0078125 */; //distance / maxDistance;
	//float index = delayTime * 1024 + _global_wind_cur_frame;
	float index = worldPos.x * 0.0078125 /* 1024 / 128 / 1024 = 1/128 */ + _global_wind_cur_frame;

	//index = fmod(index, 1024);	// texture warpMode is repeat;
	return (SAMPLE_TEXTURE2D_LOD(_global_wind_texture, sampler_global_wind_texture, float2(index, 0), 0) * _global_wind_scale * 2).xyz;
	//return _global_wind_list[index].xyz;

}

float3 GetGlobalWindValueXY(float3 worldPos)
{
	return  GetGlobalWindValue(float3(worldPos.x + worldPos.y, 0, 0));
}

float3 GetGlobalWindValueY(float3 worldPos)
{
	float3 wind = GetGlobalWindValue(float3(worldPos.y, 0, 0));
	return float3(-wind.y, wind.x, wind.z);
}

float3 GetGlobalWindValueZ(float3 worldPos)
{
	float3 wind = GetGlobalWindValue(float3(worldPos.z, 0, 0));
	return float3(wind.z, wind.y, -wind.x);
}

#endif // FOLIAGE_WIND_FIELD_INCLUDED
