Cubos de gelatina

Três cubos SoftBody3D caindo com rigidez diferente. É a própria malha que deforma.

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

Classes do Godot usadas

SoftBody3DBoxMeshStaticBody3D

O código

scripts/main.gd
extends Node

const CUBE_SIZE: float = 1.6


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

	for i in 3:
		_add_jelly(Vector3(-2.2 + i * 2.2, 2.6 + i * 0.6, 0), 0.4 + i * 0.25)

	var camera: Camera3D = Camera3D.new()
	camera.position = Vector3(0, 2.4, 6)
	add_child(camera)
	camera.look_at(Vector3(0, 0.8, 0), Vector3.UP)
	camera.current = true

	print('three SoftBody3D cubes, left is the softest')


func _add_jelly(at: Vector3, stiffness: float) -> void:
	var mesh: BoxMesh = BoxMesh.new()
	mesh.size = Vector3(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)
	mesh.subdivide_width = 3
	mesh.subdivide_height = 3
	mesh.subdivide_depth = 3

	var material: StandardMaterial3D = StandardMaterial3D.new()
	material.albedo_color = Color.from_hsv(0.45 + stiffness * 0.3, 0.5, 0.95)
	material.roughness = 0.25

	var jelly: SoftBody3D = SoftBody3D.new()
	jelly.mesh = mesh
	jelly.material_override = material
	jelly.position = at
	jelly.total_mass = 2.0
	# a low simulation precision lets the cube collapse or fall through the floor
	jelly.simulation_precision = 10
	jelly.linear_stiffness = stiffness
	jelly.pressure_coefficient = 140.0
	jelly.damping_coefficient = 0.05
	add_child(jelly)


func _add_ground() -> void:
	var size: Vector3 = Vector3(20, 1, 20)

	var mesh: BoxMesh = BoxMesh.new()
	mesh.size = size

	var material: StandardMaterial3D = StandardMaterial3D.new()
	material.albedo_color = Color('#2a2f3d')

	var visual: MeshInstance3D = MeshInstance3D.new()
	visual.mesh = mesh
	visual.material_override = material

	var shape: BoxShape3D = BoxShape3D.new()
	shape.size = size

	var collision: CollisionShape3D = CollisionShape3D.new()
	collision.shape = shape

	var ground: StaticBody3D = StaticBody3D.new()
	ground.position = Vector3(0, -0.5, 0)
	ground.add_child(visual)
	ground.add_child(collision)
	add_child(ground)


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, -30, 0)
	light.light_energy = 1.2
	light.shadow_enabled = true
	add_child(light)

Mais exemplos de Física