-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
211 lines (165 loc) · 5.12 KB
/
main.js
File metadata and controls
211 lines (165 loc) · 5.12 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
import './styles.scss'
import * as THREE from 'three'
import {
EffectComposer,
EffectPass,
RenderPass,
SMAAEffect,
GodRaysEffect
} from 'postprocessing'
let renderer,
scene,
camera
const T = THREE
/**
* Loads scene assets.
*
* @return {Promise} Returns loaded assets when ready.
*/
function load () {
const assets = new Map()
const loadingManager = new T.LoadingManager()
return new Promise((resolve, reject) => {
loadingManager.onError = reject
loadingManager.onProgress = (item, loaded, total) => {
if (loaded === total) {
resolve(assets)
}
}
loadingManager.onLoad = () => {
}
const searchImage = new Image(); const areaImage = new Image()
searchImage.addEventListener('load', function () {
assets.set('smaa-search', this)
loadingManager.itemEnd('smaa-search')
})
areaImage.addEventListener('load', function () {
assets.set('smaa-area', this)
loadingManager.itemEnd('smaa-area')
})
loadingManager.itemStart('smaa-search')
loadingManager.itemStart('smaa-area')
searchImage.src = SMAAEffect.searchImageDataURL
areaImage.src = SMAAEffect.areaImageDataURL
})
}
function initialize (assets) {
const container3d = document.querySelector('.container3d')
const width = container3d.clientWidth
const height = container3d.clientHeight
const aspect = width / height
const clock = new T.Clock()
renderer = new T.WebGLRenderer({
powerPreference: 'high-performance',
antialias: false,
stencil: false,
depth: false
})
renderer.setSize(width, height)
renderer.setClearColor(0x000000)
renderer.physicallyCorrectLights = true
window.innerWidth <= 1920 ? renderer.setPixelRatio(Math.min(2, window.devicePixelRatio)) : ''
container3d.appendChild(renderer.domElement)
scene = new T.Scene()
camera = new T.PerspectiveCamera(
45,
aspect,
0.1,
500
)
camera.lookAt(scene.position)
camera.position.z = 25
// -------------------------------------------------------------------------------lights---------------------------//
const geometry0 = new THREE.SphereGeometry(2, 32, 16)
const material0 = new THREE.MeshBasicMaterial({ color: 0xff0000 })
const bra = new THREE.Mesh(geometry0, material0)
scene.add(bra)
const pointLight = new T.PointLight(0xff0000, 150, 0, 2)
bra.add(pointLight)
pointLight.position.z = 20
// ---------------------------------------------------------------------------------meshes-------------------------//
const geometry = new THREE.PlaneGeometry(50, 50, 50, 50)
const geometry2 = new THREE.BoxGeometry(0.95, 0.95, 0.01)
const material2 = new THREE.MeshPhongMaterial({ color: 0x000000 })
for (let i = 0; i < geometry.attributes.position.count; i++) {
const p = new THREE.Vector3(
geometry.attributes.position.getX(i),
geometry.attributes.position.getY(i),
geometry.attributes.position.getZ(i)
)
const cube = new THREE.Mesh(geometry2, material2)
cube.position.set(
p.x,
p.y,
10
)
scene.add(cube)
}
// ---------------------------------------------------------------------------------interactions-------------------//
const mouse = new T.Vector2()
document.body.onmousemove = function (e) {
e.preventDefault()
mouse.x = (e.clientX / document.documentElement.clientWidth) * 2 - 1
mouse.y = -((e.clientY - pageYOffset) / document.documentElement.clientHeight) * 2 + 1
const vector = new T.Vector3(mouse.x, mouse.y, 0.5)
vector.unproject(camera)
const dir = vector.sub(camera.position).normalize()
const distance = -camera.position.z / dir.z
const pos = camera.position.clone().add(dir.multiplyScalar(distance))
bra.position.copy(pos)
}
// ++++++++++++++++++++++++postprocessing
const composer = new EffectComposer(
renderer, {
frameBufferType: T.HalfFloatType
}
)
const smaaEffect = new SMAAEffect(
assets.get('smaa-search'),
assets.get('smaa-area')
)
const GodsLight = new GodRaysEffect(camera, bra)
const renderPass = new RenderPass(scene, camera)
const smaaPass = new EffectPass(camera, smaaEffect)
const effectPass = new EffectPass(
camera,
GodsLight
)
effectPass.renderToScreen = true
composer.addPass(renderPass)
composer.addPass(smaaPass)
composer.addPass(effectPass)
/**
* Handles browser resizing.
*
* @private
* @param {Event} event - An event.
*/
window.addEventListener('resize', (function () {
let id = 0
function handleResize () {
const width = container3d.clientWidth
const height = container3d.clientHeight
composer.setSize(width, height)
camera.aspect = width / height
camera.updateProjectionMatrix()
id = 0
}
return function onResize (event) {
if (id === 0) {
id = setTimeout(handleResize, 66, event)
}
}
})());
/**
* The main render loop.
*
* @private
* @param {DOMHighResTimeStamp} now - An execution timestamp.
*/
(function render (now) {
requestAnimationFrame(render)
composer.render(clock.getDelta())
})()
}
load().then(initialize).catch(console.error)