@needle-tools/three-mesh-effects 0.2.2 → 0.2.3

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.
@@ -45,6 +45,7 @@ import {
45
45
  smoothstep,
46
46
  texture,
47
47
  textureLoad,
48
+ textureSize,
48
49
  transformNormalToView,
49
50
  uniformArray,
50
51
  vec2,
@@ -125,6 +126,27 @@ function isBlendZone(value) {
125
126
  value <= 1;
126
127
  }
127
128
 
129
+ // src/rendering/texture-slot.js
130
+ function bindTextureSlot(material, slot, initial, nodes, onChange) {
131
+ let current = initial;
132
+ Object.defineProperty(material, slot, {
133
+ configurable: true,
134
+ enumerable: true,
135
+ get: () => current,
136
+ set: (value) => {
137
+ value.channel = current.channel;
138
+ value.offset = current.offset;
139
+ value.repeat = current.repeat;
140
+ value.center = current.center;
141
+ value.rotation = current.rotation;
142
+ current = value;
143
+ for (const node of nodes) node.value = value;
144
+ onChange?.(value);
145
+ material.needsUpdate = true;
146
+ }
147
+ });
148
+ }
149
+
128
150
  // src/impostor/octahedral.js
129
151
  var PERSPECTIVE_RAY_PLANE_SIGN = -1;
130
152
 
@@ -569,6 +591,21 @@ function createCompatibleImpostorNodeMaterial(descriptor, maps) {
569
591
  float halfTexel = 0.5001 / frameResolution;
570
592
  return (frame + clamp(insideCell, vec2(halfTexel), vec2(1.0 - halfTexel))) / frames;
571
593
  }
594
+ vec4 sampleColorAtlas(vec2 coordinates) {
595
+ vec2 size = vec2(textureSize(baseColorAtlas, 0));
596
+ vec2 grid = vec2(frames);
597
+ #if COMPACT_COLOR && COMPACT_EMISSIVE
598
+ grid.x *= 2.0;
599
+ #endif
600
+ vec2 pixels = coordinates * size;
601
+ float footprint = max(length(dFdx(pixels)), length(dFdy(pixels)));
602
+ float gutter = padding * size.y / (frames * frameResolution);
603
+ float lod = clamp(log2(max(footprint, 1.0)), 0.0, floor(log2(max(gutter, 1.0))));
604
+ vec2 cell = floor(clamp(coordinates * grid, vec2(0.0), grid - 0.00001));
605
+ vec2 guard = vec2(0.5001 * exp2(lod)) / size;
606
+ vec2 safe = clamp(coordinates, cell / grid + guard, (cell + 1.0) / grid - guard);
607
+ return textureLod(baseColorAtlas, safe, lod);
608
+ }
572
609
  void main() {
573
610
  vec3 cameraLocal = (inverse(modelMatrix) * vec4(cameraPosition, 1.0)).xyz;
574
611
  vec3 cameraDirection = normalize(cameraLocal);
@@ -603,7 +640,7 @@ function createCompatibleImpostorNodeMaterial(descriptor, maps) {
603
640
  #if COMPACT_COLOR
604
641
  colorUv.x *= COMPACT_EMISSIVE == 1 ? 0.5 : 1.0;
605
642
  #endif
606
- vec4 base = texture(baseColorAtlas, colorUv);
643
+ vec4 base = sampleColorAtlas(colorUv);
607
644
  vec4 geometry = texture(geometryAtlas, atlas);
608
645
  float contour = base.a;
609
646
  float depthValue;
@@ -676,8 +713,24 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
676
713
  uvNode.x.mul(compactEmissive ? 0.5 : 1).add(emissive && compactEmissive ? 0.5 : 0),
677
714
  uvNode.y
678
715
  ) : uvNode;
679
- const sampleCapturedAlpha = (uvNode) => compressedGeometry ? texture(baseColorAtlas, colorAtlasUv(uvNode)).level(0).a : texture(baseColorAtlas, uvNode).level(0).a;
680
- const sampleContour = (uvNode) => compressedGeometry ? texture(geometryAtlas, uvNode).level(0).a : descriptor.coverageEncoding === "sdf-base-alpha" ? texture(baseColorAtlas, uvNode).level(0).a : texture(ormAtlas, uvNode).level(0).a;
716
+ const mapLod = (atlas, coordinates) => {
717
+ const size = vec2(textureSize(atlas));
718
+ const pixels = coordinates.mul(size);
719
+ const footprint = max(pixels.dFdx().length(), pixels.dFdy().length());
720
+ const gutter = padding.mul(size.y).div(frames.mul(frameResolution));
721
+ return max(footprint, 1).log2().clamp(0, max(gutter, 1).log2().floor());
722
+ };
723
+ const sampleAtlas = (atlas, coordinates, lod = float(0), columns = frames) => {
724
+ const size = vec2(textureSize(atlas));
725
+ const grid = vec2(columns, frames);
726
+ const cell = coordinates.mul(grid).clamp(0, grid.sub(1e-5)).floor();
727
+ const guard = float(0.5001).mul(float(2).pow(lod)).div(size);
728
+ const safe = coordinates.clamp(cell.div(grid).add(guard), cell.add(1).div(grid).sub(guard));
729
+ return texture(atlas, safe).level(lod);
730
+ };
731
+ const colorColumns = frames.mul(compactLayout && compactEmissive ? 2 : 1);
732
+ const sampleCapturedAlpha = (uvNode) => compressedGeometry ? sampleAtlas(baseColorAtlas, colorAtlasUv(uvNode), float(0), colorColumns).a : sampleAtlas(baseColorAtlas, uvNode).a;
733
+ const sampleContour = (uvNode) => compressedGeometry ? texture(geometryAtlas, uvNode).level(0).a : descriptor.coverageEncoding === "sdf-base-alpha" ? sampleAtlas(baseColorAtlas, uvNode).a : sampleAtlas(ormAtlas, uvNode).a;
681
734
  const viewBounds = uniformArray(
682
735
  Array.from({ length: descriptor.frames * descriptor.frames }, (_, index) => new Vector4(
683
736
  descriptor.viewBounds[index * 4],
@@ -688,10 +741,6 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
688
741
  "vec4"
689
742
  );
690
743
  const atlasResolution = float(descriptor.frames * descriptor.frameResolution);
691
- const materialAtlasResolution = float(
692
- descriptor.frames * (descriptor.compression?.materialFrameResolution ?? descriptor.frameResolution)
693
- );
694
- const maximumSafeAtlasLod = float(Math.max(0, Math.floor(Math.log2(Math.max(1, descriptor.padding)))));
695
744
  const FrameBasis = NAMED_STRUCT_FACTORY(
696
745
  { right: "vec3", up: "vec3" },
697
746
  "ImpostorFrameBasis"
@@ -769,22 +818,6 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
769
818
  const halfTexel = float(0.5001).div(frameResolution);
770
819
  return frame.add(insideCell.clamp(halfTexel, float(1).sub(halfTexel))).div(frames);
771
820
  }, "vec2");
772
- const atlasLod = Fn(([uvNode]) => {
773
- const texelCoordinate = uvNode.mul(atlasResolution);
774
- const footprint = max(
775
- texelCoordinate.dFdx().length(),
776
- texelCoordinate.dFdy().length()
777
- );
778
- return max(footprint, 1).log2().clamp(0, maximumSafeAtlasLod);
779
- }, "float");
780
- const materialAtlasLod = Fn(([uvNode]) => {
781
- const texelCoordinate = uvNode.mul(materialAtlasResolution);
782
- const footprint = max(
783
- texelCoordinate.dFdx().length(),
784
- texelCoordinate.dFdy().length()
785
- );
786
- return max(footprint, 1).log2().clamp(0, maximumSafeAtlasLod);
787
- }, "float");
788
821
  const decodeDepth16 = Fn(([packed]) => {
789
822
  if (descriptor.compression?.depthEncoding === "lossless-geometry10-rgba") {
790
823
  const bytes2 = floor(packed.rg.mul(255).add(0.5));
@@ -1017,8 +1050,6 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1017
1050
  });
1018
1051
  }
1019
1052
  const uvNode = atlasUv(frame, frameUv);
1020
- const lod = atlasLod(uvNode);
1021
- const materialLod = compactLayout ? materialAtlasLod(uvNode) : lod;
1022
1053
  reconstructed.assign(reconstructPosition(
1023
1054
  frameUv,
1024
1055
  bounds,
@@ -1027,7 +1058,7 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1027
1058
  up,
1028
1059
  sampleDepth16(uvNode)
1029
1060
  ));
1030
- const shadingSample = texture(normalDepthAtlas, uvNode).level(materialLod);
1061
+ const shadingSample = sampleAtlas(normalDepthAtlas, uvNode, mapLod(normalDepthAtlas, uvNode));
1031
1062
  const geometrySample = compressedGeometry ? texture(geometryAtlas, uvNode).level(0) : null;
1032
1063
  const viewNormal = decodeOctahedral(shadingSample.rg);
1033
1064
  const normal = right.mul(viewNormal.x).add(up.mul(viewNormal.y)).add(frameDirection.mul(viewNormal.z)).normalize();
@@ -1060,9 +1091,10 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1060
1091
  const strictValidFloat = boundsConfidence.mul(
1061
1092
  withReprojection ? alignedConfidence.mul(strictRootSolved) : float(1)
1062
1093
  );
1063
- const baseColor = texture(baseColorAtlas, colorAtlasUv(uvNode)).level(materialLod);
1094
+ const colorUv = colorAtlasUv(uvNode);
1095
+ const baseColor = sampleAtlas(baseColorAtlas, colorUv, mapLod(baseColorAtlas, colorUv), colorColumns);
1064
1096
  const capturedAlpha = sampleCapturedAlpha(uvNode);
1065
- const orm = compactLayout ? vec4(geometrySample.b, shadingSample.b, shadingSample.a, 1) : ormAtlas ? texture(ormAtlas, uvNode).level(lod) : vec4(
1097
+ const orm = compactLayout ? vec4(geometrySample.b, shadingSample.b, shadingSample.a, 1) : ormAtlas ? sampleAtlas(ormAtlas, uvNode, mapLod(ormAtlas, uvNode)) : vec4(
1066
1098
  descriptor.material.occlusion,
1067
1099
  descriptor.material.roughness,
1068
1100
  descriptor.material.metalness,
@@ -1080,7 +1112,7 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1080
1112
  faceForwardNormal,
1081
1113
  reconstructed,
1082
1114
  orm.rgb,
1083
- emissiveAtlas ? texture(emissiveAtlas, colorAtlasUv(uvNode, true)).level(materialLod).rgb : vec3(0),
1115
+ emissiveAtlas ? sampleAtlas(emissiveAtlas, colorAtlasUv(uvNode, true), mapLod(emissiveAtlas, colorAtlasUv(uvNode, true)), colorColumns).rgb : vec3(0),
1084
1116
  coverage,
1085
1117
  resolvedValidity,
1086
1118
  strictValidFloat,
@@ -1316,6 +1348,28 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1316
1348
  material.receivedShadowPositionNode = reconstructedWorldPosition;
1317
1349
  material.toneMapped = true;
1318
1350
  material.userData.impostorShaderProfile = profile;
1351
+ bindTextureSlot(material, "baseColorAtlas", maps.baseColor, [baseColorAtlas, ...compactLayout && compactEmissive && emissiveAtlas ? [emissiveAtlas] : []], (value) => {
1352
+ maps.baseColor = value;
1353
+ if (compactLayout && compactEmissive) maps.emissive = value;
1354
+ if (material.userData.impostor) {
1355
+ material.userData.impostor.atlases.baseColor = value;
1356
+ if (compactLayout && compactEmissive) material.userData.impostor.atlases.emissive = value;
1357
+ }
1358
+ });
1359
+ bindTextureSlot(material, "normalDepthAtlas", maps.normalDepth, [normalDepthAtlas, ...compactLayout && ormAtlas ? [ormAtlas] : [], ...!compressedGeometry ? [geometryAtlas] : []], (value) => {
1360
+ maps.normalDepth = value;
1361
+ if (compactLayout) maps.orm = value;
1362
+ if (material.userData.impostor) {
1363
+ material.userData.impostor.atlases.normalDepth = value;
1364
+ if (compactLayout) material.userData.impostor.atlases.orm = value;
1365
+ }
1366
+ });
1367
+ if (!compactLayout && maps.orm && ormAtlas) bindTextureSlot(material, "ormAtlas", maps.orm, [ormAtlas], (value) => {
1368
+ maps.orm = value;
1369
+ });
1370
+ if (!compactLayout && maps.emissive && emissiveAtlas) bindTextureSlot(material, "emissiveAtlas", maps.emissive, [emissiveAtlas], (value) => {
1371
+ maps.emissive = value;
1372
+ });
1319
1373
  return material;
1320
1374
  }
1321
1375
  export {