Files
retro3d/examples/modelview.ce
2025-12-13 00:46:00 -06:00

196 lines
4.4 KiB
Plaintext

// Model Viewer for retro3d
// Usage: cell run examples/modelview.ce <model_path> [style]
// style: ps1, n64, or saturn (default: ps1)
var io = use('fd')
var time_mod = use('time')
var retro3d = use('core')
// Parse command line arguments
var model_path = args[0] || "Duck.glb"
var style = args[1] || "ps1"
// Camera orbit state
var cam_distance = 5
var cam_yaw = 0
var cam_pitch = 0.3
var cam_target_y = 0
var orbit_speed = 2.0
var zoom_speed = 0.5
// Model and transform
var model = null
var transform = null
// Timing
var last_time = 0
function _init() {
log.console("retro3d Model Viewer")
log.console("Style: " + style)
log.console("Loading: " + model_path)
// Initialize retro3d with selected style
retro3d.set_style(style)
// Load the model
model = retro3d.load_model(model_path)
if (!model) {
log.console("Error: Could not load model: " + model_path)
$_.stop()
return
}
log.console("Model loaded with " + text(model.meshes.length) + " mesh(es)")
log.console(" Nodes: " + text(model.nodes.length))
log.console(" Textures: " + text(model.textures.length))
// Create transform for the model (this will be an extra parent transform)
transform = retro3d.make_transform()
// 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: [1, 1, 1, 1]
})
retro3d.set_material(mat)
last_time = time_mod.number()
log.console("")
log.console("Controls:")
log.console(" WASD - Orbit camera")
log.console(" Q/E - Zoom in/out")
log.console(" R/F - Move target up/down")
log.console(" ESC - Exit")
// Start the main loop
frame()
}
function _update(dt) {
// Handle input for camera orbit
if (retro3d._state.keys_held['a']) {
cam_yaw -= orbit_speed * dt
}
if (retro3d._state.keys_held['d']) {
cam_yaw += orbit_speed * dt
}
if (retro3d._state.keys_held['w']) {
cam_pitch += orbit_speed * dt
if (cam_pitch > 1.5) cam_pitch = 1.5
}
if (retro3d._state.keys_held['s']) {
cam_pitch -= orbit_speed * dt
if (cam_pitch < -1.5) cam_pitch = -1.5
}
// Zoom
if (retro3d._state.keys_held['q']) {
cam_distance -= zoom_speed * dt * cam_distance
if (cam_distance < 0.5) cam_distance = 0.5
}
if (retro3d._state.keys_held['e']) {
cam_distance += zoom_speed * dt * cam_distance
if (cam_distance > 100) cam_distance = 100
}
// Move target up/down
if (retro3d._state.keys_held['r']) {
cam_target_y += zoom_speed * dt
}
if (retro3d._state.keys_held['f']) {
cam_target_y -= zoom_speed * dt
}
// Exit on escape
if (retro3d._state.keys_held['escape']) {
$_.stop()
}
}
function _draw() {
// Clear with a nice gradient-ish color based on style
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 + cam_target_y
var cam_z = Math.cos(cam_yaw) * Math.cos(cam_pitch) * cam_distance
retro3d.camera_look_at(cam_x, cam_y, cam_z, 0, cam_target_y, 0)
// Draw the model
if (model) {
retro3d.draw_model(model, transform)
}
// Draw a ground grid using immediate mode
retro3d.push_state()
var grid_mat = retro3d.make_material("unlit", {
color: [0.3, 0.3, 0.3, 1]
})
retro3d.set_material(grid_mat)
retro3d.begin_lines()
retro3d.color(0.3, 1, 0.3, 1)
var grid_size = 10
var grid_step = 1
for (var i = -grid_size; i <= grid_size; i += grid_step) {
// X lines
retro3d.vertex(i, 0, -grid_size)
retro3d.vertex(i, 0, grid_size)
// Z lines
retro3d.vertex(-grid_size, 0, i)
retro3d.vertex(grid_size, 0, i)
}
retro3d.end()
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()