Files

86 lines
2.7 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Paper Airplane in Room — Combined</title>
<style>
html, body { margin:0; height:100%; background:#222; overflow:hidden; }
#gl-canvas { width:100vw; height:100vh; display:block; }
</style>
<!-- Vertex Shader: supports multi-fold uniforms; we set count=0 for non-paper geometry -->
<script id="vertex-shader" type="x-shader/x-vertex">
#version 300 es
precision highp float;
in vec4 aPosition;
in vec3 aColor;
uniform mat4 uModel;
uniform mat4 uView;
uniform mat4 uProj;
const int MAX_FOLDS = 8;
uniform int uFoldCount;
uniform float uFoldAngle[MAX_FOLDS]; // radians per fold
uniform vec3 uFoldPoint[MAX_FOLDS]; // a point on each crease (object space)
uniform vec3 uFoldAxis[MAX_FOLDS]; // unit axis along each crease (object space)
uniform vec2 uFoldNormal[MAX_FOLDS]; // 2D normal in XY for side test
uniform int uFoldSide[MAX_FOLDS]; // +1 or -1: which side rotates
uniform bool uFoldUseOrigSide[MAX_FOLDS]; // if true, side test uses ORIGINAL coords
out vec3 vColor;
vec3 rotateAroundAxis(vec3 p, vec3 axis, float ang) {
float c = cos(ang);
float s = sin(ang);
return p * c + cross(axis, p) * s + axis * dot(axis, p) * (1.0 - c);
}
void main() {
vec3 pos = aPosition.xyz; // working (possibly folded) position
vec2 origXY = aPosition.xy; // cached original XY for optional tests
// Apply folding only if uFoldCount > 0 (we'll set it to 0 for room/desk)
for (int i = 0; i < MAX_FOLDS; ++i) {
if (i >= uFoldCount) break;
float ang = uFoldAngle[i];
if (abs(ang) < 1e-6) continue;
vec2 testXY = uFoldUseOrigSide[i] ? origXY : pos.xy; // side test space
float side = dot(testXY - uFoldPoint[i].xy, uFoldNormal[i]);
if (float(uFoldSide[i]) * side > 0.0) {
vec3 axis = normalize(uFoldAxis[i]);
vec3 q = pos - uFoldPoint[i];
q = rotateAroundAxis(q, axis, ang);
pos = q + uFoldPoint[i];
}
}
vColor = aColor;
gl_Position = uProj * uView * uModel * vec4(pos, 1.0);
}
</script>
<!-- Fragment Shader -->
<script id="fragment-shader" type="x-shader/x-fragment">
#version 300 es
precision highp float;
in vec3 vColor;
out vec4 fragColor;
void main() { fragColor = vec4(vColor, 1.0); }
</script>
</head>
<body>
<canvas id="gl-canvas"></canvas>
<!-- Course helpers (must load before app) -->
<script type="text/javascript" src="../Common/MVnew.js"></script>
<script type="text/javascript" src="../Common/initShaders.js"></script>
<!-- App -->
<script type="text/javascript" src="./Project_03.js"></script>
</body>
</html>