OverviewFilesCommitsIssuesDocsReleasesPeers

proc-nodes / demo / glsl_emit_probe.gd

52 lines · gd ref:
## Dump the GLSL a graph compiles to, which is what dc-terrain actually runs.
##
## `compile_to_glsl_function` is not a debug aid: it is the terrain's runtime
## path. dc-terrain calls it once, wraps the result in its density shader and
## compiles that, so a map's field is a REAL SHADER rather than a graph walked
## per sample. What this probe exists to show is the other half of that: the
## emitter SKIPS any node whose supports_gpu() is false and, if nothing is left,
## returns a flat plane -- silently, either way.
##
## Usage: godot --headless --path demo --script res://glsl_emit_probe.gd -- out.txt
extends SceneTree

func _emit(label: String, g: ProcNodeGraph, out: Array) -> void:
	var glsl: String = g.compile_to_glsl_function("proc_density", true, PackedStringArray())
	out.append("=== %s (%d nodes)" % [label, g.get_node_count()])
	out.append(glsl)

func _init() -> void:
	var a := OS.get_cmdline_user_args()
	var out := []

	# 1. The shipped erosion map's graph, node for node.
	var g1 := ProcNodeGraph.new()
	var n1 := g1.add_node(GradientNoiseNode.new())
	var e1 := g1.add_node(ErosionNoiseNode.new())
	g1.connect_sockets(n1, 0, e1, 7)
	g1.connect_sockets(n1, 1, e1, 8)
	_emit("map_dc_erosion: GradientNoise -> ErosionNoise", g1, out)

	# 2. The same, with a Math node between them -- the ordinary way a bridged
	#    graph scales a value, and one of the nine nodes whose GLSL is off.
	var g2 := ProcNodeGraph.new()
	var n2 := g2.add_node(GradientNoiseNode.new())
	var m2 := g2.add_node(MathNode.new())
	var e2 := g2.add_node(ErosionNoiseNode.new())
	g2.connect_sockets(n2, 0, m2, 0)
	g2.connect_sockets(m2, 0, e2, 7)
	g2.connect_sockets(n2, 1, e2, 8)
	_emit("with a Math node in the chain", g2, out)

	# 3. A graph made only of nodes whose GLSL is off.
	var g3 := ProcNodeGraph.new()
	g3.add_node(NoiseTextureNode.new())
	g3.add_node(MathNode.new())
	_emit("only GPU-off nodes", g3, out)

	var text := "\n".join(PackedStringArray(out))
	if a.size() > 0:
		FileAccess.open(a[0], FileAccess.WRITE).store_string(text)
	print(text)
	quit()