OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / assets / ocean / ocean_globals.gd

30 lines · gd ref:
class_name OceanGlobals
## Registers the ocean shader's `global uniform`s at RUNTIME.
##
## Godot resolves a `global uniform` against Project Settings -> Shader Globals,
## which live in project.godot -- the file the editor rewrites and strips
## comments from, and which we now guard. Registering here instead keeps the
## ocean out of that file entirely, and means the shader cannot compile against
## a half-configured project: whoever uses it calls ensure() first.
##
## Names and types must match assets/ocean/shaders/ocean.gdshader exactly; a
## mismatch surfaces as "Global uniform '<x>' does not exist" at compile time.
static func ensure() -> void:
	_add(&'water_color', RenderingServer.GLOBAL_VAR_TYPE_COLOR, Color(0.1, 0.15, 0.18))
	_add(&'foam_color', RenderingServer.GLOBAL_VAR_TYPE_COLOR, Color(0.73, 0.67, 0.62))
	_add(&'num_cascades', RenderingServer.GLOBAL_VAR_TYPE_UINT, 0)
	_add(&'displacements', RenderingServer.GLOBAL_VAR_TYPE_SAMPLER2DARRAY, null)
	_add(&'normals', RenderingServer.GLOBAL_VAR_TYPE_SAMPLER2DARRAY, null)

static var _registered := {}

static func _add(name: StringName, type: int, default) -> void:
	# ⚠ NOT global_shader_parameter_get/_get_list to test for existence. Both
	# are EDITOR-ONLY and log "This function should never be used outside the
	# editor, it can severely damage performance." on every call at runtime.
	# Tracking it here is the only cheap way to stay idempotent.
	if _registered.has(name):
		return
	_registered[name] = true
	RenderingServer.global_shader_parameter_add(name, type, default)