-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze3d.html
More file actions
310 lines (278 loc) · 11.8 KB
/
maze3d.html
File metadata and controls
310 lines (278 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Maze 3D Viewer</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #0a0a12; font-family: 'Courier New', monospace; overflow: hidden; }
canvas { display: block; }
#ui {
position: fixed; top: 16px; left: 16px;
background: rgba(10,10,20,0.85);
border: 1px solid rgba(255,255,255,0.12);
border-radius: 8px;
padding: 14px 18px;
color: #ccc;
font-size: 12px;
line-height: 1.8;
backdrop-filter: blur(6px);
}
#ui h3 { color: #fff; margin-bottom: 6px; font-size: 13px; letter-spacing: 0.08em; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
#info {
position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
background: rgba(10,10,20,0.7);
border: 1px solid rgba(255,255,255,0.1);
border-radius: 20px;
padding: 8px 20px;
color: rgba(255,255,255,0.5);
font-size: 11px;
letter-spacing: 0.06em;
pointer-events: none;
}
</style>
</head>
<body>
<div id="ui">
<h3>LEGEND</h3>
<div><span class="dot" style="background:#8ca0b8"></span>W = wall</div>
<div><span class="dot" style="background:#4a7c59"></span>. = ground</div>
<div><span class="dot" style="background:#d4822a"></span>B = berry bush</div>
<div><span class="dot" style="background:#6b8f3e"></span>P = plain bush</div>
<div><span class="dot" style="background:#e8d44d"></span>e = edible bug</div>
<div><span class="dot" style="background:#c0392b"></span>X = poison bug</div>
<div><span class="dot" style="background:#4fc3f7"></span>A = agent start</div>
</div>
<div id="info">drag to orbit · scroll to zoom · right-drag to pan</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// ─── MAZE DATA ────────────────────────────────────────────────────────────────
const RAW = [
"WWWWWWWWWWW",
"W...B.W.B.W",
"W.WW..WW..W",
"W.........W",
"W.P.W...WWW",
"W...WA....W",
"WWW.W.W...W",
"W.......WWW",
"W.WW..W...W",
"W...e.W.X.W",
"WWWWWWWWWWW"
];
const ROWS = RAW.length;
const COLS = RAW[0].length;
// ─── SCENE SETUP ─────────────────────────────────────────────────────────────
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0d0d1a);
scene.fog = new THREE.Fog(0x0d0d1a, 18, 35);
const camera = new THREE.PerspectiveCamera(52, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.set(5, 14, -4);
camera.lookAt(5, 0, 5);
// ─── LIGHTS ──────────────────────────────────────────────────────────────────
const ambient = new THREE.AmbientLight(0x334466, 0.6);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xffeedd, 1.4);
sun.position.set(8, 18, -4);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 50;
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.bias = -0.001;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x6688cc, 0.35);
fill.position.set(-5, 8, 12);
scene.add(fill);
// Subtle point light for mood
const mood = new THREE.PointLight(0xff9944, 0.8, 12);
mood.position.set(5, 3, 5);
scene.add(mood);
// ─── MATERIALS ───────────────────────────────────────────────────────────────
function mat(color, rough=0.85, metal=0.0, emissive=0x000000, emissiveIntensity=0) {
return new THREE.MeshStandardMaterial({ color, roughness: rough, metalness: metal, emissive, emissiveIntensity });
}
const M = {
ground: mat(0x3a5c3a, 0.95),
wall: mat(0x7a8fa8, 0.7, 0.1),
wallTop: mat(0x95aac0, 0.6, 0.15),
berryB: mat(0xb85c1a, 0.6), // warm orange-brown trunk
berryTop: mat(0xff6b35, 0.4, 0.0, 0xff4400, 0.3), // glowing berry
plainB: mat(0x4a7030, 0.8),
bugE: mat(0xe8d44d, 0.3, 0.4, 0xffee00, 0.6),
bugX: mat(0xc0392b, 0.3, 0.3, 0xff0000, 0.5),
agent: mat(0x4fc3f7, 0.2, 0.7, 0x00aaff, 0.4),
floor: mat(0x2a3a2a, 0.95),
};
// ─── GEOMETRY HELPERS ────────────────────────────────────────────────────────
function addBox(x, y, z, w, h, d, material, castShadow=true, receiveShadow=true) {
const geo = new THREE.BoxGeometry(w, h, d);
const mesh = new THREE.Mesh(geo, material);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
scene.add(mesh);
return mesh;
}
function addCylinder(x, y, z, rt, rb, h, material) {
const geo = new THREE.CylinderGeometry(rt, rb, h, 10);
const mesh = new THREE.Mesh(geo, material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
scene.add(mesh);
return mesh;
}
function addSphere(x, y, z, r, material) {
const geo = new THREE.SphereGeometry(r, 14, 10);
const mesh = new THREE.Mesh(geo, material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
scene.add(mesh);
return mesh;
}
// ─── GROUND PLANE ────────────────────────────────────────────────────────────
const groundGeo = new THREE.PlaneGeometry(COLS, ROWS);
const groundMesh = new THREE.Mesh(groundGeo, M.floor);
groundMesh.rotation.x = -Math.PI / 2;
groundMesh.position.set(COLS / 2 - 0.5, 0, ROWS / 2 - 0.5);
groundMesh.receiveShadow = true;
scene.add(groundMesh);
// Grid lines on floor
const gridHelper = new THREE.GridHelper(Math.max(COLS, ROWS), Math.max(COLS, ROWS), 0x334433, 0x223322);
gridHelper.position.set(COLS/2 - 0.5, 0.01, ROWS/2 - 0.5);
scene.add(gridHelper);
// ─── BUILD MAZE ──────────────────────────────────────────────────────────────
const bugMeshes = [];
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const ch = RAW[row][col];
const cx = col; // X = column
const cz = row; // Z = row
const cy = 0;
if (ch === 'W') {
// Wall: slightly varied heights for visual interest
const h = 1.0 + (((col * 7 + row * 13) % 5) * 0.04);
addBox(cx, h/2, cz, 0.98, h, 0.98, M.wall);
// cap
addBox(cx, h + 0.04, cz, 1.02, 0.08, 1.02, M.wallTop);
} else {
// Floor tile (slight variation)
const tileH = 0.04;
const gv = ((col + row) % 2 === 0) ? 0x2e4a2e : 0x334a33;
const tileMat = mat(gv, 0.95);
addBox(cx, -tileH/2, cz, 0.99, tileH, 0.99, tileMat, false, true);
if (ch === 'B') {
// Berry bush: trunk + glowing berry cluster
addCylinder(cx, 0.4, cz, 0.12, 0.18, 0.8, M.berryB);
addSphere(cx, 1.0, cz, 0.32, M.berryTop);
// smaller berry accents
addSphere(cx + 0.18, 0.9, cz + 0.1, 0.14, M.berryTop);
addSphere(cx - 0.14, 0.85, cz - 0.15, 0.12, M.berryTop);
} else if (ch === 'P') {
// Plain bush: just green dome
addCylinder(cx, 0.3, cz, 0.1, 0.15, 0.6, M.plainB);
addSphere(cx, 0.85, cz, 0.3, mat(0x3d6e22, 0.85));
} else if (ch === 'e') {
// Edible bug: glowing yellow sphere + legs suggestion
const bugMesh = addSphere(cx, 0.18, cz, 0.16, M.bugE);
bugMeshes.push({ mesh: bugMesh, type: 'e', baseY: 0.18, cx, cz });
} else if (ch === 'X') {
// Poison bug: glowing red sphere
const bugMesh = addSphere(cx, 0.18, cz, 0.16, M.bugX);
bugMeshes.push({ mesh: bugMesh, type: 'X', baseY: 0.18, cx, cz });
} else if (ch === 'A') {
// Agent start marker: glowing pillar + sphere
addCylinder(cx, 0.5, cz, 0.08, 0.12, 1.0, mat(0x226688, 0.4, 0.6));
addSphere(cx, 1.1, cz, 0.2, M.agent);
// Ring on floor
const ringGeo = new THREE.RingGeometry(0.35, 0.45, 24);
const ringMesh = new THREE.Mesh(ringGeo, mat(0x4fc3f7, 0.3, 0.5, 0x00aaff, 0.8));
ringMesh.rotation.x = -Math.PI / 2;
ringMesh.position.set(cx, 0.02, cz);
scene.add(ringMesh);
}
}
}
}
// ─── ORBIT CONTROLS (manual implementation) ──────────────────────────────────
let isDragging = false, isRightDrag = false;
let prevMouse = { x: 0, y: 0 };
let spherical = { theta: -0.4, phi: 0.85, radius: 18 };
let panOffset = new THREE.Vector3(5, 0, 5);
function updateCamera() {
const x = spherical.radius * Math.sin(spherical.phi) * Math.sin(spherical.theta);
const y = spherical.radius * Math.cos(spherical.phi);
const z = spherical.radius * Math.sin(spherical.phi) * Math.cos(spherical.theta);
camera.position.set(
panOffset.x + x,
panOffset.y + y,
panOffset.z + z
);
camera.lookAt(panOffset.x, panOffset.y, panOffset.z);
}
updateCamera();
renderer.domElement.addEventListener('mousedown', e => {
isDragging = true;
isRightDrag = e.button === 2;
prevMouse = { x: e.clientX, y: e.clientY };
});
renderer.domElement.addEventListener('contextmenu', e => e.preventDefault());
window.addEventListener('mouseup', () => isDragging = false);
window.addEventListener('mousemove', e => {
if (!isDragging) return;
const dx = e.clientX - prevMouse.x;
const dy = e.clientY - prevMouse.y;
prevMouse = { x: e.clientX, y: e.clientY };
if (isRightDrag) {
const right = new THREE.Vector3();
const up = new THREE.Vector3(0, 1, 0);
right.crossVectors(camera.getWorldDirection(new THREE.Vector3()), up).normalize();
panOffset.addScaledVector(right, -dx * 0.02);
panOffset.y += dy * 0.02;
} else {
spherical.theta -= dx * 0.008;
spherical.phi = Math.max(0.15, Math.min(1.4, spherical.phi - dy * 0.008));
}
updateCamera();
});
renderer.domElement.addEventListener('wheel', e => {
spherical.radius = Math.max(4, Math.min(35, spherical.radius + e.deltaY * 0.02));
updateCamera();
});
// ─── ANIMATION LOOP ──────────────────────────────────────────────────────────
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const t = clock.getElapsedTime();
// Animate bugs (hover + spin)
bugMeshes.forEach(({ mesh, type, baseY, cx, cz }, i) => {
const phase = i * 1.3;
mesh.position.y = baseY + Math.sin(t * 2.5 + phase) * 0.07;
mesh.rotation.y = t * (type === 'X' ? -1.8 : 1.2) + phase;
});
// Pulse mood light
mood.intensity = 0.6 + Math.sin(t * 0.8) * 0.2;
renderer.render(scene, camera);
}
animate();
// ─── RESIZE ──────────────────────────────────────────────────────────────────
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>