shader_type spatial;
render_mode cull_disabled;

uniform vec3 color: source_color;
uniform vec3 shade: source_color;
uniform sampler2D noise: source_color;
uniform float noiseScale = 1.0;
uniform float windSpeedScale = 1.0;
uniform float windVertexOffsetScale = 1.0;

varying vec3 worldPos;
varying vec3 instance_origin;

void vertex() {
	worldPos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
	instance_origin = MODELVIEW_MATRIX[3].xyz;
	
	vec2 timeOffset = vec2(TIME/windSpeedScale);
	float noiseLevel = texture(noise, timeOffset + worldPos.xz/noiseScale).r;
	float vertexWindOffset = noiseLevel * worldPos.y * windVertexOffsetScale;
	VERTEX += vec3(1.0, 0.0, 1.0)*vertexWindOffset;
    // billboarding
    // MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0],INV_VIEW_MATRIX[1],INV_VIEW_MATRIX[2],MODEL_MATRIX[3]);
}

void fragment() {
	vec2 timeOffset = vec2(TIME/windSpeedScale);
	float noiseLevel = texture(noise, timeOffset + worldPos.xz/noiseScale).r;
	NORMAL = (VIEW_MATRIX * vec4(0.0, 1.0, 0.0, 0.0)).xyz;
	ALBEDO = mix(color, shade, UV.y) * mix(color, shade, noiseLevel);
	LIGHT_VERTEX = instance_origin;
}

 void light() {
 	float lambert = clamp(dot(NORMAL, LIGHT), 0.0, 1.0);
 	float halfLambert = pow(lambert*0.5 + 0.5, 5);
    DIFFUSE_LIGHT += halfLambert * ATTENUATION * LIGHT_COLOR / PI;
 }