156 lines
3.1 KiB
Plaintext
156 lines
3.1 KiB
Plaintext
// Simple Cube Demo for lance3d
|
|
// Usage: cell run examples/cube.ce
|
|
|
|
var time_mod = use('time')
|
|
var lance3d = use('core')
|
|
var math = use('math/radians')
|
|
|
|
// Camera orbit state
|
|
var cam_distance = 5
|
|
var cam_yaw = 0
|
|
var cam_pitch = 0.4
|
|
var auto_rotate = true
|
|
|
|
// Models and materials
|
|
var cube = null
|
|
var cube_mat = null
|
|
var ground = null
|
|
var ground_mat = null
|
|
|
|
// Timing
|
|
var last_time = 0
|
|
|
|
function _init() {
|
|
log.console("lance3d Cube Demo")
|
|
|
|
// Initialize lance3d with PS1 style
|
|
lance3d.set_style("ps1")
|
|
|
|
// Create a cube mesh
|
|
cube = lance3d.make_cube(1, 1, 1)
|
|
cube_mat = {
|
|
color_map: null,
|
|
paint: [0.8, 0.3, 0.2, 1],
|
|
coverage: "opaque",
|
|
face: "single",
|
|
lamp: "lit"
|
|
}
|
|
|
|
// Create ground plane
|
|
ground = lance3d.make_plane(10, 10)
|
|
ground_mat = {
|
|
color_map: null,
|
|
paint: [0.3, 0.5, 0.3, 1],
|
|
coverage: "opaque",
|
|
face: "single",
|
|
lamp: "lit"
|
|
}
|
|
|
|
// Set up lighting
|
|
lance3d.set_lighting({
|
|
sun_dir: [0.5, 1.0, 0.3],
|
|
sun_color: [1.0, 0.95, 0.9],
|
|
ambient: [0.3, 0.3, 0.35]
|
|
})
|
|
|
|
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 (lance3d._state.keys_held['a']) {
|
|
cam_yaw -= 2.0 * dt
|
|
auto_rotate = false
|
|
}
|
|
if (lance3d._state.keys_held['d']) {
|
|
cam_yaw += 2.0 * dt
|
|
auto_rotate = false
|
|
}
|
|
if (lance3d._state.keys_held['w']) {
|
|
cam_pitch += 2.0 * dt
|
|
if (cam_pitch > 1.5) cam_pitch = 1.5
|
|
}
|
|
if (lance3d._state.keys_held['s']) {
|
|
cam_pitch -= 2.0 * dt
|
|
if (cam_pitch < -1.5) cam_pitch = -1.5
|
|
}
|
|
|
|
// Toggle auto-rotate
|
|
if (lance3d._state.keys_pressed['space']) {
|
|
auto_rotate = !auto_rotate
|
|
}
|
|
|
|
// Exit on escape
|
|
if (lance3d._state.keys_held['escape']) {
|
|
$_.stop()
|
|
}
|
|
}
|
|
|
|
function _draw() {
|
|
// Clear
|
|
lance3d.clear(0.1, 0.05, 0.15, 1.0)
|
|
|
|
// Set up camera
|
|
lance3d.camera_perspective(60, 0.1, 100)
|
|
|
|
// Calculate camera position from orbit
|
|
var cam_x = math.sine(cam_yaw) * math.cosine(cam_pitch) * cam_distance
|
|
var cam_y = math.sine(cam_pitch) * cam_distance + 0.5
|
|
var cam_z = math.cosine(cam_yaw) * math.cosine(cam_pitch) * cam_distance
|
|
|
|
lance3d.camera_look_at(cam_x, cam_y, cam_z, 0, 0.5, 0)
|
|
|
|
// Draw the cube with transform
|
|
var cube_transform = lance3d.translation_matrix(0, 0.5, 0)
|
|
lance3d.draw_mesh(cube, cube_transform, cube_mat)
|
|
|
|
// Draw ground plane
|
|
lance3d.draw_mesh(ground, null, ground_mat)
|
|
}
|
|
|
|
function frame() {
|
|
// Begin frame
|
|
lance3d._begin_frame()
|
|
|
|
// Process events
|
|
if (!lance3d._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
|
|
lance3d._end_frame()
|
|
|
|
// Schedule next frame
|
|
$_.delay(frame, 1/60)
|
|
}
|
|
|
|
// Start
|
|
_init()
|