Files
retro3d/examples/cube.ce
2025-12-12 17:27:12 -06:00

164 lines
3.4 KiB
Plaintext

// Simple Cube Demo for retro3d
// Usage: cell run examples/cube.ce [style]
// style: ps1, n64, or saturn (default: ps1)
var time_mod = use('time')
var retro3d = use('core')
// Parse command line arguments
var args = $_.args || []
var style = args[0] || "ps1"
// Camera orbit state
var cam_distance = 5
var cam_yaw = 0
var cam_pitch = 0.4
var auto_rotate = true
// Model and transform
var cube = null
var transform = null
// Timing
var last_time = 0
function _init() {
log.console("retro3d Cube Demo")
log.console("Style: " + style)
// Initialize retro3d with selected style
retro3d.set_style(style)
// Create a cube
cube = retro3d.make_cube(1, 1, 1)
// Create transform for the cube
transform = retro3d.make_transform()
transform.y = 0.5
// Set up lighting
retro3d.set_ambient(0.3, 0.3, 0.35)
retro3d.set_light_dir(0.5, 1.0, 0.3, 1.0, 0.95, 0.9, 1.0)
// Set up a default material
var mat = retro3d.make_material("lit", {
color: [0.8, 0.3, 0.2, 1]
})
retro3d.set_material(mat)
last_time = time_mod.number()
log.console("")
log.console("Controls:")
log.console(" WASD - Orbit camera")
log.console(" Space - Toggle auto-rotate")
log.console(" ESC - Exit")
// Start the main loop
frame()
}
function _update(dt) {
// Auto rotate
if (auto_rotate) {
cam_yaw += 0.5 * dt
}
// Handle input for camera orbit
if (retro3d._state.keys_held['a']) {
cam_yaw -= 2.0 * dt
auto_rotate = false
}
if (retro3d._state.keys_held['d']) {
cam_yaw += 2.0 * dt
auto_rotate = false
}
if (retro3d._state.keys_held['w']) {
cam_pitch += 2.0 * dt
if (cam_pitch > 1.5) cam_pitch = 1.5
}
if (retro3d._state.keys_held['s']) {
cam_pitch -= 2.0 * dt
if (cam_pitch < -1.5) cam_pitch = -1.5
}
// Toggle auto-rotate
if (retro3d._state.keys_pressed[' ']) {
auto_rotate = !auto_rotate
}
// Exit on escape
if (retro3d._state.keys_held['escape']) {
$_.stop()
}
}
function _draw() {
// Clear with style-appropriate color
if (style == "ps1") {
retro3d.clear(0.1, 0.05, 0.15, 1.0)
} else if (style == "n64") {
retro3d.clear(0.0, 0.1, 0.2, 1.0)
} else {
retro3d.clear(0.05, 0.05, 0.1, 1.0)
}
// Set up camera
retro3d.camera_perspective(60, 0.1, 100)
// Calculate camera position from orbit
var cam_x = Math.sin(cam_yaw) * Math.cos(cam_pitch) * cam_distance
var cam_y = Math.sin(cam_pitch) * cam_distance + 0.5
var cam_z = Math.cos(cam_yaw) * Math.cos(cam_pitch) * cam_distance
retro3d.camera_look_at(cam_x, cam_y, cam_z, 0, 0.5, 0)
// Draw the cube
retro3d.draw_model(cube, transform)
// Draw ground plane
retro3d.push_state()
var ground_mat = retro3d.make_material("lit", {
color: [0.3, 0.5, 0.3, 1]
})
retro3d.set_material(ground_mat)
var ground = retro3d.make_plane(10, 10)
var ground_transform = retro3d.make_transform()
retro3d.draw_model(ground, ground_transform)
retro3d.pop_state()
}
function frame() {
// Begin frame
retro3d._begin_frame()
// Process events
if (!retro3d._process_events()) {
log.console("Exiting...")
$_.stop()
return
}
// Calculate delta time
var now = time_mod.number()
var dt = now - last_time
last_time = now
// Update
_update(dt)
// Draw
_draw()
// End frame (submit GPU commands)
retro3d._end_frame()
// Schedule next frame
$_.delay(frame, 1/60)
}
// Start
_init()