@damienmortini/three 0.1.192 → 0.1.195

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,378 +1,355 @@
1
- import {
2
- AdditiveBlending,
3
- Box2,
4
- BufferGeometry,
5
- Color,
6
- FramebufferTexture,
7
- InterleavedBuffer,
8
- InterleavedBufferAttribute,
9
- Mesh,
10
- MeshBasicMaterial,
11
- RawShaderMaterial,
12
- Vector2,
13
- Vector3,
14
- Vector4,
15
- RGBAFormat
16
- } from '../../../../three/src/Three.js';
17
-
1
+ import {
2
+ AdditiveBlending,
3
+ Box2,
4
+ BufferGeometry,
5
+ Color,
6
+ FramebufferTexture,
7
+ InterleavedBuffer,
8
+ InterleavedBufferAttribute,
9
+ Mesh,
10
+ MeshBasicMaterial,
11
+ RawShaderMaterial,
12
+ RGBAFormat,
13
+ Vector2,
14
+ Vector3,
15
+ Vector4 } from '../../../../three/src/Three.js';
16
+
18
17
  class Lensflare extends Mesh {
19
-
20
- constructor() {
21
-
22
- super( Lensflare.Geometry, new MeshBasicMaterial( { opacity: 0, transparent: true } ) );
23
-
24
- this.isLensflare = true;
25
-
26
- this.type = 'Lensflare';
27
- this.frustumCulled = false;
28
- this.renderOrder = Infinity;
29
-
30
- //
31
-
32
- const positionScreen = new Vector3();
33
- const positionView = new Vector3();
34
-
35
- // textures
36
-
37
- const tempMap = new FramebufferTexture( 16, 16, RGBAFormat );
38
- const occlusionMap = new FramebufferTexture( 16, 16, RGBAFormat );
39
-
40
- // material
41
-
42
- const geometry = Lensflare.Geometry;
43
-
44
- const material1a = new RawShaderMaterial( {
45
- uniforms: {
46
- 'scale': { value: null },
47
- 'screenPosition': { value: null }
48
- },
49
- vertexShader: /* glsl */`
50
-
51
- precision highp float;
52
-
53
- uniform vec3 screenPosition;
54
- uniform vec2 scale;
55
-
56
- attribute vec3 position;
57
-
58
- void main() {
59
-
60
- gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
61
-
62
- }`,
63
-
64
- fragmentShader: /* glsl */`
65
-
66
- precision highp float;
67
-
68
- void main() {
69
-
70
- gl_FragColor = vec4( 1.0, 0.0, 1.0, 1.0 );
71
-
72
- }`,
73
- depthTest: true,
74
- depthWrite: false,
75
- transparent: false
76
- } );
77
-
78
- const material1b = new RawShaderMaterial( {
79
- uniforms: {
80
- 'map': { value: tempMap },
81
- 'scale': { value: null },
82
- 'screenPosition': { value: null }
83
- },
84
- vertexShader: /* glsl */`
85
-
86
- precision highp float;
87
-
88
- uniform vec3 screenPosition;
89
- uniform vec2 scale;
90
-
91
- attribute vec3 position;
92
- attribute vec2 uv;
93
-
94
- varying vec2 vUV;
95
-
96
- void main() {
97
-
98
- vUV = uv;
99
-
100
- gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
101
-
102
- }`,
103
-
104
- fragmentShader: /* glsl */`
105
-
106
- precision highp float;
107
-
108
- uniform sampler2D map;
109
-
110
- varying vec2 vUV;
111
-
112
- void main() {
113
-
114
- gl_FragColor = texture2D( map, vUV );
115
-
116
- }`,
117
- depthTest: false,
118
- depthWrite: false,
119
- transparent: false
120
- } );
121
-
122
- // the following object is used for occlusionMap generation
123
-
124
- const mesh1 = new Mesh( geometry, material1a );
125
-
126
- //
127
-
128
- const elements = [];
129
-
130
- const shader = LensflareElement.Shader;
131
-
132
- const material2 = new RawShaderMaterial( {
133
- uniforms: {
134
- 'map': { value: null },
135
- 'occlusionMap': { value: occlusionMap },
136
- 'color': { value: new Color( 0xffffff ) },
137
- 'scale': { value: new Vector2() },
138
- 'screenPosition': { value: new Vector3() }
139
- },
140
- vertexShader: shader.vertexShader,
141
- fragmentShader: shader.fragmentShader,
142
- blending: AdditiveBlending,
143
- transparent: true,
144
- depthWrite: false
145
- } );
146
-
147
- const mesh2 = new Mesh( geometry, material2 );
148
-
149
- this.addElement = function ( element ) {
150
-
151
- elements.push( element );
152
-
153
- };
154
-
155
- //
156
-
157
- const scale = new Vector2();
158
- const screenPositionPixels = new Vector2();
159
- const validArea = new Box2();
160
- const viewport = new Vector4();
161
-
162
- this.onBeforeRender = function ( renderer, scene, camera ) {
163
-
164
- renderer.getCurrentViewport( viewport );
165
-
166
- const invAspect = viewport.w / viewport.z;
167
- const halfViewportWidth = viewport.z / 2.0;
168
- const halfViewportHeight = viewport.w / 2.0;
169
-
170
- let size = 16 / viewport.w;
171
- scale.set( size * invAspect, size );
172
-
173
- validArea.min.set( viewport.x, viewport.y );
174
- validArea.max.set( viewport.x + ( viewport.z - 16 ), viewport.y + ( viewport.w - 16 ) );
175
-
176
- // calculate position in screen space
177
-
178
- positionView.setFromMatrixPosition( this.matrixWorld );
179
- positionView.applyMatrix4( camera.matrixWorldInverse );
180
-
181
- if ( positionView.z > 0 ) return; // lensflare is behind the camera
182
-
183
- positionScreen.copy( positionView ).applyMatrix4( camera.projectionMatrix );
184
-
185
- // horizontal and vertical coordinate of the lower left corner of the pixels to copy
186
-
187
- screenPositionPixels.x = viewport.x + ( positionScreen.x * halfViewportWidth ) + halfViewportWidth - 8;
188
- screenPositionPixels.y = viewport.y + ( positionScreen.y * halfViewportHeight ) + halfViewportHeight - 8;
189
-
190
- // screen cull
191
-
192
- if ( validArea.containsPoint( screenPositionPixels ) ) {
193
-
194
- // save current RGB to temp texture
195
-
196
- renderer.copyFramebufferToTexture( screenPositionPixels, tempMap );
197
-
198
- // render pink quad
199
-
200
- let uniforms = material1a.uniforms;
201
- uniforms[ 'scale' ].value = scale;
202
- uniforms[ 'screenPosition' ].value = positionScreen;
203
-
204
- renderer.renderBufferDirect( camera, null, geometry, material1a, mesh1, null );
205
-
206
- // copy result to occlusionMap
207
-
208
- renderer.copyFramebufferToTexture( screenPositionPixels, occlusionMap );
209
-
210
- // restore graphics
211
-
212
- uniforms = material1b.uniforms;
213
- uniforms[ 'scale' ].value = scale;
214
- uniforms[ 'screenPosition' ].value = positionScreen;
215
-
216
- renderer.renderBufferDirect( camera, null, geometry, material1b, mesh1, null );
217
-
218
- // render elements
219
-
220
- const vecX = - positionScreen.x * 2;
221
- const vecY = - positionScreen.y * 2;
222
-
223
- for ( let i = 0, l = elements.length; i < l; i ++ ) {
224
-
225
- const element = elements[ i ];
226
-
227
- const uniforms = material2.uniforms;
228
-
229
- uniforms[ 'color' ].value.copy( element.color );
230
- uniforms[ 'map' ].value = element.texture;
231
- uniforms[ 'screenPosition' ].value.x = positionScreen.x + vecX * element.distance;
232
- uniforms[ 'screenPosition' ].value.y = positionScreen.y + vecY * element.distance;
233
-
234
- size = element.size / viewport.w;
235
- const invAspect = viewport.w / viewport.z;
236
-
237
- uniforms[ 'scale' ].value.set( size * invAspect, size );
238
-
239
- material2.uniformsNeedUpdate = true;
240
-
241
- renderer.renderBufferDirect( camera, null, geometry, material2, mesh2, null );
242
-
243
- }
244
-
245
- }
246
-
247
- };
248
-
249
- this.dispose = function () {
250
-
251
- material1a.dispose();
252
- material1b.dispose();
253
- material2.dispose();
254
-
255
- tempMap.dispose();
256
- occlusionMap.dispose();
257
-
258
- for ( let i = 0, l = elements.length; i < l; i ++ ) {
259
-
260
- elements[ i ].texture.dispose();
261
-
262
- }
263
-
264
- };
265
-
266
- }
267
-
268
- }
269
-
270
- //
271
-
18
+ constructor() {
19
+ super(Lensflare.Geometry, new MeshBasicMaterial({ opacity: 0, transparent: true }));
20
+
21
+ this.isLensflare = true;
22
+
23
+ this.type = 'Lensflare';
24
+ this.frustumCulled = false;
25
+ this.renderOrder = Infinity;
26
+
27
+ //
28
+
29
+ const positionScreen = new Vector3();
30
+ const positionView = new Vector3();
31
+
32
+ // textures
33
+
34
+ const tempMap = new FramebufferTexture(16, 16, RGBAFormat);
35
+ const occlusionMap = new FramebufferTexture(16, 16, RGBAFormat);
36
+
37
+ // material
38
+
39
+ const geometry = Lensflare.Geometry;
40
+
41
+ const material1a = new RawShaderMaterial({
42
+ uniforms: {
43
+ scale: { value: null },
44
+ screenPosition: { value: null },
45
+ },
46
+ vertexShader: /* glsl */`
47
+
48
+ precision highp float;
49
+
50
+ uniform vec3 screenPosition;
51
+ uniform vec2 scale;
52
+
53
+ attribute vec3 position;
54
+
55
+ void main() {
56
+
57
+ gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
58
+
59
+ }`,
60
+
61
+ fragmentShader: /* glsl */`
62
+
63
+ precision highp float;
64
+
65
+ void main() {
66
+
67
+ gl_FragColor = vec4( 1.0, 0.0, 1.0, 1.0 );
68
+
69
+ }`,
70
+ depthTest: true,
71
+ depthWrite: false,
72
+ transparent: false,
73
+ });
74
+
75
+ const material1b = new RawShaderMaterial({
76
+ uniforms: {
77
+ map: { value: tempMap },
78
+ scale: { value: null },
79
+ screenPosition: { value: null },
80
+ },
81
+ vertexShader: /* glsl */`
82
+
83
+ precision highp float;
84
+
85
+ uniform vec3 screenPosition;
86
+ uniform vec2 scale;
87
+
88
+ attribute vec3 position;
89
+ attribute vec2 uv;
90
+
91
+ varying vec2 vUV;
92
+
93
+ void main() {
94
+
95
+ vUV = uv;
96
+
97
+ gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
98
+
99
+ }`,
100
+
101
+ fragmentShader: /* glsl */`
102
+
103
+ precision highp float;
104
+
105
+ uniform sampler2D map;
106
+
107
+ varying vec2 vUV;
108
+
109
+ void main() {
110
+
111
+ gl_FragColor = texture2D( map, vUV );
112
+
113
+ }`,
114
+ depthTest: false,
115
+ depthWrite: false,
116
+ transparent: false,
117
+ });
118
+
119
+ // the following object is used for occlusionMap generation
120
+
121
+ const mesh1 = new Mesh(geometry, material1a);
122
+
123
+ //
124
+
125
+ const elements = [];
126
+
127
+ const shader = LensflareElement.Shader;
128
+
129
+ const material2 = new RawShaderMaterial({
130
+ uniforms: {
131
+ map: { value: null },
132
+ occlusionMap: { value: occlusionMap },
133
+ color: { value: new Color(0xffffff) },
134
+ scale: { value: new Vector2() },
135
+ screenPosition: { value: new Vector3() },
136
+ },
137
+ vertexShader: shader.vertexShader,
138
+ fragmentShader: shader.fragmentShader,
139
+ blending: AdditiveBlending,
140
+ transparent: true,
141
+ depthWrite: false,
142
+ });
143
+
144
+ const mesh2 = new Mesh(geometry, material2);
145
+
146
+ this.addElement = function (element) {
147
+ elements.push(element);
148
+ };
149
+
150
+ //
151
+
152
+ const scale = new Vector2();
153
+ const screenPositionPixels = new Vector2();
154
+ const validArea = new Box2();
155
+ const viewport = new Vector4();
156
+
157
+ this.onBeforeRender = function (renderer, scene, camera) {
158
+ renderer.getCurrentViewport(viewport);
159
+
160
+ const invAspect = viewport.w / viewport.z;
161
+ const halfViewportWidth = viewport.z / 2.0;
162
+ const halfViewportHeight = viewport.w / 2.0;
163
+
164
+ let size = 16 / viewport.w;
165
+ scale.set(size * invAspect, size);
166
+
167
+ validArea.min.set(viewport.x, viewport.y);
168
+ validArea.max.set(viewport.x + (viewport.z - 16), viewport.y + (viewport.w - 16));
169
+
170
+ // calculate position in screen space
171
+
172
+ positionView.setFromMatrixPosition(this.matrixWorld);
173
+ positionView.applyMatrix4(camera.matrixWorldInverse);
174
+
175
+ if (positionView.z > 0) return; // lensflare is behind the camera
176
+
177
+ positionScreen.copy(positionView).applyMatrix4(camera.projectionMatrix);
178
+
179
+ // horizontal and vertical coordinate of the lower left corner of the pixels to copy
180
+
181
+ screenPositionPixels.x = viewport.x + (positionScreen.x * halfViewportWidth) + halfViewportWidth - 8;
182
+ screenPositionPixels.y = viewport.y + (positionScreen.y * halfViewportHeight) + halfViewportHeight - 8;
183
+
184
+ // screen cull
185
+
186
+ if (validArea.containsPoint(screenPositionPixels)) {
187
+ // save current RGB to temp texture
188
+
189
+ renderer.copyFramebufferToTexture(screenPositionPixels, tempMap);
190
+
191
+ // render pink quad
192
+
193
+ let uniforms = material1a.uniforms;
194
+ uniforms['scale'].value = scale;
195
+ uniforms['screenPosition'].value = positionScreen;
196
+
197
+ renderer.renderBufferDirect(camera, null, geometry, material1a, mesh1, null);
198
+
199
+ // copy result to occlusionMap
200
+
201
+ renderer.copyFramebufferToTexture(screenPositionPixels, occlusionMap);
202
+
203
+ // restore graphics
204
+
205
+ uniforms = material1b.uniforms;
206
+ uniforms['scale'].value = scale;
207
+ uniforms['screenPosition'].value = positionScreen;
208
+
209
+ renderer.renderBufferDirect(camera, null, geometry, material1b, mesh1, null);
210
+
211
+ // render elements
212
+
213
+ const vecX = -positionScreen.x * 2;
214
+ const vecY = -positionScreen.y * 2;
215
+
216
+ for (let i = 0, l = elements.length; i < l; i++) {
217
+ const element = elements[i];
218
+
219
+ const uniforms = material2.uniforms;
220
+
221
+ uniforms['color'].value.copy(element.color);
222
+ uniforms['map'].value = element.texture;
223
+ uniforms['screenPosition'].value.x = positionScreen.x + vecX * element.distance;
224
+ uniforms['screenPosition'].value.y = positionScreen.y + vecY * element.distance;
225
+
226
+ size = element.size / viewport.w;
227
+ const invAspect = viewport.w / viewport.z;
228
+
229
+ uniforms['scale'].value.set(size * invAspect, size);
230
+
231
+ material2.uniformsNeedUpdate = true;
232
+
233
+ renderer.renderBufferDirect(camera, null, geometry, material2, mesh2, null);
234
+ }
235
+ }
236
+ };
237
+
238
+ this.dispose = function () {
239
+ material1a.dispose();
240
+ material1b.dispose();
241
+ material2.dispose();
242
+
243
+ tempMap.dispose();
244
+ occlusionMap.dispose();
245
+
246
+ for (let i = 0, l = elements.length; i < l; i++) {
247
+ elements[i].texture.dispose();
248
+ }
249
+ };
250
+ }
251
+ }
252
+
253
+ //
254
+
272
255
  class LensflareElement {
273
-
274
- constructor( texture, size = 1, distance = 0, color = new Color( 0xffffff ) ) {
275
-
276
- this.texture = texture;
277
- this.size = size;
278
- this.distance = distance;
279
- this.color = color;
280
-
281
- }
282
-
283
- }
284
-
285
- LensflareElement.Shader = {
286
-
287
- uniforms: {
288
-
289
- 'map': { value: null },
290
- 'occlusionMap': { value: null },
291
- 'color': { value: null },
292
- 'scale': { value: null },
293
- 'screenPosition': { value: null }
294
-
295
- },
296
-
297
- vertexShader: /* glsl */`
298
-
299
- precision highp float;
300
-
301
- uniform vec3 screenPosition;
302
- uniform vec2 scale;
303
-
304
- uniform sampler2D occlusionMap;
305
-
306
- attribute vec3 position;
307
- attribute vec2 uv;
308
-
309
- varying vec2 vUV;
310
- varying float vVisibility;
311
-
312
- void main() {
313
-
314
- vUV = uv;
315
-
316
- vec2 pos = position.xy;
317
-
318
- vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) );
319
- visibility += texture2D( occlusionMap, vec2( 0.5, 0.1 ) );
320
- visibility += texture2D( occlusionMap, vec2( 0.9, 0.1 ) );
321
- visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) );
322
- visibility += texture2D( occlusionMap, vec2( 0.9, 0.9 ) );
323
- visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) );
324
- visibility += texture2D( occlusionMap, vec2( 0.1, 0.9 ) );
325
- visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) );
326
- visibility += texture2D( occlusionMap, vec2( 0.5, 0.5 ) );
327
-
328
- vVisibility = visibility.r / 9.0;
329
- vVisibility *= 1.0 - visibility.g / 9.0;
330
- vVisibility *= visibility.b / 9.0;
331
-
332
- gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );
333
-
334
- }`,
335
-
336
- fragmentShader: /* glsl */`
337
-
338
- precision highp float;
339
-
340
- uniform sampler2D map;
341
- uniform vec3 color;
342
-
343
- varying vec2 vUV;
344
- varying float vVisibility;
345
-
346
- void main() {
347
-
348
- vec4 texture = texture2D( map, vUV );
349
- texture.a *= vVisibility;
350
- gl_FragColor = texture;
351
- gl_FragColor.rgb *= color;
352
-
353
- }`
354
-
355
- };
356
-
357
- Lensflare.Geometry = ( function () {
358
-
359
- const geometry = new BufferGeometry();
360
-
361
- const float32Array = new Float32Array( [
362
- - 1, - 1, 0, 0, 0,
363
- 1, - 1, 0, 1, 0,
364
- 1, 1, 0, 1, 1,
365
- - 1, 1, 0, 0, 1
366
- ] );
367
-
368
- const interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
369
-
370
- geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
371
- geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
372
- geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
373
-
374
- return geometry;
375
-
376
- } )();
377
-
378
- export { Lensflare, LensflareElement };
256
+ constructor(texture, size = 1, distance = 0, color = new Color(0xffffff)) {
257
+ this.texture = texture;
258
+ this.size = size;
259
+ this.distance = distance;
260
+ this.color = color;
261
+ }
262
+ }
263
+
264
+ LensflareElement.Shader = {
265
+
266
+ uniforms: {
267
+
268
+ map: { value: null },
269
+ occlusionMap: { value: null },
270
+ color: { value: null },
271
+ scale: { value: null },
272
+ screenPosition: { value: null },
273
+
274
+ },
275
+
276
+ vertexShader: /* glsl */`
277
+
278
+ precision highp float;
279
+
280
+ uniform vec3 screenPosition;
281
+ uniform vec2 scale;
282
+
283
+ uniform sampler2D occlusionMap;
284
+
285
+ attribute vec3 position;
286
+ attribute vec2 uv;
287
+
288
+ varying vec2 vUV;
289
+ varying float vVisibility;
290
+
291
+ void main() {
292
+
293
+ vUV = uv;
294
+
295
+ vec2 pos = position.xy;
296
+
297
+ vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) );
298
+ visibility += texture2D( occlusionMap, vec2( 0.5, 0.1 ) );
299
+ visibility += texture2D( occlusionMap, vec2( 0.9, 0.1 ) );
300
+ visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) );
301
+ visibility += texture2D( occlusionMap, vec2( 0.9, 0.9 ) );
302
+ visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) );
303
+ visibility += texture2D( occlusionMap, vec2( 0.1, 0.9 ) );
304
+ visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) );
305
+ visibility += texture2D( occlusionMap, vec2( 0.5, 0.5 ) );
306
+
307
+ vVisibility = visibility.r / 9.0;
308
+ vVisibility *= 1.0 - visibility.g / 9.0;
309
+ vVisibility *= visibility.b / 9.0;
310
+
311
+ gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );
312
+
313
+ }`,
314
+
315
+ fragmentShader: /* glsl */`
316
+
317
+ precision highp float;
318
+
319
+ uniform sampler2D map;
320
+ uniform vec3 color;
321
+
322
+ varying vec2 vUV;
323
+ varying float vVisibility;
324
+
325
+ void main() {
326
+
327
+ vec4 texture = texture2D( map, vUV );
328
+ texture.a *= vVisibility;
329
+ gl_FragColor = texture;
330
+ gl_FragColor.rgb *= color;
331
+
332
+ }`,
333
+
334
+ };
335
+
336
+ Lensflare.Geometry = (function () {
337
+ const geometry = new BufferGeometry();
338
+
339
+ const float32Array = new Float32Array([
340
+ -1, -1, 0, 0, 0,
341
+ 1, -1, 0, 1, 0,
342
+ 1, 1, 0, 1, 1,
343
+ -1, 1, 0, 0, 1,
344
+ ]);
345
+
346
+ const interleavedBuffer = new InterleavedBuffer(float32Array, 5);
347
+
348
+ geometry.setIndex([0, 1, 2, 0, 2, 3]);
349
+ geometry.setAttribute('position', new InterleavedBufferAttribute(interleavedBuffer, 3, 0, false));
350
+ geometry.setAttribute('uv', new InterleavedBufferAttribute(interleavedBuffer, 2, 3, false));
351
+
352
+ return geometry;
353
+ })();
354
+
355
+ export { Lensflare, LensflareElement };