@damienmortini/three 0.1.182 → 0.1.183

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,281 +1,281 @@
1
- import {
2
- BackSide,
3
- Mesh,
4
- Vector3,
5
- IcosahedronBufferGeometry,
6
- } from '../../../three/src/Three.js'
7
- import THREEShaderMaterial from '../material/THREEShaderMaterial.js'
8
- import SkyShader from '@damienmortini/core/shader/SkyShader.js'
9
- import GradientNoiseShader from '@damienmortini/core/shader/noise/GradientNoiseShader.js'
10
-
11
- const skyShader = {
12
- uniforms: {
13
- sunPosition: new Vector3(0, 1, 0),
14
- sunRayleigh: 1.5,
15
- sunTurbidity: 6,
16
- sunLuminance: 1,
17
- sunMieCoefficient: 0.005,
18
- sunMieDirectionalG: 0.8,
19
- moonPosition: new Vector3(0, 1, 0),
20
- moonRayleigh: 0,
21
- moonTurbidity: 1.5,
22
- moonLuminance: 1.1,
23
- moonMieCoefficient: 0.005,
24
- moonMieDirectionalG: 0.8,
25
- displaySun: 1,
26
- displayMoon: 1,
27
- },
28
- vertexShaderChunks: [
29
- ['start', `
30
- varying vec3 vWorldPosition;
31
- `],
32
- ['end', `
33
- vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
34
- vWorldPosition = worldPosition.xyz;
35
-
36
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
37
- `],
38
- ],
39
- fragmentShaderChunks: [
40
- ['start', `
41
- uniform vec3 sunPosition;
42
- uniform float sunRayleigh;
43
- uniform float sunTurbidity;
44
- uniform float sunLuminance;
45
- uniform float sunMieCoefficient;
46
- uniform float sunMieDirectionalG;
47
- uniform float displaySun;
48
- uniform float displayMoon;
49
-
50
- uniform vec3 moonPosition;
51
- uniform float moonRayleigh;
52
- uniform float moonTurbidity;
53
- uniform float moonLuminance;
54
- uniform float moonMieCoefficient;
55
- uniform float moonMieDirectionalG;
56
-
57
- varying vec3 vWorldPosition;
58
-
59
- ${SkyShader.computeSkyColor()}
60
-
61
- ${GradientNoiseShader.gradientNoise3D()}
62
-
63
- float blendScreen(float base, float blend) {
64
- return 1.0-((1.0-base)*(1.0-blend));
65
- }
66
-
67
- vec3 blendScreen(vec3 base, vec3 blend) {
68
- return vec3(blendScreen(base.r,blend.r),blendScreen(base.g,blend.g),blendScreen(base.b,blend.b));
69
- }
70
- `],
71
- ['end', `
72
- vec3 normalizedSunPosition = normalize(sunPosition);
73
- vec3 normalizedMoonPosition = normalize(moonPosition);
74
-
75
- vec4 skySunColor = computeSkyColor(vWorldPosition, normalizedSunPosition, ${SkyShader.SUN_ANGULAR_DIAMETER} * displaySun, sunRayleigh, sunTurbidity, sunLuminance, sunMieCoefficient, sunMieDirectionalG);
76
- vec4 skyMoonColor = computeSkyColor(vWorldPosition, normalizedMoonPosition, ${SkyShader.MOON_ANGULAR_DIAMETER} * displayMoon, moonRayleigh, moonTurbidity, moonLuminance, moonMieCoefficient, moonMieDirectionalG);
77
-
78
- float nightIntensity = 1. - smoothstep(-.05, 0., normalizedSunPosition.y);
79
-
80
- float moonIntensity = max(0., -dot(normalize(sunPosition.xz), normalize(moonPosition.xz)));
81
- // skyMoonColor *= moonIntensity;
82
- skyMoonColor *= nightIntensity;
83
-
84
- // Stars
85
- vec3 rayDirection = normalize(vWorldPosition);
86
- float starsIntensity = gradientNoise3D(rayDirection * 400.) * .5 + .5;
87
- starsIntensity = pow(starsIntensity, 15.);
88
- starsIntensity *= max(0., rayDirection.y);
89
- starsIntensity *= 10.;
90
- starsIntensity *= nightIntensity;
91
-
92
- skyMoonColor.rgb = blendScreen(skyMoonColor.rgb, vec3(starsIntensity));
93
-
94
- vec3 skyColor = blendScreen(skySunColor.rgb, skyMoonColor.rgb);
95
-
96
- // gl_FragColor = vec4(skyColor, (skySunColor.a + skyMoonColor.a) / 2.);
97
- gl_FragColor = vec4(skyColor, 1.);
98
- `],
99
- ],
100
- }
101
-
102
- export default class Sky extends Mesh {
103
- constructor({
104
- radius = 1,
105
- shaders = [],
106
- } = {}) {
107
- super(new IcosahedronBufferGeometry(radius, 3), new THREEShaderMaterial(Object.assign({
108
- type: 'basic',
109
- side: BackSide,
110
- shaders,
111
- }, skyShader)))
112
-
113
- this._radius = radius
114
-
115
- this.sunInclination = Math.PI * .5
116
- this.sunAzimuth = 0
117
-
118
- this.moonInclination = Math.PI * .5
119
- this.moonAzimuth = Math.PI
120
- }
121
-
122
- get radius() {
123
- return this._radius
124
- }
125
-
126
- _updatePositionFromInclinationAzimuth(position, inclination, azimuth) {
127
- const theta = inclination
128
- const phi = azimuth + Math.PI * .5
129
- position.x = this._radius * Math.cos(phi) * Math.cos(theta)
130
- position.y = this._radius * Math.sin(theta)
131
- position.z = this._radius * Math.sin(phi) * Math.cos(theta)
132
- }
133
-
134
- get displaySun() {
135
- return this.material.displaySun === 1
136
- }
137
-
138
- set displaySun(value) {
139
- this.material.displaySun = value ? 1 : 0
140
- }
141
-
142
- get displayMoon() {
143
- return this.material.displayMoon === 1
144
- }
145
-
146
- set displayMoon(value) {
147
- this.material.displayMoon = value ? 1 : 0
148
- }
149
-
150
- get sunInclination() {
151
- return this._sunInclination
152
- }
153
-
154
- set sunInclination(value) {
155
- this._sunInclination = value
156
- this._updatePositionFromInclinationAzimuth(this.sunPosition, this.sunInclination, this.sunAzimuth)
157
- }
158
-
159
- get sunAzimuth() {
160
- return this._sunAzimuth
161
- }
162
-
163
- set sunAzimuth(value) {
164
- this._sunAzimuth = value
165
- this._updatePositionFromInclinationAzimuth(this.sunPosition, this.sunInclination, this.sunAzimuth)
166
- }
167
-
168
- get sunPosition() {
169
- return this.material.sunPosition
170
- }
171
-
172
- set sunPosition(value) {
173
- this.material.sunPosition = value
174
- }
175
-
176
- get sunRayleigh() {
177
- return this.material.sunRayleigh
178
- }
179
-
180
- set sunRayleigh(value) {
181
- this.material.sunRayleigh = value
182
- }
183
-
184
- get sunTurbidity() {
185
- return this.material.sunTurbidity
186
- }
187
-
188
- set sunTurbidity(value) {
189
- this.material.sunTurbidity = value
190
- }
191
-
192
- get sunLuminance() {
193
- return this.material.sunLuminance
194
- }
195
-
196
- set sunLuminance(value) {
197
- this.material.sunLuminance = value
198
- }
199
-
200
- get sunMieCoefficient() {
201
- return this.material.sunMieCoefficient
202
- }
203
-
204
- set sunMieCoefficient(value) {
205
- this.material.sunMieCoefficient = value
206
- }
207
-
208
- get sunMieDirectionalG() {
209
- return this.material.sunMieDirectionalG
210
- }
211
-
212
- set sunMieDirectionalG(value) {
213
- this.material.sunMieDirectionalG = value
214
- }
215
-
216
- get moonInclination() {
217
- return this._moonInclination
218
- }
219
-
220
- set moonInclination(value) {
221
- this._moonInclination = value
222
- this._updatePositionFromInclinationAzimuth(this.moonPosition, this.moonInclination, this.moonAzimuth)
223
- }
224
-
225
- get moonAzimuth() {
226
- return this._moonAzimuth
227
- }
228
-
229
- set moonAzimuth(value) {
230
- this._moonAzimuth = value
231
- this._updatePositionFromInclinationAzimuth(this.moonPosition, this.moonInclination, this.moonAzimuth)
232
- }
233
-
234
- get moonPosition() {
235
- return this.material.moonPosition
236
- }
237
-
238
- set moonPosition(value) {
239
- this.material.moonPosition = value
240
- }
241
-
242
- get moonRayleigh() {
243
- return this.material.moonRayleigh
244
- }
245
-
246
- set moonRayleigh(value) {
247
- this.material.moonRayleigh = value
248
- }
249
-
250
- get moonTurbidity() {
251
- return this.material.moonTurbidity
252
- }
253
-
254
- set moonTurbidity(value) {
255
- this.material.moonTurbidity = value
256
- }
257
-
258
- get moonLuminance() {
259
- return this.material.moonLuminance
260
- }
261
-
262
- set moonLuminance(value) {
263
- this.material.moonLuminance = value
264
- }
265
-
266
- get moonMieCoefficient() {
267
- return this.material.moonMieCoefficient
268
- }
269
-
270
- set moonMieCoefficient(value) {
271
- this.material.moonMieCoefficient = value
272
- }
273
-
274
- get moonMieDirectionalG() {
275
- return this.material.moonMieDirectionalG
276
- }
277
-
278
- set moonMieDirectionalG(value) {
279
- this.material.moonMieDirectionalG = value
280
- }
281
- }
1
+ import {
2
+ BackSide,
3
+ Mesh,
4
+ Vector3,
5
+ IcosahedronBufferGeometry,
6
+ } from '../../../three/src/Three.js'
7
+ import THREEShaderMaterial from '../material/THREEShaderMaterial.js'
8
+ import SkyShader from '@damienmortini/core/shader/SkyShader.js'
9
+ import GradientNoiseShader from '@damienmortini/core/shader/noise/GradientNoiseShader.js'
10
+
11
+ const skyShader = {
12
+ uniforms: {
13
+ sunPosition: new Vector3(0, 1, 0),
14
+ sunRayleigh: 1.5,
15
+ sunTurbidity: 6,
16
+ sunLuminance: 1,
17
+ sunMieCoefficient: 0.005,
18
+ sunMieDirectionalG: 0.8,
19
+ moonPosition: new Vector3(0, 1, 0),
20
+ moonRayleigh: 0,
21
+ moonTurbidity: 1.5,
22
+ moonLuminance: 1.1,
23
+ moonMieCoefficient: 0.005,
24
+ moonMieDirectionalG: 0.8,
25
+ displaySun: 1,
26
+ displayMoon: 1,
27
+ },
28
+ vertexShaderChunks: [
29
+ ['start', `
30
+ varying vec3 vWorldPosition;
31
+ `],
32
+ ['end', `
33
+ vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
34
+ vWorldPosition = worldPosition.xyz;
35
+
36
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
37
+ `],
38
+ ],
39
+ fragmentShaderChunks: [
40
+ ['start', `
41
+ uniform vec3 sunPosition;
42
+ uniform float sunRayleigh;
43
+ uniform float sunTurbidity;
44
+ uniform float sunLuminance;
45
+ uniform float sunMieCoefficient;
46
+ uniform float sunMieDirectionalG;
47
+ uniform float displaySun;
48
+ uniform float displayMoon;
49
+
50
+ uniform vec3 moonPosition;
51
+ uniform float moonRayleigh;
52
+ uniform float moonTurbidity;
53
+ uniform float moonLuminance;
54
+ uniform float moonMieCoefficient;
55
+ uniform float moonMieDirectionalG;
56
+
57
+ varying vec3 vWorldPosition;
58
+
59
+ ${SkyShader.computeSkyColor()}
60
+
61
+ ${GradientNoiseShader.gradientNoise3D()}
62
+
63
+ float blendScreen(float base, float blend) {
64
+ return 1.0-((1.0-base)*(1.0-blend));
65
+ }
66
+
67
+ vec3 blendScreen(vec3 base, vec3 blend) {
68
+ return vec3(blendScreen(base.r,blend.r),blendScreen(base.g,blend.g),blendScreen(base.b,blend.b));
69
+ }
70
+ `],
71
+ ['end', `
72
+ vec3 normalizedSunPosition = normalize(sunPosition);
73
+ vec3 normalizedMoonPosition = normalize(moonPosition);
74
+
75
+ vec4 skySunColor = computeSkyColor(vWorldPosition, normalizedSunPosition, ${SkyShader.SUN_ANGULAR_DIAMETER} * displaySun, sunRayleigh, sunTurbidity, sunLuminance, sunMieCoefficient, sunMieDirectionalG);
76
+ vec4 skyMoonColor = computeSkyColor(vWorldPosition, normalizedMoonPosition, ${SkyShader.MOON_ANGULAR_DIAMETER} * displayMoon, moonRayleigh, moonTurbidity, moonLuminance, moonMieCoefficient, moonMieDirectionalG);
77
+
78
+ float nightIntensity = 1. - smoothstep(-.05, 0., normalizedSunPosition.y);
79
+
80
+ float moonIntensity = max(0., -dot(normalize(sunPosition.xz), normalize(moonPosition.xz)));
81
+ // skyMoonColor *= moonIntensity;
82
+ skyMoonColor *= nightIntensity;
83
+
84
+ // Stars
85
+ vec3 rayDirection = normalize(vWorldPosition);
86
+ float starsIntensity = gradientNoise3D(rayDirection * 400.) * .5 + .5;
87
+ starsIntensity = pow(starsIntensity, 15.);
88
+ starsIntensity *= max(0., rayDirection.y);
89
+ starsIntensity *= 10.;
90
+ starsIntensity *= nightIntensity;
91
+
92
+ skyMoonColor.rgb = blendScreen(skyMoonColor.rgb, vec3(starsIntensity));
93
+
94
+ vec3 skyColor = blendScreen(skySunColor.rgb, skyMoonColor.rgb);
95
+
96
+ // gl_FragColor = vec4(skyColor, (skySunColor.a + skyMoonColor.a) / 2.);
97
+ gl_FragColor = vec4(skyColor, 1.);
98
+ `],
99
+ ],
100
+ }
101
+
102
+ export default class Sky extends Mesh {
103
+ constructor({
104
+ radius = 1,
105
+ shaders = [],
106
+ } = {}) {
107
+ super(new IcosahedronBufferGeometry(radius, 3), new THREEShaderMaterial(Object.assign({
108
+ type: 'basic',
109
+ side: BackSide,
110
+ shaders,
111
+ }, skyShader)))
112
+
113
+ this._radius = radius
114
+
115
+ this.sunInclination = Math.PI * .5
116
+ this.sunAzimuth = 0
117
+
118
+ this.moonInclination = Math.PI * .5
119
+ this.moonAzimuth = Math.PI
120
+ }
121
+
122
+ get radius() {
123
+ return this._radius
124
+ }
125
+
126
+ _updatePositionFromInclinationAzimuth(position, inclination, azimuth) {
127
+ const theta = inclination
128
+ const phi = azimuth + Math.PI * .5
129
+ position.x = this._radius * Math.cos(phi) * Math.cos(theta)
130
+ position.y = this._radius * Math.sin(theta)
131
+ position.z = this._radius * Math.sin(phi) * Math.cos(theta)
132
+ }
133
+
134
+ get displaySun() {
135
+ return this.material.displaySun === 1
136
+ }
137
+
138
+ set displaySun(value) {
139
+ this.material.displaySun = value ? 1 : 0
140
+ }
141
+
142
+ get displayMoon() {
143
+ return this.material.displayMoon === 1
144
+ }
145
+
146
+ set displayMoon(value) {
147
+ this.material.displayMoon = value ? 1 : 0
148
+ }
149
+
150
+ get sunInclination() {
151
+ return this._sunInclination
152
+ }
153
+
154
+ set sunInclination(value) {
155
+ this._sunInclination = value
156
+ this._updatePositionFromInclinationAzimuth(this.sunPosition, this.sunInclination, this.sunAzimuth)
157
+ }
158
+
159
+ get sunAzimuth() {
160
+ return this._sunAzimuth
161
+ }
162
+
163
+ set sunAzimuth(value) {
164
+ this._sunAzimuth = value
165
+ this._updatePositionFromInclinationAzimuth(this.sunPosition, this.sunInclination, this.sunAzimuth)
166
+ }
167
+
168
+ get sunPosition() {
169
+ return this.material.sunPosition
170
+ }
171
+
172
+ set sunPosition(value) {
173
+ this.material.sunPosition = value
174
+ }
175
+
176
+ get sunRayleigh() {
177
+ return this.material.sunRayleigh
178
+ }
179
+
180
+ set sunRayleigh(value) {
181
+ this.material.sunRayleigh = value
182
+ }
183
+
184
+ get sunTurbidity() {
185
+ return this.material.sunTurbidity
186
+ }
187
+
188
+ set sunTurbidity(value) {
189
+ this.material.sunTurbidity = value
190
+ }
191
+
192
+ get sunLuminance() {
193
+ return this.material.sunLuminance
194
+ }
195
+
196
+ set sunLuminance(value) {
197
+ this.material.sunLuminance = value
198
+ }
199
+
200
+ get sunMieCoefficient() {
201
+ return this.material.sunMieCoefficient
202
+ }
203
+
204
+ set sunMieCoefficient(value) {
205
+ this.material.sunMieCoefficient = value
206
+ }
207
+
208
+ get sunMieDirectionalG() {
209
+ return this.material.sunMieDirectionalG
210
+ }
211
+
212
+ set sunMieDirectionalG(value) {
213
+ this.material.sunMieDirectionalG = value
214
+ }
215
+
216
+ get moonInclination() {
217
+ return this._moonInclination
218
+ }
219
+
220
+ set moonInclination(value) {
221
+ this._moonInclination = value
222
+ this._updatePositionFromInclinationAzimuth(this.moonPosition, this.moonInclination, this.moonAzimuth)
223
+ }
224
+
225
+ get moonAzimuth() {
226
+ return this._moonAzimuth
227
+ }
228
+
229
+ set moonAzimuth(value) {
230
+ this._moonAzimuth = value
231
+ this._updatePositionFromInclinationAzimuth(this.moonPosition, this.moonInclination, this.moonAzimuth)
232
+ }
233
+
234
+ get moonPosition() {
235
+ return this.material.moonPosition
236
+ }
237
+
238
+ set moonPosition(value) {
239
+ this.material.moonPosition = value
240
+ }
241
+
242
+ get moonRayleigh() {
243
+ return this.material.moonRayleigh
244
+ }
245
+
246
+ set moonRayleigh(value) {
247
+ this.material.moonRayleigh = value
248
+ }
249
+
250
+ get moonTurbidity() {
251
+ return this.material.moonTurbidity
252
+ }
253
+
254
+ set moonTurbidity(value) {
255
+ this.material.moonTurbidity = value
256
+ }
257
+
258
+ get moonLuminance() {
259
+ return this.material.moonLuminance
260
+ }
261
+
262
+ set moonLuminance(value) {
263
+ this.material.moonLuminance = value
264
+ }
265
+
266
+ get moonMieCoefficient() {
267
+ return this.material.moonMieCoefficient
268
+ }
269
+
270
+ set moonMieCoefficient(value) {
271
+ this.material.moonMieCoefficient = value
272
+ }
273
+
274
+ get moonMieDirectionalG() {
275
+ return this.material.moonMieDirectionalG
276
+ }
277
+
278
+ set moonMieDirectionalG(value) {
279
+ this.material.moonMieDirectionalG = value
280
+ }
281
+ }