Skip to content

Instantly share code, notes, and snippets.

@Garmelon
Last active September 23, 2024 20:37
Show Gist options
  • Save Garmelon/670071cbbe43d42f2d4ad0c389864cd3 to your computer and use it in GitHub Desktop.
Save Garmelon/670071cbbe43d42f2d4ad0c389864cd3 to your computer and use it in GitHub Desktop.
Godot perspecive projection shaders
extends TextureRect
export var sensitivity: Vector3 = Vector3(0.005, 0.005, TAU/4)
var euler: Vector3 = Vector3.ZERO
func _init() -> void:
sensitivity.x *= -1
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_cancel"):
if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if event.is_action_pressed("camera_reset_roll"):
euler.z = 0
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
if event is InputEventMouseMotion:
euler.x += event.relative.y * sensitivity.y
euler.x = clamp(euler.x, -TAU/4, TAU/4)
euler.y += event.relative.x * sensitivity.x
euler.y = fmod(euler.y, TAU)
func _process(delta: float) -> void:
if Input.is_action_pressed("camera_roll_counterclockwise"):
euler.z -= delta * sensitivity.z
if Input.is_action_pressed("camera_roll_clockwise"):
euler.z += delta * sensitivity.z
euler.z = clamp(euler.z, -TAU/4, TAU/4)
material.set_shader_param("rotation", Basis(euler))
shader_type canvas_item;
const float PI = 3.14159265358979;
const float TAU = PI * 2.0;
const float HALF_PI = PI / 2.0;
uniform mat3 rotation;
uniform float fov: hint_range(10, 90) = 90;
uniform float cylinder_height = 1;
void fragment() {
vec2 resolution = 1.0 / SCREEN_PIXEL_SIZE;
float theta = (PI / 180.0) * (fov / 2.0);
vec2 pixel = (SCREEN_UV * 2.0 - 1.0) * tan(theta);
pixel.y *= - resolution.y / resolution.x;
vec3 towards_pixel = rotation * vec3(pixel, -1);
float y_angle = atan(towards_pixel.x, towards_pixel.z);
float x_angle = atan(towards_pixel.y, length(towards_pixel.xz));
vec2 coords;
coords.x = mod(y_angle, TAU) / TAU;
coords.y = (tan(x_angle) + HALF_PI) / PI;
if (coords.y >= 0.0 && coords.y <= 1.0) {
COLOR = texture(TEXTURE, coords);
} else {
// Tiled "transparency" background
vec2 screen_pos = SCREEN_UV * resolution;
vec2 square = floor(screen_pos / 12.0);
if (mod(square.x + square.y, 2.0) > 0.5) {
COLOR.rgb = vec3(0.3)
} else {
COLOR.rgb = vec3(0.4);
}
}
}
shader_type canvas_item;
const float PI = 3.14159265358979;
const float TAU = PI * 2.0;
const float HALF_PI = PI / 2.0;
uniform mat3 rotation;
uniform float fov: hint_range(10, 90) = 90;
void fragment() {
vec2 resolution = 1.0 / SCREEN_PIXEL_SIZE;
float theta = (PI / 180.0) * (fov / 2.0);
vec2 pixel = (SCREEN_UV * 2.0 - 1.0) * tan(theta);
pixel.y *= - resolution.y / resolution.x;
vec3 towards_pixel = rotation * vec3(pixel, -1);
float y_angle = atan(towards_pixel.x, towards_pixel.z);
float x_angle = atan(towards_pixel.y, length(towards_pixel.xz));
vec2 coords;
coords.x = mod(y_angle, TAU) / TAU;
coords.y = (x_angle + HALF_PI) / PI;
COLOR = texture(TEXTURE, coords);
}
  1. Create a new scene with a Control
  2. Add a TextureRect with the source image
    1. Set the TextureRect layout to Full Rect
    2. Add the Camera.gd script to the TextureRect
    3. Add the perspective shader to the TextureRect
  3. Set the following actions in the project settings: camera_roll_clockwise, camera_roll_counterclockwise, camera_reset_roll
  4. Launch the scene and enjoy :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment