-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
124 lines (97 loc) · 2.91 KB
/
script.js
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
/**
* Three.js interior mapping demo
* Author: Mohsen Heydari
*/
import {OrbitControls} from 'https://threejsfundamentals.org/threejs/resources/threejs/r122/examples/jsm/controls/OrbitControls.js'
function loadFile(filename) {
return new Promise((resolve, reject) => {
let loader
if(filename.endsWith('.dds')){
loader = new THREE.DDSLoader()
}else{
loader = new THREE.FileLoader()
}
loader.load(filename,
data => { resolve(data); },
null,
error => {reject(error);}
);
});
}
const sizes = {
width: window.innerWidth,
height: window.innerHeight
}
const params = {
bottomColor: '#2c83ca',
surfaceColor: '#fcfcfc'
}
let scene, renderer, camera,
controls, windowMesh, windowMat
function initialize(){
// Scene
scene = new THREE.Scene()
scene.background = new THREE.Color(params.bottomColor)
renderer = new THREE.WebGLRenderer()
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
camera.position.set(0, 0, 1.5)
scene.add(camera)
// Controls
controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
window.addEventListener('resize', onResize)
// Add renderer to page
document.body.appendChild(renderer.domElement);
document.addEventListener('keyup', (e)=>{
if(e.code === "KeyA"){
console.log(camera.position)
console.log(camera.rotation)
}
})
setupScene()
}
async function setupScene(){
// plane
const vertexShader = await loadFile('./shaders/vertex.glsl')
const fragmentShader = await loadFile('./shaders/fragment.glsl')
const cubeMap = await loadFile('./textures/cube.dds')
cubeMap.encoding = THREE.sRGBEncoding
windowMat = new THREE.ShaderMaterial(
{
uniforms: {
cubeMap: {value: cubeMap}
},
vertexShader: vertexShader,
fragmentShader: fragmentShader
}
)
const windowGeometry = new THREE.PlaneGeometry(1, 1)
windowGeometry.computeTangents()
// Mesh
windowMesh = new THREE.Mesh(windowGeometry, windowMat)
scene.add(windowMesh)
}
function onResize(){
// Update sizes
sizes.width = window.innerWidth
sizes.height = window.innerHeight
// Update camera
camera.aspect = sizes.width / sizes.height
camera.updateProjectionMatrix()
// Update renderer
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
}
const tick = () =>
{
// Update controls
controls.update()
// Render
renderer.render( scene, camera )
// Call tick again on the next frame
window.requestAnimationFrame(tick)
}
initialize()
tick()