Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions scenes/game_elements/fx/clouds_shadow/clouds_shadow.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[gd_scene format=3 uid="uid://c0374lgarm8gj"]

[ext_resource type="Shader" uid="uid://blck4l5cl837d" path="res://scenes/game_elements/fx/clouds_shadow/components/clouds_shadow.gdshader" id="1_02eyt"]
[ext_resource type="Script" uid="uid://8ayl2vtjam10" path="res://scenes/game_elements/fx/clouds_shadow/components/clouds_shadow.gd" id="1_vhj68"]

[sub_resource type="FastNoiseLite" id="FastNoiseLite_jw523"]
frequency = 0.0075

[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_02eyt"]
width = 168
height = 256
noise = SubResource("FastNoiseLite_jw523")
seamless = true
seamless_blend_skirt = 0.8

[sub_resource type="ShaderMaterial" id="ShaderMaterial_2vvub"]
shader = ExtResource("1_02eyt")
shader_parameter/shadow_texture = SubResource("NoiseTexture2D_02eyt")
shader_parameter/amount = 0.35
shader_parameter/contrast = 25.0
shader_parameter/speed = Vector2(0.015, 0.01)
shader_parameter/alpha = 0.1

[node name="CloudsShadow" type="Parallax2D" unique_id=1515275591]
editor_description = "This Parallax2D node is used to repeat the texture so it's bigger than the viewport size. And to scroll it with the camera.
"
top_level = true
repeat_size = Vector2(1024, 1024)
repeat_times = 3
script = ExtResource("1_vhj68")

[node name="ColorRect" type="ColorRect" parent="." unique_id=1045851952]
unique_name_in_owner = true
editor_description = "Start with a small noise texture that is then upscaled to a square. The size of this ColorRect.

The texture width is about 2/3 of its height to look flattened, matching the game perspective. This way the shadow seems to be projected on the ground.
"
Comment on lines +34 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this use of editor description!

material = SubResource("ShaderMaterial_2vvub")
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_right = 1024.0
offset_bottom = 1024.0
grow_horizontal = 2
grow_vertical = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-FileCopyrightText: The Threadbare Authors
# SPDX-License-Identifier: MPL-2.0
@tool
extends Parallax2D

@export_tool_button("Random Clouds") var randomize_button: Callable = randomize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite follow how this button is supposed to be used.

Suppose that I have res://scenes/world_map/frays_end.tscn open in my editor. If I click this button, the clouds in the editor change, but when I run the scene they are back to the seed=0 clouds.

However, if I have both res://scenes/world_map/frays_end.tscn and res://scenes/game_elements/fx/clouds_shadow/clouds_shadow.tscn open. Then:

  1. Switch to the frays end tab
  2. Click Random Clouds
  3. Save Fray's End - no change on disk
  4. Switch back to the clouds_shadow.tscn scene; observe that the clouds have changed to match Fray's End
  5. Save that scene; observe that the seed has been persisted here.

If I set all of the ShaderMaterial, NoiseTexture2D and FastNoiseLite as resource_local_to_scene = true (I don't understand why all three have to be) then this doesn't happen: changing the seed in Fray's End does not persist. But then it does nothing in the editor.

I think it's fine either way - the worst that happens is that the random clouds are inadvertently changed occasionally - but maybe we want to do the NOTIFICATION_EDITOR_PRE_SAVE trick in clouds_shadow.gd to reset the seed? Or we can just leave it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, since in my prototype branch I was calling to the randomize() function directly, and testing the button only in the clouds_shadow.tscn scene, I didn't notice. I guess there is no other way than exporting a proxy "seed" property and then the noise resource is set to this value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wjt I appended a fixup commit. Let me know what do you think. If we want the seed hidden we could change the export to be @export_storage, but I think is good to expose it. I had something similar in my townies exploration #1801


@export var _seed: int:
set = _set_seed

var texture: NoiseTexture2D
var noise: FastNoiseLite

@onready var color_rect: ColorRect = %ColorRect


func _set_seed(new_seed: int) -> void:
_seed = new_seed
if noise:
noise.seed = _seed
await texture.changed


func _ready() -> void:
texture = (color_rect.material as ShaderMaterial).get_shader_parameter("shadow_texture")
noise = texture.noise
_set_seed(_seed)


func randomize() -> void:
await _set_seed(randi())
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://8ayl2vtjam10
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* SPDX-FileCopyrightText: The Threadbare Authors
* SPDX-License-Identifier: MPL-2.0
*/
shader_type canvas_item;

/**
* The shader is set to subtract because it's a shadow.
*/
render_mode blend_sub;

/**
* Use a Simplex noise for a dynamic texture. Or provide your own grayscale tileable texture!
*/
uniform sampler2D shadow_texture: filter_nearest, repeat_enable;

/**
* How much covered by clouds is the sky.
* - 0: Zero shadows.
* - 1: All dark.
* This parameter could be animated!
*/
uniform float amount: hint_range(0.0, 1.0) = 0.35;

/**
* How much contrast. Or how much defined is the shadow silhouette.
*/
uniform float contrast: hint_range(0.0, 50.0) = 25.0;

/**
* Animate the clouds.
* A 2/3 velocity emphasizes the world perspective.
*/
uniform vec2 speed = vec2(0.015,0.01);

/**
* How much alpha. Keep this subtle.
*/
uniform float alpha: hint_range(0.0, 1.0) = 0.1;


void fragment() {
// Displace the UV to animate the shadow.
vec2 uv = UV + speed * TIME;

float shadow = texture(shadow_texture, uv).r;

// Add amount value.
shadow = amount - shadow;

// Apply contrast to define the shadow silhouette.
// The result may be outside of the 0-1 range.
shadow = shadow * contrast + 0.5;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wjt very good point, I was keeping the contrast operation separate so I can comment it out, but is better to simplify and not add 0.5 and then immediately substract it. My reference for the contrast operation was https://github.com/patriciogonzalezvivo/lygia/blob/main/color/contrast.glsl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK! Sorry, If you think it's better as it was, that's fine. That reference is useful.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all, shader code should be optimized. The same happens when doing brightness + contrast operation, is preferred to do them both at once https://github.com/patriciogonzalezvivo/lygia/blob/main/color/brightnessContrast.glsl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I'm sure the shader compiler is smart enough to optimise out the + 0.5 - 0.5; I was more concerned about the shader interpreter in my mind :)


// Normalize the values to land between 0 and 1 again
// after applying contrast.
shadow = clamp(shadow, 0.0, 1.0);

// Apply alpha.
COLOR.a = alpha * shadow;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://blck4l5cl837d
37 changes: 0 additions & 37 deletions scenes/game_elements/props/clouds_overlay/clouds_overlay.tscn

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions scenes/world_map/frays_end.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
[ext_resource type="PackedScene" uid="uid://vb5o7hh5an8j" path="res://scenes/game_elements/characters/npcs/elder/template_elder.tscn" id="43_ppslc"]
[ext_resource type="PackedScene" uid="uid://c0104ickpm3ru" path="res://scenes/game_elements/props/decoration/flower/flower.tscn" id="45_cjmkx"]
[ext_resource type="PackedScene" uid="uid://cr65lmm5b0ueo" path="res://scenes/game_elements/characters/npcs/cat/cat.tscn" id="52_ppslc"]
[ext_resource type="PackedScene" uid="uid://c0374lgarm8gj" path="res://scenes/game_elements/fx/clouds_shadow/clouds_shadow.tscn" id="54_ns5r3"]
[ext_resource type="Script" uid="uid://cbj0406q05dly" path="res://scenes/game_elements/props/hint/input_key/interact_input.gd" id="55_wymun"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_h30yx"]
Expand Down Expand Up @@ -1059,3 +1060,5 @@ shape = SubResource("RectangleShape2D_djq26")

[node name="SpawnPointAfterIntro" parent="." unique_id=69976262 instance=ExtResource("37_thm8h")]
position = Vector2(62, 1747)

[node name="CloudsShadow" parent="." unique_id=1515275591 instance=ExtResource("54_ns5r3")]
Loading