@needle-tools/engine 5.1.0-alpha → 5.1.0-alpha.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 (52) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/SKILL.md +39 -21
  3. package/dist/{needle-engine.bundle-CwhCzjep.js → needle-engine.bundle-BGyKqxBH.js} +10494 -10174
  4. package/dist/{needle-engine.bundle-qDahLTqW.min.js → needle-engine.bundle-CiYtOO2O.min.js} +230 -154
  5. package/dist/{needle-engine.bundle-wM-BWPX9.umd.cjs → needle-engine.bundle-DzVx9Z8D.umd.cjs} +228 -152
  6. package/dist/needle-engine.d.ts +176 -4
  7. package/dist/needle-engine.js +571 -567
  8. package/dist/needle-engine.min.js +1 -1
  9. package/dist/needle-engine.umd.cjs +1 -1
  10. package/lib/engine/engine_components.js +5 -1
  11. package/lib/engine/engine_components.js.map +1 -1
  12. package/lib/engine/engine_init.js +2 -0
  13. package/lib/engine/engine_init.js.map +1 -1
  14. package/lib/engine/engine_physics_rapier.d.ts +3 -0
  15. package/lib/engine/engine_physics_rapier.js +13 -9
  16. package/lib/engine/engine_physics_rapier.js.map +1 -1
  17. package/lib/engine/engine_utils.js +2 -2
  18. package/lib/engine/engine_utils.js.map +1 -1
  19. package/lib/engine/xr/NeedleXRSession.d.ts +1 -0
  20. package/lib/engine/xr/NeedleXRSession.js +4 -4
  21. package/lib/engine/xr/NeedleXRSession.js.map +1 -1
  22. package/lib/engine/xr/events.d.ts +30 -3
  23. package/lib/engine/xr/events.js +38 -0
  24. package/lib/engine/xr/events.js.map +1 -1
  25. package/lib/engine/xr/init.js +1 -7
  26. package/lib/engine/xr/init.js.map +1 -1
  27. package/lib/engine-components/AnimatorController.d.ts +135 -2
  28. package/lib/engine-components/AnimatorController.js +218 -2
  29. package/lib/engine-components/AnimatorController.js.map +1 -1
  30. package/lib/engine-components/GroundProjection.d.ts +1 -0
  31. package/lib/engine-components/GroundProjection.js +184 -48
  32. package/lib/engine-components/GroundProjection.js.map +1 -1
  33. package/lib/engine-components/api.d.ts +1 -0
  34. package/lib/engine-components/api.js +1 -0
  35. package/lib/engine-components/api.js.map +1 -1
  36. package/lib/engine-components/codegen/components.d.ts +1 -0
  37. package/lib/engine-components/codegen/components.js +1 -0
  38. package/lib/engine-components/codegen/components.js.map +1 -1
  39. package/package.json +1 -1
  40. package/plugins/common/logger.js +42 -19
  41. package/plugins/vite/logger.client.js +4 -3
  42. package/src/engine/engine_components.ts +7 -4
  43. package/src/engine/engine_init.ts +2 -0
  44. package/src/engine/engine_physics_rapier.ts +14 -9
  45. package/src/engine/engine_utils.ts +2 -2
  46. package/src/engine/xr/NeedleXRSession.ts +5 -5
  47. package/src/engine/xr/events.ts +44 -1
  48. package/src/engine/xr/init.ts +0 -7
  49. package/src/engine-components/AnimatorController.ts +286 -4
  50. package/src/engine-components/GroundProjection.ts +226 -52
  51. package/src/engine-components/api.ts +1 -0
  52. package/src/engine-components/codegen/components.ts +1 -0
@@ -1,4 +1,4 @@
1
- import { ShaderMaterial, Texture } from "three";
1
+ import { CubeUVReflectionMapping, ShaderMaterial, Texture } from "three";
2
2
  import { GroundedSkybox as GroundProjection } from 'three/examples/jsm/objects/GroundedSkybox.js';
3
3
 
4
4
  import { Gizmos } from "../engine/engine_gizmos.js";
@@ -12,6 +12,174 @@ import type { ContactShadows } from "./ContactShadows.js";
12
12
 
13
13
  const debug = getParam("debuggroundprojection");
14
14
 
15
+ type GroundProjectionMaterial = GroundProjection["material"] & {
16
+ defines?: Record<string, string | number>;
17
+ };
18
+
19
+ type GroundProjectionShaderUniforms = {
20
+ needleGroundProjectionBlurriness: { value: number };
21
+ needleGroundProjectionBlending: { value: number };
22
+ needleGroundProjectionAlphaFactor: { value: number };
23
+ needleGroundProjectionBackgroundIntensity: { value: number };
24
+ };
25
+
26
+ const needleCubeUvMapVarying = /* glsl */`
27
+ #ifdef NEEDLE_USE_CUBE_UV_MAP
28
+ varying vec3 vNeedleGroundProjectionWorldDirection;
29
+ #endif
30
+ `;
31
+
32
+ const needleGroundProjectionFragmentPars = /* glsl */`
33
+ ${needleCubeUvMapVarying}
34
+ uniform float needleGroundProjectionBlurriness;
35
+ uniform float needleGroundProjectionBlending;
36
+ uniform float needleGroundProjectionAlphaFactor;
37
+ uniform float needleGroundProjectionBackgroundIntensity;
38
+
39
+ float needleGroundProjectionSmoothstep(float edge0, float edge1, float x) {
40
+ float t = clamp((x - edge0) / max(edge1 - edge0, 0.000001), 0.0, 1.0);
41
+ return t * t * (3.0 - 2.0 * t);
42
+ }
43
+
44
+ float needleGroundProjectionDistance() {
45
+ return length(vec2(0.0, vMapUv.y));
46
+ }
47
+
48
+ float needleGroundProjectionBlurFactor(float needleGroundProjectionDistanceValue) {
49
+ return clamp(needleGroundProjectionSmoothstep(0.5, 1.0, needleGroundProjectionDistanceValue * 2.0), 0.0, 1.0);
50
+ }
51
+ `;
52
+
53
+ const needleCubeUvMapFragment = /* glsl */`
54
+ #ifdef USE_MAP
55
+
56
+ float needleGroundProjectionDistanceValue = needleGroundProjectionDistance();
57
+ float needleGroundProjectionBlurFactorValue = needleGroundProjectionBlurFactor(needleGroundProjectionDistanceValue);
58
+ vec4 sampledDiffuseColor;
59
+
60
+ #ifdef NEEDLE_USE_CUBE_UV_MAP
61
+ sampledDiffuseColor = textureCubeUV(
62
+ map,
63
+ normalize( vNeedleGroundProjectionWorldDirection ),
64
+ needleGroundProjectionBlurriness * needleGroundProjectionBlurFactorValue
65
+ );
66
+ #else
67
+ #ifdef USE_MIPMAP_BIAS
68
+ sampledDiffuseColor = texture2D( map, vMapUv, mipmapBias );
69
+ #else
70
+ sampledDiffuseColor = texture2D( map, vMapUv );
71
+ #endif
72
+ #endif
73
+
74
+ #ifdef DECODE_VIDEO_TEXTURE
75
+
76
+ // use inline sRGB decode until browsers properly support SRGB8_ALPHA8 with video textures (#26516)
77
+
78
+ sampledDiffuseColor = vec4( mix( pow( sampledDiffuseColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), sampledDiffuseColor.rgb * 0.0773993808, vec3( lessThanEqual( sampledDiffuseColor.rgb, vec3( 0.04045 ) ) ) ), sampledDiffuseColor.w );
79
+
80
+ #endif
81
+
82
+ sampledDiffuseColor.rgb *= mix(1.0, needleGroundProjectionBackgroundIntensity, needleGroundProjectionBlurFactorValue);
83
+ diffuseColor *= sampledDiffuseColor;
84
+
85
+ #endif
86
+ `;
87
+
88
+ const needleGroundProjectionAlphaFragment = /* glsl */`
89
+ #ifdef USE_MAP
90
+ if (needleGroundProjectionBlending > 0.000001) {
91
+ float needleGroundProjectionBrightness = dot(diffuseColor.rgb, vec3(0.299, 0.587, 0.114));
92
+ float needleGroundProjectionStepFactor = needleGroundProjectionBlending - needleGroundProjectionBrightness * 0.1;
93
+ diffuseColor.a *= pow(
94
+ 1.0 - needleGroundProjectionBlending * needleGroundProjectionSmoothstep(
95
+ 0.35 * needleGroundProjectionStepFactor,
96
+ 0.45 * needleGroundProjectionStepFactor,
97
+ needleGroundProjectionDistanceValue
98
+ ),
99
+ 5.0
100
+ );
101
+ }
102
+ #endif
103
+ diffuseColor.a *= needleGroundProjectionAlphaFactor;
104
+ `;
105
+
106
+ function getCubeUvSize(texture: Texture) {
107
+ const imageHeight = texture.image?.height;
108
+ if (!imageHeight) return null;
109
+
110
+ const maxMip = Math.log2(imageHeight) - 2;
111
+ const texelHeight = 1 / imageHeight;
112
+ const texelWidth = 1 / (3 * Math.max(Math.pow(2, maxMip), 7 * 16));
113
+ return { texelWidth, texelHeight, maxMip };
114
+ }
115
+
116
+ function getGroundProjectionShaderUniforms(material: GroundProjectionMaterial): GroundProjectionShaderUniforms {
117
+ const userData = material.userData as GroundProjectionMaterial["userData"] & {
118
+ needleGroundProjectionUniforms?: GroundProjectionShaderUniforms;
119
+ };
120
+
121
+ return userData.needleGroundProjectionUniforms ??= {
122
+ needleGroundProjectionBlurriness: { value: 0 },
123
+ needleGroundProjectionBlending: { value: 0 },
124
+ needleGroundProjectionAlphaFactor: { value: 1 },
125
+ needleGroundProjectionBackgroundIntensity: { value: 1 }
126
+ };
127
+ }
128
+
129
+ function configureGroundProjectionMaterial(material: GroundProjectionMaterial, texture: Texture) {
130
+ const projectionUniforms = getGroundProjectionShaderUniforms(material);
131
+ material.onBeforeCompile = shader => {
132
+ shader.uniforms.needleGroundProjectionBlurriness = projectionUniforms.needleGroundProjectionBlurriness;
133
+ shader.uniforms.needleGroundProjectionBlending = projectionUniforms.needleGroundProjectionBlending;
134
+ shader.uniforms.needleGroundProjectionAlphaFactor = projectionUniforms.needleGroundProjectionAlphaFactor;
135
+ shader.uniforms.needleGroundProjectionBackgroundIntensity = projectionUniforms.needleGroundProjectionBackgroundIntensity;
136
+
137
+ shader.vertexShader = shader.vertexShader
138
+ .replace("#include <uv_pars_vertex>", `#include <uv_pars_vertex>\n${needleCubeUvMapVarying}`)
139
+ .replace(
140
+ "#include <worldpos_vertex>",
141
+ `#include <worldpos_vertex>
142
+ #ifdef NEEDLE_USE_CUBE_UV_MAP
143
+ // GroundedSkybox mirrors geometry on Z, so undo that before deriving the sampling direction.
144
+ vNeedleGroundProjectionWorldDirection = transformDirection( vec3( position.x, position.y, -position.z ), modelMatrix );
145
+ #endif`
146
+ );
147
+
148
+ shader.fragmentShader = shader.fragmentShader
149
+ .replace(
150
+ "#include <map_pars_fragment>",
151
+ `#include <map_pars_fragment>
152
+ ${needleGroundProjectionFragmentPars}
153
+ #include <cube_uv_reflection_fragment>`
154
+ )
155
+ .replace("#include <map_fragment>", needleCubeUvMapFragment)
156
+ .replace("#include <opaque_fragment>", `${needleGroundProjectionAlphaFragment}\n#include <opaque_fragment>`);
157
+ };
158
+
159
+ const defines = material.defines ??= {};
160
+ const prevDefineState = JSON.stringify(defines);
161
+ const cubeUvSize = texture.mapping === CubeUVReflectionMapping ? getCubeUvSize(texture) : null;
162
+
163
+ if (cubeUvSize) {
164
+ defines.NEEDLE_USE_CUBE_UV_MAP = 1;
165
+ defines.ENVMAP_TYPE_CUBE_UV = 1;
166
+ defines.CUBEUV_TEXEL_WIDTH = cubeUvSize.texelWidth;
167
+ defines.CUBEUV_TEXEL_HEIGHT = cubeUvSize.texelHeight;
168
+ defines.CUBEUV_MAX_MIP = `${cubeUvSize.maxMip}.0`;
169
+ }
170
+ else {
171
+ delete defines.NEEDLE_USE_CUBE_UV_MAP;
172
+ delete defines.ENVMAP_TYPE_CUBE_UV;
173
+ delete defines.CUBEUV_TEXEL_WIDTH;
174
+ delete defines.CUBEUV_TEXEL_HEIGHT;
175
+ delete defines.CUBEUV_MAX_MIP;
176
+ }
177
+
178
+ if (prevDefineState !== JSON.stringify(defines)) {
179
+ material.needsUpdate = true;
180
+ }
181
+ }
182
+
15
183
  /**
16
184
  * The [GroundProjectedEnv](https://engine.needle.tools/docs/api/GroundProjectedEnv) projects the environment map onto a virtual ground plane.
17
185
  * Creates a realistic floor from 360° panoramas/HDRIs by deforming the skybox
@@ -147,12 +315,10 @@ export class GroundProjectedEnv extends Behaviour {
147
315
  this._projection.rotation.copy(this.scene.backgroundRotation);
148
316
  }
149
317
 
150
- const blurrinessChanged = this.context.scene.backgroundBlurriness !== undefined && this._lastBlurriness != this.context.scene.backgroundBlurriness && this.context.scene.backgroundBlurriness > 0.001;
151
- if (blurrinessChanged) {
152
- this.updateProjection();
153
- }
154
- else if (this._needsTextureUpdate && this.context.scene.background instanceof Texture) {
155
- this.updateBlurriness(this.context.scene.background, this.context.scene.backgroundBlurriness);
318
+ if (this._projection && this.context.scene.background instanceof Texture) {
319
+ const blurriness = this.context.scene.backgroundBlurriness ?? 0;
320
+ const blurrinessChanged = this._lastBlurriness !== blurriness;
321
+ this.updateProjectionMaterial(this.context.scene.background, blurrinessChanged || this._needsTextureUpdate);
156
322
  }
157
323
  }
158
324
 
@@ -202,6 +368,7 @@ export class GroundProjectedEnv extends Behaviour {
202
368
 
203
369
  try {
204
370
  this._projection = new GroundProjection(backgroundTexture, this._height, this._radius, 64);
371
+ configureGroundProjectionMaterial(this._projection.material, backgroundTexture);
205
372
  }
206
373
  catch (e) {
207
374
  console.error("Error creating three GroundProjection", e);
@@ -242,9 +409,7 @@ export class GroundProjectedEnv extends Behaviour {
242
409
  this.env.height = this._height;
243
410
  */
244
411
 
245
- if (this.context.scene.backgroundBlurriness > 0.001 && this._needsTextureUpdate) {
246
- this.updateBlurriness(backgroundTexture, this.context.scene.backgroundBlurriness);
247
- }
412
+ this.updateProjectionMaterial(backgroundTexture, true);
248
413
 
249
414
  this._lastBackground = backgroundTexture;
250
415
  this._lastHeight = this._height;
@@ -254,24 +419,60 @@ export class GroundProjectedEnv extends Behaviour {
254
419
 
255
420
  private _blurrynessShader: ShaderMaterial | null = null;
256
421
  private _lastBlurriness: number = -1;
257
-
258
- private updateBlurriness(texture: Texture, blurriness: number) {
422
+
423
+ private updateProjectionMaterial(texture: Texture, forceTextureUpdate = false) {
259
424
  if (!this._projection) {
260
425
  return;
261
426
  }
262
- else if (!texture) {
263
- return;
427
+
428
+ const blurriness = this.context.scene.backgroundBlurriness ?? 0;
429
+ const useCubeUvBlur = texture.mapping === CubeUVReflectionMapping;
430
+
431
+ let targetTexture = texture;
432
+ if (!useCubeUvBlur && blurriness > 0.001) {
433
+ const hasBlurredTextureAssigned = !!this._projection.material.map && this._projection.material.map !== texture;
434
+ if (forceTextureUpdate || !hasBlurredTextureAssigned) {
435
+ targetTexture = this.updateBlurriness(texture, blurriness);
436
+ }
437
+ else if (this._projection.material.map) {
438
+ targetTexture = this._projection.material.map;
439
+ }
440
+ }
441
+
442
+ if (this._projection.material.map !== targetTexture) {
443
+ this._projection.material.map = targetTexture;
444
+ }
445
+
446
+ const appliedTexture = this._projection.material.map ?? texture;
447
+ appliedTexture.mapping = texture.mapping;
448
+ configureGroundProjectionMaterial(this._projection.material, appliedTexture);
449
+
450
+ const shaderUniforms = getGroundProjectionShaderUniforms(this._projection.material);
451
+ shaderUniforms.needleGroundProjectionBlurriness.value = useCubeUvBlur ? blurriness : 0;
452
+ shaderUniforms.needleGroundProjectionBackgroundIntensity.value = this.context.scene.backgroundIntensity ?? 1;
453
+
454
+ const wasTransparent = this._projection.material.transparent;
455
+ this._projection.material.transparent = (this.context.xr?.isAR === true && this.arBlending > 0.000001) ?? false;
456
+ shaderUniforms.needleGroundProjectionBlending.value = this._projection.material.transparent ? this.arBlending : 0;
457
+ shaderUniforms.needleGroundProjectionAlphaFactor.value = this.context.isInPassThrough ? 0.95 : 1;
458
+
459
+ if (wasTransparent !== this._projection.material.transparent) {
460
+ this._projection.material.needsUpdate = true;
264
461
  }
265
462
 
463
+ this._projection.material.depthTest = true;
464
+ this._projection.material.depthWrite = false;
465
+ this._lastBlurriness = blurriness;
266
466
  this._needsTextureUpdate = false;
467
+ }
468
+
469
+ private updateBlurriness(texture: Texture, blurriness: number): Texture {
267
470
  if (debug) console.log("Update Blurriness", blurriness);
268
471
  this._blurrynessShader ??= new ShaderMaterial({
269
472
  name: "GroundProjectionBlurriness",
270
473
  uniforms: {
271
474
  map: { value: texture },
272
- blurriness: { value: blurriness },
273
- blending: { value: 0 },
274
- alphaFactor: { value: 1 }
475
+ blurriness: { value: blurriness }
275
476
  },
276
477
  vertexShader: blurVertexShader,
277
478
  fragmentShader: blurFragmentShader
@@ -279,33 +480,10 @@ export class GroundProjectedEnv extends Behaviour {
279
480
  this._blurrynessShader.depthWrite = false;
280
481
  this._blurrynessShader.uniforms.map.value = texture;
281
482
  this._blurrynessShader.uniforms.blurriness.value = blurriness;
282
- this._lastBlurriness = blurriness;
283
483
  texture.needsUpdate = true;
284
-
285
- const wasTransparent = this._projection.material.transparent;
286
- this._projection.material.transparent = (this.context.xr?.isAR === true && this.arBlending > 0.000001) ?? false;
287
- if (this._projection.material.transparent) {
288
- this._blurrynessShader.uniforms.blending.value = this.arBlending;
289
- }
290
- else { this._blurrynessShader.uniforms.blending.value = 0; }
291
-
292
- if (this.context.isInPassThrough) {
293
- // Make the ground slightly transparent in passthrough mode
294
- this._blurrynessShader.uniforms.alphaFactor.value = 0.95;
295
- }
296
- else {
297
- this._blurrynessShader.uniforms.alphaFactor.value = 1;
298
- }
299
-
300
- // Make sure the material is updated if the transparency changed
301
- if (wasTransparent !== this._projection.material.transparent) {
302
- this._projection.material.needsUpdate = true;
303
- }
304
-
305
- // Update the texture
306
- this._projection.material.map = Graphics.copyTexture(texture, this._blurrynessShader);
307
- this._projection.material.depthTest = true;
308
- this._projection.material.depthWrite = false;
484
+ const blurredTexture = Graphics.copyTexture(texture, this._blurrynessShader);
485
+ blurredTexture.mapping = texture.mapping;
486
+ return blurredTexture;
309
487
  }
310
488
 
311
489
  }
@@ -324,8 +502,6 @@ const blurVertexShader = `
324
502
  const blurFragmentShader = `
325
503
  uniform sampler2D map;
326
504
  uniform float blurriness;
327
- uniform float alphaFactor;
328
- uniform float blending;
329
505
  varying vec2 vUv;
330
506
 
331
507
  const float PI = 3.14159265359;
@@ -356,6 +532,10 @@ const blurFragmentShader = `
356
532
  vec4 color = vec4(0.0);
357
533
  float totalWeight = 0.0;
358
534
  int blurSize = int(60.0 * min(1.0, blurriness) * blurAmount); // Adjust blur size based on distance and blurriness
535
+ if (blurSize <= 0) {
536
+ gl_FragColor = texture2D(map, vUv);
537
+ return;
538
+ }
359
539
  float lodLevel = log2(float(blurSize)) * 0.5; // Compute LOD level
360
540
 
361
541
  for (int x = -blurSize; x <= blurSize; x++) {
@@ -371,16 +551,10 @@ const blurFragmentShader = `
371
551
 
372
552
  gl_FragColor = color;
373
553
 
374
- float brightness = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));
375
- float stepFactor = blending - brightness * .1;
376
- gl_FragColor.a = pow(1.0 - blending * customSmoothstep(0.35 * stepFactor, 0.45 * stepFactor, distance), 5.);
377
- gl_FragColor.a *= alphaFactor;
378
- // gl_FragColor.rgb = vec3(1.0);
379
-
380
554
  // #include <tonemapping_fragment>
381
555
  // #include <colorspace_fragment>
382
556
 
383
557
  // Uncomment to visualize blur amount
384
558
  // gl_FragColor = vec4(blurAmount, 0.0, 0.0, 1.0);
385
559
  }
386
- `;
560
+ `;
@@ -35,6 +35,7 @@
35
35
  */
36
36
 
37
37
  export * from "./codegen/components.js";
38
+ export { AnimatorControllerBuilder, type ConditionMode, type StateOptions, type TransitionOptions } from "./AnimatorController.js";
38
39
  export { Collider } from "./Collider.js"; // export abstract type
39
40
  export { Behaviour, Component, GameObject } from "./Component.js";
40
41
 
@@ -6,6 +6,7 @@ export { Animation } from "../Animation.js";
6
6
  export { Keyframe } from "../AnimationCurve.js";
7
7
  export { AnimationCurve } from "../AnimationCurve.js";
8
8
  export { Animator } from "../Animator.js";
9
+ export { AnimatorControllerBuilder } from "../AnimatorController.js";
9
10
  export { AnimatorController } from "../AnimatorController.js";
10
11
  export { AudioListener } from "../AudioListener.js";
11
12
  export { AudioSource } from "../AudioSource.js";