1
- import * as THREE from 'three'
2
- import OrbitControls from 'three-orbitcontrols'
3
- import PLYLoader from 'three-ply-loader'
1
+ import {
2
+ Scene ,
3
+ TextureLoader ,
4
+ DefaultLoadingManager ,
5
+ Vector3 ,
6
+ Color ,
7
+ HemisphereLight ,
8
+ PerspectiveCamera ,
9
+ PointLight ,
10
+ WebGLRenderer ,
11
+ PCFSoftShadowMap ,
12
+ Geometry ,
13
+ PointsMaterial ,
14
+ Points ,
15
+ MeshPhysicalMaterial ,
16
+ Mesh ,
17
+ RepeatWrapping ,
18
+ MeshStandardMaterial ,
19
+ PlaneGeometry
20
+ } from 'three/build/three.module.js'
21
+ import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
22
+ import { PLYLoader } from 'three/examples/jsm/loaders/PLYLoader.js'
4
23
import isWebGLEnabled from 'detector-webgl'
5
24
import { isAMobileDevice } from '../common/mobile-device-detector'
6
25
import { animation } from '../common/animation'
@@ -12,10 +31,9 @@ const scene3D = () => {
12
31
}
13
32
14
33
const sceneThreeJS = ( ) => {
15
- PLYLoader ( THREE )
16
- const plyLoader = new THREE . PLYLoader ( )
17
- const scene = new THREE . Scene ( )
18
- const textureLoader = new THREE . TextureLoader ( )
34
+ const plyLoader = new PLYLoader ( )
35
+ const scene = new Scene ( )
36
+ const textureLoader = new TextureLoader ( )
19
37
const camera = camera3D ( )
20
38
const renderer = renderer3D ( )
21
39
const orbit = orbitsControls ( camera , renderer )
@@ -26,7 +44,7 @@ const sceneThreeJS = () => {
26
44
meshWithPBRMaterial ( plyLoader , dragon ( ) , mesh => scene . add ( mesh ) )
27
45
meshWithPBRMaterial ( plyLoader , bunny ( ) , mesh => scene . add ( mesh ) )
28
46
floor ( textureLoader , mesh => scene . add ( mesh ) )
29
- THREE . DefaultLoadingManager . onLoad = ( ) => renderLoop ( renderer , scene , camera , orbit )
47
+ DefaultLoadingManager . onLoad = ( ) => renderLoop ( renderer , scene , camera , orbit )
30
48
}
31
49
32
50
class Object3D {
@@ -48,8 +66,8 @@ const lucy = () => new Object3D(
48
66
clearcoatRoughness : 0.5 ,
49
67
reflectivity : 0.7
50
68
} ,
51
- new THREE . Vector3 ( 3 , - 3 , 0 ) ,
52
- new THREE . Vector3 ( 0 , - Math . PI / 3.0 , 0 )
69
+ new Vector3 ( 3 , - 3 , 0 ) ,
70
+ new Vector3 ( 0 , - Math . PI / 3.0 , 0 )
53
71
)
54
72
55
73
const dragon = ( ) => new Object3D (
@@ -62,8 +80,8 @@ const dragon = () => new Object3D(
62
80
clearcoatRoughness : 0.2 ,
63
81
reflectivity : 1
64
82
} ,
65
- new THREE . Vector3 ( - 3 , - 3 , 0 ) ,
66
- new THREE . Vector3 ( 0 , - Math . PI , 0 )
83
+ new Vector3 ( - 3 , - 3 , 0 ) ,
84
+ new Vector3 ( 0 , - Math . PI , 0 )
67
85
)
68
86
69
87
const bunny = ( ) => new Object3D (
@@ -76,8 +94,8 @@ const bunny = () => new Object3D(
76
94
clearcoatRoughness : 0.5 ,
77
95
reflectivity : 0.1
78
96
} ,
79
- new THREE . Vector3 ( 0 , - 3 , 1.5 ) ,
80
- new THREE . Vector3 ( 0 , - Math . PI , 0 )
97
+ new Vector3 ( 0 , - 3 , 1.5 ) ,
98
+ new Vector3 ( 0 , - Math . PI , 0 )
81
99
)
82
100
83
101
const orbitsControls = ( camera , renderer ) => {
@@ -87,15 +105,15 @@ const orbitsControls = (camera, renderer) => {
87
105
controls . enablePan = false
88
106
controls . keyPanSpeed = 7.0
89
107
controls . enableKeys = false
90
- controls . target = new THREE . Vector3 ( 0 , 0 , 0 )
108
+ controls . target = new Vector3 ( 0 , 0 , 0 )
91
109
controls . mouseButtons = { }
92
110
controls . dispose ( )
93
111
return controls
94
112
}
95
113
96
114
const setup = ( renderer , camera , scene ) => {
97
115
document . getElementById ( 'rendering-surface' ) . appendChild ( renderer . domElement )
98
- scene . background = new THREE . Color ( 0x303F9F )
116
+ scene . background = new Color ( 0x303F9F )
99
117
setWindowResizeListener ( camera , renderer )
100
118
}
101
119
@@ -111,19 +129,19 @@ const renderLoop = (renderer, scene, camera, orbit) => {
111
129
112
130
const lights = ( scene ) => {
113
131
scene . add ( pointLight ( ) )
114
- scene . add ( new THREE . HemisphereLight ( 0x303F9F , 0x000000 , 1 ) )
132
+ scene . add ( new HemisphereLight ( 0x303F9F , 0x000000 , 1 ) )
115
133
}
116
134
117
135
const camera3D = ( ) => {
118
- const camera = new THREE . PerspectiveCamera ( 75 , window . innerWidth / window . innerHeight , 0.1 , 1000 )
136
+ const camera = new PerspectiveCamera ( 75 , window . innerWidth / window . innerHeight , 0.1 , 1000 )
119
137
camera . position . z = 8
120
138
camera . position . y = 0
121
139
camera . position . x = 0
122
140
return camera
123
141
}
124
142
125
143
const pointLight = ( ) => {
126
- const light = new THREE . PointLight ( 0xffffff , 1 , 20 , 2 )
144
+ const light = new PointLight ( 0xffffff , 1 , 20 , 2 )
127
145
light . power = 1700
128
146
light . castShadow = true
129
147
light . shadow . mapSize . width = 512
@@ -134,61 +152,61 @@ const pointLight = () => {
134
152
}
135
153
136
154
const renderer3D = ( ) => {
137
- const renderer = new THREE . WebGLRenderer ( { alpha : true } )
155
+ const renderer = new WebGLRenderer ( { alpha : true } )
138
156
renderer . physicallyCorrectLights = true
139
157
renderer . gammaInput = true
140
158
renderer . gammaOutput = true
141
159
renderer . shadowMap . enabled = true
142
- renderer . shadowMap . type = THREE . PCFSoftShadowMap
160
+ renderer . shadowMap . type = PCFSoftShadowMap
143
161
renderer . setSize ( window . innerWidth , window . innerHeight )
144
162
return renderer
145
163
}
146
164
147
165
const stars = ( textureLoader , completeLoad ) => {
148
166
textureLoader . load ( 'assets/models/textures/circle.png' , function ( texture ) {
149
- const starsGeometry = new THREE . Geometry ( )
167
+ const starsGeometry = new Geometry ( )
150
168
for ( let i = 0 ; i < 10000 ; i ++ ) {
151
- const star = new THREE . Vector3 ( )
169
+ const star = new Vector3 ( )
152
170
star . x = 2000 * Math . random ( ) - 1000
153
171
star . y = 2000 * Math . random ( )
154
172
star . z = 2000 * Math . random ( ) - 1000
155
173
starsGeometry . vertices . push ( star )
156
174
}
157
- const starsMaterial = new THREE . PointsMaterial ( {
175
+ const starsMaterial = new PointsMaterial ( {
158
176
color : 0x888888 ,
159
177
map : texture ,
160
178
transparent : true
161
179
} )
162
- const stars = new THREE . Points ( starsGeometry , starsMaterial )
180
+ const stars = new Points ( starsGeometry , starsMaterial )
163
181
completeLoad ( stars )
164
182
} )
165
183
}
166
184
167
185
const meshWithPBRMaterial = ( plyLoader , object , completeLoad ) => {
168
- plyLoader . load ( object . path , geometry => {
169
- const material = new THREE . MeshPhysicalMaterial ( object . properties )
170
- const mesh = new THREE . Mesh ( geometry , material )
186
+ plyLoader . load ( object . path , ( geometry ) => {
187
+ geometry . computeVertexNormals ( )
188
+ const material = new MeshPhysicalMaterial ( object . properties )
189
+ const mesh = new Mesh ( geometry , material )
171
190
mesh . position . set ( object . position . x , object . position . y , object . position . z )
172
191
mesh . rotation . set ( object . rotation . x , object . rotation . y , object . rotation . z )
173
192
mesh . castShadow = true
174
- mesh . matrixAutoUpdate = false
175
- mesh . updateMatrix ( )
193
+ mesh . receiveShadow = true
176
194
completeLoad ( mesh )
177
195
} )
178
196
}
179
197
180
198
const floor = ( textureLoader , completionFunction ) => {
181
199
textureLoader . load ( 'assets/models/textures/marble.jpg' , function ( texture ) {
182
- texture . wrapS = THREE . RepeatWrapping
183
- texture . wrapT = THREE . RepeatWrapping
200
+ texture . wrapS = RepeatWrapping
201
+ texture . wrapT = RepeatWrapping
184
202
texture . repeat . set ( 100 , 100 )
185
- const floorMat = new THREE . MeshStandardMaterial ( {
203
+ const floorMat = new MeshStandardMaterial ( {
186
204
roughness : 0.7 ,
187
205
metalness : 0.1 ,
188
206
map : texture
189
207
} )
190
- const floorGeometry = new THREE . PlaneGeometry ( 1000 , 1000 )
191
- const floorMesh = new THREE . Mesh ( floorGeometry , floorMat )
208
+ const floorGeometry = new PlaneGeometry ( 1000 , 1000 )
209
+ const floorMesh = new Mesh ( floorGeometry , floorMat )
192
210
floorMesh . receiveShadow = true
193
211
floorMesh . rotation . x = - Math . PI / 2.0
194
212
floorMesh . position . y = - 3
0 commit comments