@woosh/meep-engine 2.41.0 → 2.42.1

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 (98) hide show
  1. package/core/assert.js +2 -2
  2. package/core/collection/array/array_swap.js +3 -3
  3. package/core/collection/map/AsyncLoadingCache.js +47 -0
  4. package/core/geom/3d/aabb/aabb3_compute_distance_above_plane_max.js +1 -1
  5. package/core/geom/3d/apply_mat4_transform_to_v3_array.js +2 -4
  6. package/core/geom/3d/sphere/sphere_radius_sqr_from_v3_array_transformed.js +28 -0
  7. package/core/geom/Quaternion.js +14 -0
  8. package/core/math/statistics/computeSampleSize_Cochran.js +3 -3
  9. package/editor/ecs/component/editors/geom/QuaternionEditor.js +12 -5
  10. package/engine/Engine.js +6 -1
  11. package/engine/EngineBootstrapper.js +2 -1
  12. package/engine/EngineHarness.js +13 -3
  13. package/engine/asset/AssetManager.js +97 -7
  14. package/engine/development/performance/AbstractMetric.js +1 -0
  15. package/engine/development/performance/RingBufferMetric.js +25 -4
  16. package/engine/ecs/EntityBuilder.js +29 -4
  17. package/engine/ecs/transform/Transform.js +23 -3
  18. package/engine/graphics/ecs/camera/topdown/TopDownCameraControllerSystem.js +17 -1
  19. package/engine/graphics/ecs/decal/v2/Decal.d.ts +11 -0
  20. package/engine/graphics/ecs/decal/v2/Decal.js +50 -0
  21. package/engine/graphics/ecs/decal/v2/FPDecalSystem.d.ts +8 -0
  22. package/engine/graphics/ecs/decal/v2/FPDecalSystem.js +201 -0
  23. package/engine/graphics/ecs/decal/v2/prototypeDecalSystem.js +278 -0
  24. package/engine/graphics/ecs/mesh-v2/ShadedGeometry.js +8 -1
  25. package/engine/graphics/ecs/mesh-v2/allocate_v3.js +37 -0
  26. package/engine/graphics/ecs/mesh-v2/build_three_object.js +4 -0
  27. package/engine/graphics/geometry/MikkT/AddTriToGroup.js +10 -0
  28. package/engine/graphics/geometry/MikkT/AssignRecur.js +84 -0
  29. package/engine/graphics/geometry/MikkT/AvgTSpace.js +38 -0
  30. package/engine/graphics/geometry/MikkT/Build4RuleGroups.js +96 -0
  31. package/engine/graphics/geometry/MikkT/BuildNeighborsFast.js +137 -0
  32. package/engine/graphics/geometry/MikkT/CalcTexArea.js +31 -0
  33. package/engine/graphics/geometry/MikkT/CompareSubGroups.js +26 -0
  34. package/engine/graphics/geometry/MikkT/DegenEpilogue.js +220 -0
  35. package/engine/graphics/geometry/MikkT/DegenPrologue.js +115 -0
  36. package/engine/graphics/geometry/MikkT/EvalTspace.js +128 -0
  37. package/engine/graphics/geometry/MikkT/GenerateInitialVerticesIndexList.js +48 -0
  38. package/engine/graphics/geometry/MikkT/GenerateSharedVerticesIndexList.js +184 -0
  39. package/engine/graphics/geometry/MikkT/GenerateTSpaces.js +226 -0
  40. package/engine/graphics/geometry/MikkT/GetEdge.js +45 -0
  41. package/engine/graphics/geometry/MikkT/GetNormal.js +16 -0
  42. package/engine/graphics/geometry/MikkT/GetPosition.js +25 -0
  43. package/engine/graphics/geometry/MikkT/GetTexCoord.js +18 -0
  44. package/engine/graphics/geometry/MikkT/InitTriInfo.js +180 -0
  45. package/engine/graphics/geometry/MikkT/Length.js +10 -0
  46. package/engine/graphics/geometry/MikkT/MakeIndex.js +18 -0
  47. package/engine/graphics/geometry/MikkT/MikkTSpace.js +197 -2068
  48. package/engine/graphics/geometry/MikkT/NormalizeSafe.js +21 -0
  49. package/engine/graphics/geometry/MikkT/NotZero.js +10 -0
  50. package/engine/graphics/geometry/MikkT/QuickSort.js +54 -0
  51. package/engine/graphics/geometry/MikkT/QuickSortEdges.js +71 -0
  52. package/engine/graphics/geometry/MikkT/SSubGroup.js +15 -0
  53. package/engine/graphics/geometry/MikkT/STSpace.js +36 -0
  54. package/engine/graphics/geometry/MikkT/constants/GROUP_WITH_ANY.js +1 -0
  55. package/engine/graphics/geometry/MikkT/constants/INTERNAL_RND_SORT_SEED.js +1 -0
  56. package/engine/graphics/geometry/MikkT/constants/MARK_DEGENERATE.js +1 -0
  57. package/engine/graphics/geometry/MikkT/constants/ORIENT_PRESERVING.js +1 -0
  58. package/engine/graphics/geometry/MikkT/constants/QUAD_ONE_DEGEN_TRI.js +1 -0
  59. package/engine/graphics/geometry/MikkT/m_getNormal.js +16 -0
  60. package/engine/graphics/geometry/MikkT/m_getNumFaces.js +8 -0
  61. package/engine/graphics/geometry/MikkT/m_getNumVerticesOfFace.js +11 -0
  62. package/engine/graphics/geometry/MikkT/m_getPosition.js +20 -0
  63. package/engine/graphics/geometry/MikkT/m_getTexCoord.js +16 -0
  64. package/engine/graphics/geometry/MikkT/m_setTSpace.js +35 -0
  65. package/engine/graphics/geometry/MikkT/m_setTSpaceBasic.js +22 -0
  66. package/engine/graphics/geometry/MikkT/malloc.js +16 -0
  67. package/engine/graphics/geometry/MikkT/v3_scale_dot_sub_normalize.js +52 -0
  68. package/engine/graphics/geometry/buffered/computeGeometryEquality.js +1 -1
  69. package/engine/graphics/geometry/buffered/computeGeometryHash.js +1 -1
  70. package/engine/graphics/impostors/octahedral/ImpostorBaker.js +28 -14
  71. package/engine/graphics/impostors/octahedral/ImpostorDescription.js +6 -0
  72. package/engine/graphics/impostors/octahedral/README.md +1 -0
  73. package/engine/graphics/impostors/octahedral/bake/compute_bounding_sphere.js +25 -22
  74. package/engine/graphics/impostors/octahedral/bake/compute_bounding_sphere_radius_only.js +37 -0
  75. package/engine/graphics/impostors/octahedral/bake/prepare_bake_material.js +30 -1
  76. package/engine/graphics/impostors/octahedral/grid/HemiOctahedralUvEncoder.js +1 -1
  77. package/engine/graphics/impostors/octahedral/prototypeBaker.js +121 -22
  78. package/engine/graphics/impostors/octahedral/shader/BakeShaderStandard.js +46 -7
  79. package/engine/graphics/impostors/octahedral/shader/ImpostorShaderV0.js +349 -0
  80. package/engine/graphics/impostors/octahedral/shader/ImpostorShaderV1.js +74 -0
  81. package/engine/graphics/impostors/octahedral/shader/glsl/v1/common.glsl +209 -0
  82. package/engine/graphics/impostors/octahedral/shader/glsl/v1/flagment.glsl +80 -0
  83. package/engine/graphics/impostors/octahedral/shader/glsl/v1/vertex.glsl +350 -0
  84. package/engine/graphics/micron/render/v1/getTransformedPositionsCached.js +1 -1
  85. package/engine/graphics/render/forward_plus/LightManager.js +17 -7
  86. package/engine/graphics/render/forward_plus/data/TextureBackedMemoryRegion.js +7 -1
  87. package/engine/graphics/render/forward_plus/materials/FP_SHADER_CHUNK_ACCUMULATION.js +13 -5
  88. package/engine/graphics/render/forward_plus/materials/FP_SHADER_CHUNK_PREAMBLE.js +3 -1
  89. package/engine/graphics/render/forward_plus/model/Decal.js +19 -2
  90. package/engine/graphics/render/forward_plus/plugin/MaterialTransformer.js +14 -2
  91. package/engine/graphics/render/forward_plus/query/query_bvh_frustum_from_texture.js +2 -2
  92. package/engine/graphics/texture/sampler/Sampler2D.js +10 -10
  93. package/engine/graphics/texture/sampler/prototypeSamplerFiltering.js +117 -11
  94. package/engine/graphics/texture/sampler/resize/sampler2d_downsample_mipmap.js +66 -0
  95. package/engine/graphics/texture/sampler/sampler2_d_scale_down_lanczos.js +2 -2
  96. package/engine/intelligence/behavior/util/RotationBehavior.js +69 -0
  97. package/generation/example/filters/SampleGroundMoistureFilter.js +5 -5
  98. package/package.json +1 -1
@@ -0,0 +1,349 @@
1
+ import {
2
+ AddEquation,
3
+ CustomBlending,
4
+ GLSL3,
5
+ OneFactor,
6
+ OneMinusSrcAlphaFactor,
7
+ RawShaderMaterial,
8
+ Vector3
9
+ } from "three";
10
+
11
+ /*
12
+ *
13
+ * For ray projection using projection matrix : https://encreative.blogspot.com/2019/05/computing-ray-origin-and-direction-from.html
14
+ */
15
+ const shader_vx = `
16
+
17
+ in vec2 uv;
18
+ in vec3 position;
19
+ in vec3 normal;
20
+
21
+ out vec2 vUv;
22
+ out vec4 plane0;
23
+ out vec4 plane1;
24
+ out vec4 plane2;
25
+
26
+ out vec3 local_ray_near;
27
+ out vec3 local_ray_far;
28
+
29
+ uniform mat4 modelViewMatrix;
30
+ uniform mat4 projectionMatrix;
31
+ uniform mat3 normalMatrix;
32
+
33
+ uniform vec3 uOffset;
34
+ uniform float uRadius;
35
+ uniform float uFrames;
36
+
37
+ void main() {
38
+ vUv = uv;
39
+
40
+ vec2 framesMinusOne = uFrames - vec2(1.0);
41
+
42
+ mat4 m4 = modelViewMatrix;
43
+
44
+ m4[0][0] = 1.0;
45
+ m4[0][1] = 0.0;
46
+ m4[0][2] = 0.0;
47
+
48
+ m4[1][0] = 0.0;
49
+ m4[1][1] = 1.0;
50
+ m4[1][2] = 0.0;
51
+
52
+ m4[2][0] = 0.0;
53
+ m4[2][1] = 0.0;
54
+ m4[2][2] = 1.0;
55
+
56
+ vec3 object_scale = vec3(
57
+ length(modelViewMatrix[0].xyz),
58
+ length(modelViewMatrix[1].xyz),
59
+ length(modelViewMatrix[2].xyz)
60
+ );
61
+
62
+ // scale by object's baking bounding sphere's radius
63
+ float card_diameter = uRadius*2.0;
64
+ object_scale *= card_diameter;
65
+
66
+ vec3 transformedNormal = normalize(normalMatrix * normal);
67
+
68
+ vec4 mvPosition = m4 * vec4( object_scale*(position+uOffset/card_diameter), 1.0 );
69
+
70
+ gl_Position = projectionMatrix * mvPosition;
71
+
72
+ mat4 inverse_matrix = inverse(projectionMatrix * modelViewMatrix);
73
+
74
+ //
75
+ //get 2D projection of this vertex in normalized device coordinates
76
+ vec2 pos = gl_Position.xy/gl_Position.w;
77
+
78
+ //compute ray's start and end as inversion of this coordinates
79
+ //in near and far clip planes
80
+ vec4 near_4 = inverse_matrix * (vec4(pos, -1.0, 1.0));
81
+ vec4 far_4 = inverse_matrix * (vec4(pos, 1.0, 1.0));
82
+
83
+ local_ray_near = near_4.xyz / near_4.w;
84
+ local_ray_far = far_4.xyz/ far_4.w;
85
+ }
86
+ `;
87
+ const shader_fg = `
88
+ precision highp float;
89
+ precision highp int;
90
+
91
+ const float depth_scale = 0.5;
92
+
93
+ in vec2 vUv;
94
+
95
+ in vec4 plane0;
96
+ in vec4 plane1;
97
+ in vec4 plane2;
98
+
99
+ in vec3 vViewPosition;
100
+ in vec3 vFacingDirection;
101
+
102
+ out vec4 color_out;
103
+
104
+ uniform sampler2D tBase;
105
+ uniform sampler2D tGeometry;
106
+ uniform float uFrames;
107
+ uniform float uDepthScale;
108
+ uniform bool uIsFullSphere;
109
+
110
+
111
+ in vec3 local_ray_near;
112
+ in vec3 local_ray_far;
113
+
114
+ struct Material{
115
+ vec3 diffuse;
116
+ vec3 normal;
117
+ float depth;
118
+ float occlusion;
119
+ float roughness;
120
+ float metalness;
121
+ };
122
+
123
+ vec2 VecToSphereOct(vec3 pivotToCamera)
124
+ {
125
+ vec3 octant = sign(pivotToCamera);
126
+
127
+ // |x| + |y| + |z| = 1
128
+ float sum = dot(pivotToCamera, octant);
129
+ vec3 octahedron = pivotToCamera / sum;
130
+
131
+ if (octahedron.y < 0.0){
132
+ vec3 absolute = abs(octahedron);
133
+ octahedron.xz = octant.xz * vec2(1.0 - absolute.z, 1.0 - absolute.x);
134
+ }
135
+ return octahedron.xz;
136
+ }
137
+
138
+ //for hemisphere
139
+ vec2 VecToHemiSphereOct(vec3 vec)
140
+ {
141
+ vec.y = max(vec.y, 0.001);
142
+ vec = normalize(vec);
143
+ vec3 octant = sign(vec);
144
+
145
+ // |x| + |y| + |z| = 1
146
+ float sum = dot(vec, octant);
147
+ vec3 octahedron = vec / sum;
148
+
149
+ return vec2(
150
+ octahedron.x + octahedron.z,
151
+ octahedron.z - octahedron.x
152
+ );
153
+ }
154
+
155
+ vec2 VectorToGrid(vec3 vec)
156
+ {
157
+ if (uIsFullSphere)
158
+ {
159
+ return VecToSphereOct(vec);
160
+ }
161
+ else
162
+ {
163
+ return VecToHemiSphereOct(vec);
164
+ }
165
+ }
166
+
167
+ vec4 TriangleInterpolate(vec2 uv){
168
+ uv = fract(uv);
169
+
170
+ vec2 omuv = vec2(1.0, 1.0) - uv.xy;
171
+
172
+ vec4 res = vec4(0, 0, 0, 0);
173
+ //frame 0
174
+ res.x = min(omuv.x, omuv.y);
175
+ //frame 1
176
+ res.y = abs(dot(uv, vec2(1.0, -1.0)));
177
+ //frame 2
178
+ res.z = min(uv.x, uv.y);
179
+ //mask
180
+ res.w = clamp(ceil(uv.x-uv.y),0.0, 1.0);
181
+
182
+ return res;
183
+ }
184
+
185
+
186
+ vec4 ImposterBlendWeights(sampler2D tex, vec2 frame0, vec2 frame1, vec2 frame2, vec4 weights, vec4 ddxy)
187
+ {
188
+ vec4 samp0 = textureGrad(tex, frame0, ddxy.xy, ddxy.zw);
189
+ vec4 samp1 = textureGrad(tex, frame1, ddxy.xy, ddxy.zw);
190
+ vec4 samp2 = textureGrad(tex, frame2, ddxy.xy, ddxy.zw);
191
+
192
+ vec4 result = samp0*weights.x + samp1*weights.y + samp2*weights.z;
193
+
194
+ return result;
195
+ }
196
+
197
+ vec2 recalculateUV(vec2 uv_f, vec2 frame, vec2 xy_f, vec2 frame_size, float d_scale, sampler2D depthTexture)
198
+ {
199
+ //clamp for parallax sampling
200
+ uv_f = clamp(uv_f, vec2(0), vec2(1));
201
+ vec2 uv_quad = frame_size * (frame + uv_f);
202
+ //paralax
203
+ float n_depth = (texture( depthTexture, uv_quad, -16.0 )).a;
204
+ uv_f = xy_f * (0.5-n_depth) * d_scale + uv_f;
205
+ //clamp parallax offset
206
+ uv_f = clamp(uv_f, vec2(0), vec2(1));
207
+ uv_f = frame_size * (frame + uv_f);
208
+ //clamped full UV
209
+ return clamp(uv_f, vec2(0), vec2(1));
210
+ }
211
+
212
+ void calcuateXYbasis(vec3 plane_normal, out vec3 plane_x, out vec3 plane_y)
213
+ {
214
+ vec3 up = vec3(0,1,0);
215
+ //cross product doesnt work if we look directly from bottom
216
+ if (abs(plane_normal.y) > 0.999f)
217
+ {
218
+ up = vec3(0,0,1);
219
+ }
220
+ plane_x = normalize(cross(plane_normal, up));
221
+ plane_y = normalize(cross(plane_x, plane_normal));
222
+ }
223
+
224
+ vec3 projectOnPlaneBasis(vec3 ray, vec3 plane_normal, vec3 plane_x, vec3 plane_y)
225
+ {
226
+ //reproject plane normal onto planeXY basos
227
+ return normalize(vec3(
228
+ dot(plane_x,ray),
229
+ dot(plane_y,ray),
230
+ dot(plane_normal,ray)
231
+ ));
232
+ }
233
+
234
+ vec2 recompute_uv(vec2 frame, vec2 frame_uv, sampler2D gBuffer){
235
+ vec2 frame_size = vec2(1.0/ uFrames);
236
+
237
+ vec2 source_uv = (frame + frame_uv)*frame_size;
238
+
239
+ float n_depth = texture(gBuffer, source_uv).a;
240
+
241
+ vec2 offset = clamp(length(frame_uv*2.0 - 1.0)*vec2(0.5-n_depth )*depth_scale, 0.0, 1.0);
242
+
243
+ return clamp(( frame + frame_uv+offset)*frame_size,0.0, 1.0);
244
+ // return source_uv;
245
+ }
246
+
247
+ void main(){
248
+ vec3 view_direction = normalize(local_ray_near-local_ray_far);
249
+
250
+ vec2 octahedral_uv = clamp(VectorToGrid(view_direction)*0.5 + 0.5, 0.0, 1.0);
251
+ vec2 grid = octahedral_uv * vec2(uFrames - 1.0);
252
+
253
+ vec2 gridFrac = fract(grid);
254
+ vec2 gridFloor = floor(grid);
255
+
256
+ vec4 weights = TriangleInterpolate( gridFrac );
257
+
258
+ //3 nearest frames
259
+ vec2 frame0 = gridFloor;
260
+ vec2 frame1 = gridFloor + mix(vec2(0,1),vec2(1,0),weights.w);
261
+ vec2 frame2 = gridFloor + vec2(1.0,1.0);
262
+
263
+ vec4 ddxy = vec4( dFdx(vUv.xy), dFdy(vUv.xy) );
264
+
265
+ vec2 frame_uv = vUv;
266
+ vec2 frame_size = vec2(1.0/ uFrames);
267
+
268
+ vec4 texel_color = ImposterBlendWeights(
269
+ tBase,
270
+ (frame0 + frame_uv) * frame_size,
271
+ (frame1 + frame_uv) * frame_size,
272
+ (frame2 + frame_uv) * frame_size,
273
+ weights, ddxy
274
+ );
275
+
276
+ // texel_color = vec4(texel_color.aaa, 1.0);
277
+
278
+ if(texel_color.a <= 0.5){
279
+ texel_color.r = 1.0;
280
+ discard;
281
+ }
282
+
283
+ color_out = texel_color;
284
+ // color_out = vec4( snapped_oct_uv, 1.0, 1.0);
285
+ }
286
+ `;
287
+
288
+ export class ImpostorShaderV0 extends RawShaderMaterial {
289
+ constructor() {
290
+ super({
291
+ fragmentShader: shader_fg,
292
+ vertexShader: shader_vx,
293
+ uniforms: {
294
+ /**
295
+ * RGB + Alpha
296
+ */
297
+ tBase: {
298
+ value: null
299
+ },
300
+ /**
301
+ * Normal+Depth
302
+ */
303
+ tGeometry: {
304
+ value: null
305
+ },
306
+ /**
307
+ * Material properties: Occlusion, Roughness, Metalness
308
+ * Alpha unused
309
+ */
310
+ tMaterial: {
311
+ value: null
312
+ },
313
+ /**
314
+ * Number of frames
315
+ */
316
+ uFrames: {
317
+ value: 0
318
+ },
319
+ /**
320
+ * Radius of bounding sphere of the impostor
321
+ */
322
+ uRadius: {
323
+ value: 0
324
+ },
325
+ /**
326
+ * Impostor offset
327
+ */
328
+ uOffset: {
329
+ value: new Vector3(0, 0, 0)
330
+ },
331
+ uIsFullSphere: {
332
+ value: false
333
+ },
334
+ uDepthScale:{
335
+ // value should be in range between 0 and 1
336
+ value:1
337
+ }
338
+ },
339
+ glslVersion: GLSL3
340
+ });
341
+
342
+ // Save some effort by disabling blending
343
+ this.blending = CustomBlending;
344
+ this.blendEquation = AddEquation;
345
+ this.blendSrc = OneFactor;
346
+ this.blendDst = OneMinusSrcAlphaFactor;
347
+
348
+ }
349
+ }
@@ -0,0 +1,74 @@
1
+ import {
2
+ AddEquation,
3
+ CustomBlending,
4
+ GLSL3,
5
+ OneFactor,
6
+ OneMinusSrcAlphaFactor,
7
+ RawShaderMaterial,
8
+ Vector3
9
+ } from "three";
10
+ import shader_vx from '../shader/glsl/v1/vertex.glsl';
11
+ import shader_fg from '../shader/glsl/v1/flagment.glsl';
12
+
13
+ export class ImpostorShaderV1 extends RawShaderMaterial {
14
+ constructor() {
15
+ super({
16
+ fragmentShader: shader_fg,
17
+ vertexShader: shader_vx,
18
+ uniforms: {
19
+ /**
20
+ * RGB + Alpha
21
+ */
22
+ tBase: {
23
+ value: null
24
+ },
25
+ /**
26
+ * Normal+Depth
27
+ */
28
+ tGeometry: {
29
+ value: null
30
+ },
31
+ /**
32
+ * Material properties: Occlusion, Roughness, Metalness
33
+ * Alpha unused
34
+ */
35
+ tMaterial: {
36
+ value: null
37
+ },
38
+ /**
39
+ * Number of frames
40
+ */
41
+ uFrames: {
42
+ value: 0
43
+ },
44
+ /**
45
+ * Radius of bounding sphere of the impostor
46
+ */
47
+ uRadius: {
48
+ value: 0
49
+ },
50
+ /**
51
+ * Impostor offset
52
+ */
53
+ uOffset: {
54
+ value: new Vector3(0, 0, 0)
55
+ },
56
+ uIsFullSphere: {
57
+ value: false
58
+ },
59
+ uDepthScale:{
60
+ // value should be in range between 0 and 1
61
+ value:1
62
+ }
63
+ },
64
+ glslVersion: GLSL3
65
+ });
66
+
67
+ // Save some effort by disabling blending
68
+ this.blending = CustomBlending;
69
+ this.blendEquation = AddEquation;
70
+ this.blendSrc = OneFactor;
71
+ this.blendDst = OneMinusSrcAlphaFactor;
72
+
73
+ }
74
+ }
@@ -0,0 +1,209 @@
1
+ vec2 VecToSphereOct(vec3 pivotToCamera)
2
+ {
3
+ vec3 octant = sign(pivotToCamera);
4
+
5
+ // |x| + |y| + |z| = 1
6
+ float sum = dot(pivotToCamera, octant);
7
+ vec3 octahedron = pivotToCamera / sum;
8
+
9
+ if (octahedron.y < 0f)
10
+ {
11
+ vec3 absolute = abs(octahedron);
12
+ octahedron.xz = octant.xz * vec2(1.0f - absolute.z, 1.0f - absolute.x);
13
+ }
14
+ return octahedron.xz;
15
+ }
16
+
17
+ vec2 VecToHemiSphereOct(vec3 pivotToCamera)
18
+ {
19
+ pivotToCamera.y = max(pivotToCamera.y, 0.001);
20
+ pivotToCamera = normalize(pivotToCamera);
21
+ vec3 octant = sign(pivotToCamera);
22
+
23
+ // |x| + |y| + |z| = 1
24
+ float sum = dot(pivotToCamera, octant);
25
+ vec3 octahedron = pivotToCamera / sum;
26
+
27
+ return vec2(
28
+ octahedron.x + octahedron.z,
29
+ octahedron.z - octahedron.x);
30
+ }
31
+
32
+ vec2 VectorToGrid(vec3 vec)
33
+ {
34
+ if (isFullSphere)
35
+ {
36
+ return VecToSphereOct(vec);
37
+ }
38
+ else
39
+ {
40
+ return VecToHemiSphereOct(vec);
41
+ }
42
+ }
43
+
44
+ //for sphere
45
+ vec3 OctaSphereEnc(vec2 coord)
46
+ {
47
+ coord = (coord - 0.5) * 2.0;
48
+ vec3 position = vec3(coord.x, 0f, coord.y);
49
+ vec2 absolute = abs(position.xz);
50
+ position.y = 1f - absolute.x - absolute.y;
51
+
52
+ if (position.y < 0f)
53
+ {
54
+ position.xz = sign(position.xz) * vec2(1.0f - absolute.y, 1.0f - absolute.x);
55
+ }
56
+
57
+ return position;
58
+ }
59
+
60
+ //for hemisphere
61
+ vec3 OctaHemiSphereEnc(vec2 coord)
62
+ {
63
+ //coord = (0, 0.27)
64
+ //pos = -0.27, 0, -0.63
65
+
66
+ vec3 position = vec3(coord.x - coord.y, 0f, -1.0 + coord.x + coord.y);
67
+ vec2 absolute = abs(position.xz);
68
+ position.y = 1f - absolute.x - absolute.y;
69
+ return position;
70
+ }
71
+
72
+ vec3 GridToVector(vec2 coord)
73
+ {
74
+ if (isFullSphere)
75
+ {
76
+ return OctaSphereEnc(coord);
77
+ }
78
+ else
79
+ {
80
+ return OctaHemiSphereEnc(coord);
81
+ }
82
+ }
83
+
84
+ vec3 FrameXYToRay(vec2 frame, vec2 frameCountMinusOne)
85
+ {
86
+ //divide frame x y by framecount minus one to get 0-1
87
+ vec2 f = (frame.xy/ frameCountMinusOne);
88
+ //bias and scale to -1 to 1
89
+
90
+ vec3 vec = GridToVector(f);
91
+ vec = normalize(vec);
92
+ return vec;
93
+ }
94
+
95
+ vec3 SpriteProjection(vec3 pivotToCameraRayLocal, vec2 size, vec2 loc_uv)
96
+ {
97
+ vec3 z = normalize(pivotToCameraRayLocal);
98
+ vec3 x, y;
99
+ vec3 up = vec3(0,1,0);
100
+ //cross product doesnt work if we look directly from bottom
101
+ if (abs(z.y) > 0.999f)
102
+ {
103
+ up = vec3(0,0,-1);
104
+ }
105
+ x = normalize(cross(up, z));
106
+ y = normalize(cross(x, z));
107
+
108
+ loc_uv -= vec2(0.5,0.5);
109
+ vec2 uv = (loc_uv) * 2.0; //-1 to 1
110
+
111
+ vec3 newX = x * uv.x;
112
+ vec3 newY = y * uv.y;
113
+
114
+ vec2 vecSize = size * 0.5;
115
+
116
+ newX *= vecSize.x;
117
+ newY *= vecSize.y;
118
+
119
+ return newX + newY;
120
+ }
121
+
122
+ vec4 quadBlendWieghts(vec2 coords)
123
+ {
124
+ vec4 res;
125
+ /* 0 0 0
126
+ 0 0 0
127
+ 1 0 0 */
128
+ res.x = min(1f - coords.x, 1f - coords.y);
129
+ /* 1 0 0
130
+ 0 0 0
131
+ 0 0 1 */
132
+ res.y = abs(coords.x - coords.y);
133
+ /* 0 0 1
134
+ 0 0 0
135
+ 0 0 0 */
136
+ res.z = min(coords.x, coords.y);
137
+ /* 0 0 0
138
+ 0 0 1
139
+ 0 1 1 */
140
+ res.w = ceil(coords.x - coords.y);
141
+ //res.xyz /= (res.x + res.y + res.z);
142
+ return res;
143
+ }
144
+
145
+
146
+ //this function works well in orthogonal projection. It works okeyish with further distances of perspective projection
147
+ vec2 virtualPlaneUV(vec3 plane_normal,vec3 plane_x, vec3 plane_y, vec3 pivotToCameraRay, vec3 vertexToCameraRay, float size)
148
+ {
149
+ plane_normal = normalize(plane_normal);
150
+ plane_x = normalize(plane_x);
151
+ plane_y = normalize(plane_y);
152
+
153
+ // plane_normal is normalized but pivotToCameraRay & vertexToCameraRay are NOT
154
+ // so what are we doing here ?
155
+ // We are calculating length of pivotToCameraRay projected onto plane_normal
156
+ // so pivotToCameraRay is vector to camera from CENTER OF object
157
+ // we are recalculting this distance taking into account new plane normal
158
+ float projectedNormalRayLength = dot(plane_normal, pivotToCameraRay);
159
+ // tihs is direction is almost the same as origin, but its to individual vertex
160
+ // not sure this is correct for perspective projection
161
+ float projectedVertexRayLength = dot(plane_normal, vertexToCameraRay);
162
+ // basically its length difference betwen center and vertex - "not precise"
163
+ // projectedVertexRayLength is bigger than projectedNormalRayLength when vertex is
164
+ // further than "main front facing billboard"
165
+ // so offsetLength is getting smaller, otherwise is getting bigger
166
+ float offsetLength = projectedNormalRayLength/projectedVertexRayLength;
167
+
168
+ // ok so offsetLength is just a length
169
+ // we want a vector so we multiply it by vertexToCameraRay to get this offset
170
+ // now what are we REALY doing is calculuating distance difference
171
+ // se are SUBSTRACTING pivotToCameraRay vector
172
+ // we would get difference between center of plane and vertex rotated
173
+ vec3 offsetVector = vertexToCameraRay * offsetLength - pivotToCameraRay;
174
+
175
+ // we got the offset of rotated vertex, but we need to offset it from correct plane axis
176
+ // so again we projecting length of intersection (offset of rotated vertex) onto plane_x
177
+ // and plane_y
178
+ vec2 duv = vec2(
179
+ dot(plane_x , offsetVector),
180
+ dot(plane_y, offsetVector)
181
+ );
182
+
183
+ //we are in space -1 to 1
184
+ duv /= 2.0 * size;
185
+ duv += 0.5;
186
+ return duv;
187
+ }
188
+
189
+ void calcuateXYbasis(vec3 plane_normal, out vec3 plane_x, out vec3 plane_y)
190
+ {
191
+ vec3 up = vec3(0,1,0);
192
+ //cross product doesnt work if we look directly from bottom
193
+ if (abs(plane_normal.y) > 0.999f)
194
+ {
195
+ up = vec3(0,0,1);
196
+ }
197
+ plane_x = normalize(cross(plane_normal, up));
198
+ plane_y = normalize(cross(plane_x, plane_normal));
199
+ }
200
+
201
+ vec3 projectOnPlaneBasis(vec3 ray, vec3 plane_normal, vec3 plane_x, vec3 plane_y)
202
+ {
203
+ //reproject plane normal onto planeXY basos
204
+ return normalize(vec3(
205
+ dot(plane_x,ray),
206
+ dot(plane_y,ray),
207
+ dot(plane_normal,ray)
208
+ ));
209
+ }
@@ -0,0 +1,80 @@
1
+ precision highp float;
2
+ precision highp int;
3
+
4
+ const float depth_scale = 1.0;
5
+
6
+ uniform float uFrames;
7
+ uniform sampler2D tBase;
8
+ uniform sampler2D tGeometry;
9
+
10
+ out vec4 color_out;
11
+
12
+ in vec2 uv_frame1;
13
+ in vec2 xy_frame1;
14
+ flat in vec2 frame1;
15
+ flat in vec3 frame1_normal;
16
+ in vec2 uv_frame2;
17
+ in vec2 xy_frame2;
18
+ flat in vec2 frame2;
19
+ flat in vec3 frame2_normal;
20
+ in vec2 uv_frame3;
21
+ in vec2 xy_frame3;
22
+ flat in vec2 frame3;
23
+ flat in vec3 frame3_normal;
24
+ in vec4 quad_blend_weights;
25
+
26
+ vec2 recalculateUV(vec2 uv_f, vec2 frame, vec2 xy_f, vec2 frame_size, float d_scale, sampler2D depthTexture)
27
+ {
28
+ //clamp for parallax sampling
29
+ uv_f = clamp(uv_f, 0.0, 1.0);
30
+ vec2 uv_quad = frame_size * (frame + uv_f);
31
+ //paralax
32
+ float n_depth = texture(depthTexture, uv_quad).a;
33
+ uv_f = xy_f * (0.5-n_depth) * d_scale + uv_f;
34
+ //clamp parallax offset
35
+ uv_f = clamp(uv_f, 0.0, 1.0);
36
+ uv_f = frame_size * (frame + uv_f);
37
+ //clamped full UV
38
+ return clamp(uv_f, 0.0, 1.0);
39
+ }
40
+
41
+ vec4 blenderColors(vec2 uv_1, vec2 uv_2, vec2 uv_3, vec4 grid_weights, sampler2D atlasTexture)
42
+ {
43
+ vec4 quad_a, quad_b, quad_c;
44
+
45
+ quad_a = texture(atlasTexture, uv_1);
46
+ quad_b = texture(atlasTexture, uv_2);
47
+ quad_c = texture(atlasTexture, uv_3);
48
+ return quad_a * grid_weights.x + quad_b * grid_weights.y + quad_c * grid_weights.z;
49
+ }
50
+
51
+ void main()
52
+ {
53
+ vec2 quad_size = vec2(1.0) / uFrames;
54
+ vec2 uv_f1 = recalculateUV(uv_frame1, frame1, xy_frame1, quad_size, depth_scale, tGeometry);
55
+ vec2 uv_f2 = recalculateUV(uv_frame2, frame2, xy_frame2, quad_size, depth_scale, tGeometry);
56
+ vec2 uv_f3 = recalculateUV(uv_frame3, frame3, xy_frame3, quad_size, depth_scale, tGeometry);
57
+
58
+ vec4 baseTex = blenderColors(uv_f1, uv_f2, uv_f3, quad_blend_weights, tBase);
59
+
60
+ // vec4 ormTex = blenderColors(uv_f1, uv_f2, uv_f3, quad_blend_weights, imposterTextureOrm);
61
+ //
62
+ // vec3 normalTex = blendedNormals(
63
+ // uv_f1, frame1_normal,
64
+ // uv_f2, frame2_normal,
65
+ // uv_f3, frame3_normal,
66
+ // TANGENT, BINORMAL,
67
+ // quad_blend_weights, imposterTextureNormal
68
+ // );
69
+ //
70
+ // ALBEDO = baseTex.rgb * albedo.rgb;
71
+ // NORMAL =normalTex.xyz;
72
+ //
73
+ // METALLIC = ormTex.b * metallic;
74
+ // SPECULAR = specular;
75
+ // ROUGHNESS = ormTex.g * roughness;
76
+
77
+
78
+ color_out = baseTex;
79
+ // color_out = vec4(1.0);
80
+ }