-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.html
More file actions
135 lines (124 loc) · 4.45 KB
/
render.html
File metadata and controls
135 lines (124 loc) · 4.45 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Floating Point Precision Errors - Shape Selector</title>
<style>
body { margin: 0; overflow: hidden; }
#ui {
position: absolute;
top: 10px;
left: 10px;
background: rgba(255,255,255,0.8);
padding: 10px;
border-radius: 5px;
font-family: sans-serif;
z-index: 10;
}
input, select { margin-right: 5px; }
button { padding: 4px 8px; }
</style>
</head>
<body>
<!-- UI panel with shape selector and coordinate inputs -->
<div id="ui">
<label for="shapeSelector">Shape:</label>
<select id="shapeSelector">
<option value="sphere">Sphere</option>
<option value="pyramid">Pyramid</option>
<option value="cube">Cube</option>
<option value="torusKnot">Torus Knot</option>
</select>
<br>
<input type="text" id="x" placeholder="x" value="0">
<input type="text" id="y" placeholder="y" value="0">
<input type="text" id="z" placeholder="z" value="0">
<button id="teleport">Teleport</button>
</div>
<!-- Include Three.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Set up the scene, camera, and renderer.
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1e8);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Global variable to hold the current mesh.
let currentMesh = null;
// Create a mesh based on the selected shape.
function createMesh(shape) {
let geometry;
switch (shape) {
case "sphere":
geometry = new THREE.SphereGeometry(2, 128, 128);
break;
case "pyramid":
// A square pyramid using ConeGeometry with 4 radial segments.
geometry = new THREE.ConeGeometry(2, 3, 4);
break;
case "cube":
geometry = new THREE.BoxGeometry(2, 2, 2);
break;
case "torusKnot":
// A high resolution torus knot.
geometry = new THREE.TorusKnotGeometry(2, 0.5, 300, 20);
break;
default:
geometry = new THREE.SphereGeometry(2, 128, 128);
}
return new THREE.Mesh(geometry, new THREE.MeshNormalMaterial());
}
// Update the mesh in the scene.
function updateMesh(shape) {
if (currentMesh) {
scene.remove(currentMesh);
}
currentMesh = createMesh(shape);
scene.add(currentMesh);
}
// Initialize with the default shape from the selector.
const shapeSelector = document.getElementById("shapeSelector");
updateMesh(shapeSelector.value);
// Listen for shape selection changes.
shapeSelector.addEventListener("change", function() {
updateMesh(this.value);
});
// Add an axes helper for reference.
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// Parameters for an orbiting camera.
let angle = 0;
const orbitRadius = 10;
// Animation loop: the camera orbits around the current mesh.
function animate() {
requestAnimationFrame(animate);
angle += 0.005;
if (currentMesh) {
camera.position.x = currentMesh.position.x + orbitRadius * Math.cos(angle);
camera.position.z = currentMesh.position.z + orbitRadius * Math.sin(angle);
camera.position.y = currentMesh.position.y + 5; // Elevated view.
camera.lookAt(currentMesh.position);
}
renderer.render(scene, camera);
}
animate();
// Teleport the current mesh to the entered coordinates.
document.getElementById("teleport").addEventListener("click", () => {
const x = parseFloat(document.getElementById("x").value) || 0;
const y = parseFloat(document.getElementById("y").value) || 0;
const z = parseFloat(document.getElementById("z").value) || 0;
if (currentMesh) {
currentMesh.position.set(x, y, z);
console.log(`Teleported mesh to: (${x}, ${y}, ${z})`);
}
});
// Update camera and renderer on window resize.
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>