-
Notifications
You must be signed in to change notification settings - Fork 280
Reinstate clouds shadow #1921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reinstate clouds shadow #1921
Changes from all commits
c185fa9
db79216
15de401
d56b714
5ed7f5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| " | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 However, if I have both
If I set all of the ShaderMaterial, NoiseTexture2D and FastNoiseLite as 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| @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; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| // 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 |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
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!