Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 8b68c56

Browse files
authored
Merge pull request #25 from chicio/threejs-refactoring 🚀
Threejs ES6 modules 🚀
2 parents 39dc7a1 + ad96ae2 commit 8b68c56

12 files changed

+403
-253
lines changed

_includes/critical.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_includes/dependencies-js-blog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</script>
1717
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_SVG"></script>
1818
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js" data-cfasync="false"></script>
19-
<script type="text/javascript" src="/assets/js/index.blog.min.js?rev=e823f7277aab1012c75c94959d3c452f"></script>
19+
<script type="text/javascript" src="/assets/js/index.blog.min.js?rev=3b14e20ce1e0edf9851cbcfc3fc43716"></script>
2020
<script type="text/javascript">
2121
ChicioCodingBlog.init('{{ include.track-category }}')
2222
</script>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js" data-cfasync="false"></script>
2-
<script type="text/javascript" src="/assets/js/index.home.min.js?rev=3fc3c8b12417db797434dd47a8d487a5"></script>
2+
<script type="text/javascript" src="/assets/js/index.home.min.js?rev=f7767520ee533a32871c3eb6a1dd425e"></script>
33
{%include service-worker.html %}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"/assets/js/index.blog.min.js?rev=e823f7277aab1012c75c94959d3c452f",
1+
"/assets/js/index.blog.min.js?rev=3b14e20ce1e0edf9851cbcfc3fc43716",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"/assets/js/index.home.min.js?rev=3fc3c8b12417db797434dd47a8d487a5",
1+
"/assets/js/index.home.min.js?rev=f7767520ee533a32871c3eb6a1dd425e",

_js/home/scene-3D.js

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
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'
423
import isWebGLEnabled from 'detector-webgl'
524
import { isAMobileDevice } from '../common/mobile-device-detector'
625
import { animation } from '../common/animation'
@@ -12,10 +31,9 @@ const scene3D = () => {
1231
}
1332

1433
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()
1937
const camera = camera3D()
2038
const renderer = renderer3D()
2139
const orbit = orbitsControls(camera, renderer)
@@ -26,7 +44,7 @@ const sceneThreeJS = () => {
2644
meshWithPBRMaterial(plyLoader, dragon(), mesh => scene.add(mesh))
2745
meshWithPBRMaterial(plyLoader, bunny(), mesh => scene.add(mesh))
2846
floor(textureLoader, mesh => scene.add(mesh))
29-
THREE.DefaultLoadingManager.onLoad = () => renderLoop(renderer, scene, camera, orbit)
47+
DefaultLoadingManager.onLoad = () => renderLoop(renderer, scene, camera, orbit)
3048
}
3149

3250
class Object3D {
@@ -48,8 +66,8 @@ const lucy = () => new Object3D(
4866
clearcoatRoughness: 0.5,
4967
reflectivity: 0.7
5068
},
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)
5371
)
5472

5573
const dragon = () => new Object3D(
@@ -62,8 +80,8 @@ const dragon = () => new Object3D(
6280
clearcoatRoughness: 0.2,
6381
reflectivity: 1
6482
},
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)
6785
)
6886

6987
const bunny = () => new Object3D(
@@ -76,8 +94,8 @@ const bunny = () => new Object3D(
7694
clearcoatRoughness: 0.5,
7795
reflectivity: 0.1
7896
},
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)
8199
)
82100

83101
const orbitsControls = (camera, renderer) => {
@@ -87,15 +105,15 @@ const orbitsControls = (camera, renderer) => {
87105
controls.enablePan = false
88106
controls.keyPanSpeed = 7.0
89107
controls.enableKeys = false
90-
controls.target = new THREE.Vector3(0, 0, 0)
108+
controls.target = new Vector3(0, 0, 0)
91109
controls.mouseButtons = {}
92110
controls.dispose()
93111
return controls
94112
}
95113

96114
const setup = (renderer, camera, scene) => {
97115
document.getElementById('rendering-surface').appendChild(renderer.domElement)
98-
scene.background = new THREE.Color(0x303F9F)
116+
scene.background = new Color(0x303F9F)
99117
setWindowResizeListener(camera, renderer)
100118
}
101119

@@ -111,19 +129,19 @@ const renderLoop = (renderer, scene, camera, orbit) => {
111129

112130
const lights = (scene) => {
113131
scene.add(pointLight())
114-
scene.add(new THREE.HemisphereLight(0x303F9F, 0x000000, 1))
132+
scene.add(new HemisphereLight(0x303F9F, 0x000000, 1))
115133
}
116134

117135
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)
119137
camera.position.z = 8
120138
camera.position.y = 0
121139
camera.position.x = 0
122140
return camera
123141
}
124142

125143
const pointLight = () => {
126-
const light = new THREE.PointLight(0xffffff, 1, 20, 2)
144+
const light = new PointLight(0xffffff, 1, 20, 2)
127145
light.power = 1700
128146
light.castShadow = true
129147
light.shadow.mapSize.width = 512
@@ -134,61 +152,61 @@ const pointLight = () => {
134152
}
135153

136154
const renderer3D = () => {
137-
const renderer = new THREE.WebGLRenderer({ alpha: true })
155+
const renderer = new WebGLRenderer({ alpha: true })
138156
renderer.physicallyCorrectLights = true
139157
renderer.gammaInput = true
140158
renderer.gammaOutput = true
141159
renderer.shadowMap.enabled = true
142-
renderer.shadowMap.type = THREE.PCFSoftShadowMap
160+
renderer.shadowMap.type = PCFSoftShadowMap
143161
renderer.setSize(window.innerWidth, window.innerHeight)
144162
return renderer
145163
}
146164

147165
const stars = (textureLoader, completeLoad) => {
148166
textureLoader.load('assets/models/textures/circle.png', function (texture) {
149-
const starsGeometry = new THREE.Geometry()
167+
const starsGeometry = new Geometry()
150168
for (let i = 0; i < 10000; i++) {
151-
const star = new THREE.Vector3()
169+
const star = new Vector3()
152170
star.x = 2000 * Math.random() - 1000
153171
star.y = 2000 * Math.random()
154172
star.z = 2000 * Math.random() - 1000
155173
starsGeometry.vertices.push(star)
156174
}
157-
const starsMaterial = new THREE.PointsMaterial({
175+
const starsMaterial = new PointsMaterial({
158176
color: 0x888888,
159177
map: texture,
160178
transparent: true
161179
})
162-
const stars = new THREE.Points(starsGeometry, starsMaterial)
180+
const stars = new Points(starsGeometry, starsMaterial)
163181
completeLoad(stars)
164182
})
165183
}
166184

167185
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)
171190
mesh.position.set(object.position.x, object.position.y, object.position.z)
172191
mesh.rotation.set(object.rotation.x, object.rotation.y, object.rotation.z)
173192
mesh.castShadow = true
174-
mesh.matrixAutoUpdate = false
175-
mesh.updateMatrix()
193+
mesh.receiveShadow = true
176194
completeLoad(mesh)
177195
})
178196
}
179197

180198
const floor = (textureLoader, completionFunction) => {
181199
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
184202
texture.repeat.set(100, 100)
185-
const floorMat = new THREE.MeshStandardMaterial({
203+
const floorMat = new MeshStandardMaterial({
186204
roughness: 0.7,
187205
metalness: 0.1,
188206
map: texture
189207
})
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)
192210
floorMesh.receiveShadow = true
193211
floorMesh.rotation.x = -Math.PI / 2.0
194212
floorMesh.position.y = -3

0 commit comments

Comments
 (0)