Skip to content
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

Lightmeter options + improvements #295

Merged
merged 1 commit into from
Sep 28, 2024
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
1 change: 1 addition & 0 deletions COGITO/Components/Attributes/LightMeterAttribute.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ render_target_update_mode = 4
[node name="LightDetection" type="Node3D" parent="SubViewport"]

[node name="Camera3D" type="Camera3D" parent="SubViewport/LightDetection"]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0)
far = 20.0

[node name="TextureRect" type="TextureRect" parent="SubViewport/LightDetection"]
Expand Down
23 changes: 18 additions & 5 deletions COGITO/Components/Attributes/cogito_light_meter_attribute.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
extends CogitoAttribute
class_name CogitoLightmeter
# NOTE: This is currently quite expensive on performance due to the get_average_color function.

@export_group("Lightmeter Performance")
##Should light be updating constantly or only when player moves? Disable to constantly update at the cost of performance
@export var update_on_move_only : bool = true
##Lower for better performance
@export var subviewport_resolution : int = 256
##Interpolation used for get_average_color function. Default for Lightmeter is Lanczos, change for better performance
@export var interpolation_method: Image.Interpolation = Image.INTERPOLATE_LANCZOS


@onready var sub_viewport := $SubViewport
@onready var light_detection := $SubViewport/LightDetection
Expand All @@ -9,16 +18,20 @@ extends CogitoAttribute

@onready var previous_position : Vector3 = get_parent().global_position

func _ready() -> void:
sub_viewport.size = Vector2(subviewport_resolution, subviewport_resolution)

func get_average_color(texture: ViewportTexture) -> Color:
var image = texture.get_image() # Get the Image of the input texture
image.resize(1, 1, Image.INTERPOLATE_LANCZOS) # Resize the image to one pixel
image.resize(1, 1,interpolation_method) # Resize the image to one pixel
return image.get_pixel(0, 0) # Read the color of that pixel

func _process(_delta: float) -> void:
# A little check to reduce how many times the light detection is done.
if previous_position == get_parent().global_position:
return

#Skip updating every frame is update_on_move_only is enabled
if update_on_move_only:
# A little check to reduce how many times the light detection is done.
if previous_position == get_parent().global_position:
return
light_detection.global_position = get_parent().global_position # Make light detection follow the player
previous_position = light_detection.global_position
var texture = sub_viewport.get_texture() # Get the ViewportTexture from the SubViewport
Expand Down