Godot Project Settings
#Camera Control extends Node3D @export var circular_radius: float = 0.0 @export var circular_speed: float = 0.2 @export var cam: Camera3D @export var follow: Node3D @export var followlerp: bool var selfx := 0.0 var selfz := 0.0 func _ready() -> void: selfx = position.x selfz = position.z func _process(_delta: float) -> void: if follow: position.x = selfx + follow.position.x position.z = selfz + follow.position.z if followlerp: position.x = selfx + lerp(follow.position.x, position.x, 0.02) position.z = selfz + lerp(follow.position.z, position.z, 0.02) if Input.is_action_pressed("ui_copy"): cam.size = lerp(cam.size, 20.0, 0.2) if Input.is_action_pressed("ui_cut"): cam.size = lerp(cam.size, 10.0, 0.2)
#Camera3D class_name Camera3DTexelSnapped3 extends Camera3D @export var snap := true @export var snap_objects := true var texel_error := Vector2.ZERO @onready var _prev_rotation := global_rotation @onready var _snap_space := global_transform var _texel_size: float = 0.0 var _snap_nodes: Array[Node] var _pre_snapped_positions: Array[Vector3] func _ready() -> void: RenderingServer.frame_post_draw.connect(_snap_objects_revert) self.rotation.y = 0.8853982 func _process(_delta: float) -> void: self.rotation.y = 0.7853982 # rotation changes the snap space if global_rotation != _prev_rotation: _prev_rotation = global_rotation _snap_space = global_transform _texel_size = size / float((get_viewport() as SubViewport).size.y) # camera position in snap space var snap_space_position := global_position * _snap_space # snap! var snapped_snap_space_position := snap_space_position.snapped(Vector3.ONE * _texel_size) # how much we snapped (in snap space) var snap_error := snapped_snap_space_position - snap_space_position if snap: # apply camera offset as to not affect the actual transform h_offset = snap_error.x v_offset = snap_error.y # error in screen texels (will be used later) texel_error = Vector2(snap_error.x, -snap_error.y) / _texel_size if snap_objects: _snap_objects.call_deferred() else: texel_error = Vector2.ZERO func _snap_objects() -> void: _snap_nodes = get_tree().get_nodes_in_group("snap") _pre_snapped_positions.resize(_snap_nodes.size()) for i in _snap_nodes.size(): var node := _snap_nodes[i] as Node3D var pos := node.global_position _pre_snapped_positions[i] = pos var snap_space_pos := pos * _snap_space var snapped_snap_space_pos := snap_space_pos.snapped(Vector3(_texel_size, _texel_size, 0.0)) node.global_position = _snap_space * snapped_snap_space_pos func _snap_objects_revert() -> void: for i in _snap_nodes.size(): (_snap_nodes[i] as Node3D).global_position = _pre_snapped_positions[i] _snap_nodes.clear()
#Control Node extends Control @export var viewport: SubViewport @export var pixel_movement := true @export var sub_pixel_movement_at_integer_scale := true @export var _sprite: Sprite2D func _process(_delta: float) -> void: var screen_size := Vector2(get_window().size) # viewport size minus padding var game_size := Vector2(viewport.size - Vector2i(2, 2)) var display_scale := screen_size / game_size # maintain aspect ratio var display_scale_min: float = minf(display_scale.x, display_scale.y) _sprite.scale = Vector2(display_scale_min, display_scale_min) # scale and center control node size = (_sprite.scale * game_size).round() position = ((screen_size - size) / 2).round() # smooth! if pixel_movement: var cam := viewport.get_camera_3d() as Camera3DTexelSnapped3 var pixel_error: Vector2 = cam.texel_error * _sprite.scale _sprite.position = -_sprite.scale + pixel_error var is_integer_scale := display_scale == display_scale.floor() if is_integer_scale and not sub_pixel_movement_at_integer_scale: _sprite.position = _sprite.position.round()
// Sprite2D Shader Material // based on code by t3ssel8r: https://youtu.be/d6tp43wZqps // adapted to Godot by denovodavid shader_type canvas_item; render_mode unshaded; void fragment() { // box filter size in texel units vec2 box_size = clamp(fwidth(UV) / TEXTURE_PIXEL_SIZE, 1e-5, 1); // scale uv by texture size to get texel coordinate vec2 tx = UV / TEXTURE_PIXEL_SIZE - 0.5 * box_size; // compute offset for pixel-sized box filter vec2 tx_offset = smoothstep(vec2(1) - box_size, vec2(1), fract(tx)); // compute bilinear sample uv coordinates vec2 uv = (floor(tx) + 0.5 + tx_offset) * TEXTURE_PIXEL_SIZE; // sample the texture COLOR = textureGrad(TEXTURE, uv, dFdx(UV), dFdy(UV)); }
var direction := (Vector3(input_dir.x, 0, input_dir.y)).rotated(Vector3.UP, main_camera.rotation.y).normalized()
@onready var root = get_node("/root/") #later in function root.size_changed.connect(your_function) #----------------------- OR --------------------------------# get_tree().get_root().size_changed.connect(your_function)
#signalbus.gd signal _hello_world(val)
#button.gd var worldName := "Earth" func helloWorld()-> void: SignalBus.emit_signal("_hello_world", worldName) # OR SignalBus._hello_world.emit(worldName)
#player.gd func _ready()-> void: SignalBus.connect("_hello_world", helloWorld) func helloWorld(val): print("Hello ", val)
onready var amount = 20 onready var coin = preload("res://coin.tscn") func _drop(): for i in range(amount): var new_coin = coin.instance() get_parent().add_child(new_coin) print("coin: ", new_coin.name)
var enemies = get_tree().get_nodes_in_group("enemies")
func update_texture(texture: Texture): var reference_frames: SpriteFrames = $AnimatedSprite.frames var updated_frames = SpriteFrames.new() for animation in reference_frames.get_animation_names(): if animation != "default": updated_frames.add_animation(animation) updated_frames.set_animation_speed(animation, reference_frames.get_animation_speed(animation)) updated_frames.set_animation_loop(animation, reference_frames.get_animation_loop(animation)) for i in reference_frames.get_frame_count(animation): var updated_texture: AtlasTexture = reference_frames.get_frame(animation, i).duplicate() updated_texture.atlas = texture updated_frames.add_frame(animation, updated_texture) updated_frames.remove_animation("default") $AnimatedSprite.frames = updated_frames
From Reddit: MamaDespik
@onready var mesh_instance_3d: MeshInstance3D = $MeshInstance3D @onready var sub_viewport: SubViewport = $SubViewport func _ready() -> void: mesh_instance_3d.get_surface_override_material(0).albedo_texture = sub_viewport.get_texture()