forked from SLMetaverse/hiring-3D-assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
203 lines (161 loc) · 6.54 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3d Rooms in three.js | Puncoz</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000;
}
</style>
</head>
<body>
<div id="container"></div>
<!-- Import maps polyfill -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three/build/three.module.js"
}
}
</script>
<script type="module">
import * as THREE from "https://unpkg.com/three/build/three.module.js"
import { OrbitControls } from "https://unpkg.com/three/examples/jsm/controls/OrbitControls.js"
import { TextGeometry } from "https://unpkg.com/three/examples/jsm/geometries/TextGeometry.js"
import { FontLoader } from "https://unpkg.com/three/examples/jsm/loaders/FontLoader.js"
const TOP = "TOP"
const BOTTOM = "BOTTOM"
const LEFT = "L"
const RIGHT = "R"
const FRONT = "F"
const BACK = "B"
const roomSize = 70
const roomPlan = ["E:F", "F:LR:B", "FR:F:L", "FRF::B", "FL:L:R", "FLL:L:R", "FLLL:L:R", "FLLLL:F:R", "FLLLLF:F:B", "FLLLLFF::B"]
const roomInitialPosition = { x: 100, y: 100, z: 100 }
const openTop = true
const printRoomNumber = (scene, roomNumber, position) => {
const loader = new FontLoader()
loader.load("https://threejs.org/examples/fonts/optimer_regular.typeface.json", function(font) {
const geometry = new TextGeometry(roomNumber.toString(), {
font: font,
size: 0.4 * roomSize,
height: 0.5,
})
geometry.center()
const material = new THREE.MeshLambertMaterial({
color: 0x686868,
})
const mesh = new THREE.Mesh(geometry, material)
mesh.position.x = position.x
mesh.position.y = position.y
mesh.position.z = position.z
mesh.name = "bhText"
scene.add(mesh)
})
}
const calculatePosition = (position, direction, step) => {
const dir = [TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT].indexOf(direction)
const axis = ["y", "y", "z", "z", "x", "x"][dir]
const operator = [1, -1, -1, 1, -1, 1][dir]
return { ...position, [axis]: position[axis] + operator * step }
}
const calculateRotation = (direction) => {
const dir = [TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT].indexOf(direction)
const axis = ["x", "x", "z", "z", "y", "y"][dir]
const operator = [-1, 1, 0, 0, 1, -1][dir]
return {
axis,
angle: operator * Math.PI / 2,
}
}
const renderRoomWalls = (scene, planeGeometry, position, color, openSides = "", noWalls = "") => {
const openSidesArray = openSides.split("")
const noWallsArray = noWalls.split("")
;[TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT].forEach(side => {
if (openTop && side === TOP) {
return
}
if (noWallsArray.includes(side)) {
return
}
const wallPosition = calculatePosition(position, side, roomSize / 2)
const { axis: rAxis, angle: rAngle } = calculateRotation(side)
const wallProperties = { color, side: THREE.DoubleSide }
if (openSidesArray.includes(side)) {
wallProperties.transparent = true
wallProperties.opacity = 0.1
}
const wall = new THREE.Mesh(planeGeometry, new THREE.MeshPhongMaterial(wallProperties))
wall.position.set(wallPosition.x, wallPosition.y, wallPosition.z)
wall.rotation[rAxis] = rAngle
scene.add(wall)
})
}
const createRoom = (scene, planeGeometry, plan, index) => {
const [pos, openSides, noWalls] = plan.split(":")
const color = new THREE.Color(0xffffff).setHex(Math.random() * 0xffffff)
const position = pos.split("").reduce((p, d) => ({ ...p, ...calculatePosition(p, d, roomSize) }), roomInitialPosition)
renderRoomWalls(scene, planeGeometry, position, color, openSides, noWalls)
printRoomNumber(scene, index + 1, position)
const light = new THREE.PointLight(color, 1, 150)
light.position.set(position.x, position.y, position.z)
scene.add(light)
}
const createRooms = (scene) => {
const planeGeometry = new THREE.PlaneGeometry(roomSize, roomSize)
roomPlan.forEach((plan, index) => {
createRoom(scene, planeGeometry, plan, index)
})
}
const init = () => {
const container = document.getElementById("container")
// renderer
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
container.appendChild(renderer.domElement)
renderer.localClippingEnabled = true
// scene
const scene = new THREE.Scene()
// camera
const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 5000)
camera.position.set(500, 700, 500)
camera.useQuaternion = true
// camera controls
const cameraControls = new OrbitControls(camera, renderer.domElement)
cameraControls.target.set(0, 40, 0)
cameraControls.maxDistance = 500
cameraControls.minDistance = 200
cameraControls.update()
createRooms(scene)
// main light
const mainLight = new THREE.AmbientLight(0x404040, 1.5, 250) // soft white light
scene.add(mainLight)
const onWindowResize = () => {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
}
window.addEventListener("resize", onWindowResize)
return {
scene,
camera,
renderer,
}
}
const { scene, camera, renderer } = init()
const quaternion = new THREE.Quaternion(...Object.values(roomInitialPosition), 1)
quaternion.setFromAxisAngle(new THREE.Vector3(0, 0.1, 0), Math.PI / 180)
const animate = () => {
requestAnimationFrame(animate)
scene.applyQuaternion(quaternion)
// render the main scene
renderer.render(scene, camera)
}
animate()
</script>
</body>
</html>