-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests_box_geometry.html
249 lines (218 loc) · 7.11 KB
/
tests_box_geometry.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>TESTS</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
type="text/css"
media="screen"
href="css/index.css"
/>
</head>
<body>
<div id="models-container" class="box animate-bottom">
<canvas
class="box side-pane"
id="model-view"
willReadFrequently="true"
></canvas>
</div>
<script type="module">
const style = getComputedStyle(document.body);
const TEXT_COLOR = style
.getPropertyValue("--gui-text-color")
.trim();
const BACKGROUND_COLOR = style
.getPropertyValue("--gui-background-color")
.trim();
const TITLE_BACKGROUND_COLOR = style
.getPropertyValue("--gui-title-background-color")
.trim();
const PLOT_GRID_COLOR = style
.getPropertyValue("--plot-grid-color")
.trim();
const FONT_FAMILY = style.getPropertyValue("--font-family").trim();
const FOCUS_COLOR = style.getPropertyValue("--focus-color").trim();
const LINES_COLOR = style.getPropertyValue("--lines-color").trim();
import * as THREE from "./js/build/three.module.js";
import { OrbitControls } from "./js/build/OrbitControls.js";
import { AxisGridHelper } from "./js/build/minigui.js";
import GUI from "https://cdn.jsdelivr.net/npm/lil-gui@0.17/+esm";
const canvas = document.getElementById("model-view");
THREE.Object3D.DefaultUp = new THREE.Vector3(0, 0, 1);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
});
const scene = new THREE.Scene();
const gh = new AxisGridHelper(scene, 4, 4, 4);
gh.visible = false;
const fov = 40;
const aspect = 1; // the canvas default
const near = 0.01;
const far = 200;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(25, 25, 25);
camera.lookAt(0, 0, 0);
scene.add(camera);
const controls = new OrbitControls(camera, canvas);
controls.target.set(0, 0, 0);
controls.update();
// Lights
const light = new THREE.PointLight(0xffffff, 1.0);
camera.add(light);
const material = new THREE.MeshLambertMaterial({
color: FOCUS_COLOR,
emissive: FOCUS_COLOR,
});
const lineMaterial = new THREE.MeshLambertMaterial({
color: PLOT_GRID_COLOR,
linewidth: 3,
wireframe: true,
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: LINES_COLOR,
linewidth: 3,
});
const gui = new GUI({ title: "Settings" });
const config = { divisions: 1, tipo: "Prisma" };
gui.add(config, "divisions", 1, 10, 1)
.onChange(updateGeometry)
.name("Divisiones")
.listen();
gui.add(config, "tipo", ["Prisma", "Cubo", "Tetrahedro"])
.onChange(updateGeometry)
.name("Geometría")
.listen();
gui.add(gh, "visible").name("Grid").listen();
const types = {
Prisma: transformation1,
Cubo: transformation0,
Tetrahedro: transformation2,
};
let n = config["divisions"];
var geometry = new THREE.BoxGeometry(1, 1, 1, n, n, n);
types[config["tipo"]](geometry);
var mesh = new THREE.Mesh(geometry, material);
var edgesGeometry = new THREE.EdgesGeometry(geometry);
var lines = new THREE.Mesh(geometry, lineMaterial);
var edges = new THREE.LineSegments(edgesGeometry, edgeMaterial);
scene.add(mesh);
scene.add(lines);
scene.add(edges);
function updateGeometry() {
scene.remove(mesh);
mesh.geometry.dispose();
mesh.material.dispose();
scene.remove(lines);
lines.geometry.dispose();
lines.material.dispose();
scene.remove(edges);
edges.geometry.dispose();
edges.material.dispose();
let n = config["divisions"];
var geometry = new THREE.BoxGeometry(1, 1, 1, n, n, n);
types[config["tipo"]](geometry);
mesh = new THREE.Mesh(geometry, material);
edgesGeometry = new THREE.EdgesGeometry(geometry);
lines = new THREE.Mesh(geometry, lineMaterial);
edges = new THREE.LineSegments(edgesGeometry, edgeMaterial);
scene.add(mesh);
scene.add(lines);
scene.add(edges);
render();
zoomExtents();
}
updateGeometry();
zoomExtents();
let animationFrameID = requestAnimationFrame(update);
function transformation0(geo) {
const Z = [];
for (let i = 0; i < geo.attributes.position.count; i++) {
const x = geo.attributes.position.getX(i);
const y = geo.attributes.position.getY(i);
const z = geo.attributes.position.getZ(i);
Z.push([x * 2, y * 2, z * 2]);
geo.attributes.position.setX(i, x * 2);
geo.attributes.position.setY(i, y * 2);
geo.attributes.position.setZ(i, z * 2);
}
return Z;
}
function transformation1(geo) {
for (let i = 0; i < geo.attributes.position.count; i++) {
const x = geo.attributes.position.getX(i) + 0.5;
const y = geo.attributes.position.getY(i) + 0.5;
const z = geo.attributes.position.getZ(i) + 0.5;
geo.attributes.position.setX(i, x * (1 - y));
geo.attributes.position.setY(i, y);
geo.attributes.position.setZ(i, z);
}
}
function transformation2(geo) {
for (let i = 0; i < geo.attributes.position.count; i++) {
const x = geo.attributes.position.getX(i) + 0.5;
const y = geo.attributes.position.getY(i) + 0.5;
const z = geo.attributes.position.getZ(i) + 0.5;
geo.attributes.position.setX(i, x * (1 - y) * (1 - z));
geo.attributes.position.setY(i, y * (1 - z));
geo.attributes.position.setZ(i, z);
}
}
function resizeRendererToDisplaySize() {
const canvas = renderer.domElement;
const pixelRatio = window.devicePixelRatio;
const width = (canvas.clientWidth * pixelRatio) | 0;
const height = (canvas.clientHeight * pixelRatio) | 0;
const needResize =
canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render() {
if (resizeRendererToDisplaySize()) {
const canvas = renderer.domElement;
const aspect = canvas.clientWidth / canvas.clientHeight;
camera.aspect = aspect;
camera.updateProjectionMatrix();
}
renderer.render(scene, camera);
}
function update() {
render();
animationFrameID = requestAnimationFrame(update);
}
function zoomExtents() {
let vFoV = camera.getEffectiveFOV();
let hFoV = camera.fov * camera.aspect;
let FoV = Math.min(vFoV, hFoV);
let FoV2 = FoV / 2;
let dir = new THREE.Vector3();
camera.getWorldDirection(dir);
let bb = mesh.geometry.boundingBox;
let bs = mesh.geometry.boundingSphere;
let bsWorld = bs.center.clone();
mesh.localToWorld(bsWorld);
let th = (FoV2 * Math.PI) / 180.0;
let sina = Math.sin(th);
let R = bs.radius;
let FL = R / sina;
let cameraDir = new THREE.Vector3();
camera.getWorldDirection(cameraDir);
let cameraOffs = cameraDir.clone();
cameraOffs.multiplyScalar(-FL);
let newCameraPos = bsWorld.clone().add(cameraOffs);
camera.position.copy(newCameraPos);
camera.lookAt(bsWorld);
controls.target.copy(bsWorld);
controls.update();
}
</script>
</body>
</html>