1
- import * as DefaultEnvironment from "../objects/DefaultEnvironment.js" ;
2
- import * as Utils from "../utils/index.js" ;
3
-
4
- import {
5
- ClearPass ,
6
- EffectPass ,
7
- GeometryPass ,
8
- LensDistortionEffect ,
9
- MixBlendFunction ,
10
- RenderPipeline ,
11
- ToneMappingEffect
12
- } from "postprocessing" ;
13
1
import {
14
2
CubeTextureLoader ,
15
3
HalfFloatType ,
16
4
LoadingManager ,
17
5
PerspectiveCamera ,
18
- SRGBColorSpace ,
19
6
Scene ,
7
+ SRGBColorSpace ,
20
8
Texture ,
21
9
WebGLRenderer
22
10
} from "three" ;
23
11
12
+ import {
13
+ ClearPass ,
14
+ EffectPass ,
15
+ GeometryPass ,
16
+ LensDistortionEffect ,
17
+ RenderPipeline ,
18
+ ToneMappingEffect
19
+ } from "postprocessing" ;
20
+
24
21
import { Pane } from "tweakpane" ;
25
22
import { SpatialControls } from "spatial-controls" ;
23
+ import * as DefaultEnvironment from "../objects/DefaultEnvironment.js" ;
24
+ import * as Utils from "../utils/index.js" ;
26
25
27
26
function load ( ) : Promise < Map < string , Texture > > {
28
27
@@ -33,8 +32,7 @@ function load(): Promise<Map<string, Texture>> {
33
32
return new Promise < Map < string , Texture > > ( ( resolve , reject ) => {
34
33
35
34
loadingManager . onLoad = ( ) => resolve ( assets ) ;
36
- loadingManager . onError = ( url ) =>
37
- reject ( new Error ( `Failed to load ${ url } ` ) ) ;
35
+ loadingManager . onError = ( url ) => reject ( new Error ( `Failed to load ${ url } ` ) ) ;
38
36
39
37
cubeTextureLoader . load ( Utils . getSkyboxUrls ( "space" , ".jpg" ) , ( t ) => {
40
38
@@ -47,118 +45,108 @@ function load(): Promise<Map<string, Texture>> {
47
45
48
46
}
49
47
50
- window . addEventListener (
51
- "load" ,
52
- ( ) =>
53
- void load ( ) . then ( ( assets ) => {
54
-
55
- // Renderer
56
-
57
- const renderer = new WebGLRenderer ( {
58
- powerPreference : "high-performance" ,
59
- antialias : false ,
60
- stencil : false ,
61
- depth : false
62
- } ) ;
63
-
64
- renderer . debug . checkShaderErrors = Utils . isLocalhost ;
65
- const container = document . querySelector ( ".viewport" ) as HTMLElement ;
66
- container . prepend ( renderer . domElement ) ;
67
-
68
- // Camera & Controls
69
-
70
- const camera = new PerspectiveCamera ( ) ;
71
- const controls = new SpatialControls (
72
- camera . position ,
73
- camera . quaternion ,
74
- renderer . domElement
75
- ) ;
76
- const settings = controls . settings ;
77
- settings . rotation . sensitivity = 2.2 ;
78
- settings . rotation . damping = 0.05 ;
79
- settings . translation . damping = 0.1 ;
80
- controls . position . set ( 0 , 1.5 , 10 ) ;
81
- controls . lookAt ( 0 , 1.35 , 0 ) ;
82
-
83
- // Scene, Lights, Objects
84
-
85
- const scene = new Scene ( ) ;
86
- const skyMap = assets . get ( "sky" ) as Texture ;
87
- scene . background = skyMap ;
88
- scene . environment = skyMap ;
89
- scene . fog = DefaultEnvironment . createFog ( ) ;
90
- scene . add ( DefaultEnvironment . createEnvironment ( ) ) ;
91
-
92
- // Post Processing
93
-
94
- const effect = new LensDistortionEffect ( ) ;
95
- effect . blendMode . blendFunction = new MixBlendFunction ( ) ;
96
-
97
- const pipeline = new RenderPipeline ( renderer ) ;
98
- pipeline . add (
99
- new ClearPass ( ) ,
100
- new GeometryPass ( scene , camera , {
101
- frameBufferType : HalfFloatType ,
102
- samples : 4
103
- } ) ,
104
- new EffectPass ( effect , new ToneMappingEffect ( ) )
105
- ) ;
106
-
107
- // Settings
108
-
109
- const pane = new Pane ( {
110
- container : container . querySelector ( ".tp" ) as HTMLElement
111
- } ) ;
112
- const fpsGraph = Utils . createFPSGraph ( pane ) ;
113
-
114
- const folder = pane . addFolder ( { title : "Settings" } ) ;
115
- folder . addBinding ( effect , "skew" , { min : 0 , max : 10 , step : 1 } ) ;
116
- folder . addBinding ( effect , "distortion" , {
117
- x : { min : - 1 , max : 1 , step : 0.1 } ,
118
- y : { min : - 1 , max : 1 , step : 0.1 }
119
- } ) ;
120
- folder . addBinding ( effect , "principalPoint" , {
121
- x : { min : - 1 , max : 1 , step : 0.1 } ,
122
- y : { min : - 1 , max : 1 , step : 0.1 }
123
- } ) ;
124
- folder . addBinding ( effect , "focalLength" , {
125
- x : { min : 0 , max : 2 , step : 0.1 } ,
126
- y : { min : 0 , max : 2 , step : 0.1 }
127
- } ) ;
128
-
129
- Utils . addBlendModeBindings ( folder , effect . blendMode ) ;
130
-
131
- // Resize Handler
132
-
133
- function onResize ( ) : void {
134
-
135
- const width = container . clientWidth ,
136
- height = container . clientHeight ;
137
- camera . aspect = width / height ;
138
- camera . fov = Utils . calculateVerticalFoV (
139
- 90 ,
140
- Math . max ( camera . aspect , 16 / 9 )
141
- ) ;
142
- camera . updateProjectionMatrix ( ) ;
143
- pipeline . setSize ( width , height ) ;
144
-
145
- }
146
-
147
- window . addEventListener ( "resize" , onResize ) ;
148
- onResize ( ) ;
149
-
150
- // Render Loop
151
-
152
- requestAnimationFrame ( function render ( timestamp : number ) : void {
153
-
154
- fpsGraph . begin ( ) ;
155
- controls . update ( timestamp ) ;
156
- pipeline . render ( timestamp ) ;
157
- fpsGraph . end ( ) ;
158
-
159
- requestAnimationFrame ( render ) ;
160
-
161
- } ) ;
162
-
163
- } )
164
- ) ;
48
+ window . addEventListener ( "load" , ( ) => void load ( ) . then ( ( assets ) => {
49
+
50
+ // Renderer
51
+
52
+ const renderer = new WebGLRenderer ( {
53
+ powerPreference : "high-performance" ,
54
+ antialias : false ,
55
+ stencil : false ,
56
+ depth : false
57
+ } ) ;
58
+
59
+ renderer . debug . checkShaderErrors = Utils . isLocalhost ;
60
+ const container = document . querySelector ( ".viewport" ) as HTMLElement ;
61
+ container . prepend ( renderer . domElement ) ;
62
+
63
+ // Camera & Controls
64
+
65
+ const camera = new PerspectiveCamera ( ) ;
66
+ const controls = new SpatialControls (
67
+ camera . position ,
68
+ camera . quaternion ,
69
+ renderer . domElement
70
+ ) ;
71
+ const settings = controls . settings ;
72
+ settings . rotation . sensitivity = 2.2 ;
73
+ settings . rotation . damping = 0.05 ;
74
+ settings . translation . damping = 0.1 ;
75
+ controls . position . set ( 0 , 1.5 , 10 ) ;
76
+ controls . lookAt ( 0 , 1.35 , 0 ) ;
77
+
78
+ // Scene, Lights, Objects
79
+
80
+ const scene = new Scene ( ) ;
81
+ const skyMap = assets . get ( "sky" ) as Texture ;
82
+ scene . background = skyMap ;
83
+ scene . environment = skyMap ;
84
+ scene . fog = DefaultEnvironment . createFog ( ) ;
85
+ scene . add ( DefaultEnvironment . createEnvironment ( ) ) ;
86
+
87
+ // Post Processing
88
+
89
+ const effect = new LensDistortionEffect ( ) ;
90
+
91
+ const pipeline = new RenderPipeline ( renderer ) ;
92
+ pipeline . add (
93
+ new ClearPass ( ) ,
94
+ new GeometryPass ( scene , camera , {
95
+ frameBufferType : HalfFloatType ,
96
+ samples : 4
97
+ } ) ,
98
+ new EffectPass ( effect , new ToneMappingEffect ( ) )
99
+ ) ;
100
+
101
+ // Settings
102
+
103
+ const pane = new Pane ( { container : container . querySelector ( ".tp" ) as HTMLElement } ) ;
104
+ const fpsGraph = Utils . createFPSGraph ( pane ) ;
105
+
106
+ const folder = pane . addFolder ( { title : "Settings" } ) ;
107
+ folder . addBinding ( effect , "skew" , { min : - 90 , max : 90 , step : 0.1 } ) ;
108
+
109
+ folder . addBinding ( effect , "distortion" , {
110
+ x : { min : - 1 , max : 1 , step : 0.01 } ,
111
+ y : { min : - 1 , max : 1 , step : 0.01 }
112
+ } ) ;
113
+
114
+ folder . addBinding ( effect , "principalPoint" , {
115
+ x : { min : - 1 , max : 1 , step : 0.01 } ,
116
+ y : { min : - 1 , max : 1 , step : 0.01 }
117
+ } ) ;
118
+
119
+ folder . addBinding ( effect , "focalLength" , {
120
+ x : { min : 0 , max : 2 , step : 0.01 } ,
121
+ y : { min : 0 , max : 2 , step : 0.01 }
122
+ } ) ;
123
+
124
+ // Resize Handler
125
+
126
+ function onResize ( ) : void {
127
+
128
+ const width = container . clientWidth , height = container . clientHeight ;
129
+ camera . aspect = width / height ;
130
+ camera . fov = Utils . calculateVerticalFoV ( 90 , Math . max ( camera . aspect , 16 / 9 ) ) ;
131
+ camera . updateProjectionMatrix ( ) ;
132
+ pipeline . setSize ( width , height ) ;
133
+
134
+ }
135
+
136
+ window . addEventListener ( "resize" , onResize ) ;
137
+ onResize ( ) ;
138
+
139
+ // Render Loop
140
+
141
+ requestAnimationFrame ( function render ( timestamp : number ) : void {
142
+
143
+ fpsGraph . begin ( ) ;
144
+ controls . update ( timestamp ) ;
145
+ pipeline . render ( timestamp ) ;
146
+ fpsGraph . end ( ) ;
147
+
148
+ requestAnimationFrame ( render ) ;
149
+
150
+ } ) ;
151
+
152
+ } ) ) ;
0 commit comments