Well first off a uniform
variable is a way of passing data from the main program to the shader. Once you have a uniform defined like what I did, it will show up in ShaderMaterial
s that use that shader. This how you will change the aspect ratio from outside the shader to anywhere else, like gdscript.
tool # Without this keyword, the script cannot run in the editor.
extends Sprite
# This function gets called whenever the node, and all of its children get added to the scene tree.
func _ready():
# This method allows a notification for when the object moves, rotates or scales to be sent.
set_transform_notification(true)
# This function gets called whenever a notification is sent
# In this case, we're only concerned about the transform notification
func _notification(what):
if what == NOTIFICATION_TRANSFORM_CHANGED:
# set_shader_param allows us to pass a value to the material's shader uniform.
material.set_shader_param("aspect_ratio", scale.x / scale.y)
You can learn more about gdscript here, and learn more about shaders here.