Shader spatial

Uma esfera cujos vértices surfam uma onda enquanto a emissão pulsa, escrita num shader spatial.

O editor acima está rodando este projeto. Mude uma linha e ele recarrega.

Classes do Godot usadas

MeshInstance3DShaderMaterialWorldEnvironment

O código

scripts/main.gd
extends Node

var sphere: MeshInstance3D


func _ready() -> void:
	_add_environment()
	_add_light()

	var mesh: SphereMesh = SphereMesh.new()
	mesh.radius = 1.6
	mesh.height = 3.2
	mesh.radial_segments = 96
	mesh.rings = 48

	var material: ShaderMaterial = ShaderMaterial.new()
	material.shader = Get.shader('pulse')

	sphere = MeshInstance3D.new()
	sphere.mesh = mesh
	sphere.material_override = material
	add_child(sphere)

	var camera: Camera3D = Camera3D.new()
	camera.position = Vector3(0, 1.5, 5.5)
	add_child(camera)
	camera.look_at(Vector3.ZERO, Vector3.UP)
	camera.current = true

	print('open pulse.gdshader and change the uniforms, the project reloads on save')


func _process(delta: float) -> void:
	sphere.rotate_y(delta * 0.5)


func _add_environment() -> void:
	var sky_material: ProceduralSkyMaterial = ProceduralSkyMaterial.new()
	sky_material.sky_top_color = Color(0.03, 0.04, 0.08)
	sky_material.sky_horizon_color = Color(0.1, 0.12, 0.2)
	sky_material.ground_bottom_color = Color(0.01, 0.01, 0.03)
	sky_material.ground_horizon_color = Color(0.07, 0.08, 0.13)

	var sky: Sky = Sky.new()
	sky.sky_material = sky_material

	var env: Environment = Environment.new()
	env.background_mode = Environment.BG_SKY
	env.sky = sky
	env.ambient_light_source = Environment.AMBIENT_SOURCE_SKY
	env.ambient_light_energy = 0.6
	env.glow_enabled = true

	var world: WorldEnvironment = WorldEnvironment.new()
	world.environment = env
	add_child(world)


func _add_light() -> void:
	var light: DirectionalLight3D = DirectionalLight3D.new()
	light.rotation_degrees = Vector3(-45, -40, 0)
	light.light_energy = 1.0
	add_child(light)
shaders/pulse.gdshader
shader_type spatial;

uniform vec3 base_color: source_color = vec3(0.1, 0.16, 0.4);
uniform vec3 glow_color: source_color = vec3(0.0, 0.6, 1.0);
uniform float wave_height = 0.14;
uniform float wave_bands = 9.0;
uniform float speed = 1.6;

void vertex() {
	float wave = sin(VERTEX.y * wave_bands + TIME * speed);
	VERTEX += NORMAL * wave * wave_height;
}

void fragment() {
	float pulse = 0.5 + 0.5 * sin(VERTEX.y * wave_bands + TIME * speed);

	ALBEDO = base_color;
	ROUGHNESS = 0.25;
	METALLIC = 0.4;
	EMISSION = glow_color * pulse * 1.5;
}

Mais exemplos de Shaders