Hi! I'm making a code for reading the image of a SubViewport node and then running a for loop to get all pixels colors values and averaging each channel (red, green and blue) to get each pixel`s lightness value. Next I'm appeding these value to an array.
The SubViewport image is my by getting the image of an orthogonal camera pointed at a sphere.
For some reason when I'm debugging I found that all pixel color values were (0,0,0).
(by the way how do I insert code properly here?)
This is the code I'm using:
extends Node3D
var LightLevel : float
Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
# The following code will take a screenshot of our camera and create an array of floats
# that will be used to average the float values so we can determine our general lightness
# values. Obs: An image is like an array of pixels
#var image : Image = get_node("SubViewportContainer/SubViewport").get_texture().get_image()
var LightDetectSV : SubViewport = get_tree().get_nodes_in_group("LightDetectSubViewport")[0]
var image : Image = LightDetectSV.get_texture().get_image()
var floats : Array[float]
for y in range(0, image.get_height()):
for x in range (0, image.get_width()):
var pixel = image.get_pixel(x,y)
var lightValue = (pixel.r + pixel.g + pixel.b) / 3
floats.append(lightValue)
return average(floats)
pass
func average(numbers: Array[float]) :
var sum = 0.0
for n in numbers:
sum += n
return sum / numbers.size()
Here is the link for this project folder:
https://drive.google.com/drive/folders/1C2btE2fwocHtXslp1rv5zKpiYL_rWnyl?usp=sharing