- Create a new scene with a
Control
- Add a
TextureRect
with the source image- Set the
TextureRect
layout toFull Rect
- Add the
Camera.gd
script to theTextureRect
- Add the perspective shader to the
TextureRect
- Set the
- Set the following actions in the project settings:
camera_roll_clockwise
,camera_roll_counterclockwise
,camera_reset_roll
- Launch the scene and enjoy :)
Last active
September 23, 2024 20:37
-
-
Save Garmelon/670071cbbe43d42f2d4ad0c389864cd3 to your computer and use it in GitHub Desktop.
Godot perspecive projection shaders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment