Tour de primitivas

Las seis mallas primitivas en un anillo que gira, cada una con su propio material: metal, rugoso y emisivo.

El editor de arriba está ejecutando este proyecto. Cambia una línea y se recarga.

Clases de Godot usadas

MeshInstance3DStandardMaterial3DDirectionalLight3D

El código

scripts/main.gd
extends Node

const RING_RADIUS: float = 3.2

var pivot: Node3D


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

	var meshes: Array[Mesh] = [
		BoxMesh.new(),
		SphereMesh.new(),
		CylinderMesh.new(),
		TorusMesh.new(),
		PrismMesh.new(),
		CapsuleMesh.new(),
	]

	pivot = Node3D.new()
	add_child(pivot)

	for i in meshes.size():
		var angle: float = TAU * i / meshes.size()

		var item: MeshInstance3D = MeshInstance3D.new()
		item.mesh = meshes[i]
		item.material_override = _make_material(i)
		item.position = Vector3(cos(angle), 0, sin(angle)) * RING_RADIUS
		pivot.add_child(item)

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


func _process(delta: float) -> void:
	pivot.rotate_y(delta * 0.4)

	for item in pivot.get_children():
		item.rotate_x(delta * 0.7)


func _make_material(index: int) -> StandardMaterial3D:
	var material: StandardMaterial3D = StandardMaterial3D.new()
	material.albedo_color = Color.from_hsv(float(index) / 6.0, 0.45, 0.9)
	material.metallic = 0.8 if index % 2 == 0 else 0.0
	material.roughness = 0.15 if index % 2 == 0 else 0.7

	if index == 3:
		material.emission_enabled = true
		material.emission = Color('#8da5f3')
		material.emission_energy_multiplier = 1.5

	return material


func _add_environment() -> void:
	var sky_material: ProceduralSkyMaterial = ProceduralSkyMaterial.new()
	sky_material.sky_top_color = Color(0.05, 0.06, 0.12)
	sky_material.sky_horizon_color = Color(0.14, 0.16, 0.24)
	sky_material.ground_bottom_color = Color(0.02, 0.02, 0.04)
	sky_material.ground_horizon_color = Color(0.1, 0.11, 0.16)

	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.8

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


func _add_light() -> void:
	var light: DirectionalLight3D = DirectionalLight3D.new()
	light.rotation_degrees = Vector3(-50, -40, 0)
	light.light_energy = 1.3
	add_child(light)

Más ejemplos de Escena 3D