[gd_scene format=3 uid="uid://cc3qat6un2323"]

[ext_resource type="Texture2D" uid="uid://xygpf7c886pj" path="res://Assets/Spritesheets/Trees/Medium_Oak_Tree.png" id="1_i2ia5"]
[ext_resource type="PackedScene" uid="uid://d24gcv3umq60k" path="res://Entities/Characters/Utility/Occlusion/occlusion_culler.tscn" id="1_jbed0"]

[sub_resource type="CircleShape2D" id="CircleShape2D_jbed0"]
radius = 12.0

[sub_resource type="Shader" id="Shader_jbed0"]
code = "shader_type canvas_item;

uniform bool render_noise = false;
uniform sampler2D noise_texture : repeat_enable; // set in inspector
uniform float amplitude : hint_range(0.0, 0.5, 0.01) = 0.2;
uniform float time_scale : hint_range(0.0, 5.0, 0.01) = 0.04;
uniform float noise_scale : hint_range(0.0, 2.0, 0.0001) = 0.001;
uniform float rotation_strength : hint_range(0.0, 5.0, 0.1) = 1;
uniform vec2 rotation_pivot = vec2(0.5, 1);
varying vec2 world_position;

void vertex(){
    world_position = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
}

vec2 get_sample_pos(vec2 pos, float scale, float offset) {
	pos *= scale;
	pos += offset;
	return pos;
}

vec2 rotate_vec(vec2 vec, vec2 pivot, float rotation) {
	float cosa = cos(rotation);
	float sina = sin(rotation);
	vec -= pivot;
	return vec2(
		cosa * vec.x - sina * vec.y,
		cosa * vec.y + sina * vec.x
	) + pivot;
}

void fragment() {
	// get noise from texture
	vec2 noise_sample_pos = get_sample_pos(world_position, noise_scale, TIME * time_scale);
	float noise_amount = texture(noise_texture, noise_sample_pos).r - 0.5f;

	// get rotation position around a pivot
	float rotation = amplitude * noise_amount;
	vec2 rotated_uvs = rotate_vec(UV, rotation_pivot, rotation);

	// blend original uvs and rotated uvs based on distance to pivot
	float dist = distance(UV, rotation_pivot) * rotation_strength;
	vec2 result_uvs = mix(UV, rotated_uvs, dist);

	// output color
	COLOR = texture(TEXTURE, result_uvs);

	// optional, preview noise texture for debugging
	if (render_noise) {
		vec4 noise_color = texture(noise_texture, noise_sample_pos);
		COLOR = noise_color;
	}
}"

[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_o0u4i"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_b23hf"]
shader = SubResource("Shader_jbed0")
shader_parameter/render_noise = false
shader_parameter/noise_texture = SubResource("NoiseTexture2D_o0u4i")
shader_parameter/amplitude = 0.05999999865888
shader_parameter/time_scale = 0.04
shader_parameter/noise_scale = 0.001
shader_parameter/rotation_strength = 1.0
shader_parameter/rotation_pivot = Vector2(0.5, 1)

[sub_resource type="AtlasTexture" id="AtlasTexture_5otsd"]
atlas = ExtResource("1_i2ia5")
region = Rect2(32, 0, 32, 48)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_0srx6"]
size = Vector2(6, 3)

[node name="Tree 01" type="StaticBody2D" unique_id=2046930104]
y_sort_enabled = true

[node name="Occlusion Culler" parent="." unique_id=1362480066 node_paths=PackedStringArray("nodes_to_occlude") instance=ExtResource("1_jbed0")]
nodes_to_occlude = [NodePath("../Sprite2D")]

[node name="CollisionShape2D" type="CollisionShape2D" parent="Occlusion Culler" unique_id=731605527]
position = Vector2(0, -10)
shape = SubResource("CircleShape2D_jbed0")

[node name="Sprite2D" type="Sprite2D" parent="." unique_id=1420512565]
y_sort_enabled = true
material = SubResource("ShaderMaterial_b23hf")
position = Vector2(0, 8)
texture = SubResource("AtlasTexture_5otsd")
offset = Vector2(0, -8)

[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=870152657]
position = Vector2(0, 6.5)
shape = SubResource("RectangleShape2D_0srx6")
