Files
CCUCoding/CSCI440/Project/Project_03.js
T

1060 lines
40 KiB
JavaScript

/*
================================================================================
Main JS File for the Project_03
--------------------------------------------------------------------------------
File layout (search for tags):
[SETUP] WebGL state and uniforms
[GEOM] Helpers for meshes (boxes, panels)
[ROOM] Build room, desk, CHAIR (chair now matches desk height)
[PAPER] Build right-half mesh and define fold sequence
[ANIM] Fold timeline & easing
[VIEW] Resize, camera, orbit controls
[RENDER]Draw everything; apply folds to paper only
[BOOT] Entry point
================================================================================
Most of this was generated by AI and then tweaked by myself. I used the AI to build frameworks especially for things such as the room building and the flight
behavior.
*/
"use strict";
/* ===== WebGL state ===== */
let gl, program;
/* ===== Uniform locations ===== */
let uModelLoc, uViewLoc, uProjLoc;
let uFoldCountLoc, uFoldAngleLoc, uFoldPointLoc, uFoldAxisLoc, uFoldNormalLoc, uFoldSideLoc, uFoldUseOrigSideLoc;
/* ===== Geometry VAOs ===== */
let VAOroom = null, roomCount = 0;
let VAOdesk = null, deskCount = 0;
let VAOpaperHalf = null, paperHalfCount = 0;
/* ===== Camera (orbit controls) ===== */
const TARGET = window.vec3(0.0, 0.86, -2.40); // paper center on the desk
const ORBIT = { radius: 1.6, minR: 0.5, maxR: 8.0, theta: -0.8, phi: 0.8, minPhi: -1.2, maxPhi: 1.2 };
const ORBIT_SENS = { rotate: 1.2, zoom: 0.25 };
const FOLLOW_OFFSET = { back: 1.5, up: -1.5, lookAhead: -0.6 }; // relative chase-cam offsets
let isDragging = false; let lastX = 0, lastY = 0;
const FLOOR_Y = 0.0;
const CEILING_Y = 2.4;
const WALL_LIMIT = { x: 2.85, z: 2.85 }; // keep motion inside 3x3 room shell
/* ===== Timeline ===== */
let startTime = 0;
let flightFrozen = false; // becomes true once we collide with a wall
let flightFreezeMatrix = null; // cached transform while frozen
/* ============================== Utilities ============================== */
/**
* Linear interpolation helper used by both the folds and the flight path.
* @param {number} a starting value
* @param {number} b ending value
* @param {number} t interpolation parameter in [0,1]
*/
function lerp(a, b, t) { return a + (b - a) * t; }
/** Convenience constructors so we can create literal vec2/vec3 values inline. */
function v2(x, y) { return [x, y]; }
function v3(x, y, z) { return [x, y, z]; }
/**
* Normalize a 2D vector; returns the zero vector unchanged to avoid NaNs.
*/
function norm2(v) { const m = Math.hypot(v[0], v[1]) || 1; return [v[0]/m, v[1]/m]; }
/** Returns a perpendicular 2D vector rotated 90 degrees counter-clockwise. */
function perp2(v) { return [-v[1], v[0]]; }
/**
* Symmetric smoothing curve used for both folding and flight eases.
* Gives an acceleration/deceleration feel rather than linear interpolation.
*/
function easeInOut(t){ return (t < 0.5) ? (2 * t * t) : (1 - Math.pow(-2 * t + 2, 2) / 2); }
/** Numeric clamp so we never overshoot animation ranges. */
function clamp(x, a, b){ return Math.max(a, Math.min(b, x)); }
/**
* Convert the spherical orbit parameters into a camera eye position that
* orbits the target point on the desk.
*/
function orbitToEye() {
const r = ORBIT.radius, th = ORBIT.theta, ph = ORBIT.phi;
const cp = Math.cos(ph), sp = Math.sin(ph);
const ct = Math.cos(th), st = Math.sin(th);
const offset = window.vec3(r * cp * st, r * sp, r * cp * ct);
return window.add(TARGET, offset);
}
/**
* Camera rig that hugs the airplane: stays behind it, slightly above, and
* mirrors its pitch so the viewer rides along during flight.
*/
function followCamera(now, planeMatrix, planeOrigin) {
if (!startTime) return null;
const flightElapsed = now - startTime;
const flightStart = totalFoldTimeMs + FLIGHT_PATH.delay;
const shouldFollow = flightFrozen || (flightElapsed > flightStart);
if (!shouldFollow) return null;
const pos = window.vec3(planeOrigin[0], planeOrigin[1], planeOrigin[2]);
const aheadPt = window.mult(planeMatrix, window.vec4(0,0,1,1));
const upPt = window.mult(planeMatrix, window.vec4(0,1,0,1));
let forward = window.subtract(window.vec3(aheadPt[0], aheadPt[1], aheadPt[2]), pos);
let up = window.subtract(window.vec3(upPt[0], upPt[1], upPt[2]), pos);
const forwardLen = Math.hypot(forward[0], forward[1], forward[2]);
const upLen = Math.hypot(up[0], up[1], up[2]);
if (forwardLen < 1e-3 || upLen < 1e-3) return null;
forward = window.normalize(forward);
up = window.normalize(up);
const eye = window.vec3(
pos[0] - forward[0] * FOLLOW_OFFSET.back + up[0] * FOLLOW_OFFSET.up,
pos[1] - forward[1] * FOLLOW_OFFSET.back + up[1] * FOLLOW_OFFSET.up,
pos[2] - forward[2] * FOLLOW_OFFSET.back + up[2] * FOLLOW_OFFSET.up,
);
const target = window.vec3(
pos[0] + forward[0] * FOLLOW_OFFSET.lookAhead,
pos[1] + forward[1] * FOLLOW_OFFSET.lookAhead,
pos[2] + forward[2] * FOLLOW_OFFSET.lookAhead,
);
return { eye, target, up };
}
/* [GEOM] Mesh helper functions
- pushQuad / pushBox*: build triangles with per-vertex colors
- transformVerts8: transforms a unit cube's 8 corners by matrix M
- pushPanel: thin "panel" aligned to X or Z (used for walls/trim)
*/
/* ======================= Helpers for building meshes ======================= */
const COLOR_PALLET = [
window.vec3(1.0, 0.0, 0.0), // 0 Red
window.vec3(0.0, 1.0, 0.0), // 1 Green
window.vec3(0.0, 0.0, 1.0), // 2 Blue
window.vec3(1.0, 0.0, 1.0), // 3 Magenta
window.vec3(1.0, 1.0, 0.0), // 4 Yellow
window.vec3(0.0, 1.0, 1.0), // 5 Cyan
window.vec3(0.6, 0.3, 0.0), // 6 Brown
window.vec3(0.1, 0.1, 0.1), // 7 Dark gray
window.vec3(1.0, 1.0, 1.0), // 8 White
window.vec3(0.85, 0.85, 0.85), // 9 Light gray
window.vec3(0.2, 0.2, 0.2), // 10 Charcoal
window.vec3(0.75, 0.6, 0.4), // 11 Wood light
window.vec3(0.35, 0.2, 0.1), // 12 Wood dark
window.vec3(0.9, 0.9, 0.95), // 13 Off-white
window.vec3(0.5, 0.2, 0.7), // 14 Purple
window.vec3(0.2, 0.6, 0.8), // 15 Teal
window.vec3(0.8, 0.4, 0.2), // 16 Orange
window.vec3(1.0, 0.55, 0.45), // 17 Coral
window.vec3(0.0, 0.75, 0.95), // 18 sky
];
// base primitives from the scene
const cubeCentered = [
window.vec4(-0.5, 0.5, -0.5, 1.0),
window.vec4( 0.5, 0.5, -0.5, 1.0),
window.vec4( 0.5, 0.5, 0.5, 1.0),
window.vec4(-0.5, 0.5, 0.5, 1.0),
window.vec4(-0.5, -0.5, -0.5, 1.0),
window.vec4( 0.5, -0.5, -0.5, 1.0),
window.vec4( 0.5, -0.5, 0.5, 1.0),
window.vec4(-0.5, -0.5, 0.5, 1.0),
];
/**
* Push a quad (two triangles) into the shared position/color arrays.
* Positions are vec4 values so they can be multiplied directly by mat4s later.
*/
function pushQuad(posArr, colArr, v0, v1, v2, v3, color) {
posArr.push(v0, v1, v2, v0, v2, v3);
for (let i = 0; i < 6; i++) colArr.push(color);
}
/** Apply a mat4 to a cube's eight corners; used when building scaled boxes. */
function transformVerts8(verts8, M) { const out = []; for (let v of verts8) out.push(window.mult(M, v)); return out; }
/** Emit a box mesh using pre-transformed corners. */
function pushBoxFrom8(posArr, colArr, v, color) {
const [a,b,c,d,e,f,g,h] = v;
pushQuad(posArr, colArr, a,b,c,d, color);
pushQuad(posArr, colArr, e,f,g,h, color);
pushQuad(posArr, colArr, a,d,h,e, color);
pushQuad(posArr, colArr, b,a,e,f, color);
pushQuad(posArr, colArr, c,b,f,g, color);
pushQuad(posArr, colArr, d,c,g,h, color);
}
/**
* Convenience wrapper: scale/translate the unit cube to create a box primitive.
*/
function pushBoxCentered(posArr, colArr, center, sx, sy, sz, color) {
const S = window.scale(sx, sy, sz);
const T = window.translate(center[0], center[1], center[2]);
const M = window.mult(T, S);
const vv = transformVerts8(cubeCentered, M);
pushBoxFrom8(posArr, colArr, vv, color);
}
/**
* Add a very thin box aligned along an axis, useful for walls and trim.
* `facing` picks whether the "length" is along X or Z.
*/
function pushPanel(posArr, colArr, center, sx, sy, facing, color) {
if (facing === "x") pushBoxCentered(posArr, colArr, center, 0.01, sy, sx, color);
else pushBoxCentered(posArr, colArr, center, sx, sy, 0.01, color);
}
/* ========================= Build ROOM + DESK ========================= */
/* [ROOM] Build the room shell, trim, window, door, and props.
Also builds the DESK (raised to realistic height) and a CHAIR whose
seat/back align with the new desk height.
Returns VAOs for room and desk geometry.
*/
/**
* Build all static room/desk/chair geometry and return the VAOs/counts.
* The room occupies +/-3 units, so we procedurally assemble walls, props,
* and trim, shoving every triangle into either the room VAO or desk VAO.
*/
function buildRoomAndDesk() {
const positionsRoom = [], colorsRoom = [];
const positionsDesk = [], colorsDesk = [];
// -------- Floor slab and baseboards --------
const floorY = -0.001, roomHalfX = 3.0, roomHalfZ = 3.0, wallH = 1.8;
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(0.0, floorY, 0.0), roomHalfX*2.0, 0.002, roomHalfZ*2.0, COLOR_PALLET[15]);
const baseH = 0.05, baseT = 0.02;
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(0.0, baseH/2.0, roomHalfZ-0.01), roomHalfX*2.0, baseH, baseT, COLOR_PALLET[9]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(0.0, baseH/2.0, -roomHalfZ+0.01), roomHalfX*2.0, baseH, baseT, COLOR_PALLET[9]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3( roomHalfX-0.01, baseH/2.0, 0.0), baseT, baseH, roomHalfZ*2.0, COLOR_PALLET[9]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(-roomHalfX+0.01, baseH/2.0, 0.0), baseT, baseH, roomHalfZ*2.0, COLOR_PALLET[9]);
// -------- Wall panels --------
pushPanel(positionsRoom, colorsRoom, window.vec3(0.0, wallH/2.0, roomHalfZ), roomHalfX*2.0, wallH, "z", COLOR_PALLET[9]);
pushPanel(positionsRoom, colorsRoom, window.vec3(0.0, wallH/2.0, -roomHalfZ), roomHalfX*2.0, wallH, "z", COLOR_PALLET[9]);
pushPanel(positionsRoom, colorsRoom, window.vec3( roomHalfX, wallH/2.0, 0.0), roomHalfZ*2.0, wallH, "x", COLOR_PALLET[9]);
pushPanel(positionsRoom, colorsRoom, window.vec3(-roomHalfX, wallH/2.0, 0.0), roomHalfZ*2.0, wallH, "x", COLOR_PALLET[9]);
// -------- Crown molding --------
const trimY = wallH - 0.02;
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(0.0, trimY, roomHalfZ - 0.02), roomHalfX*2.0, 0.015, 0.03, COLOR_PALLET[13]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(0.0, trimY, -roomHalfZ + 0.02), roomHalfX*2.0, 0.015, 0.03, COLOR_PALLET[13]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3( roomHalfX - 0.02, trimY, 0.0), 0.03, 0.015, roomHalfZ*2.0, COLOR_PALLET[13]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(-roomHalfX + 0.02, trimY, 0.0), 0.03, 0.015, roomHalfZ*2.0, COLOR_PALLET[13]);
// -------- Window frame, glass, and exterior accent --------
const winW = 1.2, winH = 0.7, winY = 1.1, winZ = roomHalfZ - 0.005;
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(0.0, winY, winZ), winW+0.05, winH+0.05, 0.03, COLOR_PALLET[11]);
pushPanel(positionsRoom, colorsRoom, window.vec3(0.0, winY, winZ - 0.015), winW, winH, "z", COLOR_PALLET[5]);
pushPanel(positionsRoom, colorsRoom, window.vec3(0.0, winY, winZ - 0.06), winW*1.2, winH*1.2, "z", COLOR_PALLET[18]);
// -------- Door slab, trim, and knob --------
const doorW = 0.8, doorH = 1.6, doorX = roomHalfX, doorY = doorH/2.0, doorZ = -1.2;
pushPanel(positionsRoom, colorsRoom, window.vec3(doorX - 0.004, doorY, doorZ), doorH, doorW, "x", COLOR_PALLET[13]);
pushPanel(positionsRoom, colorsRoom, window.vec3(doorX - 0.02, doorY, doorZ), doorH*0.98, doorW*0.98, "x", COLOR_PALLET[8]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(doorX - 0.04, 0.95, doorZ + doorW*0.3), 0.03, 0.03, 0.03, COLOR_PALLET[6]);
// -------- Rug, planter, lamp, and couch props --------
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(-0.0, 0.001, -2.25), 3.0, 0.002, 1.5, COLOR_PALLET[17]);
//Planter
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(-1.0, 0.15, -2.75), 0.4, 0.3, 0.4, COLOR_PALLET[10]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(-1.0, 0.2, -2.75), 0.32, 0.23, 0.32, COLOR_PALLET[11]);
//light
pushBoxCentered(positionsRoom, colorsRoom, window.vec3( 1.1, 0.02, -2.7), 0.22, 0.02, 0.22, COLOR_PALLET[10]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3( 1.1, 0.7, -2.7), 0.04, 1.36, 0.04, COLOR_PALLET[10]);
pushBoxCentered(positionsRoom, colorsRoom, window.vec3( 1.1, 1.35, -2.7), 0.35, 0.18, 0.35, COLOR_PALLET[13]);
//Couch
const couchCenter = window.vec3(0, 0.15, 2.7);
pushBoxCentered(positionsRoom, colorsRoom, couchCenter, 1.2, 0.3, 0.5, COLOR_PALLET[11]); // seat
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(couchCenter[0], couchCenter[1] + 0.25, couchCenter[2] + 0.15), 1.2, 0.4, 0.25, COLOR_PALLET[11]); // back
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(couchCenter[0] - 0.525, couchCenter[1] + 0.05, couchCenter[2]), 0.12, 0.4, 0.45, COLOR_PALLET[10]); // left arm
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(couchCenter[0] + 0.525, couchCenter[1] + 0.05, couchCenter[2]), 0.12, 0.4, 0.45, COLOR_PALLET[10]); // right arm
// -------- Chair: seat, back, and legs --------
// Chair seat (raised to match desk height): center y=0.48, size ~ 0.35x0.05x0.35
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(0.0, 0.48, -2.0), 0.35, 0.05, 0.35, COLOR_PALLET[6]);
// Chair back (bottom aligns with seat top): center y=0.68, height 0.40
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(0.0, 0.5, -1.85), 0.25, 0.5, 0.06, COLOR_PALLET[6]);
// Chair legs: extend from floor (y=0) up to seat bottom (~0.455).
// Height ~= 0.455, center y ~= 0.2275 (rounded to 0.228)
[window.vec3(-0.12, 0.228, -1.85), window.vec3(0.12, 0.228, -1.85),
window.vec3(-0.12, 0.228, -2.15), window.vec3(0.12, 0.228, -2.15)].forEach(c =>
pushBoxCentered(positionsRoom, colorsRoom, c, 0.04, 0.456, 0.04, COLOR_PALLET[7])
);
// -------- Floating shelves and books --------
const shelfX = -roomHalfX + 0.12;
[0.2,0.5,0.8,1.1].forEach(h =>
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(shelfX, h, -0.9), 0.22, 0.03, 0.48, COLOR_PALLET[6])
);
const bookYs = [0.22,0.52,0.82,1.12];
bookYs.forEach((h,i)=>{
for(let k=0;k<4;k++){
const z = -0.9 - 0.18 + k*0.12;
const col = [COLOR_PALLET[14], COLOR_PALLET[15], COLOR_PALLET[17]][(i+k)%3];
pushBoxCentered(positionsRoom, colorsRoom, window.vec3(shelfX, h+0.08, z), 0.06, 0.16, 0.08, col);
}
});
// -------- Desk top slab --------
// Desk (top + legs)
// Desk top (raised): top y=0.85, bottom y=0.75
const deskTop = [
window.vec4(-0.65, 0.85, -2.99, 1.0), window.vec4(-0.65, 0.85, -1.99, 1.0),
window.vec4( 0.65, 0.85, -1.99, 1.0), window.vec4( 0.65, 0.85, -2.99, 1.0),
window.vec4(-0.65, 0.75, -2.99, 1.0), window.vec4(-0.65, 0.75, -1.99, 1.0),
window.vec4( 0.65, 0.75, -1.99, 1.0), window.vec4( 0.65, 0.75, -2.99, 1.0),
];
const brown = COLOR_PALLET[6];
pushQuad(positionsDesk, colorsDesk, deskTop[0],deskTop[1],deskTop[2],deskTop[3], brown);
pushQuad(positionsDesk, colorsDesk, deskTop[4],deskTop[5],deskTop[6],deskTop[7], brown);
pushQuad(positionsDesk, colorsDesk, deskTop[0],deskTop[3],deskTop[7],deskTop[4], brown);
pushQuad(positionsDesk, colorsDesk, deskTop[1],deskTop[0],deskTop[4],deskTop[5], brown);
pushQuad(positionsDesk, colorsDesk, deskTop[2],deskTop[1],deskTop[5],deskTop[6], brown);
pushQuad(positionsDesk, colorsDesk, deskTop[3],deskTop[2],deskTop[6],deskTop[7], brown);
// Desk legs: centers at underside of desk (y=0.75) with vertical scale 0.75 so feet touch floor
const legCenters = [
window.vec3(-0.6, 0.75, -2.90), window.vec3(0.6, 0.75, -2.90),
window.vec3(-0.6, 0.75, -2.1), window.vec3(0.6, 0.75, -2.1),
];
legCenters.forEach((c)=>{
// Leg thickness and height (height=0.75 so bottom ~ 0.0 if center at 0.75)
const S = window.scale(0.05, 0.75, 0.05);
const T = window.translate(c[0], c[1], c[2]);
const M = window.mult(T, S);
const unit = [
window.vec4(-0.5, 0.0, -0.5, 1.0), window.vec4( 0.5, 0.0, -0.5, 1.0),
window.vec4( 0.5, 0.0, 0.5, 1.0), window.vec4(-0.5, 0.0, 0.5, 1.0),
window.vec4(-0.5, -1.0,-0.5, 1.0), window.vec4( 0.5, -1.0,-0.5, 1.0),
window.vec4( 0.5, -1.0, 0.5, 1.0), window.vec4(-0.5, -1.0, 0.5, 1.0),
];
pushBoxFrom8(positionsDesk, colorsDesk, transformVerts8(unit, M), brown);
});
// Make VAOs
function makeVAO(posArray, colArray) {
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const pbuf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pbuf);
gl.bufferData(gl.ARRAY_BUFFER, window.flatten(posArray), gl.STATIC_DRAW);
const aPosition = gl.getAttribLocation(program, "aPosition");
gl.enableVertexAttribArray(aPosition);
gl.vertexAttribPointer(aPosition, 4, gl.FLOAT, false, 0, 0);
const cbuf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cbuf);
gl.bufferData(gl.ARRAY_BUFFER, window.flatten(colArray), gl.STATIC_DRAW);
const aColor = gl.getAttribLocation(program, "aColor");
gl.enableVertexAttribArray(aColor);
gl.vertexAttribPointer(aColor, 3, gl.FLOAT, false, 0, 0);
gl.bindVertexArray(null);
return { vao, count: posArray.length };
}
const roomVAO = makeVAO(positionsRoom, colorsRoom);
const deskVAO = makeVAO(positionsDesk, colorsDesk);
return { roomVAO, deskVAO };
}
/* ==================== Build only the RIGHT HALF of paper ==================== */
/* [PAPER] Build only the RIGHT HALF of the paper as a tessellated rectangle.
We'll draw it twice: once as-is (right half), and once mirrored across X
to form the left half. The folding shader conditions which side rotates.
*/
/**
* Generate a triangulated grid for one half of the sheet of paper.
* This gives us fine enough tessellation to bend convincingly during folds.
*/
function createRightHalfGrid(cols = 36, rows = 24) {
const W = 0.35, H = 0.5;
const xC = 0.0, xR = +W * 0.5;
const yB = -H * 0.5, yT = +H * 0.5;
const xs = new Float32Array(cols + 1);
const ys = new Float32Array(rows + 1);
for (let i = 0; i <= cols; i++) xs[i] = lerp(xC, xR, i / cols);
for (let j = 0; j <= rows; j++) ys[j] = lerp(yB, yT, j / rows);
const positions = [], colors = [];
const pushTri = (ax, ay, bx, by, cx, cy) => {
positions.push(ax, ay, 0, bx, by, 0, cx, cy, 0);
for (let k = 0; k < 3; k++) colors.push(0.96, 0.96, 0.96);
};
for (let j = 0; j < rows; j++) {
for (let i = 0; i < cols; i++) {
const xA = xs[i], yA = ys[j];
const xB = xs[i+1], yB = ys[j];
const xC2 = xs[i+1], yC2 = ys[j+1];
const xD = xs[i], yD = ys[j+1];
pushTri(xA,yA, xB,yB, xC2,yC2);
pushTri(xA,yA, xC2,yC2, xD,yD);
}
}
paperHalfCount = positions.length / 3;
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
// Positions: provide full vec4 (x,y,z,1) so it matches aPosition
const pos4 = new Float32Array(paperHalfCount * 4);
for (let i=0,j=0;i<paperHalfCount;i++,j+=3){
const k = i*4; pos4[k]=positions[j]; pos4[k+1]=positions[j+1]; pos4[k+2]=positions[j+2]; pos4[k+3]=1.0;
}
const posBuf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, posBuf);
gl.bufferData(gl.ARRAY_BUFFER, pos4, gl.STATIC_DRAW);
const aPosition = gl.getAttribLocation(program, "aPosition");
gl.enableVertexAttribArray(aPosition);
gl.vertexAttribPointer(aPosition, 4, gl.FLOAT, false, 0, 0);
const colBuf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colBuf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
const aColor = gl.getAttribLocation(program, "aColor");
gl.enableVertexAttribArray(aColor);
gl.vertexAttribPointer(aColor, 3, gl.FLOAT, false, 0, 0);
gl.bindVertexArray(null);
return { W, H, vao };
}
/* ============ One-sided airplane folds (RIGHT half only) ============ */
const folds = [];
const foldDur = [];
let totalFoldTimeMs = 0;
const FLIGHT_PATH = {
delay: 600, // pause after final fold before launch (ms)
duration: 4000, // full travel time once airborne (ms)
maxHeight: 0.90, // lift above desk (target height)
forwardDistance: 3.0, // no glide section
lateralArc: 0.0,
bankAngle: 0.0,
pitchDown: 0
};
const CRUISE_PATH = {
duration: 4800,
turnRadius: 1.0,
dropHeight: 0.00,
bankAngle: 22,
pitchDown: -6,
yawTurn: 140,
turnDirection: 1
};
const CRUISE_PATH2 = {
duration: 4200,
turnRadius: 0.7,
dropHeight: 0.05,
bankAngle: 28,
pitchDown: -4,
yawTurn: 80,
turnDirection: 1
};
const LANDING_PATH = {
duration: 4000,
forward: -2.0,
lateral: 0.6,
dropHeight: 0.05,
bankAngle: 28,
pitchDown: -10,
yawTurn: 80,
};
/* [PAPER] Define the fold sequence (times and crease lines) for a one-sided
airplane construction. The left side is mirrored in the render pass.
Folds (in order):
1) Top-right corner towards centerline (diagonal crease) ~ 180° close
2) Fuse along the centerline x=0 (bring right over left) ~ 90°
3) Wing crease at x = W*0.25 (fold wing down) ~ -0.95 rad
*/
/**
* Author the sequence of folds that constructs the paper airplane.
* Each entry declares the crease line, which side is mobile, and the angle.
*/
function buildRightFolds(W, H) {
folds.length = 0; foldDur.length = 0;
// 1) Top-right corner -> centerline diagonal
{
const a = v2(0, H/2), b = v2(+W/2, 0);
const axisDir = norm2([b[0]-a[0], b[1]-a[1]]);
const n = norm2(perp2(axisDir));
// Fold 1: diagonal corner toward centerline (~90°)
folds.push({ p: v3(a[0], a[1], 0), axis: v3(axisDir[0], axisDir[1], 0), normal: v2(n[0], n[1]), side: +1, target: Math.PI, useOrig: true });
foldDur.push(1400);
}
// 2) Fuse along x = 0 (centerline)
{
// Fold 2: fuse along centerline x=0 (~90°)
folds.push({ p: v3(0,0,0), axis: v3(0,1,0), normal: v2(1,0), side: +1, target: Math.PI/2, useOrig: true });
foldDur.push(1400);
}
// 3) Right wing crease at x = +W*0.25, fold down
{
const wingX = W*0.25;
// Fold 3: wing crease down (~60°)
folds.push({ p: v3(+wingX,0,0), axis: v3(0,1,0), normal: v2(1,0), side: +1, target: -Math.PI/3, useOrig: true });
foldDur.push(1200);
}
totalFoldTimeMs = foldDur.reduce((sum, dur) => sum + dur, 0);
}
/* ==================== Animate folds sequentially ==================== */
/* [ANIM] Time -> fold angles mapping.
- Each fold has a duration; we ease between 0 and its target angle.
- Earlier folds "finish" before later ones begin to move.
*/
/**
* Given the absolute animation time, compute the current fold angle array.
* Returns a typed array so we can upload it directly as uniforms.
*/
function foldProgress(nowMs) {
const tAbs = nowMs - startTime;
const n = folds.length;
const ang = new Float32Array(n);
let t = tAbs;
for (let i = 0; i < n; i++) {
const d = foldDur[i] || 1000;
if (t <= 0) { ang[i] = 0; break; }
const seg = Math.min(1, t / d);
ang[i] = (0.001) + easeInOut(seg) * folds[i].target;
t -= d;
if (seg < 1) {
for (let j=i+1;j<n;j++) ang[j]=0;
break;
}
}
return ang;
}
/* =========================== Paper flight path =========================== */
/*
Design:
- We keep your phase functions for *horizontal* motion + orientation:
• launchPhaseState -> forward only
• cruisePhaseState -> arcs around the room
• landingPhaseState -> final approach vector
- Vertical motion (Y) is computed by ONE function:
globalHeightForFlightTime(flightTimeMs)
- That height is then injected into the composed state after all
horizontal states are combined, so there are no more per-phase
inconsistencies and the plane only descends after the first cruise.
Timeline:
T0 = 0 at the moment flight begins (after folds + delay).
Let:
L = FLIGHT_PATH.duration (launch)
C1 = CRUISE_PATH.duration (first cruise)
C2 = CRUISE_PATH2.duration (second cruise)
D = LANDING_PATH.duration (landing)
Total flight time:
Ttotal = L + C1 + C2 + D
Height curve:
• Segment 1 (0 -> L): climb from desk to maxHeight (easeInOut)
• Segment 2 (L -> L+C1): stay near maxHeight (slight variance)
• Segment 3 (L+C1 -> L+C1+C2): gentle descent
• Segment 4 (rest): stronger descent to floor height
*/
function launchPhaseState(progress) {
const p = clamp(progress, 0, 1);
// Horizontal: simple forward motion during launch
const forward = lerp(0, FLIGHT_PATH.forwardDistance, p);
return {
pos: [0, 0, forward], // y handled globally
pitch: FLIGHT_PATH.pitchDown * p,
bank: FLIGHT_PATH.bankAngle * p,
yaw: 0,
};
}
function cruisePhaseState(config, timeMs) {
const v = clamp(timeMs / Math.max(1, config.duration), 0, 1);
const angle = v * Math.PI; // half circle
const radius = config.turnRadius;
const dir = config.turnDirection || 1;
const forward = radius * Math.sin(angle);
const lateral = dir * radius * (1 - Math.cos(angle));
const pitch = config.pitchDown * v;
const bank = Math.sin(angle) * config.bankAngle * dir;
const yaw = config.yawTurn * v * dir;
return {
pos: [lateral, 0, forward], // y handled globally
pitch,
bank,
yaw,
};
}
function landingPhaseState(timeMs) {
const v = clamp(timeMs / Math.max(1, LANDING_PATH.duration), 0, 1);
const t = easeInOut(v);
const forward = t * LANDING_PATH.forward;
const lateral = t * LANDING_PATH.lateral;
const pitch = LANDING_PATH.pitchDown * v;
const bank = LANDING_PATH.bankAngle * Math.sin(v * Math.PI);
const yaw = LANDING_PATH.yawTurn * v;
return {
pos: [lateral, 0, forward], // y handled globally
pitch,
bank,
yaw,
};
}
/* -------- Global vertical motion (height over entire flight) -------- */
/**
* Evaluate the master vertical spline for the full flight timeline.
* Input is absolute flight time (after folds), output is world-space Y.
*/
function globalHeightForFlightTime(flightTimeMs) {
const L = FLIGHT_PATH.duration;
const C1 = CRUISE_PATH.duration;
const C2 = CRUISE_PATH2.duration;
const D = LANDING_PATH.duration;
const Ttotal = L + C1 + C2 + D;
const t = clamp(flightTimeMs / Ttotal, 0, 1);
const Hmax = FLIGHT_PATH.maxHeight; // peak height above desk
const Hdesk = 0.0; // "landing" reference (floor plane)
// Breakpoints in normalized time
const tL = L / Ttotal; // end of launch
const tC1 = (L + C1) / Ttotal; // end of first cruise
const tC2 = (L + C1 + C2) / Ttotal; // end of second cruise
let h; // normalized height 0..1 (scaled by Hmax at the end)
if (t <= tL) {
// 1) Launch: climb from desk to max
const u = clamp(t / tL, 0, 1);
h = easeInOut(u);
} else if (t <= tC1) {
// 2) First cruise: keep essentially level near the top
const u = clamp((t - tL) / (tC1 - tL), 0, 1);
const wobble = 0.03 * Math.sin(u * Math.PI * 2.0); // tiny variation
h = 1.0 + wobble;
} else if (t <= tC2) {
// 3) Second cruise: gentle descent from 1.0 -> 0.6
const u = clamp((t - tC1) / (tC2 - tC1), 0, 1);
h = lerp(1.0, 0.6, easeInOut(u));
} else {
// 4) Landing: stronger descent from ~0.6 -> 0.02
const u = clamp((t - tC2) / (1.0 - tC2), 0, 1);
h = lerp(0.6, 0.02, easeInOut(u));
}
// Map normalized height to world space; add floor offset if needed.
return Hdesk + Hmax * h;
}
/* --------------- Transform composition helpers --------------- */
/**
* Convert a pose (translation + Euler angles) into a model matrix.
* Yaw is applied first so later pitch/bank feel correct in world space.
*/
function composeTransform(state) {
let M = window.mat4();
M = window.mult(window.translate(state.pos[0], state.pos[1], state.pos[2]), M);
M = window.mult(window.rotate(state.yaw || 0, window.vec3(0,1,0)), M);
M = window.mult(window.rotate(state.pitch || 0, window.vec3(1,0,0)), M);
M = window.mult(window.rotate(state.bank || 0, window.vec3(0,0,1)), M);
return M;
}
/**
* Stitch a new local segment onto the accumulated pose from the prior phase.
* Only yaw reorients the translation so pitch/bank don't create fake climb.
*/
function combineStates(base, delta) {
const yawRad = (base.yaw || 0) * Math.PI / 180;
const cosY = Math.cos(yawRad), sinY = Math.sin(yawRad);
const dx = delta.pos[0], dz = delta.pos[2];
const rotX = dx * cosY - dz * sinY;
const rotZ = dx * sinY + dz * cosY;
return {
pos: [
base.pos[0] + rotX,
base.pos[1] + delta.pos[1], // y is still 0 in all locals
base.pos[2] + rotZ,
],
pitch: base.pitch + delta.pitch,
bank: base.bank + delta.bank,
yaw: (base.yaw || 0) + (delta.yaw || 0),
};
}
/* ---------------------- Master time → model matrix ---------------------- */
/**
* Master controller: map the wall-clock timestamp to the plane's transform.
* Handles phase switching, global height injection, and landing clamping.
*/
function paperFlightMatrix(nowMs) {
if (!startTime) return window.mat4();
const elapsed = nowMs - startTime;
const launchStart = totalFoldTimeMs + FLIGHT_PATH.delay;
// Still folding / waiting
if (elapsed <= launchStart) return window.mat4();
const flightTime = elapsed - launchStart;
const L = FLIGHT_PATH.duration;
const C1 = CRUISE_PATH.duration;
const C2 = CRUISE_PATH2.duration;
const D = LANDING_PATH.duration;
const Ttotal = L + C1 + C2 + D;
// Clamp to end of path (plane stays on floor after landing)
const clampedTime = Math.min(flightTime, Ttotal);
// Global height for this time
const globalY = globalHeightForFlightTime(clampedTime);
// ---- Phase 1: launch ----
const launchDur = L;
const launchProgress = clamp(clampedTime / launchDur, 0, 1);
const launchState = launchPhaseState(launchProgress);
if (clampedTime <= launchDur) {
// Inject global height and return
launchState.pos[1] = globalY;
return composeTransform(launchState);
}
// ---- Phase 2: first cruise ----
let phaseTime = clampedTime - launchDur;
const cruiseLocal1 = cruisePhaseState(CRUISE_PATH, phaseTime);
const cruiseState1 = combineStates(
{ pos: launchState.pos.slice(), pitch: launchState.pitch, bank: launchState.bank, yaw: launchState.yaw },
cruiseLocal1
);
if (phaseTime <= C1) {
cruiseState1.pos[1] = globalY;
return composeTransform(cruiseState1);
}
// ---- Phase 3: second cruise ----
phaseTime -= C1;
const cruiseLocal2 = cruisePhaseState(CRUISE_PATH2, phaseTime);
const cruiseState2 = combineStates(cruiseState1, cruiseLocal2);
if (phaseTime <= C2) {
cruiseState2.pos[1] = globalY;
return composeTransform(cruiseState2);
}
// ---- Phase 4: landing ----
const landingTime = phaseTime - C2;
const landingLocal = landingPhaseState(landingTime);
const landingState = combineStates(cruiseState2, landingLocal);
// Final snap: if we've exhausted Ttotal, pin to floor height
if (flightTime >= Ttotal) {
landingState.pos[1] = 0.0; // floor plane; adjust if your desk offset differs
} else {
landingState.pos[1] = globalY;
}
return composeTransform(landingState);
}
/* ============================= Resize / View ============================ */
/* [VIEW] Maintain correct viewport and perspective on resize. */
/**
* Keep the canvas/device pixel ratio in sync so the projection stays crisp.
* Called whenever the window resizes and once during init.
*/
function resizeCanvas() {
const canvas = gl.canvas;
const dpr = window.devicePixelRatio || 1;
const w = Math.floor(canvas.clientWidth * dpr);
const h = Math.floor(canvas.clientHeight * dpr);
if (canvas.width !== w || canvas.height !== h) {
canvas.width = w; canvas.height = h;
gl.viewport(0, 0, w, h);
if (program) {
const aspect = w / h;
const P = window.perspective(45.0, aspect, 0.01, 30.0);
gl.useProgram(program);
gl.uniformMatrix4fv(uProjLoc, false, window.flatten(P));
}
}
}
/* ====================== Orbit control handlers ========================= */
/* [VIEW] Simple orbit controls:
- Drag to rotate (theta, phi)
- Wheel to zoom (radius)
- Double-click to reset camera pose
* Wire mouse input to the orbit parameters:
* - drag => theta/phi
* - wheel => radius
* - double-click => reset pose
*/
function attachOrbitControls(canvas) {
canvas.addEventListener("mousedown", (e) => {
if (e.button !== 0) return;
isDragging = true; lastX = e.clientX; lastY = e.clientY; e.preventDefault();
});
window.addEventListener("mousemove", (e) => {
if (!isDragging) return;
const dx = e.clientX - lastX, dy = e.clientY - lastY;
lastX = e.clientX; lastY = e.clientY;
const w = gl.canvas.clientWidth, h = gl.canvas.clientHeight;
const dim = Math.max(1, Math.min(w, h));
ORBIT.theta += (dx / dim) * Math.PI * ORBIT_SENS.rotate;
ORBIT.phi += (dy / dim) * Math.PI * ORBIT_SENS.rotate * -1;
ORBIT.phi = clamp(ORBIT.phi, ORBIT.minPhi, ORBIT.maxPhi);
});
window.addEventListener("mouseup", () => { isDragging = false; });
canvas.addEventListener("wheel", (e) => {
const delta = Math.sign(e.deltaY);
const factor = Math.exp(delta * ORBIT_SENS.zoom);
ORBIT.radius = clamp(ORBIT.radius * factor, ORBIT.minR, ORBIT.maxR);
e.preventDefault();
}, { passive: false });
canvas.addEventListener("dblclick", () => { ORBIT.radius = 3.0; ORBIT.theta = 0.0; ORBIT.phi = 0.30; });
}
/* ================================ Init ================================ */
/* [BOOT] Initialize GL, compile shaders, build geometry, seed animation. */
/**
* Application entry point. Creates the WebGL2 context, compiles shaders,
* builds geometry/folds, hooks up controls, and begins the render loop.
*/
function init() {
const canvas = document.getElementById("gl-canvas");
canvas.style.width = "100vw"; canvas.style.height = "100vh";
gl = canvas.getContext("webgl2", { antialias: true });
if (!gl) { alert("WebGL 2.0 not available"); return; }
resizeCanvas();
window.addEventListener("resize", resizeCanvas);
attachOrbitControls(canvas);
gl.clearColor(0.6, 0.75, 0.95, 1.0); // soft background
gl.enable(gl.DEPTH_TEST);
gl.disable(gl.CULL_FACE);
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
// Matrix uniforms
uModelLoc = gl.getUniformLocation(program, "uModel");
uViewLoc = gl.getUniformLocation(program, "uView");
uProjLoc = gl.getUniformLocation(program, "uProj");
// Multi-fold uniforms
uFoldCountLoc = gl.getUniformLocation(program, "uFoldCount");
uFoldAngleLoc = gl.getUniformLocation(program, "uFoldAngle");
uFoldPointLoc = gl.getUniformLocation(program, "uFoldPoint");
uFoldAxisLoc = gl.getUniformLocation(program, "uFoldAxis");
uFoldNormalLoc = gl.getUniformLocation(program, "uFoldNormal");
uFoldSideLoc = gl.getUniformLocation(program, "uFoldSide");
uFoldUseOrigSideLoc = gl.getUniformLocation(program, "uFoldUseOrigSide");
// Projection / initial View
const aspect = gl.canvas.width / gl.canvas.height;
const P = window.perspective(45.0, aspect, 0.01, 30.0);
gl.uniformMatrix4fv(uProjLoc, false, window.flatten(P));
// Build scene VAOs
const { roomVAO, deskVAO } = buildRoomAndDesk();
VAOroom = roomVAO.vao; roomCount = roomVAO.count;
VAOdesk = deskVAO.vao; deskCount = deskVAO.count;
// Build paper (right half)
const { W, H, vao } = createRightHalfGrid(36, 24);
VAOpaperHalf = vao;
buildRightFolds(W, H);
startTime = performance.now();
requestAnimationFrame(render);
}
/* ============================ Fold uniforms ============================ */
/* [ANIM] Upload fold parameters for the current frame to the shader. */
/**
* Push the current fold descriptor arrays into the shader uniforms.
* This is called every frame once the fold animation starts.
*/
function uploadFoldUniforms(angles) {
const MAX_FOLDS = 8; // must match shader
const count = Math.min(folds.length, MAX_FOLDS);
const points = new Float32Array(MAX_FOLDS * 3);
const axes = new Float32Array(MAX_FOLDS * 3);
const normals = new Float32Array(MAX_FOLDS * 2);
const sides = new Int32Array(MAX_FOLDS);
const useOrigI = new Int32Array(MAX_FOLDS);
for (let i = 0; i < count; i++) {
const f = folds[i];
points.set(f.p, i * 3);
axes.set( f.axis, i * 3);
normals.set(f.normal, i * 2);
sides[i] = f.side | 0;
useOrigI[i] = f.useOrig ? 1 : 0;
}
const angFull = new Float32Array(MAX_FOLDS);
for (let i=0;i<count;i++) angFull[i] = angles[i] || 0;
gl.uniform1i(uFoldCountLoc, count);
gl.uniform1fv(uFoldAngleLoc, angFull);
gl.uniform3fv(uFoldPointLoc, points);
gl.uniform3fv(uFoldAxisLoc, axes);
gl.uniform2fv(uFoldNormalLoc, normals);
gl.uniform1iv(uFoldSideLoc, sides);
gl.uniform1iv(uFoldUseOrigSideLoc, useOrigI);
}
/* ================================ Render ================================ */
/* [RENDER] Draw order:
1) Room (no folds)
2) Desk (no folds)
3) Paper right-half (folds on)
4) Paper left-half (mirrored, folds on)
*/
/**
* Main frame function: update fold/flight state and draw the entire scene.
* Uses requestAnimationFrame timestamps so animation stays in real time.
*/
function render(now) {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// place paper onto desk: y ~ 0.111, z center ~ -2.40
const Tdesk = window.translate(0.0, 0.86, -2.40);
const Rx = window.rotate(-90, window.vec3(1,0,0));
const basePaper = window.mult(Tdesk, Rx);
let Mpaper;
let worldOrigin;
// Once the plane hits a wall we freeze the matrix so the camera + geometry
// stays parked; otherwise recompute from the live timeline.
if (flightFrozen && flightFreezeMatrix) {
Mpaper = flightFreezeMatrix;
worldOrigin = window.mult(Mpaper, window.vec4(0,0,0,1));
} else {
Mpaper = window.mult(paperFlightMatrix(now), basePaper);
worldOrigin = window.mult(Mpaper, window.vec4(0,0,0,1));
if (worldOrigin[1] < FLOOR_Y) {
const lift = window.translate(0, FLOOR_Y - worldOrigin[1], 0);
Mpaper = window.mult(lift, Mpaper);
worldOrigin[1] = FLOOR_Y;
} else if (worldOrigin[1] > CEILING_Y) {
const drop = window.translate(0, CEILING_Y - worldOrigin[1], 0);
Mpaper = window.mult(drop, Mpaper);
worldOrigin[1] = CEILING_Y;
}
let hitWall = false; // track whether we need to freeze motion this frame
if (worldOrigin[0] < -WALL_LIMIT.x) {
const push = window.translate(-WALL_LIMIT.x - worldOrigin[0], 0, 0);
Mpaper = window.mult(push, Mpaper);
worldOrigin[0] = -WALL_LIMIT.x;
hitWall = true;
} else if (worldOrigin[0] > WALL_LIMIT.x) {
const pull = window.translate(WALL_LIMIT.x - worldOrigin[0], 0, 0);
Mpaper = window.mult(pull, Mpaper);
worldOrigin[0] = WALL_LIMIT.x;
hitWall = true;
}
if (worldOrigin[2] < -WALL_LIMIT.z) {
const push = window.translate(0, 0, -WALL_LIMIT.z - worldOrigin[2]);
Mpaper = window.mult(push, Mpaper);
worldOrigin[2] = -WALL_LIMIT.z;
hitWall = true;
} else if (worldOrigin[2] > WALL_LIMIT.z) {
const pull = window.translate(0, 0, WALL_LIMIT.z - worldOrigin[2]);
Mpaper = window.mult(pull, Mpaper);
worldOrigin[2] = WALL_LIMIT.z;
hitWall = true;
}
if (hitWall) {
flightFrozen = true;
flightFreezeMatrix = Mpaper;
}
}
// Camera: try follow mode, fallback to orbit controls
const followRig = followCamera(now, Mpaper, worldOrigin);
const eye = followRig ? followRig.eye : orbitToEye();
const target = followRig ? followRig.target : TARGET;
const upVec = followRig ? followRig.up : window.vec3(0,1,0);
const V = window.lookAt(eye, target, upVec);
gl.uniformMatrix4fv(uViewLoc, false, window.flatten(V));
// ---------- Draw room (no folds) ----------
gl.uniform1i(uFoldCountLoc, 0); // disable folding
gl.bindVertexArray(VAOroom);
gl.uniformMatrix4fv(uModelLoc, false, window.flatten(window.mat4()));
gl.drawArrays(gl.TRIANGLES, 0, roomCount);
// ---------- Draw desk (no folds) ----------
gl.bindVertexArray(VAOdesk);
gl.uniformMatrix4fv(uModelLoc, false, window.flatten(window.mat4()));
gl.drawArrays(gl.TRIANGLES, 0, deskCount);
// ---------- Draw paper (folding, two passes: right half, then mirrored left) ----------
const angles = foldProgress(now);
uploadFoldUniforms(angles);
gl.bindVertexArray(VAOpaperHalf);
// PASS 1: right half
gl.uniformMatrix4fv(uModelLoc, false, window.flatten(Mpaper));
gl.drawArrays(gl.TRIANGLES, 0, paperHalfCount);
// PASS 2: mirrored left half
const Sx = window.scale(-1, 1, 1);
const Mleft = window.mult(Mpaper, Sx);
gl.uniformMatrix4fv(uModelLoc, false, window.flatten(Mleft));
gl.drawArrays(gl.TRIANGLES, 0, paperHalfCount);
gl.bindVertexArray(null);
requestAnimationFrame(render);
}
/* ============================== Boot strap ============================= */
window.addEventListener("DOMContentLoaded", init);