@woosh/meep-engine 2.33.24 → 2.33.28

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.
Files changed (126) hide show
  1. package/core/binary/BitSet.js +151 -167
  2. package/core/binary/BitSet.spec.js +99 -1
  3. package/core/binary/ValidatingBitSetWrapper.js +81 -0
  4. package/core/bvh2/aabb3/AABB3.js +3 -10
  5. package/core/bvh2/binary/IndexedBinaryBVH.js +1 -1
  6. package/core/bvh2/traversal/ThreeClippingPlaneComputingBVHVisitor.js +5 -1
  7. package/core/bvh2/traversal/__detailed_box_volume_intersection.js +22 -5
  8. package/core/cache/Cache.d.ts +4 -2
  9. package/core/cache/Cache.js +5 -2
  10. package/core/collection/HashMap.d.ts +9 -2
  11. package/core/collection/HashSet.d.ts +7 -2
  12. package/core/collection/HashSet.js +7 -2
  13. package/core/collection/array/binarySearchHighIndex.js +1 -1
  14. package/core/collection/array/copyArray.js +2 -2
  15. package/core/collection/array/typed/typed_array_copy.js +1 -1
  16. package/core/geom/3d/frustum/array_setFromProjectionMatrix.js +3 -3
  17. package/core/geom/3d/normal/hemioct/unit_hemioct.spec.js +1 -1
  18. package/core/geom/3d/shape/AbstractShape3D.js +37 -1
  19. package/core/geom/3d/shape/TransformedShape3D.js +121 -10
  20. package/core/geom/3d/shape/TransformedShape3D.spec.js +33 -0
  21. package/core/geom/3d/shape/UnionShape3D.js +255 -0
  22. package/core/geom/3d/shape/UnitCubeShape3D.js +88 -0
  23. package/core/geom/3d/shape/UnitCubeShape3D.spec.js +42 -0
  24. package/core/geom/3d/shape/UnitSphereShape3D.js +39 -0
  25. package/core/geom/3d/shape/util/compute_signed_distance_gradient_by_sampling.js +48 -0
  26. package/core/geom/3d/shape/util/shape_to_visual_entity.js +159 -0
  27. package/core/geom/random/randomPointInSphere.js +3 -2
  28. package/core/json/resolvePathByArray.js +2 -2
  29. package/core/model/ObservedBoolean.d.ts +2 -0
  30. package/core/parser/simple/Token.d.ts +13 -0
  31. package/core/parser/simple/Token.js +61 -27
  32. package/{engine/graphics/render/forward_plus/materials → core/primitives/strings}/insert_after.js +3 -0
  33. package/core/primitives/strings/insert_after_or_replace.js +27 -0
  34. package/editor/process/symbolic/makeHelperBoxGeometry.js +44 -0
  35. package/editor/tools/FoliagePaintTool.js +4 -4
  36. package/editor/view/ecs/EntityEditor.js +2 -2
  37. package/editor/view/ecs/components/particles/ParticleEmitterController.js +2 -2
  38. package/engine/EngineHarness.js +1 -0
  39. package/engine/animation/AnimationUtils.js +5 -5
  40. package/engine/asset/loaders/GLTFAssetLoader.js +52 -1
  41. package/engine/asset/loaders/image/ImageDecoderWorker.js +4 -3
  42. package/engine/asset/loaders/image/ImageRGBADataAsset.js +3 -2
  43. package/engine/asset/loaders/image/ImageRGBADataLoader.js +3 -3
  44. package/engine/asset/loaders/image/codec/NativeImageDecoder.js +2 -1
  45. package/engine/asset/loaders/image/png/PNG.js +113 -6
  46. package/engine/asset/loaders/image/png/PNGReader.js +5 -0
  47. package/engine/asset/loaders/material/bitmap2sampler_gl.js +1 -1
  48. package/engine/asset/loaders/material/computeImageBitmapEquality.js +6 -1
  49. package/engine/asset/loaders/material/computeTextureEquality.js +40 -49
  50. package/engine/ecs/EntityBuilder.js +25 -5
  51. package/engine/ecs/EntityComponentDataset.js +1 -1
  52. package/engine/ecs/attachment/AttachmentBinding.js +8 -0
  53. package/engine/ecs/attachment/AttachmentSystem.js +4 -0
  54. package/engine/ecs/attachment/TransformAttachmentBinding.js +14 -44
  55. package/engine/ecs/components/Tag.js +27 -6
  56. package/engine/ecs/foliage/InstancedFoliage.js +277 -266
  57. package/engine/ecs/foliage/ecs/Foliage2System.js +7 -7
  58. package/engine/ecs/foliage/ecs/InstancedMeshComponent.js +37 -0
  59. package/engine/ecs/foliage/ecs/{Foliage2.js → InstancedMeshLayer.js} +3 -45
  60. package/engine/ecs/foliage/ecs/InstancedMeshSerializationAdapter.js +6 -5
  61. package/engine/ecs/foliage/ecs/InstancedMeshUtils.js +7 -6
  62. package/engine/ecs/fow/FogOfWar.js +4 -0
  63. package/engine/ecs/fow/FogOfWarSystem.js +2 -0
  64. package/engine/ecs/gui/hud/HeadsUpDisplaySystem.js +2 -0
  65. package/engine/ecs/storage/binary/BinarySerializationRegistry.js +1 -1
  66. package/engine/ecs/system/AbstractContextSystem.js +1 -1
  67. package/engine/ecs/systems/TagSystem.js +17 -0
  68. package/engine/ecs/terrain/ecs/layers/TerrainLayer.js +12 -16
  69. package/engine/ecs/transform/Transform.js +19 -6
  70. package/engine/graphics/GraphicsEngine.js +11 -6
  71. package/engine/graphics/composit/CompositLayer.js +17 -4
  72. package/engine/graphics/composit/CompositingStages.js +8 -0
  73. package/engine/graphics/composit/LayerCompositer.js +35 -6
  74. package/engine/graphics/ecs/highlight/plugin/OutlineRenderPlugin.js +11 -7
  75. package/engine/graphics/ecs/light/Light.js +2 -0
  76. package/engine/graphics/ecs/light/binding/three/ThreeLightBinding.js +5 -1
  77. package/engine/graphics/ecs/mesh-v2/DrawMode.js +5 -0
  78. package/engine/graphics/ecs/mesh-v2/ShadedGeometry.js +32 -18
  79. package/engine/graphics/geometry/instancing/InstancedMeshGroup.js +14 -72
  80. package/engine/graphics/geometry/instancing/rewriteMaterial.js +70 -0
  81. package/engine/graphics/material/manager/MaterialManager.js +2 -1
  82. package/engine/graphics/micron/render/instanced/AttributeDataTexture.js +6 -4
  83. package/engine/graphics/micron/render/instanced/ThreeInstancedAdapter.js +0 -2
  84. package/engine/graphics/particles/ParticleEmitterLibrary.js +3 -0
  85. package/engine/graphics/particles/ecs/{ParticleEmitterSystem2.js → ParticleEmitterSystem.js} +17 -9
  86. package/engine/graphics/particles/particular/engine/ParticularEngine.js +8 -2
  87. package/engine/graphics/particles/particular/engine/emitter/ParticleEmitter.js +14 -25
  88. package/engine/graphics/particles/particular/engine/emitter/ParticleEmitterFlag.js +5 -0
  89. package/engine/graphics/particles/particular/engine/emitter/ParticlePool.js +90 -2
  90. package/engine/graphics/particles/particular/engine/emitter/ParticlePool.spec.js +7 -7
  91. package/engine/graphics/particles/particular/engine/renderers/billboard/BILLBOARD_PARTICLE_SPECIFICATION.js +15 -12
  92. package/engine/graphics/particles/particular/engine/renderers/billboard/ParticleBillboardMaterial.js +262 -51
  93. package/engine/graphics/particles/particular/engine/shader/ShaderManager.js +81 -48
  94. package/engine/graphics/particles/particular/engine/shader/debugAtlas.js +14 -0
  95. package/engine/graphics/particles/particular/engine/utils/volume/AttributeValue.js +141 -0
  96. package/engine/graphics/particles/particular/engine/utils/volume/ParticleVolume.js +148 -54
  97. package/engine/graphics/particles/particular/engine/utils/volume/lut_to_sampler.js +27 -0
  98. package/engine/graphics/particles/particular/engine/utils/volume/prototypeParticleVolume.js +544 -164
  99. package/engine/graphics/render/buffer/simple-fx/ao/AmbientOcclusionPostProcessEffect.js +15 -10
  100. package/engine/graphics/render/forward_plus/materials/FP_INJECTION_POINT_ACCUMULATION.js +1 -0
  101. package/engine/graphics/render/forward_plus/materials/FP_SHADER_CHUNK_ACCUMULATION.js +32 -0
  102. package/engine/graphics/render/forward_plus/materials/FP_SHADER_CHUNK_DECODE_PARS.js +51 -0
  103. package/engine/graphics/render/forward_plus/materials/FP_SHADER_CHUNK_PREAMBLE.js +25 -0
  104. package/engine/graphics/render/forward_plus/materials/FPlusDebugMaterial.js +3 -3
  105. package/engine/graphics/render/forward_plus/materials/ForwardPlusThreeMaterial.js +1 -1
  106. package/engine/graphics/render/forward_plus/materials/fp_build_fragment_shader.js +31 -120
  107. package/engine/graphics/render/forward_plus/materials/fp_build_vertex_lighting_shared.js +35 -0
  108. package/engine/graphics/render/forward_plus/materials/fp_build_vertex_shader.js +4 -5
  109. package/engine/graphics/render/forward_plus/plugin/MaterialTransformer.js +10 -3
  110. package/engine/graphics/render/forward_plus/plugin/ptototypeFPPlugin.js +588 -53
  111. package/engine/graphics/render/forward_plus/prototypeLightManager.js +31 -6
  112. package/engine/graphics/render/forward_plus/query/point_light_inside_volume.js +10 -9
  113. package/engine/graphics/shaders/TerrainShader.js +4 -5
  114. package/engine/graphics/shadows/ShadowMapRenderer.js +8 -2
  115. package/engine/graphics/texture/atlas/ManagedTextureAtlas.js +12 -1
  116. package/engine/graphics/texture/sampler/Sampler2D.js +3 -1
  117. package/engine/graphics/texture/sampler/convertTexture2Sampler2D.js +1 -1
  118. package/engine/graphics/texture/sampler/copy_Sampler2D_channel_data.js +66 -0
  119. package/engine/graphics/util/composeMatrix4.js +33 -23
  120. package/engine/graphics/util/decompose_matrix_4_array.js +61 -0
  121. package/engine/intelligence/behavior/ecs/BehaviorComponent.js +30 -14
  122. package/engine/knowledge/database/StaticKnowledgeDataTable.js +6 -1
  123. package/engine/navigation/ecs/components/Path.js +0 -5
  124. package/package.json +1 -1
  125. package/view/minimap/gl/MinimapWorldGL.js +12 -5
  126. package/core/geom/3d/shape/CompositeShape.js +0 -60
@@ -1,17 +1,17 @@
1
1
  import { ParticlePool } from "./ParticlePool.js";
2
- import { ParticleSpecification } from "../../group/ParticleSpecification.js";
3
- import { ParticleAttribute } from "../../group/ParticleAttribute.js";
4
- import { ParticleDataType } from "../../group/ParticleDataType.js";
5
- import { ParticleAttributeType } from "../../group/ParticleAttributeType.js";
2
+ import { VertexDataSpec } from "../../../../geometry/VertexDataSpec.js";
3
+ import { DataType } from "../../../../../../core/collection/table/DataType.js";
4
+ import { AttributeSpec } from "../../../../geometry/AttributeSpec.js";
6
5
 
7
6
  /**
8
7
  *
9
8
  * @returns {ParticlePool}
10
9
  */
11
10
  function samplePool() {
12
- const specification = new ParticleSpecification();
13
- specification.add(new ParticleAttribute('a', ParticleAttributeType.Scalar, ParticleDataType.Float32));
14
- specification.add(new ParticleAttribute('b', ParticleAttributeType.Vector4, ParticleDataType.Float32));
11
+ const specification = new VertexDataSpec();
12
+
13
+ specification.add(AttributeSpec.fromJSON({name:'a', type:DataType.Float32, itemSize:1}));
14
+ specification.add(AttributeSpec.fromJSON({name:'b', type:DataType.Float32, itemSize:4}));
15
15
 
16
16
  const pool = new ParticlePool(specification);
17
17
 
@@ -1,7 +1,6 @@
1
- import { ParticleSpecification } from "../../../group/ParticleSpecification.js";
2
- import { ParticleAttribute } from "../../../group/ParticleAttribute.js";
3
- import { ParticleAttributeType } from "../../../group/ParticleAttributeType.js";
4
- import { ParticleDataType } from "../../../group/ParticleDataType.js";
1
+ import { VertexDataSpec } from "../../../../../geometry/VertexDataSpec.js";
2
+ import { AttributeSpec } from "../../../../../geometry/AttributeSpec.js";
3
+ import { DataType } from "../../../../../../../core/collection/table/DataType.js";
5
4
 
6
5
  export const BILLBOARD_PARTICLE_ATTRIBUTE_POSITION = 0;
7
6
  export const BILLBOARD_PARTICLE_ATTRIBUTE_UV = 1;
@@ -10,12 +9,16 @@ export const BILLBOARD_PARTICLE_ATTRIBUTE_VELOCITY = 3;
10
9
  export const BILLBOARD_PARTICLE_ATTRIBUTE_COLOR = 5;
11
10
  export const BILLBOARD_PARTICLE_ATTRIBUTE_BLEND = 6;
12
11
 
13
- export const BILLBOARD_PARTICLE_SPECIFICATION = new ParticleSpecification();
12
+ export const BILLBOARD_PARTICLE_SPECIFICATION = new VertexDataSpec();
14
13
 
15
- BILLBOARD_PARTICLE_SPECIFICATION.add(new ParticleAttribute('position', ParticleAttributeType.Vector3, ParticleDataType.Float32));
16
- BILLBOARD_PARTICLE_SPECIFICATION.add(new ParticleAttribute('atlasPatch', ParticleAttributeType.Vector4, ParticleDataType.Float32));
17
- BILLBOARD_PARTICLE_SPECIFICATION.add(new ParticleAttribute('size', ParticleAttributeType.Scalar, ParticleDataType.Float32));
18
- BILLBOARD_PARTICLE_SPECIFICATION.add(new ParticleAttribute('velocity', ParticleAttributeType.Vector3, ParticleDataType.Float32));
19
- BILLBOARD_PARTICLE_SPECIFICATION.add(new ParticleAttribute('rotation', ParticleAttributeType.Scalar, ParticleDataType.Float32));
20
- BILLBOARD_PARTICLE_SPECIFICATION.add(new ParticleAttribute('color', ParticleAttributeType.Vector4, ParticleDataType.Float32));
21
- BILLBOARD_PARTICLE_SPECIFICATION.add(new ParticleAttribute('blending', ParticleAttributeType.Scalar, ParticleDataType.Uint8));
14
+ BILLBOARD_PARTICLE_SPECIFICATION.add(AttributeSpec.fromJSON({ name: 'position', itemSize: 3, type: DataType.Float32 }));
15
+ BILLBOARD_PARTICLE_SPECIFICATION.add(AttributeSpec.fromJSON({
16
+ name: 'atlasPatch',
17
+ itemSize: 4,
18
+ type: DataType.Float32
19
+ }));
20
+ BILLBOARD_PARTICLE_SPECIFICATION.add(AttributeSpec.fromJSON({ name: 'size', itemSize: 1, type: DataType.Float32 }));
21
+ BILLBOARD_PARTICLE_SPECIFICATION.add(AttributeSpec.fromJSON({ name: 'velocity', itemSize: 3, type: DataType.Float32 }));
22
+ BILLBOARD_PARTICLE_SPECIFICATION.add(AttributeSpec.fromJSON({ name: 'rotation', itemSize: 1, type: DataType.Float32 }));
23
+ BILLBOARD_PARTICLE_SPECIFICATION.add(AttributeSpec.fromJSON({ name: 'color', itemSize: 1, type: DataType.Float32 }));
24
+ BILLBOARD_PARTICLE_SPECIFICATION.add(AttributeSpec.fromJSON({ name: 'blending', itemSize: 1, type: DataType.Uint8 }));
@@ -1,8 +1,141 @@
1
- import { AddEquation, CustomBlending, Matrix4, OneFactor, OneMinusSrcAlphaFactor, ShaderMaterial } from "three";
1
+ import {
2
+ AddEquation,
3
+ CustomBlending,
4
+ GLSL3,
5
+ Matrix4,
6
+ OneFactor,
7
+ OneMinusSrcAlphaFactor,
8
+ ShaderMaterial,
9
+ UniformsLib,
10
+ UniformsUtils
11
+ } from "three";
2
12
  import { Vector2 as ThreeVector2 } from "three/src/math/Vector2.js";
3
13
 
4
14
  const DEPTH_ERROR_MARGIN = 0.0001;
5
15
 
16
+ const CHUNK_LAMBERT_LIGHTING = `
17
+ vec3 diffuse = vec3( 1.0 );
18
+
19
+ GeometricContext geometry;
20
+ geometry.position = mvPosition.xyz;
21
+ geometry.normal = normalize( transformedNormal );
22
+ geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );
23
+
24
+ GeometricContext backGeometry;
25
+ backGeometry.position = geometry.position;
26
+ backGeometry.normal = -geometry.normal;
27
+ backGeometry.viewDir = geometry.viewDir;
28
+
29
+ vLightFront = vec3( 0.0 );
30
+ vIndirectFront = vec3( 0.0 );
31
+ #ifdef DOUBLE_SIDED
32
+ vLightBack = vec3( 0.0 );
33
+ vIndirectBack = vec3( 0.0 );
34
+ #endif
35
+
36
+ IncidentLight directLight;
37
+ float dotNL;
38
+ vec3 directLightColor_Diffuse;
39
+
40
+ vIndirectFront += getAmbientLightIrradiance( ambientLightColor );
41
+
42
+ vIndirectFront += getLightProbeIrradiance( lightProbe, geometry.normal );
43
+
44
+ #ifdef DOUBLE_SIDED
45
+
46
+ vIndirectBack += getAmbientLightIrradiance( ambientLightColor );
47
+
48
+ vIndirectBack += getLightProbeIrradiance( lightProbe, backGeometry.normal );
49
+
50
+ #endif
51
+
52
+ #if NUM_POINT_LIGHTS > 0
53
+
54
+ #pragma unroll_loop_start
55
+ for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
56
+
57
+ getPointLightInfo( pointLights[ i ], geometry, directLight );
58
+
59
+ dotNL = dot( geometry.normal, directLight.direction );
60
+ directLightColor_Diffuse = directLight.color;
61
+
62
+ vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
63
+
64
+ #ifdef DOUBLE_SIDED
65
+
66
+ vLightBack += saturate( - dotNL ) * directLightColor_Diffuse;
67
+
68
+ #endif
69
+
70
+ }
71
+ #pragma unroll_loop_end
72
+
73
+ #endif
74
+
75
+ #if NUM_SPOT_LIGHTS > 0
76
+
77
+ #pragma unroll_loop_start
78
+ for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
79
+
80
+ getSpotLightInfo( spotLights[ i ], geometry, directLight );
81
+
82
+ dotNL = dot( geometry.normal, directLight.direction );
83
+ directLightColor_Diffuse = directLight.color;
84
+
85
+ vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
86
+
87
+ #ifdef DOUBLE_SIDED
88
+
89
+ vLightBack += saturate( - dotNL ) * directLightColor_Diffuse;
90
+
91
+ #endif
92
+ }
93
+ #pragma unroll_loop_end
94
+
95
+ #endif
96
+
97
+ #if NUM_DIR_LIGHTS > 0
98
+
99
+ #pragma unroll_loop_start
100
+ for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
101
+
102
+ getDirectionalLightInfo( directionalLights[ i ], geometry, directLight );
103
+
104
+ dotNL = dot( geometry.normal, directLight.direction );
105
+ directLightColor_Diffuse = directLight.color;
106
+
107
+ vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
108
+
109
+ #ifdef DOUBLE_SIDED
110
+
111
+ vLightBack += saturate( - dotNL ) * directLightColor_Diffuse;
112
+
113
+ #endif
114
+
115
+ }
116
+ #pragma unroll_loop_end
117
+
118
+ #endif
119
+
120
+ #if NUM_HEMI_LIGHTS > 0
121
+
122
+ #pragma unroll_loop_start
123
+ for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
124
+
125
+ vIndirectFront += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
126
+
127
+ #ifdef DOUBLE_SIDED
128
+
129
+ vIndirectBack += getHemisphereLightIrradiance( hemisphereLights[ i ], backGeometry.normal );
130
+
131
+ #endif
132
+
133
+ }
134
+ #pragma unroll_loop_end
135
+
136
+ #endif
137
+ `;
138
+
6
139
  function make_vertex_shader() {
7
140
  return `
8
141
  uniform float cameraNear;
@@ -23,6 +156,29 @@ function make_vertex_shader() {
23
156
  varying vec2 vRotationMultiplier;
24
157
  varying float vBlending;
25
158
 
159
+
160
+ #if defined(LIGHTS_ENABLED)
161
+ #include <common>
162
+ #include <bsdfs>
163
+ #include <lights_pars_begin>
164
+
165
+ struct LambertMaterial {
166
+ vec3 diffuseColor;
167
+ };
168
+
169
+ void RE_Direct_Lambert( const in IncidentLight directLight, const in GeometricContext geometry, const in LambertMaterial material, inout ReflectedLight reflectedLight ) {
170
+ vec3 irradiance = directLight.color;
171
+ reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
172
+ }
173
+
174
+ void RE_IndirectDiffuse_Lambert( const in vec3 irradiance, const in GeometricContext geometry, const in LambertMaterial material, inout ReflectedLight reflectedLight ) {
175
+ reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
176
+ }
177
+
178
+ #define RE_Direct RE_Direct_Lambert
179
+ #define RE_IndirectDiffuse RE_IndirectDiffuse_Lambert
180
+ #endif
181
+
26
182
  void main() {
27
183
 
28
184
  vColor = color;
@@ -32,9 +188,20 @@ function make_vertex_shader() {
32
188
 
33
189
  mat4 modelViewProjectionMatrix = projectionMatrix * modelViewMatrix;
34
190
 
191
+ vec3 transformed = position;
192
+
35
193
  vec4 position4 = vec4( position, 1.0 );
36
-
194
+
195
+ #if defined(LIGHTS_ENABLED)
196
+ vec4 mvPosition = modelViewMatrix * position4;
197
+ vec3 vViewPosition = -mvPosition.xyz;
198
+
199
+ vec3 transformedNormal = vec3(0.0,0.0,1.0);
200
+ #else
201
+ #endif
202
+
37
203
  gl_Position = modelViewProjectionMatrix * position4;
204
+
38
205
 
39
206
  float rotationGlobal = rotation;
40
207
 
@@ -59,18 +226,48 @@ function make_vertex_shader() {
59
226
 
60
227
  vRotationMultiplier = vec2( cos(rotationGlobal),sin(rotationGlobal) );
61
228
 
62
- float radius = vSize/2.0;
229
+ float radius = vSize * 0.5;
63
230
 
231
+ float projected_radius = resolution.y * projectionMatrix[1][1] * radius / gl_Position.w;;
232
+
64
233
  #ifdef DEPTH_SOFT_ENABLED
65
234
 
66
- vFadeDistance = 1.0 / radius;
235
+ vFadeDistance = 1.0 / radius;
67
236
 
68
237
  #endif
69
-
70
- gl_PointSize = resolution.y * projectionMatrix[1][1] * radius / gl_Position.w;
238
+
239
+ gl_PointSize = projected_radius;
240
+
241
+ #if defined(LIGHTS_ENABLED)
242
+ vec3 vLightFront;
243
+ vec3 vIndirectFront;
244
+
245
+ #include <worldpos_vertex>
246
+ ${CHUNK_LAMBERT_LIGHTING}
247
+
248
+ LambertMaterial material = LambertMaterial(vColor.rgb);
249
+
250
+ ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
251
+ vec3 totalEmissiveRadiance = vec3(0.0);
252
+
253
+ reflectedLight.indirectDiffuse += vIndirectFront;
254
+ reflectedLight.indirectDiffuse *= BRDF_Lambert( vColor.rgb );
255
+
256
+ reflectedLight.directDiffuse = vLightFront;
257
+ reflectedLight.directDiffuse *= BRDF_Lambert( vColor.rgb ); // shadowmask goes here
258
+
259
+ //@injection_point FORWARD_PLUS:accumulation
260
+
261
+ vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;
262
+
263
+ vColor.rgb = outgoingLight;
264
+ #endif
265
+
71
266
  }`;
72
267
  }
73
268
 
269
+ //${CHUNK_LAMBERT_LIGHTING}
270
+ //#include <lights_lambert_vertex>
74
271
  function make_fragment_shader() {
75
272
  return `
76
273
  uniform sampler2D tSpriteAtlas;
@@ -88,6 +285,9 @@ function make_fragment_shader() {
88
285
  varying vec2 vRotationMultiplier;
89
286
  varying float vBlending;
90
287
 
288
+
289
+ out highp vec4 pc_fragColor;
290
+
91
291
  float depthToLinear( float z ) {
92
292
  float f = cameraFar;
93
293
  float n = cameraNear;
@@ -163,7 +363,7 @@ function make_fragment_shader() {
163
363
  discard;
164
364
  }
165
365
 
166
- vec4 texel = spriteColor *vColor;
366
+ vec4 texel = spriteColor * vColor;
167
367
 
168
368
  // premultiply alpha
169
369
  texel.xyz *= texel.a;
@@ -180,68 +380,78 @@ function make_fragment_shader() {
180
380
 
181
381
  #endif
182
382
 
183
- gl_FragColor = texel;
383
+ pc_fragColor = texel;
184
384
 
185
385
  }`;
186
386
  }
187
387
 
188
- /*
189
- NOTES:
190
- - To implement per-particle blend modes, we need to have access to what we're blending on to, that is - the color buffer
191
- Having the color buffer - we can do just about any kind of a blend
192
-
193
- - Support animation by adding animation sprite sheet to the main sprite atlas, record resolution of the sheet (in frames X,Y) and interpolate over tiles based on some parameter
388
+ /**
389
+ * NOTES:
390
+ * - To implement per-particle blend modes, we need to have access to what we're blending on to, that is - the color buffer
391
+ * Having the color buffer - we can do just about any kind of a blend
392
+ *
393
+ * - Support animation by adding animation sprite sheet to the main sprite atlas, record resolution of the sheet (in frames X,Y) and interpolate over tiles based on some parameter
394
+ * @param {boolean} [lights]
194
395
  */
195
- export function make_particle_billboard_shader() {
396
+ export function make_particle_billboard_shader({ lights = false } = {}) {
397
+ let uniforms = {
398
+ tSpriteAtlas: {
399
+ type: 't',
400
+ value: null
401
+ },
402
+ cameraNear: {
403
+ type: 'f',
404
+ value: 1,
405
+ },
406
+ cameraFar: {
407
+ type: 'f',
408
+ value: 1,
409
+ },
410
+ tDepth: {
411
+ type: 't',
412
+ value: null
413
+ },
414
+ tColor: {
415
+ type: 't',
416
+ value: null
417
+ },
418
+ modelViewMatrix: {
419
+ type: 'm4',
420
+ value: new Matrix4()
421
+ },
422
+ projectionMatrix: {
423
+ type: 'm4',
424
+ value: new Matrix4()
425
+ },
426
+ resolution: {
427
+ type: 'v2',
428
+ value: new ThreeVector2()
429
+ }
430
+ };
431
+
432
+ if (lights) {
433
+ // mix in light uniforms
434
+ uniforms = Object.assign(uniforms, UniformsUtils.clone(UniformsLib.lights));
435
+ }
436
+
196
437
  const shader = new ShaderMaterial({
197
438
  defines: {
198
439
  DEPTH_READ_ENABLED: true,
199
440
  DEPTH_SOFT_ENABLED: true,
200
441
  ALIGN_DIRECTION_ENABLED: false,
442
+ LIGHTS_ENABLED: lights,
443
+ LIGHTING_VERTEX_ONLY: true
201
444
  },
202
- uniforms: {
203
- tSpriteAtlas: {
204
- type: 't',
205
- value: null
206
- },
207
- cameraNear: {
208
- type: 'f',
209
- value: 1,
210
- },
211
- cameraFar: {
212
- type: 'f',
213
- value: 1,
214
- },
215
- tDepth: {
216
- type: 't',
217
- value: null
218
- },
219
- tColor: {
220
- type: 't',
221
- value: null
222
- },
223
- modelViewMatrix: {
224
- type: 'm4',
225
- value: new Matrix4()
226
- },
227
- projectionMatrix: {
228
- type: 'm4',
229
- value: new Matrix4()
230
- },
231
- resolution: {
232
- type: 'v2',
233
- value: new ThreeVector2()
234
- }
235
- },
445
+ uniforms: uniforms,
236
446
  vertexShader: make_vertex_shader(),
237
447
  fragmentShader: make_fragment_shader(),
238
448
  blending: CustomBlending,
239
- lights: false,
449
+ lights: lights,
240
450
  depthTest: true,
241
451
  depthWrite: false,
242
452
  transparent: true,
243
453
  vertexColors: false,
244
- glslVersion: 2
454
+ glslVersion: GLSL3
245
455
  });
246
456
 
247
457
  shader.blending = CustomBlending;
@@ -249,5 +459,6 @@ export function make_particle_billboard_shader() {
249
459
  shader.blendSrc = OneFactor;
250
460
  shader.blendDst = OneMinusSrcAlphaFactor;
251
461
 
462
+
252
463
  return shader;
253
464
  }
@@ -6,17 +6,22 @@ import {
6
6
  LinearMipMapLinearFilter,
7
7
  Matrix4,
8
8
  RGBAFormat,
9
- UnsignedByteType
9
+ UnsignedByteType,
10
+ Vector2 as ThreeVector2
10
11
  } from "three";
11
12
  import Vector2 from "../../../../../../core/geom/Vector2.js";
12
13
  import { HashMap } from "../../../../../../core/collection/HashMap.js";
13
14
  import { ParticleEmitterFlag } from "../emitter/ParticleEmitterFlag.js";
14
15
  import { writeSample2DDataToDataTexture } from "../../../../texture/sampler/writeSampler2DDataToDataTexture.js";
15
- import { TextureAtlasDebugger } from "../../../../texture/atlas/TextureAtlasDebugger.js";
16
16
  import { MaterialRecord } from "./MaterialRecord.js";
17
17
  import { make_particle_billboard_shader } from "../renderers/billboard/ParticleBillboardMaterial.js";
18
18
 
19
- const EMITTER_MATERIAL_FLAGS = ParticleEmitterFlag.DepthSoftDisabled | ParticleEmitterFlag.DepthReadDisabled | ParticleEmitterFlag.AlignOnVelocity;
19
+ const EMITTER_MATERIAL_FLAGS =
20
+ ParticleEmitterFlag.DepthSoftDisabled
21
+ | ParticleEmitterFlag.DepthReadDisabled
22
+ | ParticleEmitterFlag.AlignOnVelocity
23
+ | ParticleEmitterFlag.Lit
24
+ ;
20
25
 
21
26
  /**
22
27
  *
@@ -82,21 +87,21 @@ function buildDataTexture(
82
87
  return result;
83
88
  }
84
89
 
85
- function debugAtlas(atlas, options) {
86
-
87
- const spriteAtlasDebugger = new TextureAtlasDebugger(atlas, options);
88
-
89
- spriteAtlasDebugger.vAtlasContent.link();
90
- document.body.appendChild(spriteAtlasDebugger.vAtlasContent.el);
91
- }
92
-
93
90
  export class ShaderManager {
94
91
  /**
95
92
  *
96
93
  * @param {AssetManager} assetManager
94
+ * @param {MaterialManager} material_manager
97
95
  * @constructor
98
96
  */
99
- constructor(assetManager) {
97
+ constructor(assetManager, material_manager) {
98
+ /**
99
+ *
100
+ * @type {MaterialManager}
101
+ * @private
102
+ */
103
+ this.__material_manager = material_manager;
104
+
100
105
  /**
101
106
  *
102
107
  * @type {ManagedAtlas}
@@ -159,8 +164,35 @@ export class ShaderManager {
159
164
  //watch atlas updates
160
165
  this.spriteAtlas.atlas.on.painted.add(this.updateSpriteTexture, this);
161
166
 
162
- // debugAtlas(this.parameterSheet.atlas, { scale: new Vector2(16, 16) });
163
- // debugAtlas(this.spriteAtlas.atlas, { scale: new Vector2(0.5, 0.5) });
167
+ /**
168
+ * Common shader uniforms, inherited by every material and updated centrally (here)
169
+ * @private
170
+ */
171
+ this.__common_uniforms = {
172
+ tSpriteAtlas: {
173
+ value: this.spriteTexture
174
+ },
175
+ tDepth: {
176
+ value: null
177
+ },
178
+ resolution: {
179
+ value: new ThreeVector2()
180
+ },
181
+ projectionMatrix: {
182
+ value: null
183
+ },
184
+ modelViewMatrix: {
185
+ value: null
186
+ },
187
+ cameraNear: {
188
+ value: 0
189
+ },
190
+ cameraFar: {
191
+ value: 0
192
+ }
193
+ };
194
+
195
+ // debugAtlas(this.spriteAtlas.atlas, { scale: new Vector2(1, 1) });
164
196
  }
165
197
 
166
198
  /**
@@ -184,8 +216,6 @@ export class ShaderManager {
184
216
  writeSample2DDataToDataTexture(atlas, dataTexture);
185
217
 
186
218
  this.materialMap.forEach(function (entry) {
187
- const material = entry.material;
188
- material.uniforms.tSpriteAtlas.value = dataTexture;
189
219
 
190
220
  entry.emitters.forEach(function (emitter) {
191
221
  //mark sprites for an update
@@ -202,19 +232,24 @@ export class ShaderManager {
202
232
  * @param {ParticleEmitter} emitter
203
233
  */
204
234
  buildMaterial(emitter) {
205
- const shader = make_particle_billboard_shader();
235
+ const lights_enabled = emitter.getFlag(ParticleEmitterFlag.Lit);
206
236
 
207
- shader.uniforms.tSpriteAtlas.value = this.spriteTexture;
208
- shader.uniforms.tDepth.value = this.depthTexture;
209
- shader.uniforms.resolution.value.set(this.viewportSize.x, this.viewportSize.y)
237
+ const shader = make_particle_billboard_shader({ lights: lights_enabled });
210
238
 
211
- shader.defines = {
212
- DEPTH_READ_ENABLED: !emitter.getFlag(ParticleEmitterFlag.DepthReadDisabled),
213
- DEPTH_SOFT_ENABLED: !emitter.getFlag(ParticleEmitterFlag.DepthSoftDisabled),
214
- ALIGN_DIRECTION_ENABLED: emitter.getFlag(ParticleEmitterFlag.AlignOnVelocity)
215
- };
239
+ shader.uniforms = Object.assign(shader.uniforms, this.__common_uniforms);
240
+
241
+ // write defines
242
+ const defs = shader.defines;
216
243
 
217
- return shader;
244
+ defs.DEPTH_READ_ENABLED = !emitter.getFlag(ParticleEmitterFlag.DepthReadDisabled);
245
+ defs.DEPTH_SOFT_ENABLED = !emitter.getFlag(ParticleEmitterFlag.DepthSoftDisabled);
246
+ defs.ALIGN_DIRECTION_ENABLED = emitter.getFlag(ParticleEmitterFlag.AlignOnVelocity);
247
+
248
+ const ref = this.__material_manager.obtain(shader);
249
+
250
+ // TODO track referecen and release it eventually
251
+
252
+ return ref.getValue();
218
253
  }
219
254
 
220
255
  /**
@@ -225,7 +260,12 @@ export class ShaderManager {
225
260
  const spriteAtlas = this.spriteAtlas;
226
261
 
227
262
  // load sprites
228
- emitter.layers.forEach(function (layer) {
263
+ const particle_layers = emitter.layers;
264
+ const layer_count = particle_layers.length;
265
+
266
+ for (let i = 0; i < layer_count; i++) {
267
+ const layer = particle_layers.get(i);
268
+
229
269
  const patchPromise = spriteAtlas.acquire(layer.imageURL);
230
270
 
231
271
  patchPromise.then(function (patch) {
@@ -233,7 +273,7 @@ export class ShaderManager {
233
273
  }, (e) => {
234
274
  console.warn(`Failed to load sprite '${layer.imageURL}':`, e);
235
275
  });
236
- });
276
+ }
237
277
 
238
278
  // obtain material
239
279
  let material, entry;
@@ -312,10 +352,12 @@ export class ShaderManager {
312
352
  const spriteAtlas = this.spriteAtlas;
313
353
 
314
354
  //release sprites
315
- emitter.layers.forEach(function (layer) {
355
+ const particle_layers = emitter.layers;
356
+ const layer_count = particle_layers.length;
357
+ for (let i = 0; i < layer_count; i++) {
358
+ const layer = particle_layers.get(i);
316
359
  spriteAtlas.release(layer.imageURL);
317
- });
318
-
360
+ }
319
361
 
320
362
  if (entry.count <= 0) {
321
363
  //last usage removed
@@ -348,15 +390,12 @@ export class ShaderManager {
348
390
  const cameraNear = camera.near;
349
391
  const cameraFar = camera.far;
350
392
 
351
- // TODO use a shader uniform structure to avoid iteration
352
- this.materialMap.forEach(function (entry) {
353
- const uniforms = entry.material.uniforms;
393
+ const uniforms = this.__common_uniforms;
354
394
 
355
- uniforms.projectionMatrix.value = projectionMatrix;
356
- uniforms.modelViewMatrix.value = modelViewMatrix;
357
- uniforms.cameraNear.value = cameraNear;
358
- uniforms.cameraFar.value = cameraFar;
359
- });
395
+ uniforms.projectionMatrix.value = projectionMatrix;
396
+ uniforms.modelViewMatrix.value = modelViewMatrix;
397
+ uniforms.cameraNear.value = cameraNear;
398
+ uniforms.cameraFar.value = cameraFar;
360
399
  }
361
400
 
362
401
  /**
@@ -366,11 +405,8 @@ export class ShaderManager {
366
405
  setDepthTexture(texture) {
367
406
  this.depthTexture = texture;
368
407
 
369
- // TODO use a shader uniform structure to avoid iteration
370
- this.materialMap.forEach(function (entry) {
371
- const material = entry.material;
372
- material.uniforms.tDepth.value = texture;
373
- });
408
+ this.__common_uniforms.tDepth.value = texture;
409
+
374
410
  }
375
411
 
376
412
  /**
@@ -381,10 +417,7 @@ export class ShaderManager {
381
417
  setViewportSize(x, y) {
382
418
  this.viewportSize.set(x, y);
383
419
 
384
- // TODO use a shader uniform structure to avoid iteration
385
- this.materialMap.forEach(function (entry) {
386
- entry.material.uniforms.resolution.value.set(x, y);
387
- });
420
+ this.__common_uniforms.resolution.value.set(x, y);
388
421
  }
389
422
  }
390
423
 
@@ -0,0 +1,14 @@
1
+ import { TextureAtlasDebugger } from "../../../../texture/atlas/TextureAtlasDebugger.js";
2
+
3
+ /**
4
+ *
5
+ * @param {TextureAtlas} atlas
6
+ * @param {*} options
7
+ */
8
+ export function debugAtlas(atlas, options) {
9
+
10
+ const spriteAtlasDebugger = new TextureAtlasDebugger(atlas, options);
11
+
12
+ spriteAtlasDebugger.vAtlasContent.link();
13
+ document.body.appendChild(spriteAtlasDebugger.vAtlasContent.el);
14
+ }