Skip to content

Commit

Permalink
Merge pull request #1 from calvinchd/shader-calc-fov
Browse files Browse the repository at this point in the history
Calculate camera vertical FOV within shader
  • Loading branch information
tiffany352 authored Dec 14, 2024
2 parents bc7a4cc + 66a15f4 commit c8254c7
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 19 deletions.
4 changes: 1 addition & 3 deletions addons/starlight/Star.gdshader
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ uniform float meters_per_lightyear = 100.0;
uniform float color_gamma : hint_range(0.0, 10.0) = 1.0;
uniform bool clamp_output = false;

// This should really be a godot shader builtin.
uniform float camera_vertical_fov = 70;

// PSF cropping related uniforms:
uniform float min_size_ratio : hint_range(0, 1.0) = 1.0;
uniform float max_luminosity;
Expand Down Expand Up @@ -57,6 +54,7 @@ void vertex() {
// Do actual perspective projection.
vec4 projected = PROJECTION_MATRIX * vec4(modelview_pos, 1.0);
// Figure out the scaling to use in clip space so that we can measure in degrees instead of device pixels.
float camera_vertical_fov = degrees(atan(-1.0 / PROJECTION_MATRIX[1][1]) * 2.0);
vec2 one_degree = vec2(1.0, VIEWPORT_SIZE.x / VIEWPORT_SIZE.y) / camera_vertical_fov;
// Offset the projected position to form the quad.
POSITION = projected + vec4(VERTEX.xy * projected.w * size * one_degree, 0.0, 0.0);
Expand Down
17 changes: 1 addition & 16 deletions addons/starlight/StarManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,13 @@ static func blackbody_to_rgb(kelvin):

var material: ShaderMaterial
var mesh: MultiMesh
# Hide these parameters because they're set by StarManager:
var internal_shader_params = {
'camera_vertical_fov': true
}


# This forwards the shader parameters, which would otherwise be inaccessible because the Material
# is generated at runtime.
func _get_property_list():
var props = []
var shader_params := RenderingServer.get_shader_parameter_list(shader.get_rid())
for p in shader_params:
if internal_shader_params.has(p.name):
continue
var cp = {}
for k in p:
cp[k] = p[k]
Expand Down Expand Up @@ -185,12 +178,4 @@ func set_star_list(star_list: Array[Star]):


func _process(_delta):
var camera = get_viewport().get_camera_3d()
var fov = 70
# The camera can be null if the scene doesn't have one set.
# This value will also not match the editor camera.
if camera:
fov = camera.fov

material.shader = shader
material.set_shader_parameter('camera_vertical_fov', fov)
pass

0 comments on commit c8254c7

Please sign in to comment.