-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.html
More file actions
257 lines (210 loc) · 7.38 KB
/
lab1.html
File metadata and controls
257 lines (210 loc) · 7.38 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
<html>
<head>
<meta charset=utf-8>
<title>Projeto 1 - CCI36 - 2021</title>
<!-- Another THREE.JS Example by Prof. Carlos Forster from
Technological Institute of Aeronautics 27/8/2021 -->
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
<style>
input.chk {
width:20px;
height: 20px;
}
</style>
</head>
<body style="overflow: visible">
<!-- canvas id="hicanvas" width="600" height="600"></canvas -->
<div class="container">
<h1>Projeto 1 - CCI36 - 2021</h1>
<p style="font-size:160%;">Selecione os itens que deseja visualizar:</p>
<form>
<label class="checkbox-inline" style="font-size:160%; width: 300%; height: 300%;">
<input class="chk" type="checkbox" id="chkCar" value="on"> Carro
</label>
<label class="checkbox-inline" style="font-size:160%;">
<input class="chk" type="checkbox" id="chkHelper" value="on">Eixos e Plano
</label>
</form>
</div>
<canvas id="offscreen" width="1024" height="680" style="display:none"></canvas>
<img id="ico" width="1024" height="680" src="static/mesa.jpg" />
<script type="module">
import * as THREE from 'https://cdn.skypack.dev/three@0.132.2';
// import { OrbitControls } from "https://threejs.org/examples/jsm/controls/OrbitControls.js";
const imageWidth = 2048
const imageHeight = 1360
var k = document.getElementById("ico");
var b = k.getBoundingClientRect();
// console.log(b);
var x = document.createElement("CANVAS");
x.style.position = "absolute";
x.width = b.width;
x.height = b.height;
x.style.top = "" + b.top + "px";
x.style.left = "" + b.left + "px";
document.body.appendChild(x);
function createProjCamera() {
const m = new THREE.Matrix4();
m.set(
0.73309873, -0.079348 , -0.67547772, 1.5497599 ,
-0.24203979, 0.89771331, -0.36814067, 2.28566293,
0.63559657, 0.43337594, 0.63890711, -19.26725785,
0. , 0. , 0. , 1. ,
); // calculado no script python
const fov = 14;
const aspect = 2048 / 1360;
const camera = new THREE.PerspectiveCamera(9, aspect, 0.001, 20000);
camera.applyMatrix4(m);
camera.position.set( 14.19826186, 6.42106631, 11.66329729,); // calculado no script python
camera.lookAt(0, 0, 0);
camera.setViewOffset(
imageWidth,
imageHeight,
177, // calculado no script python
-167, // calculado no script python
imageWidth,
imageHeight
);
camera.updateMatrixWorld();
return camera;
}
var canvas = x
const scene = new THREE.Scene()
const camera = createProjCamera()
const renderer = new THREE.WebGLRenderer({ antialias: true, canvas: canvas, alpha: true })
renderer.shadowMap.enabled = true
// adding a plane to scene
const gridHelper = new THREE.GridHelper( 30, 100 );
scene.add( gridHelper );
// adding axes lines to scene
var axesHelper = new THREE.AxesHelper(400)
axesHelper.position.set(0, 0, 0)
// const controls = new OrbitControls( camera, renderer.domElement );
// window.onresize = canvasResize
// function canvasResize() {
// // camera.aspect = window.innerWidth / window.innerHeight
// // camera.updateProjectionMatrix();
// }
function createCarBase() {
const geometry = new THREE.BoxGeometry(4.5, 0.8, 1.8);
const material = new THREE.MeshLambertMaterial({ color: 0x00ffff });
const carBase = new THREE.Mesh(geometry, material);
carBase.castShadow = true
return carBase;
}
function createCarTop() {
const geometry = new THREE.BoxGeometry(3, 0.8, 1.8);
const material = new THREE.MeshLambertMaterial({ color: 0x00ffff });
const carTop = new THREE.Mesh(geometry, material);
carTop.castShadow = true
carTop.position.y += 0.8;
carTop.position.x -= 0.75;
return carTop;
}
function createWheels() {
const geometry = new THREE.CylinderGeometry(0.4, 0.4, 0.19);
const material = new THREE.MeshLambertMaterial({ color: 0x000000 });
const wheel = new THREE.Mesh(geometry, material);
wheel.castShadow = true
wheel.position.y = -0.4;
wheel.rotation.x += Math.PI / 2;
var dz = 1 - 0.19 / 2 + 0.01;
var dx = 1.2;
var wheels = new THREE.Group()
var positions = [[dx, dz], [dx, -dz], [-dx, dz], [-dx, -dz]]
for (var i = 0; i < positions.length; i++) {
var w = wheel.clone()
w.position.x = positions[i][0]
w.position.z = positions[i][1]
wheels.add(w)
}
return wheels
}
const carBase = createCarBase();
const carTop = createCarTop();
const wheels = createWheels();
const car = new THREE.Group()
car.add(carBase); car.add(wheels); car.add(carTop);
car.position.set(0, 0, 0)
car.rotation.set(0, -Math.PI, 0)
car.scale.set(.1, .1, .1);
car.castShadow = true;
// scene.add( carBase )
scene.add(camera);
scene.add(car);
scene.add(axesHelper);
car.visible = true;
axesHelper.visible = true;
gridHelper.visible = true;
var chkCar = document.getElementById("chkCar");
var chkHelper = document.getElementById("chkHelper");
chkCar.checked = true;
chkHelper.checked = true;
document.getElementById("chkCar").onclick = function() {
if (chkCar.checked) {
car.visible = true;
} else {
car.visible = false;
}
};
document.getElementById("chkHelper").onclick = function() {
if (chkHelper.checked) {
axesHelper.visible = true;
gridHelper.visible = true;
} else {
axesHelper.visible = false;
gridHelper.visible = false;
}
};
function createBox(){
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshLambertMaterial( {color: 0x00ff00} );
const cube = new THREE.Mesh( geometry, material );
return cube;
}
const cube = createBox();
// scene.add(cube);
const light = new THREE.PointLight( '#ffffff' , 1);
light.position.set(-5, 1, 0);
light.castShadow = true;
scene.add( light );
const helperLight = new THREE.PointLightHelper(light);
// scene.add(helperLight);
const planeGeometry = new THREE.PlaneGeometry( 50, 50 );
const planeMaterial = new THREE.ShadowMaterial()
const plane = new THREE.Mesh( planeGeometry, planeMaterial );
plane.receiveShadow = true;
plane.rotation.x = -Math.PI/2
plane.position.y = 0;
car.position.y = 0.08;
// plane.visible = false;
console.log(car)
scene.add( plane );
// const color = 0xFFFFFF;
// const intensity = 1;
// const alight = new THREE.AmbientLight(color, intensity);
// // scene.add(alight);
const circle = { center_x: -3, center_z: -3, radius: 2 }
var theta = 0
function animate() {
requestAnimationFrame(animate);
car.position.x = circle.center_x + circle.radius * Math.cos(theta)
car.position.z = circle.center_z + circle.radius * Math.sin(theta)
car.position
// console.log(car.position)
theta += 0.01
car.rotation.y = (3 * Math.PI / 2 - theta)
// cube.rotation.x = theta
renderer.render(scene, camera)
}
console.log(car)
// console.log(camera)
animate()
</script>
</body>
</html>