Rejilla en onda
Una rejilla de 196 cubos montados en una onda senoidal. Dos bucles anidados al montarla, una línea de matemáticas por fotograma.
El editor de arriba está ejecutando este proyecto. Cambia una línea y se recarga.
Clases de Godot usadas
MeshInstance3DBoxMeshCamera3D
El código
scripts/main.gd
extends Node
const GRID: int = 14
const SPACING: float = 0.9
const AMPLITUDE: float = 1.2
var cubes: Array[MeshInstance3D] = []
var elapsed: float = 0.0
func _ready() -> void:
_add_environment()
_add_light()
var mesh: BoxMesh = BoxMesh.new()
mesh.size = Vector3(0.6, 0.6, 0.6)
var offset: float = (GRID - 1) * SPACING * 0.5
for x in GRID:
var material: StandardMaterial3D = StandardMaterial3D.new()
material.albedo_color = Color.from_hsv(float(x) / GRID * 0.4 + 0.5, 0.5, 0.95)
material.roughness = 0.35
for z in GRID:
var cube: MeshInstance3D = MeshInstance3D.new()
cube.mesh = mesh
cube.material_override = material
cube.position = Vector3(x * SPACING - offset, 0, z * SPACING - offset)
add_child(cube)
cubes.append(cube)
var camera: Camera3D = Camera3D.new()
camera.position = Vector3(0, 8, 13)
add_child(camera)
camera.look_at(Vector3.ZERO, Vector3.UP)
camera.current = true
func _process(delta: float) -> void:
elapsed += delta
for cube in cubes:
var wave: float = sin(cube.position.x * 0.8 + elapsed * 2.0) * cos(cube.position.z * 0.8 + elapsed * 1.4)
cube.position.y = wave * AMPLITUDE
func _add_environment() -> void:
var sky_material: ProceduralSkyMaterial = ProceduralSkyMaterial.new()
sky_material.sky_top_color = Color(0.04, 0.05, 0.1)
sky_material.sky_horizon_color = Color(0.12, 0.14, 0.22)
sky_material.ground_bottom_color = Color(0.02, 0.02, 0.04)
sky_material.ground_horizon_color = Color(0.09, 0.1, 0.15)
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.9
var world: WorldEnvironment = WorldEnvironment.new()
world.environment = env
add_child(world)
func _add_light() -> void:
var light: DirectionalLight3D = DirectionalLight3D.new()
light.rotation_degrees = Vector3(-60, -30, 0)
light.light_energy = 1.1
add_child(light)