@@ -16,145 +16,124 @@ import {
1616 Mesh ,
1717 RepeatWrapping ,
1818 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'
19+ MeshPhysicalMaterialParameters ,
20+ PlaneGeometry ,
21+ GammaEncoding
22+ } from 'three'
23+ import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
24+ import { PLYLoader } from 'three/examples/jsm/loaders/PLYLoader'
2325import { animation } from '../common/animation'
2426
25- const sceneThreeJS = ( ) => {
26- const plyLoader = new PLYLoader ( )
27- const scene = new Scene ( )
28- const textureLoader = new TextureLoader ( )
29- const camera = camera3D ( )
30- const renderer = renderer3D ( )
31- const orbit = orbitsControls ( camera , renderer )
32- setup ( renderer , camera , scene )
33- lights ( scene )
34- stars ( textureLoader , stars => scene . add ( stars ) )
35- meshWithPBRMaterial ( plyLoader , lucy ( ) , mesh => scene . add ( mesh ) )
36- meshWithPBRMaterial ( plyLoader , dragon ( ) , mesh => scene . add ( mesh ) )
37- meshWithPBRMaterial ( plyLoader , bunny ( ) , mesh => scene . add ( mesh ) )
38- floor ( textureLoader , mesh => scene . add ( mesh ) )
39- DefaultLoadingManager . onLoad = ( ) => renderLoop ( renderer , scene , camera , orbit )
40- }
41-
42- class Object3D {
43- constructor ( path , properties , position , rotation ) {
44- this . path = path
45- this . properties = properties
46- this . position = position
47- this . rotation = rotation
48- }
27+ interface Object3D {
28+ path : string ;
29+ properties : MeshPhysicalMaterialParameters ;
30+ position : Vector3 ;
31+ rotation : Vector3 ;
4932}
5033
51- const lucy = ( ) => new Object3D (
52- 'assets/models/lucy.ply' ,
53- {
34+ const lucy = ( ) : Object3D => ( {
35+ path : 'assets/models/lucy.ply' ,
36+ properties : {
5437 color : 0x3F51B5 ,
5538 roughness : 0.5 ,
5639 metalness : 0.7 ,
5740 clearcoat : 0.5 ,
5841 clearcoatRoughness : 0.5 ,
5942 reflectivity : 0.7
6043 } ,
61- new Vector3 ( 3 , - 3 , 0 ) ,
62- new Vector3 ( 0 , - Math . PI / 3.0 , 0 )
63- )
44+ position : new Vector3 ( 3 , - 3 , 0 ) ,
45+ rotation : new Vector3 ( 0 , - Math . PI / 3.0 , 0 )
46+ } )
6447
65- const dragon = ( ) => new Object3D (
66- 'assets/models/dragon.ply' ,
67- {
48+ const dragon = ( ) : Object3D => ( {
49+ path : 'assets/models/dragon.ply' ,
50+ properties : {
6851 color : 0x448AFF ,
6952 roughness : 0.1 ,
7053 metalness : 0.9 ,
7154 clearcoat : 0.0 ,
7255 clearcoatRoughness : 0.2 ,
7356 reflectivity : 1
7457 } ,
75- new Vector3 ( - 3 , - 3 , 0 ) ,
76- new Vector3 ( 0 , - Math . PI , 0 )
77- )
58+ position : new Vector3 ( - 3 , - 3 , 0 ) ,
59+ rotation : new Vector3 ( 0 , - Math . PI , 0 )
60+ } )
7861
79- const bunny = ( ) => new Object3D (
80- 'assets/models/bunny.ply' ,
81- {
62+ const bunny = ( ) : Object3D => ( {
63+ path : 'assets/models/bunny.ply' ,
64+ properties : {
8265 color : 0xCCFFFF ,
8366 roughness : 0.9 ,
8467 metalness : 0.1 ,
8568 clearcoat : 0.0 ,
8669 clearcoatRoughness : 0.5 ,
8770 reflectivity : 0.1
8871 } ,
89- new Vector3 ( 0 , - 3 , 1.5 ) ,
90- new Vector3 ( 0 , - Math . PI , 0 )
91- )
72+ position : new Vector3 ( 0 , - 3 , 1.5 ) ,
73+ rotation : new Vector3 ( 0 , - Math . PI , 0 )
74+ } )
75+
76+ const camera3D = ( ) : PerspectiveCamera => {
77+ const camera = new PerspectiveCamera ( 75 , window . innerWidth / window . innerHeight , 0.1 , 1000 )
78+ camera . position . z = 8
79+ camera . position . y = 0
80+ camera . position . x = 0
81+ return camera
82+ }
9283
93- const orbitsControls = ( camera , renderer ) => {
84+ const renderer3D = ( ) : WebGLRenderer => {
85+ const renderer = new WebGLRenderer ( { alpha : true } )
86+ renderer . physicallyCorrectLights = true
87+ renderer . outputEncoding = GammaEncoding
88+ renderer . shadowMap . enabled = true
89+ renderer . shadowMap . type = PCFSoftShadowMap
90+ renderer . setSize ( window . innerWidth , window . innerHeight )
91+ return renderer
92+ }
93+
94+ const orbitsControls = ( camera : PerspectiveCamera , renderer : WebGLRenderer ) : OrbitControls => {
9495 const controls = new OrbitControls ( camera , renderer . domElement )
9596 controls . enableZoom = false
9697 controls . autoRotate = true
9798 controls . enablePan = false
9899 controls . keyPanSpeed = 7.0
99100 controls . enableKeys = false
100101 controls . target = new Vector3 ( 0 , 0 , 0 )
101- controls . mouseButtons = { }
102102 controls . dispose ( )
103103 return controls
104104}
105105
106- const setup = ( renderer , camera , scene ) => {
106+ const setWindowResizeListener = ( camera : PerspectiveCamera , renderer : WebGLRenderer ) : void => {
107+ window . addEventListener ( 'resize' , ( ) => {
108+ camera . aspect = window . innerWidth / window . innerHeight
109+ camera . updateProjectionMatrix ( )
110+ renderer . setSize ( window . innerWidth , window . innerHeight )
111+ } , false )
112+ }
113+
114+ const setup = ( renderer : WebGLRenderer , camera : PerspectiveCamera , scene : Scene ) : void => {
107115 document . getElementById ( 'rendering-surface' ) . appendChild ( renderer . domElement )
108116 scene . background = new Color ( 0x303F9F )
109117 setWindowResizeListener ( camera , renderer )
110118}
111119
112- const renderLoop = ( renderer , scene , camera , orbit ) => {
113- const renderFrame = ( ) => {
114- requestAnimationFrame ( renderFrame )
115- orbit . update ( )
116- renderer . render ( scene , camera )
117- }
118- showRenderingSurfaceAnimation ( )
119- renderFrame ( )
120- }
121-
122- const lights = ( scene ) => {
123- scene . add ( pointLight ( ) )
124- scene . add ( new HemisphereLight ( 0x303F9F , 0x000000 , 1 ) )
125- }
126-
127- const camera3D = ( ) => {
128- const camera = new PerspectiveCamera ( 75 , window . innerWidth / window . innerHeight , 0.1 , 1000 )
129- camera . position . z = 8
130- camera . position . y = 0
131- camera . position . x = 0
132- return camera
133- }
134-
135- const pointLight = ( ) => {
120+ const pointLight = ( ) : PointLight => {
136121 const light = new PointLight ( 0xffffff , 1 , 20 , 2 )
137122 light . power = 1700
138123 light . castShadow = true
139124 light . shadow . mapSize . width = 512
140- light . shadow . mapSize . heigth = 512
125+ light . shadow . mapSize . height = 512
141126 light . shadow . radius = 1.5
142127 light . position . set ( 0 , 5 , 3 )
143128 return light
144129}
145130
146- const renderer3D = ( ) => {
147- const renderer = new WebGLRenderer ( { alpha : true } )
148- renderer . physicallyCorrectLights = true
149- renderer . gammaInput = true
150- renderer . gammaOutput = true
151- renderer . shadowMap . enabled = true
152- renderer . shadowMap . type = PCFSoftShadowMap
153- renderer . setSize ( window . innerWidth , window . innerHeight )
154- return renderer
131+ const lights = ( scene : Scene ) : void => {
132+ scene . add ( pointLight ( ) )
133+ scene . add ( new HemisphereLight ( 0x303F9F , 0x000000 , 1 ) )
155134}
156135
157- const stars = ( textureLoader , completeLoad ) => {
136+ const stars = ( textureLoader : TextureLoader , completeLoad : ( starts : Points ) => void ) : void => {
158137 textureLoader . load ( 'assets/models/textures/circle.png' , function ( texture ) {
159138 const starsGeometry = new Geometry ( )
160139 for ( let i = 0 ; i < 10000 ; i ++ ) {
@@ -174,7 +153,7 @@ const stars = (textureLoader, completeLoad) => {
174153 } )
175154}
176155
177- const meshWithPBRMaterial = ( plyLoader , object , completeLoad ) => {
156+ const meshWithPBRMaterial = ( plyLoader : PLYLoader , object : Object3D , completeLoad : ( mesh : Mesh ) => void ) : void => {
178157 plyLoader . load ( object . path , ( geometry ) => {
179158 geometry . computeVertexNormals ( )
180159 const material = new MeshPhysicalMaterial ( object . properties )
@@ -187,7 +166,7 @@ const meshWithPBRMaterial = (plyLoader, object, completeLoad) => {
187166 } )
188167}
189168
190- const floor = ( textureLoader , completionFunction ) => {
169+ const floor = ( textureLoader : TextureLoader , completionFunction : ( mesh : Mesh ) => void ) : void => {
191170 textureLoader . load ( 'assets/models/textures/marble.jpg' , function ( texture ) {
192171 texture . wrapS = RepeatWrapping
193172 texture . wrapT = RepeatWrapping
@@ -208,14 +187,33 @@ const floor = (textureLoader, completionFunction) => {
208187 } )
209188}
210189
211- const showRenderingSurfaceAnimation = ( ) => animation ( 'rendering-surface' , 'show' )
190+ const showRenderingSurfaceAnimation = ( ) : void => animation ( 'rendering-surface' , 'show' )
212191
213- const setWindowResizeListener = ( camera , renderer ) => {
214- window . addEventListener ( 'resize' , ( ) => {
215- camera . aspect = window . innerWidth / window . innerHeight
216- camera . updateProjectionMatrix ( )
217- renderer . setSize ( window . innerWidth , window . innerHeight )
218- } , false )
192+ const renderLoop = ( renderer : WebGLRenderer , scene : Scene , camera : PerspectiveCamera , orbit : OrbitControls ) : void => {
193+ const renderFrame = ( ) : void => {
194+ requestAnimationFrame ( renderFrame )
195+ orbit . update ( )
196+ renderer . render ( scene , camera )
197+ }
198+ showRenderingSurfaceAnimation ( )
199+ renderFrame ( )
200+ }
201+
202+ const sceneThreeJS = ( ) : void => {
203+ const plyLoader = new PLYLoader ( )
204+ const scene = new Scene ( )
205+ const textureLoader = new TextureLoader ( )
206+ const camera = camera3D ( )
207+ const renderer = renderer3D ( )
208+ const orbit = orbitsControls ( camera , renderer )
209+ setup ( renderer , camera , scene )
210+ lights ( scene )
211+ stars ( textureLoader , stars => scene . add ( stars ) )
212+ meshWithPBRMaterial ( plyLoader , lucy ( ) , mesh => scene . add ( mesh ) )
213+ meshWithPBRMaterial ( plyLoader , dragon ( ) , mesh => scene . add ( mesh ) )
214+ meshWithPBRMaterial ( plyLoader , bunny ( ) , mesh => scene . add ( mesh ) )
215+ floor ( textureLoader , mesh => scene . add ( mesh ) )
216+ DefaultLoadingManager . onLoad = ( ) : void => renderLoop ( renderer , scene , camera , orbit )
219217}
220218
221219export { sceneThreeJS }
0 commit comments