Skip to content

Commit 9f82819

Browse files
Complete - Lab 6 Cameras
1 parent d5eb360 commit 9f82819

5 files changed

Lines changed: 919 additions & 0 deletions

File tree

‎Lab - 06 - Cameras/index.html‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Lab 6</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="main.js"></script>
12+
</body>
13+
</html>

‎Lab - 06 - Cameras/main.js‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import * as THREE from 'three'
2+
import { PLYLoader } from 'three/examples/jsm/Addons.js';
3+
4+
const scene = new THREE.Scene()
5+
scene.background = new THREE.Color(0xcccccc);
6+
7+
8+
const aspect = window.innerWidth / window.innerHeight;
9+
10+
const perspective = new THREE.PerspectiveCamera(
11+
75, aspect,
12+
0.1,
13+
1000)
14+
perspective.position.set(2, 2, 10)
15+
perspective.lookAt(0, 0, 0)
16+
scene.add(perspective)
17+
18+
const size = 8
19+
const orthographic = new THREE.OrthographicCamera(
20+
-size * aspect,
21+
size * aspect,
22+
size,
23+
-size,
24+
0.1,
25+
100
26+
)
27+
orthographic.position.set(0, 0, 10)
28+
orthographic.lookAt(0, 0, 0)
29+
30+
const cameras = [perspective, orthographic]
31+
let current = 0;
32+
console.log('Active : Perspective')
33+
34+
const renderer = new THREE.WebGLRenderer({ antialias: true })
35+
renderer.setSize(window.innerWidth, window.innerHeight)
36+
document.body.appendChild(renderer.domElement)
37+
38+
const ambient = new THREE.AmbientLight(0xffffff, 0.35)
39+
scene.add(ambient)
40+
const dir = new THREE.DirectionalLight(0xffffff, 2.5)
41+
dir.position.set(5, 8, 4)
42+
scene.add(dir)
43+
44+
const depth = [0, -3, -7]
45+
const colors = [0xFF0000, 0x00FF00, 0x0000FF]
46+
47+
48+
new PLYLoader().load('models/3DObject.ply', (geo) => {
49+
geo.center();
50+
51+
depth.forEach((z, i) => {
52+
const mesh = new THREE.Mesh(geo,
53+
new THREE.MeshStandardMaterial({ color: colors[i], roughness: 0.5 }))
54+
mesh.scale.setScalar(0.02)
55+
mesh.rotation.x = Math.PI
56+
mesh.position.x = (i * 1) * 2
57+
mesh.position.z = z
58+
scene.add(mesh)
59+
})
60+
})
61+
62+
addEventListener('keydown', (e) => {
63+
if (e.code === 'Space') {
64+
e.preventDefault()
65+
current = current + 1
66+
if (current >= cameras.length) {
67+
current = 0
68+
}
69+
console.log('Active ', current === 0 ? 'Perspective ' : 'Orthographic')
70+
}
71+
})
72+
73+
74+
function animate() {
75+
requestAnimationFrame(animate)
76+
renderer.render(scene, cameras[current])
77+
}
78+
animate()
14.8 MB
Binary file not shown.

0 commit comments

Comments
 (0)