@needle-tools/three-mesh-effects 0.0.0-stage → 0.1.0-alpha
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.
- package/LICENSE +21 -0
- package/README.md +88 -3
- package/build/deformation-runtime.js +2143 -0
- package/build/deformation-runtime.js.map +7 -0
- package/build/impostor-runtime-needle-engine.js +42 -0
- package/build/impostor-runtime-needle-engine.js.map +7 -0
- package/build/impostor-runtime.js +1333 -0
- package/build/impostor-runtime.js.map +7 -0
- package/build/internal/deformation/cloth-surface.js +288 -0
- package/build/internal/deformation/cloth-surface.js.map +7 -0
- package/build/internal/deformation/fur-surface.js +130 -0
- package/build/internal/deformation/fur-surface.js.map +7 -0
- package/build/internal/deformation/ik-constraints.js +44 -0
- package/build/internal/deformation/ik-constraints.js.map +7 -0
- package/build/internal/deformation/joints-json.js +197 -0
- package/build/internal/deformation/joints-json.js.map +7 -0
- package/build/internal/deformation/limits.js +16 -0
- package/build/internal/deformation/limits.js.map +7 -0
- package/build/internal/deformation/motion-settings.js +12 -0
- package/build/internal/deformation/motion-settings.js.map +7 -0
- package/build/internal/deformation/pin-cloth.js +196 -0
- package/build/internal/deformation/pin-cloth.js.map +7 -0
- package/build/internal/deformation/pin-ik.js +650 -0
- package/build/internal/deformation/pin-ik.js.map +7 -0
- package/build/internal/deformation/pin-types.js +12 -0
- package/build/internal/deformation/pin-types.js.map +7 -0
- package/build/internal/deformation/pin-wiggle.js +249 -0
- package/build/internal/deformation/pin-wiggle.js.map +7 -0
- package/build/internal/deformation/pose-slots.js +84 -0
- package/build/internal/deformation/pose-slots.js.map +7 -0
- package/build/internal/deformation/pose-timeline.js +210 -0
- package/build/internal/deformation/pose-timeline.js.map +7 -0
- package/build/internal/deformation/rig-drivers.js +48 -0
- package/build/internal/deformation/rig-drivers.js.map +7 -0
- package/build/internal/deformation/rig-extension.js +334 -0
- package/build/internal/deformation/rig-extension.js.map +7 -0
- package/build/internal/deformation/rig-runtime.js +2141 -0
- package/build/internal/deformation/rig-runtime.js.map +7 -0
- package/build/internal/deformation/wind.js +34 -0
- package/build/internal/deformation/wind.js.map +7 -0
- package/build/internal/impostor/impostor-metadata.js +77 -0
- package/build/internal/impostor/impostor-metadata.js.map +7 -0
- package/build/internal/impostor/impostor-node-material.js +1331 -0
- package/build/internal/impostor/impostor-node-material.js.map +7 -0
- package/build/internal/impostor/impostor-runtime-support.js +162 -0
- package/build/internal/impostor/impostor-runtime-support.js.map +7 -0
- package/build/internal/impostor/octahedral.js +232 -0
- package/build/internal/impostor/octahedral.js.map +7 -0
- package/build/internal/pixel-impostor/pixel-impostor-display.js +728 -0
- package/build/internal/pixel-impostor/pixel-impostor-display.js.map +7 -0
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js +71 -0
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +7 -0
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js +150 -0
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +7 -0
- package/build/internal/public/deformation-runtime.js +2143 -0
- package/build/internal/public/deformation-runtime.js.map +7 -0
- package/build/internal/public/impostor-runtime-needle-engine.js +42 -0
- package/build/internal/public/impostor-runtime-needle-engine.js.map +7 -0
- package/build/internal/public/impostor-runtime.js +1333 -0
- package/build/internal/public/impostor-runtime.js.map +7 -0
- package/build/internal/public/pixel-impostor-runtime.js +876 -0
- package/build/internal/public/pixel-impostor-runtime.js.map +7 -0
- package/build/internal/public/wrap-effects.js +5640 -0
- package/build/internal/public/wrap-effects.js.map +7 -0
- package/build/internal/rendering/skin-projection.js +4630 -0
- package/build/internal/rendering/skin-projection.js.map +7 -0
- package/build/internal/rendering/surface-query.js +4519 -0
- package/build/internal/rendering/surface-query.js.map +7 -0
- package/build/internal/rendering/wireframe.js +112 -0
- package/build/internal/rendering/wireframe.js.map +7 -0
- package/build/internal/rendering/wrap-preview-settings.js +126 -0
- package/build/internal/rendering/wrap-preview-settings.js.map +7 -0
- package/build/internal/rendering/wrap-preview.js +5610 -0
- package/build/internal/rendering/wrap-preview.js.map +7 -0
- package/build/internal/rendering/wrap-skin.js +4746 -0
- package/build/internal/rendering/wrap-skin.js.map +7 -0
- package/build/pixel-impostor-runtime.js +876 -0
- package/build/pixel-impostor-runtime.js.map +7 -0
- package/build/types/deformation/cloth-surface.d.ts +39 -0
- package/build/types/deformation/cloth-surface.js +1 -0
- package/build/types/deformation/fur-surface.d.ts +27 -0
- package/build/types/deformation/fur-surface.js +1 -0
- package/build/types/deformation/ik-constraints.d.ts +18 -0
- package/build/types/deformation/ik-constraints.js +1 -0
- package/build/types/deformation/joints-json.d.ts +17 -0
- package/build/types/deformation/joints-json.js +1 -0
- package/build/types/deformation/limits.d.ts +23 -0
- package/build/types/deformation/limits.js +1 -0
- package/build/types/deformation/motion-settings.d.ts +9 -0
- package/build/types/deformation/motion-settings.js +1 -0
- package/build/types/deformation/pin-cloth.d.ts +28 -0
- package/build/types/deformation/pin-cloth.js +1 -0
- package/build/types/deformation/pin-ik.d.ts +37 -0
- package/build/types/deformation/pin-ik.js +1 -0
- package/build/types/deformation/pin-types.d.ts +53 -0
- package/build/types/deformation/pin-types.js +1 -0
- package/build/types/deformation/pin-wiggle.d.ts +16 -0
- package/build/types/deformation/pin-wiggle.js +1 -0
- package/build/types/deformation/pose-slots.d.ts +41 -0
- package/build/types/deformation/pose-slots.js +1 -0
- package/build/types/deformation/pose-timeline.d.ts +65 -0
- package/build/types/deformation/pose-timeline.js +1 -0
- package/build/types/deformation/rig-drivers.d.ts +19 -0
- package/build/types/deformation/rig-drivers.js +1 -0
- package/build/types/deformation/rig-extension.d.ts +33 -0
- package/build/types/deformation/rig-extension.js +1 -0
- package/build/types/deformation/rig-runtime.d.ts +95 -0
- package/build/types/deformation/rig-runtime.js +1 -0
- package/build/types/deformation/wind.d.ts +16 -0
- package/build/types/deformation/wind.js +1 -0
- package/build/types/impostor/impostor-metadata.d.ts +61 -0
- package/build/types/impostor/impostor-metadata.js +1 -0
- package/build/types/impostor/impostor-node-material.d.ts +140 -0
- package/build/types/impostor/impostor-node-material.js +1 -0
- package/build/types/impostor/impostor-runtime-support.d.ts +52 -0
- package/build/types/impostor/impostor-runtime-support.js +1 -0
- package/build/types/impostor/octahedral.d.ts +84 -0
- package/build/types/impostor/octahedral.js +1 -0
- package/build/types/pixel-impostor/pixel-impostor-display.d.ts +48 -0
- package/build/types/pixel-impostor/pixel-impostor-display.js +1 -0
- package/build/types/pixel-impostor/pixel-impostor-metadata.d.ts +34 -0
- package/build/types/pixel-impostor/pixel-impostor-metadata.js +1 -0
- package/build/types/pixel-impostor/pixel-impostor-runtime.d.ts +18 -0
- package/build/types/pixel-impostor/pixel-impostor-runtime.js +1 -0
- package/build/types/public/deformation-runtime.d.ts +3 -0
- package/build/types/public/deformation-runtime.js +1 -0
- package/build/types/public/impostor-runtime-needle-engine.d.ts +31 -0
- package/build/types/public/impostor-runtime-needle-engine.js +1 -0
- package/build/types/public/impostor-runtime.d.ts +6 -0
- package/build/types/public/impostor-runtime.js +1 -0
- package/build/types/public/pixel-impostor-runtime.d.ts +3 -0
- package/build/types/public/pixel-impostor-runtime.js +1 -0
- package/build/types/public/wrap-effects.d.ts +4 -0
- package/build/types/public/wrap-effects.js +1 -0
- package/build/types/rendering/skin-projection.d.ts +44 -0
- package/build/types/rendering/skin-projection.js +1 -0
- package/build/types/rendering/surface-query.d.ts +66 -0
- package/build/types/rendering/surface-query.js +1 -0
- package/build/types/rendering/wireframe.d.ts +25 -0
- package/build/types/rendering/wireframe.js +1 -0
- package/build/types/rendering/wrap-preview-settings.d.ts +22 -0
- package/build/types/rendering/wrap-preview-settings.js +1 -0
- package/build/types/rendering/wrap-preview.d.ts +39 -0
- package/build/types/rendering/wrap-preview.js +1 -0
- package/build/types/rendering/wrap-skin.d.ts +14 -0
- package/build/types/rendering/wrap-skin.js +1 -0
- package/build/wrap-effects.js +5640 -0
- package/build/wrap-effects.js.map +7 -0
- package/package.json +94 -6
- package/src/deformation/cloth-surface.ts +223 -0
- package/src/deformation/fur-surface.ts +129 -0
- package/src/deformation/ik-constraints.ts +37 -0
- package/src/deformation/joints-json.ts +94 -0
- package/src/deformation/limits.ts +25 -0
- package/src/deformation/motion-settings.ts +9 -0
- package/src/deformation/pin-cloth.ts +149 -0
- package/src/deformation/pin-ik.ts +582 -0
- package/src/deformation/pin-types.ts +22 -0
- package/src/deformation/pin-wiggle.ts +119 -0
- package/src/deformation/pose-slots.ts +106 -0
- package/src/deformation/pose-timeline.ts +242 -0
- package/src/deformation/rig-drivers.ts +32 -0
- package/src/deformation/rig-extension.ts +87 -0
- package/src/deformation/rig-runtime.ts +416 -0
- package/src/deformation/wind.ts +36 -0
- package/src/impostor/impostor-metadata.js +137 -0
- package/src/impostor/impostor-node-material.js +1581 -0
- package/src/impostor/impostor-runtime-support.js +139 -0
- package/src/impostor/octahedral.js +280 -0
- package/src/pixel-impostor/pixel-impostor-display.js +794 -0
- package/src/pixel-impostor/pixel-impostor-metadata.js +74 -0
- package/src/pixel-impostor/pixel-impostor-runtime.js +78 -0
- package/src/public/deformation-runtime.ts +3 -0
- package/src/public/impostor-runtime-needle-engine.js +56 -0
- package/src/public/impostor-runtime.js +21 -0
- package/src/public/pixel-impostor-runtime.js +5 -0
- package/src/public/wrap-effects.js +21 -0
- package/src/rendering/skin-projection.js +113 -0
- package/src/rendering/surface-query.js +88 -0
- package/src/rendering/wireframe.js +115 -0
- package/src/rendering/wrap-preview-settings.js +123 -0
- package/src/rendering/wrap-preview.js +645 -0
- package/src/rendering/wrap-skin.js +109 -0
|
@@ -0,0 +1,1331 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
// src/impostor/impostor-node-material.js
|
|
4
|
+
import {
|
|
5
|
+
ClampToEdgeWrapping,
|
|
6
|
+
DoubleSide,
|
|
7
|
+
GLSL3,
|
|
8
|
+
LinearFilter,
|
|
9
|
+
LinearMipmapLinearFilter,
|
|
10
|
+
MeshStandardNodeMaterial,
|
|
11
|
+
NoColorSpace,
|
|
12
|
+
SRGBColorSpace,
|
|
13
|
+
ShaderMaterial,
|
|
14
|
+
Vector3,
|
|
15
|
+
Vector4
|
|
16
|
+
} from "three/webgpu";
|
|
17
|
+
import * as TSL from "three/tsl";
|
|
18
|
+
import {
|
|
19
|
+
Fn,
|
|
20
|
+
If,
|
|
21
|
+
Loop,
|
|
22
|
+
abs,
|
|
23
|
+
any,
|
|
24
|
+
bool,
|
|
25
|
+
cameraFar,
|
|
26
|
+
cameraNear,
|
|
27
|
+
cameraPosition,
|
|
28
|
+
cameraWorldMatrix,
|
|
29
|
+
cross,
|
|
30
|
+
dot,
|
|
31
|
+
exp,
|
|
32
|
+
float,
|
|
33
|
+
floor,
|
|
34
|
+
fract,
|
|
35
|
+
fwidth,
|
|
36
|
+
int,
|
|
37
|
+
ivec2,
|
|
38
|
+
max,
|
|
39
|
+
min,
|
|
40
|
+
mix,
|
|
41
|
+
modelViewMatrix,
|
|
42
|
+
modelWorldMatrix,
|
|
43
|
+
modelWorldMatrixInverse,
|
|
44
|
+
positionGeometry,
|
|
45
|
+
smoothstep,
|
|
46
|
+
texture,
|
|
47
|
+
textureLoad,
|
|
48
|
+
transformNormalToView,
|
|
49
|
+
uniformArray,
|
|
50
|
+
vec2,
|
|
51
|
+
vec3,
|
|
52
|
+
vec4,
|
|
53
|
+
viewZToOrthographicDepth,
|
|
54
|
+
viewZToPerspectiveDepth
|
|
55
|
+
} from "three/tsl";
|
|
56
|
+
|
|
57
|
+
// src/impostor/impostor-metadata.js
|
|
58
|
+
var NEEDLE_MESH_IMPOSTOR = "NEEDLE_mesh_impostor";
|
|
59
|
+
function parseImpostorDescriptor(value) {
|
|
60
|
+
if (!value || typeof value !== "object") return null;
|
|
61
|
+
const descriptor = (
|
|
62
|
+
/** @type {Partial<ImpostorDescriptor>} */
|
|
63
|
+
value
|
|
64
|
+
);
|
|
65
|
+
if (descriptor.version !== 1 || descriptor.mapping !== "hemisphere" && descriptor.mapping !== "full" && descriptor.mapping !== "single" || !isPositiveInteger(descriptor.frames) || (descriptor.mapping === "single" ? descriptor.frames !== 1 || !isUnitVec3(descriptor.captureDirection) : false) || !isPositiveInteger(descriptor.frameResolution) || !isNonNegativeInteger(descriptor.padding) || !isPositiveFinite(descriptor.radius) || descriptor.compression !== void 0 && (descriptor.compression.version !== 4 || descriptor.compression.layout !== "compact-atlas-v4" || descriptor.compression.depthEncoding !== "lossless-geometry10-rgba" || !isPositiveInteger(descriptor.compression.materialFrameResolution) || typeof descriptor.compression.emissive !== "boolean") || !isFiniteVec3(descriptor.boundsMin) || !isFiniteVec3(descriptor.boundsMax) || !isFiniteVec3(descriptor.center) || !Array.isArray(descriptor.viewBounds) || descriptor.viewBounds.length !== (descriptor.frames ?? 0) * (descriptor.frames ?? 0) * 4 || !descriptor.viewBounds.every(Number.isFinite) || descriptor.coverageEncoding !== "captured-alpha+sdf-material" && descriptor.coverageEncoding !== "sdf-base-alpha" || !isUnitInterval(descriptor.captureAlphaCutoff) || !isUnitInterval(descriptor.alphaCutoff) || !isBlendZone(descriptor.blendZone) || !isNonNegativeInteger(descriptor.parallaxSteps) || typeof descriptor.depthDerivedWeights !== "boolean" || typeof descriptor.castShadow !== "boolean" || typeof descriptor.receiveShadow !== "boolean" || !descriptor.material || !isUnitInterval(descriptor.material.occlusion) || !isUnitInterval(descriptor.material.roughness) || !isUnitInterval(descriptor.material.metalness)) return null;
|
|
66
|
+
const valid = (
|
|
67
|
+
/** @type {ImpostorDescriptor} */
|
|
68
|
+
descriptor
|
|
69
|
+
);
|
|
70
|
+
return {
|
|
71
|
+
version: 1,
|
|
72
|
+
mapping: valid.mapping,
|
|
73
|
+
frames: valid.frames,
|
|
74
|
+
...valid.mapping === "single" ? { captureDirection: valid.captureDirection } : {},
|
|
75
|
+
frameResolution: valid.frameResolution,
|
|
76
|
+
padding: valid.padding,
|
|
77
|
+
radius: valid.radius,
|
|
78
|
+
boundsMin: valid.boundsMin,
|
|
79
|
+
boundsMax: valid.boundsMax,
|
|
80
|
+
center: valid.center,
|
|
81
|
+
viewBounds: valid.viewBounds,
|
|
82
|
+
coverageEncoding: valid.coverageEncoding,
|
|
83
|
+
captureAlphaCutoff: valid.captureAlphaCutoff,
|
|
84
|
+
alphaCutoff: valid.alphaCutoff,
|
|
85
|
+
blendZone: valid.blendZone,
|
|
86
|
+
parallaxSteps: valid.parallaxSteps,
|
|
87
|
+
depthDerivedWeights: valid.depthDerivedWeights,
|
|
88
|
+
castShadow: valid.castShadow,
|
|
89
|
+
receiveShadow: valid.receiveShadow,
|
|
90
|
+
material: valid.material,
|
|
91
|
+
...valid.compression ? { compression: valid.compression } : {}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function isFiniteVec3(value) {
|
|
95
|
+
return Array.isArray(value) && value.length === 3 && value.every(Number.isFinite);
|
|
96
|
+
}
|
|
97
|
+
function isUnitVec3(value) {
|
|
98
|
+
if (!isFiniteVec3(value)) return false;
|
|
99
|
+
const vector = (
|
|
100
|
+
/** @type {number[]} */
|
|
101
|
+
value
|
|
102
|
+
);
|
|
103
|
+
return Math.abs(Math.hypot(vector[0], vector[1], vector[2]) - 1) < 1e-4;
|
|
104
|
+
}
|
|
105
|
+
function isPositiveInteger(value) {
|
|
106
|
+
return Number.isInteger(value) && /** @type {number} */
|
|
107
|
+
value > 0;
|
|
108
|
+
}
|
|
109
|
+
function isNonNegativeInteger(value) {
|
|
110
|
+
return Number.isInteger(value) && /** @type {number} */
|
|
111
|
+
value >= 0;
|
|
112
|
+
}
|
|
113
|
+
function isPositiveFinite(value) {
|
|
114
|
+
return Number.isFinite(value) && /** @type {number} */
|
|
115
|
+
value > 0;
|
|
116
|
+
}
|
|
117
|
+
function isUnitInterval(value) {
|
|
118
|
+
return Number.isFinite(value) && /** @type {number} */
|
|
119
|
+
value >= 0 && /** @type {number} */
|
|
120
|
+
value <= 1;
|
|
121
|
+
}
|
|
122
|
+
function isBlendZone(value) {
|
|
123
|
+
return Number.isFinite(value) && /** @type {number} */
|
|
124
|
+
value >= 0.35 && /** @type {number} */
|
|
125
|
+
value <= 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/impostor/octahedral.js
|
|
129
|
+
var PERSPECTIVE_RAY_PLANE_SIGN = -1;
|
|
130
|
+
|
|
131
|
+
// src/impostor/impostor-runtime-support.js
|
|
132
|
+
import {
|
|
133
|
+
DataTexture,
|
|
134
|
+
FloatType,
|
|
135
|
+
NearestFilter,
|
|
136
|
+
RGBAFormat
|
|
137
|
+
} from "three";
|
|
138
|
+
var REPROJECTION_CONFIDENCE_INNER_TEXELS = 0.25;
|
|
139
|
+
var REPROJECTION_CONFIDENCE_OUTER_TEXELS = 16;
|
|
140
|
+
function impostorContourUnionSharpness(frames) {
|
|
141
|
+
if (!Number.isInteger(frames) || frames < 2) {
|
|
142
|
+
throw new RangeError("Impostor contour union requires at least two frames per atlas axis.");
|
|
143
|
+
}
|
|
144
|
+
return Math.max(1, 16 / frames);
|
|
145
|
+
}
|
|
146
|
+
function createImpostorViewBoundsTexture(descriptor) {
|
|
147
|
+
const texture2 = new DataTexture(
|
|
148
|
+
new Float32Array(descriptor.viewBounds),
|
|
149
|
+
descriptor.frames,
|
|
150
|
+
descriptor.frames,
|
|
151
|
+
RGBAFormat,
|
|
152
|
+
FloatType
|
|
153
|
+
);
|
|
154
|
+
texture2.name = "impostor view bounds";
|
|
155
|
+
texture2.minFilter = NearestFilter;
|
|
156
|
+
texture2.magFilter = NearestFilter;
|
|
157
|
+
texture2.generateMipmaps = false;
|
|
158
|
+
texture2.needsUpdate = true;
|
|
159
|
+
return texture2;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/impostor/impostor-node-material.js
|
|
163
|
+
var NAMED_STRUCT_FACTORY = Reflect.get(TSL, "struct");
|
|
164
|
+
var RUNTIME_FEATURE_NAMES = (
|
|
165
|
+
/** @type {const} */
|
|
166
|
+
[
|
|
167
|
+
"interpolation",
|
|
168
|
+
"reprojection",
|
|
169
|
+
"depthWeights",
|
|
170
|
+
"perspectiveCorrection",
|
|
171
|
+
"cellCorrection",
|
|
172
|
+
"boundedReprojection"
|
|
173
|
+
]
|
|
174
|
+
);
|
|
175
|
+
var DEFAULT_IMPOSTOR_RUNTIME_FEATURES = Object.freeze({
|
|
176
|
+
interpolation: true,
|
|
177
|
+
reprojection: true,
|
|
178
|
+
depthWeights: true,
|
|
179
|
+
perspectiveCorrection: true,
|
|
180
|
+
cellCorrection: false,
|
|
181
|
+
boundedReprojection: true
|
|
182
|
+
});
|
|
183
|
+
var IMPOSTOR_RUNTIME_SAMPLE_LIMITS = Object.freeze({
|
|
184
|
+
raySamples: 32,
|
|
185
|
+
depthSamples: 8
|
|
186
|
+
});
|
|
187
|
+
var IMPOSTOR_SHADOW_CASTER_DEPTH_BIAS = 2e-3;
|
|
188
|
+
var HEMISPHERE_HORIZON_TRANSITION_SINE = Math.sin(Math.PI / 180);
|
|
189
|
+
function rendererSupportsImpostorNodeMaterials(renderer) {
|
|
190
|
+
if (typeof NAMED_STRUCT_FACTORY !== "function") return false;
|
|
191
|
+
return renderer?.isWebGPURenderer === true || renderer?.capabilities?.supportsNodeMaterials === true;
|
|
192
|
+
}
|
|
193
|
+
function resolveImpostorRuntimeFeatures(descriptor, input = {}) {
|
|
194
|
+
const requested = {
|
|
195
|
+
...DEFAULT_IMPOSTOR_RUNTIME_FEATURES,
|
|
196
|
+
raySamples: descriptor.parallaxSteps * 4,
|
|
197
|
+
depthSamples: descriptor.parallaxSteps,
|
|
198
|
+
blendZone: descriptor.blendZone,
|
|
199
|
+
...input
|
|
200
|
+
};
|
|
201
|
+
for (const name of RUNTIME_FEATURE_NAMES) requested[name] = requested[name] !== false;
|
|
202
|
+
requested.raySamples = clampRuntimeSamples(
|
|
203
|
+
requested.raySamples,
|
|
204
|
+
IMPOSTOR_RUNTIME_SAMPLE_LIMITS.raySamples
|
|
205
|
+
);
|
|
206
|
+
requested.depthSamples = clampRuntimeSamples(
|
|
207
|
+
requested.depthSamples,
|
|
208
|
+
IMPOSTOR_RUNTIME_SAMPLE_LIMITS.depthSamples
|
|
209
|
+
);
|
|
210
|
+
requested.blendZone = Math.min(1, Math.max(0.35, Number(requested.blendZone) || descriptor.blendZone));
|
|
211
|
+
const interpolation = requested.interpolation;
|
|
212
|
+
const reprojection = interpolation && requested.reprojection && requested.raySamples > 0;
|
|
213
|
+
const depthWeights = interpolation && requested.depthWeights && descriptor.depthDerivedWeights;
|
|
214
|
+
const perspectiveCorrection = requested.perspectiveCorrection;
|
|
215
|
+
const cellCorrection = interpolation && perspectiveCorrection && requested.cellCorrection;
|
|
216
|
+
const boundedReprojection = reprojection && requested.boundedReprojection;
|
|
217
|
+
return {
|
|
218
|
+
requested,
|
|
219
|
+
effective: {
|
|
220
|
+
interpolation,
|
|
221
|
+
reprojection,
|
|
222
|
+
depthWeights,
|
|
223
|
+
perspectiveCorrection,
|
|
224
|
+
cellCorrection,
|
|
225
|
+
boundedReprojection
|
|
226
|
+
},
|
|
227
|
+
profile: {
|
|
228
|
+
interpolation,
|
|
229
|
+
reprojection,
|
|
230
|
+
depthWeights,
|
|
231
|
+
perspectiveCorrection,
|
|
232
|
+
cellCorrection,
|
|
233
|
+
boundedReprojection,
|
|
234
|
+
frameSamples: interpolation ? 3 : 1,
|
|
235
|
+
raySamples: reprojection ? requested.raySamples : 0,
|
|
236
|
+
depthSamples: reprojection ? requested.depthSamples : 0,
|
|
237
|
+
blendZone: requested.blendZone
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function clampRuntimeSamples(value, maximum) {
|
|
242
|
+
const numeric = Number(value);
|
|
243
|
+
if (!Number.isFinite(numeric)) return 0;
|
|
244
|
+
return Math.min(maximum, Math.max(0, Math.round(numeric)));
|
|
245
|
+
}
|
|
246
|
+
function upgradeImpostorScene(root, bindings = /* @__PURE__ */ new Map(), inputFeatures = {}, renderer) {
|
|
247
|
+
let upgraded = 0;
|
|
248
|
+
root.traverse((object) => {
|
|
249
|
+
const binding = bindings.get(object);
|
|
250
|
+
const metadata = binding?.descriptor ?? object.userData?.gltfExtensions?.[NEEDLE_MESH_IMPOSTOR];
|
|
251
|
+
if (metadata === void 0) return;
|
|
252
|
+
if (!("isMesh" in object) || object.isMesh !== true) {
|
|
253
|
+
throw new Error("Impostor metadata must belong to a mesh.");
|
|
254
|
+
}
|
|
255
|
+
const descriptor = parseImpostorDescriptor(metadata);
|
|
256
|
+
if (!descriptor) {
|
|
257
|
+
throw new Error("Unsupported impostor GLB descriptor; rebake this asset with the current baker.");
|
|
258
|
+
}
|
|
259
|
+
if (!binding) {
|
|
260
|
+
throw new Error("NEEDLE_mesh_impostor textures require the registered GLTFLoader plugin.");
|
|
261
|
+
}
|
|
262
|
+
const fallback = Array.isArray(object.material) ? object.material[0] : object.material;
|
|
263
|
+
if (!fallback) return;
|
|
264
|
+
const compactLayout = descriptor.compression?.layout === "compact-atlas-v4";
|
|
265
|
+
if (!binding.maps.baseColor || !binding.maps.normalDepth) {
|
|
266
|
+
throw new Error("Impostor GLB is missing a required base-color or normal-depth atlas.");
|
|
267
|
+
}
|
|
268
|
+
const compressedGeometry = descriptor.compression?.depthEncoding === "lossless-geometry10-rgba";
|
|
269
|
+
if (compressedGeometry && !binding.maps.geometry) {
|
|
270
|
+
throw new Error("Compressed impostor GLB is missing its geometry atlas.");
|
|
271
|
+
}
|
|
272
|
+
binding.maps.baseColor.colorSpace = SRGBColorSpace;
|
|
273
|
+
binding.maps.normalDepth.colorSpace = NoColorSpace;
|
|
274
|
+
if (binding.maps.material) binding.maps.material.colorSpace = NoColorSpace;
|
|
275
|
+
if (binding.maps.geometry) binding.maps.geometry.colorSpace = NoColorSpace;
|
|
276
|
+
if (binding.maps.emissive) binding.maps.emissive.colorSpace = SRGBColorSpace;
|
|
277
|
+
for (const atlas of [
|
|
278
|
+
binding.maps.baseColor,
|
|
279
|
+
binding.maps.normalDepth,
|
|
280
|
+
binding.maps.material,
|
|
281
|
+
binding.maps.geometry,
|
|
282
|
+
binding.maps.emissive
|
|
283
|
+
]) {
|
|
284
|
+
if (!atlas) continue;
|
|
285
|
+
atlas.wrapS = ClampToEdgeWrapping;
|
|
286
|
+
atlas.wrapT = ClampToEdgeWrapping;
|
|
287
|
+
atlas.minFilter = LinearMipmapLinearFilter;
|
|
288
|
+
atlas.magFilter = LinearFilter;
|
|
289
|
+
atlas.generateMipmaps = !atlas.isCompressedTexture;
|
|
290
|
+
atlas.needsUpdate = true;
|
|
291
|
+
}
|
|
292
|
+
if (compressedGeometry && binding.maps.geometry) {
|
|
293
|
+
binding.maps.geometry.minFilter = LinearFilter;
|
|
294
|
+
binding.maps.geometry.generateMipmaps = false;
|
|
295
|
+
binding.maps.geometry.needsUpdate = true;
|
|
296
|
+
}
|
|
297
|
+
const viewBoundsTexture = createImpostorViewBoundsTexture(descriptor);
|
|
298
|
+
const maps = {
|
|
299
|
+
baseColor: binding.maps.baseColor,
|
|
300
|
+
normalDepth: binding.maps.normalDepth,
|
|
301
|
+
orm: compactLayout ? binding.maps.normalDepth : binding.maps.material,
|
|
302
|
+
emissive: compactLayout ? descriptor.compression?.emissive ? binding.maps.baseColor : null : binding.maps.emissive,
|
|
303
|
+
geometry: compressedGeometry ? binding.maps.geometry : null,
|
|
304
|
+
viewBounds: viewBoundsTexture
|
|
305
|
+
};
|
|
306
|
+
const nodeMaterials = rendererSupportsImpostorNodeMaterials(renderer);
|
|
307
|
+
const runtime = {
|
|
308
|
+
backend: nodeMaterials ? "three-tsl" : "three-webgl-compat",
|
|
309
|
+
descriptor,
|
|
310
|
+
viewBoundsTexture,
|
|
311
|
+
atlases: {
|
|
312
|
+
baseColor: binding.maps.baseColor,
|
|
313
|
+
normalDepth: binding.maps.normalDepth,
|
|
314
|
+
orm: compactLayout ? binding.maps.normalDepth : binding.maps.material,
|
|
315
|
+
emissive: compactLayout ? descriptor.compression?.emissive ? binding.maps.baseColor : null : binding.maps.emissive,
|
|
316
|
+
geometry: compressedGeometry ? binding.maps.geometry : null
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
const requestedFeatures = {
|
|
320
|
+
...DEFAULT_IMPOSTOR_RUNTIME_FEATURES,
|
|
321
|
+
raySamples: descriptor.parallaxSteps * 4,
|
|
322
|
+
depthSamples: descriptor.parallaxSteps,
|
|
323
|
+
blendZone: descriptor.blendZone,
|
|
324
|
+
...inputFeatures
|
|
325
|
+
};
|
|
326
|
+
const debugControls = Object.fromEntries(RUNTIME_FEATURE_NAMES.map((name) => [
|
|
327
|
+
name,
|
|
328
|
+
{ value: requestedFeatures[name] ? 1 : 0 }
|
|
329
|
+
]));
|
|
330
|
+
debugControls.raySamples = { value: descriptor.parallaxSteps * 4 };
|
|
331
|
+
debugControls.depthSamples = { value: descriptor.parallaxSteps };
|
|
332
|
+
debugControls.blendZone = { value: descriptor.blendZone };
|
|
333
|
+
debugControls.setFeatures = (features) => {
|
|
334
|
+
const next = resolveImpostorRuntimeFeatures(descriptor, features);
|
|
335
|
+
if (JSON.stringify(next.requested) === JSON.stringify(requestedFeatures)) return false;
|
|
336
|
+
Object.assign(requestedFeatures, next.requested);
|
|
337
|
+
for (const name of RUNTIME_FEATURE_NAMES) {
|
|
338
|
+
debugControls[name].value = requestedFeatures[name] ? 1 : 0;
|
|
339
|
+
}
|
|
340
|
+
debugControls.raySamples.value = requestedFeatures.raySamples;
|
|
341
|
+
debugControls.depthSamples.value = requestedFeatures.depthSamples;
|
|
342
|
+
debugControls.blendZone.value = requestedFeatures.blendZone;
|
|
343
|
+
const previous = object.material;
|
|
344
|
+
const replacement = createPbrImpostorNodeMaterial(
|
|
345
|
+
descriptor,
|
|
346
|
+
maps,
|
|
347
|
+
requestedFeatures,
|
|
348
|
+
renderer
|
|
349
|
+
);
|
|
350
|
+
replacement.userData.impostor = runtime;
|
|
351
|
+
replacement.userData.impostorDebug = debugControls;
|
|
352
|
+
debugControls.profile = replacement.userData.impostorShaderProfile;
|
|
353
|
+
object.material = replacement;
|
|
354
|
+
if (previous !== fallback) previous.dispose();
|
|
355
|
+
return true;
|
|
356
|
+
};
|
|
357
|
+
const material = createPbrImpostorNodeMaterial(descriptor, maps, requestedFeatures, renderer);
|
|
358
|
+
material.userData.impostor = runtime;
|
|
359
|
+
material.userData.impostorDebug = debugControls;
|
|
360
|
+
debugControls.profile = material.userData.impostorShaderProfile;
|
|
361
|
+
object.material = material;
|
|
362
|
+
object.castShadow = descriptor.castShadow;
|
|
363
|
+
object.receiveShadow = descriptor.receiveShadow;
|
|
364
|
+
object.frustumCulled = false;
|
|
365
|
+
fallback.dispose();
|
|
366
|
+
upgraded++;
|
|
367
|
+
});
|
|
368
|
+
return upgraded;
|
|
369
|
+
}
|
|
370
|
+
var NeedleImpostorLoaderPlugin = class {
|
|
371
|
+
/**
|
|
372
|
+
* @param {unknown} parser GLTFLoader parser supplied by `loader.register()`.
|
|
373
|
+
* @param {{
|
|
374
|
+
* onUpgrade?: (count: number, result: import('three/addons/loaders/GLTFLoader.js').GLTF) => void,
|
|
375
|
+
* features?: Partial<ImpostorRuntimeFeatures>,
|
|
376
|
+
* renderer?: ImpostorRenderer,
|
|
377
|
+
* }=} options
|
|
378
|
+
*/
|
|
379
|
+
constructor(parser, options = {}) {
|
|
380
|
+
this.name = NEEDLE_MESH_IMPOSTOR;
|
|
381
|
+
this.parser = parser;
|
|
382
|
+
this.options = options;
|
|
383
|
+
}
|
|
384
|
+
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
|
|
385
|
+
async afterRoot(result) {
|
|
386
|
+
const parser = (
|
|
387
|
+
/** @type {{
|
|
388
|
+
* json: {meshes?: Array<{primitives: Array<{extensions?: Record<string, unknown>}>}>},
|
|
389
|
+
* associations: Map<import('three').Object3D, {meshes?: number, primitives?: number}>,
|
|
390
|
+
* getDependency(type: 'texture', index: number): Promise<import('three').Texture>,
|
|
391
|
+
* }} */
|
|
392
|
+
this.parser
|
|
393
|
+
);
|
|
394
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
395
|
+
const roots = result.scenes?.length ? result.scenes : [result.scene];
|
|
396
|
+
const visited = /* @__PURE__ */ new Set();
|
|
397
|
+
let upgraded = 0;
|
|
398
|
+
for (const root of roots) {
|
|
399
|
+
if (!root || visited.has(root)) continue;
|
|
400
|
+
visited.add(root);
|
|
401
|
+
const pending = [];
|
|
402
|
+
root.traverse((object) => {
|
|
403
|
+
if (!("isMesh" in object) || object.isMesh !== true) return;
|
|
404
|
+
const association = parser.associations.get(object);
|
|
405
|
+
if (association?.meshes === void 0 || association.primitives === void 0) return;
|
|
406
|
+
const raw = parser.json.meshes?.[association.meshes]?.primitives[association.primitives]?.extensions?.[NEEDLE_MESH_IMPOSTOR];
|
|
407
|
+
if (!raw || typeof raw !== "object") return;
|
|
408
|
+
const definition = (
|
|
409
|
+
/** @type {Record<string, unknown>} */
|
|
410
|
+
raw
|
|
411
|
+
);
|
|
412
|
+
const descriptor = parseImpostorDescriptor(definition);
|
|
413
|
+
if (!descriptor) {
|
|
414
|
+
throw new Error("Unsupported NEEDLE_mesh_impostor descriptor; rebake this asset.");
|
|
415
|
+
}
|
|
416
|
+
const rawTextures = definition.textures;
|
|
417
|
+
if (!rawTextures || typeof rawTextures !== "object") {
|
|
418
|
+
throw new Error("NEEDLE_mesh_impostor has no texture bindings.");
|
|
419
|
+
}
|
|
420
|
+
pending.push((async () => {
|
|
421
|
+
const maps = (
|
|
422
|
+
/** @type {Record<string, import('three').Texture>} */
|
|
423
|
+
{}
|
|
424
|
+
);
|
|
425
|
+
for (const role of ["baseColor", "normalDepth", "material", "emissive", "geometry"]) {
|
|
426
|
+
const info = Reflect.get(rawTextures, role);
|
|
427
|
+
const index = info && typeof info === "object" ? Reflect.get(info, "index") : void 0;
|
|
428
|
+
if (!Number.isInteger(index)) continue;
|
|
429
|
+
maps[role] = await parser.getDependency("texture", index);
|
|
430
|
+
}
|
|
431
|
+
if (!maps.baseColor || !maps.normalDepth) {
|
|
432
|
+
throw new Error("NEEDLE_mesh_impostor is missing a required atlas binding.");
|
|
433
|
+
}
|
|
434
|
+
bindings.set(object, {
|
|
435
|
+
descriptor,
|
|
436
|
+
maps: (
|
|
437
|
+
/** @type {any} */
|
|
438
|
+
maps
|
|
439
|
+
)
|
|
440
|
+
});
|
|
441
|
+
})());
|
|
442
|
+
});
|
|
443
|
+
await Promise.all(pending);
|
|
444
|
+
upgraded += upgradeImpostorScene(
|
|
445
|
+
root,
|
|
446
|
+
bindings,
|
|
447
|
+
this.options.features,
|
|
448
|
+
this.options.renderer
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
this.options.onUpgrade?.(upgraded, result);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
function registerNeedleImpostorLoader(loader, options = {}) {
|
|
455
|
+
loader.register((parser) => new NeedleImpostorLoaderPlugin(parser, options));
|
|
456
|
+
return loader;
|
|
457
|
+
}
|
|
458
|
+
function createCompatibleImpostorNodeMaterial(descriptor, maps) {
|
|
459
|
+
const compactLayout = descriptor.compression?.layout === "compact-atlas-v4";
|
|
460
|
+
const compressedGeometry = descriptor.compression?.depthEncoding === "lossless-geometry10-rgba";
|
|
461
|
+
const compactEmissive = descriptor.compression?.emissive === true;
|
|
462
|
+
const boundsCount = descriptor.frames * descriptor.frames;
|
|
463
|
+
const material = new ShaderMaterial({
|
|
464
|
+
name: "Needle PBR impostor WebGL compatibility",
|
|
465
|
+
side: DoubleSide,
|
|
466
|
+
transparent: false,
|
|
467
|
+
depthTest: true,
|
|
468
|
+
depthWrite: true,
|
|
469
|
+
glslVersion: GLSL3,
|
|
470
|
+
uniforms: {
|
|
471
|
+
baseColorAtlas: { value: maps.baseColor },
|
|
472
|
+
geometryAtlas: { value: maps.geometry ?? maps.normalDepth },
|
|
473
|
+
viewBounds: {
|
|
474
|
+
value: Array.from({ length: boundsCount }, (_, index) => new Vector4(
|
|
475
|
+
descriptor.viewBounds[index * 4],
|
|
476
|
+
descriptor.viewBounds[index * 4 + 1],
|
|
477
|
+
descriptor.viewBounds[index * 4 + 2],
|
|
478
|
+
descriptor.viewBounds[index * 4 + 3]
|
|
479
|
+
))
|
|
480
|
+
},
|
|
481
|
+
frames: { value: descriptor.frames },
|
|
482
|
+
frameResolution: { value: descriptor.frameResolution },
|
|
483
|
+
padding: { value: descriptor.padding },
|
|
484
|
+
radius: { value: descriptor.radius },
|
|
485
|
+
captureDirection: { value: new Vector3(...descriptor.captureDirection ?? [0, 1, 0]) }
|
|
486
|
+
},
|
|
487
|
+
defines: {
|
|
488
|
+
VIEW_BOUNDS_COUNT: boundsCount,
|
|
489
|
+
FULL_SPHERE: descriptor.mapping === "full" ? 1 : 0,
|
|
490
|
+
SINGLE_VIEW: descriptor.mapping === "single" ? 1 : 0,
|
|
491
|
+
COMPACT_COLOR: compactLayout ? 1 : 0,
|
|
492
|
+
COMPACT_EMISSIVE: compactEmissive ? 1 : 0,
|
|
493
|
+
COMPRESSED_GEOMETRY: compressedGeometry ? 1 : 0
|
|
494
|
+
},
|
|
495
|
+
vertexShader: (
|
|
496
|
+
/* glsl */
|
|
497
|
+
`
|
|
498
|
+
out vec3 vPlaneLocal;
|
|
499
|
+
void main() {
|
|
500
|
+
mat4 inverseModel = inverse(modelMatrix);
|
|
501
|
+
mat4 cameraWorld = inverse(viewMatrix);
|
|
502
|
+
vec3 rightLocal = normalize((inverseModel * vec4(cameraWorld[0].xyz, 0.0)).xyz);
|
|
503
|
+
vec3 upLocal = normalize((inverseModel * vec4(cameraWorld[1].xyz, 0.0)).xyz);
|
|
504
|
+
vPlaneLocal = rightLocal * position.x + upLocal * position.y;
|
|
505
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(vPlaneLocal, 1.0);
|
|
506
|
+
}
|
|
507
|
+
`
|
|
508
|
+
),
|
|
509
|
+
fragmentShader: (
|
|
510
|
+
/* glsl */
|
|
511
|
+
`
|
|
512
|
+
uniform sampler2D baseColorAtlas;
|
|
513
|
+
uniform sampler2D geometryAtlas;
|
|
514
|
+
uniform vec4 viewBounds[VIEW_BOUNDS_COUNT];
|
|
515
|
+
uniform float frames;
|
|
516
|
+
uniform float frameResolution;
|
|
517
|
+
uniform float padding;
|
|
518
|
+
uniform float radius;
|
|
519
|
+
uniform vec3 captureDirection;
|
|
520
|
+
uniform mat4 modelMatrix;
|
|
521
|
+
uniform mat4 modelViewMatrix;
|
|
522
|
+
uniform mat4 projectionMatrix;
|
|
523
|
+
in vec3 vPlaneLocal;
|
|
524
|
+
out vec4 fragColor;
|
|
525
|
+
|
|
526
|
+
float nonZeroSign(float value) { return value < 0.0 ? -1.0 : 1.0; }
|
|
527
|
+
vec2 directionToGrid(vec3 direction) {
|
|
528
|
+
direction = normalize(direction);
|
|
529
|
+
#if !FULL_SPHERE
|
|
530
|
+
direction.y = max(direction.y, 0.000001);
|
|
531
|
+
#endif
|
|
532
|
+
direction /= abs(direction.x) + abs(direction.y) + abs(direction.z);
|
|
533
|
+
#if FULL_SPHERE
|
|
534
|
+
vec2 folded = vec2(
|
|
535
|
+
nonZeroSign(direction.x) * (1.0 - abs(direction.z)),
|
|
536
|
+
nonZeroSign(direction.z) * (1.0 - abs(direction.x))
|
|
537
|
+
);
|
|
538
|
+
return (direction.y < 0.0 ? folded : direction.xz) * 0.5 + 0.5;
|
|
539
|
+
#else
|
|
540
|
+
return vec2(direction.x + direction.z, direction.z - direction.x) * 0.5 + 0.5;
|
|
541
|
+
#endif
|
|
542
|
+
}
|
|
543
|
+
vec3 gridToDirection(vec2 coordinate) {
|
|
544
|
+
#if FULL_SPHERE
|
|
545
|
+
float x = coordinate.x * 2.0 - 1.0;
|
|
546
|
+
float z = coordinate.y * 2.0 - 1.0;
|
|
547
|
+
float y = 1.0 - abs(x) - abs(z);
|
|
548
|
+
if (y < 0.0) {
|
|
549
|
+
float foldedX = nonZeroSign(x) * (1.0 - abs(z));
|
|
550
|
+
float foldedZ = nonZeroSign(z) * (1.0 - abs(x));
|
|
551
|
+
x = foldedX;
|
|
552
|
+
z = foldedZ;
|
|
553
|
+
}
|
|
554
|
+
return normalize(vec3(x, y, z));
|
|
555
|
+
#else
|
|
556
|
+
return normalize(vec3(
|
|
557
|
+
coordinate.x - coordinate.y,
|
|
558
|
+
1.0 - abs(coordinate.x - coordinate.y) - abs(-1.0 + coordinate.x + coordinate.y),
|
|
559
|
+
-1.0 + coordinate.x + coordinate.y
|
|
560
|
+
));
|
|
561
|
+
#endif
|
|
562
|
+
}
|
|
563
|
+
vec2 atlasUv(vec2 frame, vec2 frameUv) {
|
|
564
|
+
float interiorPixels = max(frameResolution - padding * 2.0, 1.0);
|
|
565
|
+
float interiorScale = interiorPixels / frameResolution;
|
|
566
|
+
float gutterUv = padding / interiorPixels;
|
|
567
|
+
vec2 insideCell = padding / frameResolution
|
|
568
|
+
+ clamp(frameUv, vec2(-gutterUv), vec2(1.0 + gutterUv)) * interiorScale;
|
|
569
|
+
float halfTexel = 0.5001 / frameResolution;
|
|
570
|
+
return (frame + clamp(insideCell, vec2(halfTexel), vec2(1.0 - halfTexel))) / frames;
|
|
571
|
+
}
|
|
572
|
+
void main() {
|
|
573
|
+
vec3 cameraLocal = (inverse(modelMatrix) * vec4(cameraPosition, 1.0)).xyz;
|
|
574
|
+
vec3 cameraDirection = normalize(cameraLocal);
|
|
575
|
+
#if SINGLE_VIEW
|
|
576
|
+
vec2 frame = vec2(0.0);
|
|
577
|
+
vec3 frameDirection = normalize(captureDirection);
|
|
578
|
+
#else
|
|
579
|
+
vec2 frame = clamp(
|
|
580
|
+
floor(directionToGrid(cameraDirection) * (frames - 1.0) + 0.5),
|
|
581
|
+
vec2(0.0),
|
|
582
|
+
vec2(frames - 1.0)
|
|
583
|
+
);
|
|
584
|
+
vec3 frameDirection = gridToDirection(frame / (frames - 1.0));
|
|
585
|
+
#endif
|
|
586
|
+
vec3 referenceUp = abs(frameDirection.y) > 0.999 ? vec3(0.0, 0.0, -1.0) : vec3(0.0, 1.0, 0.0);
|
|
587
|
+
vec3 right = normalize(cross(referenceUp, frameDirection));
|
|
588
|
+
vec3 up = normalize(cross(frameDirection, right));
|
|
589
|
+
int frameIndex = int(frame.y * frames + frame.x);
|
|
590
|
+
vec4 bounds = viewBounds[frameIndex];
|
|
591
|
+
float denominator = dot(cameraDirection, frameDirection);
|
|
592
|
+
float safeDenominator = nonZeroSign(denominator) * max(abs(denominator), 0.05);
|
|
593
|
+
vec3 framePlanePosition = vPlaneLocal - cameraDirection
|
|
594
|
+
* (dot(vPlaneLocal, frameDirection) / safeDenominator);
|
|
595
|
+
vec2 frameUv = (vec2(dot(framePlanePosition, right), dot(framePlanePosition, up))
|
|
596
|
+
- bounds.xy) / (bounds.zw - bounds.xy);
|
|
597
|
+
float interiorPixels = max(frameResolution - padding * 2.0, 1.0);
|
|
598
|
+
float gutterUv = padding / interiorPixels;
|
|
599
|
+
if (any(lessThan(frameUv, vec2(-gutterUv)))
|
|
600
|
+
|| any(greaterThan(frameUv, vec2(1.0 + gutterUv)))) discard;
|
|
601
|
+
vec2 atlas = atlasUv(frame, frameUv);
|
|
602
|
+
vec2 colorUv = atlas;
|
|
603
|
+
#if COMPACT_COLOR
|
|
604
|
+
colorUv.x *= COMPACT_EMISSIVE == 1 ? 0.5 : 1.0;
|
|
605
|
+
#endif
|
|
606
|
+
vec4 base = texture(baseColorAtlas, colorUv);
|
|
607
|
+
vec4 geometry = texture(geometryAtlas, atlas);
|
|
608
|
+
float contour = base.a;
|
|
609
|
+
float depthValue;
|
|
610
|
+
#if COMPRESSED_GEOMETRY
|
|
611
|
+
vec2 packed = floor(geometry.rg * 255.0 + 0.5);
|
|
612
|
+
depthValue = (packed.x * 4.0 + floor(packed.y / 85.0 + 0.5)) / 1023.0;
|
|
613
|
+
contour = geometry.a;
|
|
614
|
+
#else
|
|
615
|
+
vec2 packed = floor(geometry.ba * 255.0 + 0.5);
|
|
616
|
+
depthValue = (packed.x * 256.0 + packed.y) / 65535.0;
|
|
617
|
+
#endif
|
|
618
|
+
float width = max(fwidth(contour), 1.0 / 255.0);
|
|
619
|
+
float coverage = smoothstep(0.5 - width, 0.5 + width, contour);
|
|
620
|
+
if (coverage < 0.25) discard;
|
|
621
|
+
vec2 plane = mix(bounds.xy, bounds.zw, frameUv);
|
|
622
|
+
vec3 reconstructedLocal = right * plane.x + up * plane.y
|
|
623
|
+
+ frameDirection * (radius - depthValue * radius * 2.0);
|
|
624
|
+
vec4 clip = projectionMatrix * modelViewMatrix * vec4(reconstructedLocal, 1.0);
|
|
625
|
+
gl_FragDepth = clip.z / clip.w * 0.5 + 0.5;
|
|
626
|
+
fragColor = vec4(base.rgb, coverage);
|
|
627
|
+
#if defined(TONE_MAPPING)
|
|
628
|
+
fragColor.rgb = toneMapping(fragColor.rgb);
|
|
629
|
+
#endif
|
|
630
|
+
fragColor = linearToOutputTexel(fragColor);
|
|
631
|
+
}
|
|
632
|
+
`
|
|
633
|
+
)
|
|
634
|
+
});
|
|
635
|
+
material.alphaToCoverage = true;
|
|
636
|
+
material.toneMapped = true;
|
|
637
|
+
material.userData.impostorShaderProfile = {
|
|
638
|
+
interpolation: false,
|
|
639
|
+
reprojection: false,
|
|
640
|
+
depthWeights: false,
|
|
641
|
+
perspectiveCorrection: false,
|
|
642
|
+
cellCorrection: false,
|
|
643
|
+
boundedReprojection: false,
|
|
644
|
+
frameSamples: 1,
|
|
645
|
+
raySamples: 0,
|
|
646
|
+
depthSamples: 0,
|
|
647
|
+
blendZone: descriptor.blendZone
|
|
648
|
+
};
|
|
649
|
+
return material;
|
|
650
|
+
}
|
|
651
|
+
function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, renderer) {
|
|
652
|
+
if (!rendererSupportsImpostorNodeMaterials(renderer)) {
|
|
653
|
+
return createCompatibleImpostorNodeMaterial(descriptor, maps);
|
|
654
|
+
}
|
|
655
|
+
const { effective: features, profile } = resolveImpostorRuntimeFeatures(descriptor, inputFeatures);
|
|
656
|
+
const frames = float(descriptor.frames);
|
|
657
|
+
const frameResolution = float(descriptor.frameResolution);
|
|
658
|
+
const padding = float(descriptor.padding);
|
|
659
|
+
const radius = float(descriptor.radius);
|
|
660
|
+
const captureAlphaCutoff = float(descriptor.captureAlphaCutoff);
|
|
661
|
+
const reprojectionSurfaceCutoff = descriptor.coverageEncoding === "sdf-base-alpha" ? float(0.5) : min(captureAlphaCutoff, float(0.25));
|
|
662
|
+
const blendCutoff = float((1 - profile.blendZone) * 0.5);
|
|
663
|
+
const coarseSearchSteps = profile.raySamples;
|
|
664
|
+
const fullSphere = descriptor.mapping === "full";
|
|
665
|
+
const singleView = descriptor.mapping === "single";
|
|
666
|
+
const singleCaptureDirection = vec3(...descriptor.captureDirection ?? [0, 1, 0]).normalize();
|
|
667
|
+
const baseColorAtlas = texture(maps.baseColor);
|
|
668
|
+
const normalDepthAtlas = texture(maps.normalDepth);
|
|
669
|
+
const geometryAtlas = texture(maps.geometry ?? maps.normalDepth);
|
|
670
|
+
const ormAtlas = maps.orm ? texture(maps.orm) : null;
|
|
671
|
+
const emissiveAtlas = maps.emissive ? texture(maps.emissive) : null;
|
|
672
|
+
const compressedGeometry = descriptor.compression?.depthEncoding === "lossless-geometry10-rgba";
|
|
673
|
+
const compactLayout = descriptor.compression?.layout === "compact-atlas-v4";
|
|
674
|
+
const compactEmissive = descriptor.compression?.emissive === true;
|
|
675
|
+
const colorAtlasUv = (uvNode, emissive = false) => compactLayout ? vec2(
|
|
676
|
+
uvNode.x.mul(compactEmissive ? 0.5 : 1).add(emissive && compactEmissive ? 0.5 : 0),
|
|
677
|
+
uvNode.y
|
|
678
|
+
) : 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;
|
|
681
|
+
const viewBounds = uniformArray(
|
|
682
|
+
Array.from({ length: descriptor.frames * descriptor.frames }, (_, index) => new Vector4(
|
|
683
|
+
descriptor.viewBounds[index * 4],
|
|
684
|
+
descriptor.viewBounds[index * 4 + 1],
|
|
685
|
+
descriptor.viewBounds[index * 4 + 2],
|
|
686
|
+
descriptor.viewBounds[index * 4 + 3]
|
|
687
|
+
)),
|
|
688
|
+
"vec4"
|
|
689
|
+
);
|
|
690
|
+
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
|
+
const FrameBasis = NAMED_STRUCT_FACTORY(
|
|
696
|
+
{ right: "vec3", up: "vec3" },
|
|
697
|
+
"ImpostorFrameBasis"
|
|
698
|
+
);
|
|
699
|
+
const FrameSample = NAMED_STRUCT_FACTORY({
|
|
700
|
+
base: "vec4",
|
|
701
|
+
surfaceAlpha: "float",
|
|
702
|
+
normalLocal: "vec3",
|
|
703
|
+
positionLocal: "vec3",
|
|
704
|
+
orm: "vec3",
|
|
705
|
+
emissive: "vec3",
|
|
706
|
+
contour: "float",
|
|
707
|
+
validity: "float",
|
|
708
|
+
strictValidity: "float",
|
|
709
|
+
rayError: "float",
|
|
710
|
+
footprint: "float"
|
|
711
|
+
}, "ImpostorFrameSample");
|
|
712
|
+
const BlendedSample = NAMED_STRUCT_FACTORY({
|
|
713
|
+
base: "vec4",
|
|
714
|
+
normalLocal: "vec3",
|
|
715
|
+
positionLocal: "vec3",
|
|
716
|
+
orm: "vec3",
|
|
717
|
+
emissive: "vec3",
|
|
718
|
+
capturedCoverage: "float",
|
|
719
|
+
distanceCoverage: "float",
|
|
720
|
+
cropSupport: "float",
|
|
721
|
+
coverageWidth: "float",
|
|
722
|
+
transition: "float"
|
|
723
|
+
}, "ImpostorBlendedSample");
|
|
724
|
+
const basisMember = (value, name) => value.get(name);
|
|
725
|
+
const frameMember = (value, name) => value.get(name);
|
|
726
|
+
const blendedMember = (value, name) => value.get(name);
|
|
727
|
+
const nonZeroSign = Fn(([value]) => value.lessThan(0).select(float(-1), float(1)), "float");
|
|
728
|
+
const buildFrameBasis = ([direction]) => {
|
|
729
|
+
const referenceUp = abs(direction.y).greaterThan(0.999).select(vec3(0, 0, -1), vec3(0, 1, 0));
|
|
730
|
+
const right = referenceUp.cross(direction).normalize();
|
|
731
|
+
return FrameBasis(right, direction.cross(right).normalize());
|
|
732
|
+
};
|
|
733
|
+
const frameBasis = Fn(buildFrameBasis);
|
|
734
|
+
const gridToDirection = Fn(([coordinate]) => {
|
|
735
|
+
const hemisphere = vec3(
|
|
736
|
+
coordinate.x.sub(coordinate.y),
|
|
737
|
+
float(1).sub(abs(coordinate.x.sub(coordinate.y))).sub(abs(float(-1).add(coordinate.x).add(coordinate.y))),
|
|
738
|
+
float(-1).add(coordinate.x).add(coordinate.y)
|
|
739
|
+
).normalize();
|
|
740
|
+
const fullX = coordinate.x.mul(2).sub(1).toVar();
|
|
741
|
+
const fullZ = coordinate.y.mul(2).sub(1).toVar();
|
|
742
|
+
const fullY = float(1).sub(abs(fullX)).sub(abs(fullZ)).toVar();
|
|
743
|
+
const foldedX = nonZeroSign(fullX).mul(float(1).sub(abs(fullZ)));
|
|
744
|
+
const foldedZ = nonZeroSign(fullZ).mul(float(1).sub(abs(fullX)));
|
|
745
|
+
const full = fullY.lessThan(0).select(vec3(foldedX, fullY, foldedZ), vec3(fullX, fullY, fullZ)).normalize();
|
|
746
|
+
return fullSphere ? full : hemisphere;
|
|
747
|
+
}, "vec3");
|
|
748
|
+
const directionToGrid = Fn(([inputDirection]) => {
|
|
749
|
+
const direction = inputDirection.normalize().toVar();
|
|
750
|
+
if (!fullSphere) {
|
|
751
|
+
direction.y.assign(max(direction.y, 1e-6));
|
|
752
|
+
}
|
|
753
|
+
direction.divAssign(abs(direction.x).add(abs(direction.y)).add(abs(direction.z)));
|
|
754
|
+
const hemisphere = vec2(direction.x.add(direction.z), direction.z.sub(direction.x));
|
|
755
|
+
const folded = vec2(
|
|
756
|
+
nonZeroSign(direction.x).mul(float(1).sub(abs(direction.z))),
|
|
757
|
+
nonZeroSign(direction.z).mul(float(1).sub(abs(direction.x)))
|
|
758
|
+
);
|
|
759
|
+
const full = direction.y.lessThan(0).select(folded, direction.xz);
|
|
760
|
+
return (fullSphere ? full : hemisphere).mul(0.5).add(0.5).clamp(0, 1);
|
|
761
|
+
}, "vec2");
|
|
762
|
+
const atlasUv = Fn(([frame, frameUv]) => {
|
|
763
|
+
const interiorPixels = max(frameResolution.sub(padding.mul(2)), 1);
|
|
764
|
+
const interiorScale = interiorPixels.div(frameResolution);
|
|
765
|
+
const gutterUv = padding.div(interiorPixels);
|
|
766
|
+
const insideCell = padding.div(frameResolution).add(
|
|
767
|
+
frameUv.clamp(gutterUv.negate(), float(1).add(gutterUv)).mul(interiorScale)
|
|
768
|
+
);
|
|
769
|
+
const halfTexel = float(0.5001).div(frameResolution);
|
|
770
|
+
return frame.add(insideCell.clamp(halfTexel, float(1).sub(halfTexel))).div(frames);
|
|
771
|
+
}, "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
|
+
const decodeDepth16 = Fn(([packed]) => {
|
|
789
|
+
if (descriptor.compression?.depthEncoding === "lossless-geometry10-rgba") {
|
|
790
|
+
const bytes2 = floor(packed.rg.mul(255).add(0.5));
|
|
791
|
+
return bytes2.x.mul(4).add(floor(bytes2.y.div(85).add(0.5))).div(1023);
|
|
792
|
+
}
|
|
793
|
+
const bytes = floor(packed.ba.mul(255).add(0.5));
|
|
794
|
+
return bytes.x.mul(256).add(bytes.y).div(65535);
|
|
795
|
+
}, "float");
|
|
796
|
+
const loadDepth16 = Fn(([texel]) => decodeDepth16(textureLoad(
|
|
797
|
+
geometryAtlas,
|
|
798
|
+
ivec2(texel).clamp(ivec2(0), ivec2(atlasResolution.sub(1)))
|
|
799
|
+
)), "float");
|
|
800
|
+
const sampleDepth16 = Fn(([uvNode]) => {
|
|
801
|
+
const pixel = uvNode.mul(atlasResolution).sub(0.5);
|
|
802
|
+
const base = floor(pixel);
|
|
803
|
+
const fraction = fract(pixel);
|
|
804
|
+
const lower = mix(
|
|
805
|
+
loadDepth16(base),
|
|
806
|
+
loadDepth16(base.add(vec2(1, 0))),
|
|
807
|
+
fraction.x
|
|
808
|
+
);
|
|
809
|
+
const upper = mix(
|
|
810
|
+
loadDepth16(base.add(vec2(0, 1))),
|
|
811
|
+
loadDepth16(base.add(1)),
|
|
812
|
+
fraction.x
|
|
813
|
+
);
|
|
814
|
+
return mix(lower, upper, fraction.y);
|
|
815
|
+
}, "float");
|
|
816
|
+
const decodeOctahedral = Fn(([encoded]) => {
|
|
817
|
+
const f = encoded.mul(2).sub(1).toVar();
|
|
818
|
+
const value = vec3(f, float(1).sub(abs(f.x)).sub(abs(f.y))).toVar();
|
|
819
|
+
const folded = vec2(
|
|
820
|
+
float(1).sub(abs(value.y)).mul(nonZeroSign(value.x)),
|
|
821
|
+
float(1).sub(abs(value.x)).mul(nonZeroSign(value.y))
|
|
822
|
+
);
|
|
823
|
+
If(value.z.lessThan(0), () => {
|
|
824
|
+
value.xy.assign(folded);
|
|
825
|
+
});
|
|
826
|
+
return value.normalize();
|
|
827
|
+
}, "vec3");
|
|
828
|
+
const reconstructPosition = Fn(([frameUv, bounds, frameDirection, right, up, depthValue]) => {
|
|
829
|
+
const plane = mix(bounds.xy, bounds.zw, frameUv);
|
|
830
|
+
return right.mul(plane.x).add(up.mul(plane.y)).add(frameDirection.mul(radius.sub(depthValue.mul(radius).mul(2))));
|
|
831
|
+
}, "vec3");
|
|
832
|
+
const cameraLocal = modelWorldMatrixInverse.mul(vec4(cameraPosition, 1)).xyz;
|
|
833
|
+
const cameraRightLocal = modelWorldMatrixInverse.mul(vec4(cameraWorldMatrix[0].xyz, 0)).xyz.normalize();
|
|
834
|
+
const cameraUpLocal = modelWorldMatrixInverse.mul(vec4(cameraWorldMatrix[1].xyz, 0)).xyz.normalize();
|
|
835
|
+
const cameraOutLocal = modelWorldMatrixInverse.mul(vec4(cameraWorldMatrix[2].xyz, 0)).xyz.normalize();
|
|
836
|
+
const projectionCameraDirection = Fn(([], builder) => builder.camera.isPerspectiveCamera ? cameraLocal.normalize() : cameraOutLocal, "vec3")();
|
|
837
|
+
const horizontalCamera = vec3(
|
|
838
|
+
projectionCameraDirection.x,
|
|
839
|
+
0,
|
|
840
|
+
projectionCameraDirection.z
|
|
841
|
+
);
|
|
842
|
+
const horizontalUp = vec3(cameraUpLocal.x, 0, cameraUpLocal.z);
|
|
843
|
+
const horizonFromPosition = horizontalCamera.div(max(horizontalCamera.length(), 1e-6));
|
|
844
|
+
const horizonFromCameraUp = horizontalUp.div(max(horizontalUp.length(), 1e-6));
|
|
845
|
+
const horizonDirection = horizontalCamera.length().greaterThan(1e-4).select(horizonFromPosition, horizonFromCameraUp);
|
|
846
|
+
const cameraDirection = projectionCameraDirection;
|
|
847
|
+
const mappingVisibility = fullSphere || singleView ? float(1) : smoothstep(-1, -0.9995, cameraDirection.y);
|
|
848
|
+
const horizonTransition = smoothstep(
|
|
849
|
+
0,
|
|
850
|
+
HEMISPHERE_HORIZON_TRANSITION_SINE,
|
|
851
|
+
max(cameraDirection.y, 0)
|
|
852
|
+
);
|
|
853
|
+
const clampedCameraDirection = fullSphere || singleView ? cameraDirection : mix(horizonDirection, cameraDirection, horizonTransition).normalize();
|
|
854
|
+
const horizonBillboardUp = cross(horizonDirection, cameraRightLocal).normalize();
|
|
855
|
+
const billboardUpLocal = fullSphere || singleView ? cameraUpLocal : mix(horizonBillboardUp, cameraUpLocal, horizonTransition).normalize();
|
|
856
|
+
const samplingCamera = clampedCameraDirection.mul(cameraLocal.length());
|
|
857
|
+
const samplingBillboardPosition = cameraRightLocal.mul(positionGeometry.x).add(billboardUpLocal.mul(positionGeometry.y));
|
|
858
|
+
const displayBillboardPosition = cameraRightLocal.mul(positionGeometry.x).add(cameraUpLocal.mul(positionGeometry.y));
|
|
859
|
+
const planeLocal = samplingBillboardPosition.toVarying("vImpostorPlaneLocal");
|
|
860
|
+
const createFrameSampler = (withReprojection) => {
|
|
861
|
+
const buildFrameSample = ([frame, planePosition, rayDirection]) => {
|
|
862
|
+
const frameDirection = singleView ? singleCaptureDirection : gridToDirection(frame.div(frames.sub(1)));
|
|
863
|
+
const basis = frameBasis(frameDirection);
|
|
864
|
+
const right = basisMember(basis, "right");
|
|
865
|
+
const up = basisMember(basis, "up");
|
|
866
|
+
const bounds = viewBounds.element(int(frame.y.mul(frames).add(frame.x)));
|
|
867
|
+
const rayPlaneDenominator = dot(rayDirection, frameDirection);
|
|
868
|
+
const safeRayPlaneDenominator = nonZeroSign(rayPlaneDenominator).mul(max(abs(rayPlaneDenominator), 0.05));
|
|
869
|
+
const framePlanePosition = planePosition.sub(rayDirection.mul(
|
|
870
|
+
dot(planePosition, frameDirection).div(safeRayPlaneDenominator)
|
|
871
|
+
));
|
|
872
|
+
const projectedFrameUv = vec2(
|
|
873
|
+
dot(framePlanePosition, right),
|
|
874
|
+
dot(framePlanePosition, up)
|
|
875
|
+
).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
|
|
876
|
+
const interiorResolution = max(frameResolution.sub(padding.mul(2)), 1);
|
|
877
|
+
const gutterUv = padding.div(interiorResolution);
|
|
878
|
+
const minimumFrameUv = gutterUv.negate();
|
|
879
|
+
const maximumFrameUv = float(1).add(gutterUv);
|
|
880
|
+
const frameUv = projectedFrameUv.clamp(minimumFrameUv, maximumFrameUv).toVar();
|
|
881
|
+
const reconstructed = planePosition.toVar();
|
|
882
|
+
const rootSolved = bool(!withReprojection).toVar();
|
|
883
|
+
const strictRootSolved = bool(!withReprojection).toVar();
|
|
884
|
+
if (withReprojection) {
|
|
885
|
+
const rootFound = bool(false).toVar();
|
|
886
|
+
const rootLower = float(0).toVar();
|
|
887
|
+
const rootUpper = float(1).toVar();
|
|
888
|
+
const previousHasSurface = bool(false).toVar();
|
|
889
|
+
const previousSearchDepth = float(0).toVar();
|
|
890
|
+
const previousSurfaceDepth = float(1).toVar();
|
|
891
|
+
const coarseStep = float(1).div(coarseSearchSteps);
|
|
892
|
+
Loop(coarseSearchSteps, ({ i }) => {
|
|
893
|
+
const searchDepth = float(i).add(1).mul(coarseStep);
|
|
894
|
+
const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
|
|
895
|
+
const searchRayPosition = planePosition.add(rayDirection.mul(
|
|
896
|
+
searchPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
|
|
897
|
+
));
|
|
898
|
+
const searchUv = vec2(
|
|
899
|
+
dot(searchRayPosition, right),
|
|
900
|
+
dot(searchRayPosition, up)
|
|
901
|
+
).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
|
|
902
|
+
const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv))).or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
|
|
903
|
+
If(searchOutOfBounds, () => {
|
|
904
|
+
previousHasSurface.assign(false);
|
|
905
|
+
});
|
|
906
|
+
If(searchOutOfBounds.not(), () => {
|
|
907
|
+
const searchAtlasUv = atlasUv(frame, searchUv);
|
|
908
|
+
const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
|
|
909
|
+
const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
|
|
910
|
+
const hasSurface = searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff);
|
|
911
|
+
const depthDelta = searchDepth.sub(searchSurfaceDepth);
|
|
912
|
+
const crossesDepth = previousHasSurface.and(previousSearchDepth.lessThan(previousSurfaceDepth)).and(depthDelta.greaterThanEqual(0));
|
|
913
|
+
const continuousDepthStep = abs(searchSurfaceDepth.sub(previousSurfaceDepth)).lessThanEqual(coarseStep.mul(2));
|
|
914
|
+
const crossesCoveredSurface = crossesDepth.and(continuousDepthStep);
|
|
915
|
+
const entersAtSurface = previousHasSurface.not().and(depthDelta.greaterThanEqual(0)).and(depthDelta.lessThanEqual(coarseStep.mul(1.15)));
|
|
916
|
+
const boundedRoot = crossesCoveredSurface.or(entersAtSurface);
|
|
917
|
+
If(
|
|
918
|
+
rootFound.not().and(hasSurface).and(features.boundedReprojection ? boundedRoot : depthDelta.greaterThanEqual(0)),
|
|
919
|
+
() => {
|
|
920
|
+
rootLower.assign(max(searchDepth.sub(coarseStep), 0));
|
|
921
|
+
rootUpper.assign(searchDepth);
|
|
922
|
+
rootFound.assign(true);
|
|
923
|
+
rootSolved.assign(true);
|
|
924
|
+
strictRootSolved.assign(true);
|
|
925
|
+
}
|
|
926
|
+
);
|
|
927
|
+
If(rootFound.not().and(hasSurface), () => {
|
|
928
|
+
previousHasSurface.assign(true);
|
|
929
|
+
previousSearchDepth.assign(searchDepth);
|
|
930
|
+
previousSurfaceDepth.assign(searchSurfaceDepth);
|
|
931
|
+
});
|
|
932
|
+
If(hasSurface.not(), () => {
|
|
933
|
+
previousHasSurface.assign(false);
|
|
934
|
+
});
|
|
935
|
+
});
|
|
936
|
+
});
|
|
937
|
+
If(rootFound.not(), () => {
|
|
938
|
+
const iterativeUv = frameUv.toVar();
|
|
939
|
+
const iterativeDepth = sampleDepth16(atlasUv(frame, iterativeUv)).toVar();
|
|
940
|
+
const iterationResidual = float(1).toVar();
|
|
941
|
+
const iterationValid = bool(true).toVar();
|
|
942
|
+
Loop(profile.depthSamples * 2, () => {
|
|
943
|
+
If(iterationValid, () => {
|
|
944
|
+
const iterativePlaneDepth = radius.sub(iterativeDepth.mul(radius).mul(2));
|
|
945
|
+
const iterativeRayPosition = planePosition.add(rayDirection.mul(
|
|
946
|
+
iterativePlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
|
|
947
|
+
));
|
|
948
|
+
const nextUv = vec2(
|
|
949
|
+
dot(iterativeRayPosition, right),
|
|
950
|
+
dot(iterativeRayPosition, up)
|
|
951
|
+
).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
|
|
952
|
+
const nextOutOfBounds = any(nextUv.lessThan(vec2(minimumFrameUv))).or(any(nextUv.greaterThan(vec2(maximumFrameUv))));
|
|
953
|
+
If(nextOutOfBounds, () => {
|
|
954
|
+
iterationValid.assign(false);
|
|
955
|
+
});
|
|
956
|
+
If(nextOutOfBounds.not(), () => {
|
|
957
|
+
const nextAtlasUv = atlasUv(frame, nextUv);
|
|
958
|
+
const nextCoverage = sampleCapturedAlpha(nextAtlasUv);
|
|
959
|
+
const nextDepth = sampleDepth16(nextAtlasUv);
|
|
960
|
+
iterationResidual.assign(abs(nextDepth.sub(iterativeDepth)));
|
|
961
|
+
iterativeUv.assign(nextUv);
|
|
962
|
+
iterativeDepth.assign(nextDepth);
|
|
963
|
+
If(nextCoverage.lessThan(reprojectionSurfaceCutoff), () => {
|
|
964
|
+
iterationValid.assign(false);
|
|
965
|
+
});
|
|
966
|
+
});
|
|
967
|
+
});
|
|
968
|
+
});
|
|
969
|
+
If(
|
|
970
|
+
iterationValid.and(iterationResidual.lessThanEqual(coarseStep)),
|
|
971
|
+
() => {
|
|
972
|
+
frameUv.assign(iterativeUv);
|
|
973
|
+
rootSolved.assign(true);
|
|
974
|
+
}
|
|
975
|
+
);
|
|
976
|
+
});
|
|
977
|
+
If(rootFound, () => {
|
|
978
|
+
Loop(profile.depthSamples, () => {
|
|
979
|
+
const searchDepth = rootLower.add(rootUpper).mul(0.5);
|
|
980
|
+
const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
|
|
981
|
+
const searchRayPosition = planePosition.add(rayDirection.mul(
|
|
982
|
+
searchPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
|
|
983
|
+
));
|
|
984
|
+
const searchUv = vec2(
|
|
985
|
+
dot(searchRayPosition, right),
|
|
986
|
+
dot(searchRayPosition, up)
|
|
987
|
+
).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
|
|
988
|
+
const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv))).or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
|
|
989
|
+
const midpointIsUpper = bool(false).toVar();
|
|
990
|
+
If(searchOutOfBounds.not(), () => {
|
|
991
|
+
const searchAtlasUv = atlasUv(frame, searchUv);
|
|
992
|
+
const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
|
|
993
|
+
const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
|
|
994
|
+
midpointIsUpper.assign(
|
|
995
|
+
searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff).and(searchDepth.greaterThanEqual(searchSurfaceDepth))
|
|
996
|
+
);
|
|
997
|
+
});
|
|
998
|
+
If(midpointIsUpper, () => {
|
|
999
|
+
rootUpper.assign(searchDepth);
|
|
1000
|
+
});
|
|
1001
|
+
If(midpointIsUpper.not(), () => {
|
|
1002
|
+
rootLower.assign(searchDepth);
|
|
1003
|
+
});
|
|
1004
|
+
});
|
|
1005
|
+
const rootDepth = rootLower.add(rootUpper).mul(0.5);
|
|
1006
|
+
const rootPlaneDepth = radius.sub(rootDepth.mul(radius).mul(2));
|
|
1007
|
+
const rootRayPosition = planePosition.add(rayDirection.mul(
|
|
1008
|
+
rootPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
|
|
1009
|
+
));
|
|
1010
|
+
frameUv.assign(vec2(
|
|
1011
|
+
dot(rootRayPosition, right),
|
|
1012
|
+
dot(rootRayPosition, up)
|
|
1013
|
+
).sub(bounds.xy).div(bounds.zw.sub(bounds.xy)).clamp(
|
|
1014
|
+
minimumFrameUv,
|
|
1015
|
+
maximumFrameUv
|
|
1016
|
+
));
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
const uvNode = atlasUv(frame, frameUv);
|
|
1020
|
+
const lod = atlasLod(uvNode);
|
|
1021
|
+
const materialLod = compactLayout ? materialAtlasLod(uvNode) : lod;
|
|
1022
|
+
reconstructed.assign(reconstructPosition(
|
|
1023
|
+
frameUv,
|
|
1024
|
+
bounds,
|
|
1025
|
+
frameDirection,
|
|
1026
|
+
right,
|
|
1027
|
+
up,
|
|
1028
|
+
sampleDepth16(uvNode)
|
|
1029
|
+
));
|
|
1030
|
+
const shadingSample = texture(normalDepthAtlas, uvNode).level(materialLod);
|
|
1031
|
+
const geometrySample = compressedGeometry ? texture(geometryAtlas, uvNode).level(0) : null;
|
|
1032
|
+
const viewNormal = decodeOctahedral(shadingSample.rg);
|
|
1033
|
+
const normal = right.mul(viewNormal.x).add(up.mul(viewNormal.y)).add(frameDirection.mul(viewNormal.z)).normalize();
|
|
1034
|
+
const faceForwardNormal = dot(normal, rayDirection).lessThan(0).select(normal.negate(), normal);
|
|
1035
|
+
const closestRayPoint = planePosition.add(
|
|
1036
|
+
rayDirection.mul(dot(reconstructed.sub(planePosition), rayDirection))
|
|
1037
|
+
);
|
|
1038
|
+
const footprint = max(
|
|
1039
|
+
bounds.z.sub(bounds.x),
|
|
1040
|
+
bounds.w.sub(bounds.y)
|
|
1041
|
+
).div(interiorResolution);
|
|
1042
|
+
const rayError = reconstructed.sub(closestRayPoint).length();
|
|
1043
|
+
const finalFrameEdgeDistance = min(
|
|
1044
|
+
min(frameUv.x.sub(minimumFrameUv), frameUv.y.sub(minimumFrameUv)),
|
|
1045
|
+
min(maximumFrameUv.sub(frameUv.x), maximumFrameUv.sub(frameUv.y))
|
|
1046
|
+
);
|
|
1047
|
+
const boundsConfidence = smoothstep(
|
|
1048
|
+
0,
|
|
1049
|
+
float(1).div(interiorResolution),
|
|
1050
|
+
finalFrameEdgeDistance
|
|
1051
|
+
);
|
|
1052
|
+
const alignedConfidence = float(1).sub(smoothstep(
|
|
1053
|
+
REPROJECTION_CONFIDENCE_INNER_TEXELS,
|
|
1054
|
+
REPROJECTION_CONFIDENCE_OUTER_TEXELS,
|
|
1055
|
+
rayError.div(max(footprint, 1e-6))
|
|
1056
|
+
));
|
|
1057
|
+
const validFloat = boundsConfidence.mul(
|
|
1058
|
+
withReprojection ? alignedConfidence.mul(rootSolved) : float(1)
|
|
1059
|
+
);
|
|
1060
|
+
const strictValidFloat = boundsConfidence.mul(
|
|
1061
|
+
withReprojection ? alignedConfidence.mul(strictRootSolved) : float(1)
|
|
1062
|
+
);
|
|
1063
|
+
const baseColor = texture(baseColorAtlas, colorAtlasUv(uvNode)).level(materialLod);
|
|
1064
|
+
const capturedAlpha = sampleCapturedAlpha(uvNode);
|
|
1065
|
+
const orm = compactLayout ? vec4(geometrySample.b, shadingSample.b, shadingSample.a, 1) : ormAtlas ? texture(ormAtlas, uvNode).level(lod) : vec4(
|
|
1066
|
+
descriptor.material.occlusion,
|
|
1067
|
+
descriptor.material.roughness,
|
|
1068
|
+
descriptor.material.metalness,
|
|
1069
|
+
1
|
|
1070
|
+
);
|
|
1071
|
+
const coverage = sampleContour(uvNode);
|
|
1072
|
+
const resolvedValidity = validFloat;
|
|
1073
|
+
return FrameSample(
|
|
1074
|
+
// Validity is a blend weight, not an attribute. Premultiplying both the
|
|
1075
|
+
// sampled color/alpha here and the frame weight below applied crop
|
|
1076
|
+
// confidence twice; low-confidence crop-edge samples consequently
|
|
1077
|
+
// rendered as black outlines instead of fading away.
|
|
1078
|
+
vec4(baseColor.rgb, coverage),
|
|
1079
|
+
capturedAlpha,
|
|
1080
|
+
faceForwardNormal,
|
|
1081
|
+
reconstructed,
|
|
1082
|
+
orm.rgb,
|
|
1083
|
+
emissiveAtlas ? texture(emissiveAtlas, colorAtlasUv(uvNode, true)).level(materialLod).rgb : vec3(0),
|
|
1084
|
+
coverage,
|
|
1085
|
+
resolvedValidity,
|
|
1086
|
+
strictValidFloat,
|
|
1087
|
+
rayError,
|
|
1088
|
+
footprint
|
|
1089
|
+
);
|
|
1090
|
+
};
|
|
1091
|
+
return Fn(buildFrameSample);
|
|
1092
|
+
};
|
|
1093
|
+
const samplePlanarFrame = createFrameSampler(false);
|
|
1094
|
+
const sampleReprojectedFrame = features.reprojection ? createFrameSampler(true) : null;
|
|
1095
|
+
const buildBlendedSample = ([], builder) => {
|
|
1096
|
+
const samplingCameraLocal = samplingCamera;
|
|
1097
|
+
const centerViewDirection = samplingCameraLocal.normalize();
|
|
1098
|
+
const usePerspectiveCorrection = features.perspectiveCorrection && builder.camera.isPerspectiveCamera;
|
|
1099
|
+
const rayDirection = usePerspectiveCorrection ? samplingCameraLocal.sub(planeLocal).normalize() : centerViewDirection;
|
|
1100
|
+
if (singleView) {
|
|
1101
|
+
const singleFrame = vec2(0, 0);
|
|
1102
|
+
const raw = features.reprojection ? sampleReprojectedFrame(singleFrame, planeLocal, rayDirection) : samplePlanarFrame(singleFrame, planeLocal, rayDirection);
|
|
1103
|
+
const validity = frameMember(raw, "validity");
|
|
1104
|
+
const capturedCoverage = frameMember(raw, "surfaceAlpha").mul(validity);
|
|
1105
|
+
const distanceCoverage2 = min(
|
|
1106
|
+
frameMember(raw, "contour"),
|
|
1107
|
+
capturedCoverage
|
|
1108
|
+
);
|
|
1109
|
+
return BlendedSample(
|
|
1110
|
+
frameMember(raw, "base"),
|
|
1111
|
+
frameMember(raw, "normalLocal"),
|
|
1112
|
+
frameMember(raw, "positionLocal"),
|
|
1113
|
+
frameMember(raw, "orm"),
|
|
1114
|
+
frameMember(raw, "emissive"),
|
|
1115
|
+
capturedCoverage,
|
|
1116
|
+
distanceCoverage2,
|
|
1117
|
+
validity,
|
|
1118
|
+
float(0),
|
|
1119
|
+
float(0)
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
const centerGrid = directionToGrid(centerViewDirection).mul(frames.sub(1));
|
|
1123
|
+
const perspectiveGrid = usePerspectiveCorrection ? directionToGrid(
|
|
1124
|
+
samplingCameraLocal.add(planeLocal.mul(PERSPECTIVE_RAY_PLANE_SIGN)).normalize()
|
|
1125
|
+
).mul(frames.sub(1)) : centerGrid;
|
|
1126
|
+
const maximumBase = vec2(frames.sub(2));
|
|
1127
|
+
const centerBase = min(floor(centerGrid), maximumBase);
|
|
1128
|
+
const weightGrid = features.cellCorrection ? perspectiveGrid : centerGrid;
|
|
1129
|
+
const base = centerBase;
|
|
1130
|
+
const centerFraction = centerGrid.sub(base);
|
|
1131
|
+
const fraction = weightGrid.sub(base);
|
|
1132
|
+
const horizontal = centerFraction.x.greaterThan(centerFraction.y);
|
|
1133
|
+
const ordinaryFrame1 = base.add(horizontal.select(vec2(1, 0), vec2(0, 1)));
|
|
1134
|
+
const ordinaryWeights = horizontal.select(
|
|
1135
|
+
vec3(float(1).sub(fraction.x), fraction.x.sub(fraction.y), fraction.y),
|
|
1136
|
+
vec3(float(1).sub(fraction.y), fraction.y.sub(fraction.x), fraction.x)
|
|
1137
|
+
);
|
|
1138
|
+
const foldCell = fullSphere ? bool(false) : base.x.add(base.y).equal(frames.sub(2));
|
|
1139
|
+
const baseDirection = gridToDirection(base.div(frames.sub(1)));
|
|
1140
|
+
const rightDirection = gridToDirection(base.add(vec2(1, 0)).div(frames.sub(1)));
|
|
1141
|
+
const upDirection = gridToDirection(base.add(vec2(0, 1)).div(frames.sub(1)));
|
|
1142
|
+
const diagonalDirection = gridToDirection(base.add(1).div(frames.sub(1)));
|
|
1143
|
+
const antiDiagonal = foldCell.or(
|
|
1144
|
+
dot(rightDirection, upDirection).greaterThan(dot(baseDirection, diagonalDirection))
|
|
1145
|
+
);
|
|
1146
|
+
const antiLower = centerFraction.x.add(centerFraction.y).lessThanEqual(1);
|
|
1147
|
+
const antiFrame0 = antiLower.select(base, base.add(1));
|
|
1148
|
+
const antiFrame1 = antiLower.select(base.add(vec2(1, 0)), base.add(vec2(0, 1)));
|
|
1149
|
+
const antiFrame2 = antiLower.select(base.add(vec2(0, 1)), base.add(vec2(1, 0)));
|
|
1150
|
+
const antiWeights = antiLower.select(
|
|
1151
|
+
vec3(float(1).sub(fraction.x).sub(fraction.y), fraction.x, fraction.y),
|
|
1152
|
+
vec3(fraction.x.add(fraction.y).sub(1), float(1).sub(fraction.x), float(1).sub(fraction.y))
|
|
1153
|
+
);
|
|
1154
|
+
const frame0 = antiDiagonal.select(antiFrame0, base);
|
|
1155
|
+
const frame1 = antiDiagonal.select(antiFrame1, ordinaryFrame1);
|
|
1156
|
+
const frame2 = antiDiagonal.select(antiFrame2, base.add(1));
|
|
1157
|
+
const weights = max(antiDiagonal.select(antiWeights, ordinaryWeights), vec3(0)).toVar();
|
|
1158
|
+
weights.divAssign(max(dot(weights, vec3(1)), 1e-4));
|
|
1159
|
+
weights.assign(max(weights.sub(blendCutoff), vec3(0)));
|
|
1160
|
+
weights.divAssign(max(dot(weights, vec3(1)), 1e-4));
|
|
1161
|
+
const frame0Nearest = weights.x.greaterThanEqual(weights.y).and(weights.x.greaterThanEqual(weights.z));
|
|
1162
|
+
const nearestFrame = frame0Nearest.select(
|
|
1163
|
+
frame0,
|
|
1164
|
+
weights.y.greaterThanEqual(weights.z).select(frame1, frame2)
|
|
1165
|
+
).toVar("impostorNearestFrame");
|
|
1166
|
+
if (!features.interpolation) {
|
|
1167
|
+
const raw = samplePlanarFrame(nearestFrame, planeLocal, rayDirection);
|
|
1168
|
+
return BlendedSample(
|
|
1169
|
+
frameMember(raw, "base"),
|
|
1170
|
+
frameMember(raw, "normalLocal"),
|
|
1171
|
+
frameMember(raw, "positionLocal"),
|
|
1172
|
+
frameMember(raw, "orm"),
|
|
1173
|
+
frameMember(raw, "emissive"),
|
|
1174
|
+
frameMember(raw, "surfaceAlpha"),
|
|
1175
|
+
frameMember(raw, "surfaceAlpha"),
|
|
1176
|
+
frameMember(raw, "validity"),
|
|
1177
|
+
float(0),
|
|
1178
|
+
float(0)
|
|
1179
|
+
);
|
|
1180
|
+
}
|
|
1181
|
+
const a = features.reprojection ? sampleReprojectedFrame(frame0, planeLocal, rayDirection) : samplePlanarFrame(frame0, planeLocal, rayDirection);
|
|
1182
|
+
const b = features.reprojection ? sampleReprojectedFrame(frame1, planeLocal, rayDirection) : samplePlanarFrame(frame1, planeLocal, rayDirection);
|
|
1183
|
+
const c = features.reprojection ? sampleReprojectedFrame(frame2, planeLocal, rayDirection) : samplePlanarFrame(frame2, planeLocal, rayDirection);
|
|
1184
|
+
const angularWeights = weights.toVar("impostorAngularWeights");
|
|
1185
|
+
if (features.depthWeights) {
|
|
1186
|
+
const residual = vec3(
|
|
1187
|
+
frameMember(a, "rayError").div(frameMember(a, "footprint")),
|
|
1188
|
+
frameMember(b, "rayError").div(frameMember(b, "footprint")),
|
|
1189
|
+
frameMember(c, "rayError").div(frameMember(c, "footprint"))
|
|
1190
|
+
).div(0.5);
|
|
1191
|
+
weights.mulAssign(max(exp(residual.mul(residual).mul(-0.5)), vec3(0.05)));
|
|
1192
|
+
}
|
|
1193
|
+
weights.divAssign(max(dot(weights, vec3(1)), 1e-4));
|
|
1194
|
+
const visibilityWeights = weights.toVar("impostorVisibilityWeights");
|
|
1195
|
+
const resolvedSampleValidity = vec3(
|
|
1196
|
+
frameMember(a, "validity"),
|
|
1197
|
+
frameMember(b, "validity"),
|
|
1198
|
+
frameMember(c, "validity")
|
|
1199
|
+
);
|
|
1200
|
+
const strictSampleValidity = vec3(
|
|
1201
|
+
frameMember(a, "strictValidity"),
|
|
1202
|
+
frameMember(b, "strictValidity"),
|
|
1203
|
+
frameMember(c, "strictValidity")
|
|
1204
|
+
);
|
|
1205
|
+
const strictSupport = dot(angularWeights, strictSampleValidity);
|
|
1206
|
+
const seedValidity = max(resolvedSampleValidity.sub(strictSampleValidity), vec3(0));
|
|
1207
|
+
const seedWeight = float(1).sub(smoothstep(0, 0.01, strictSupport));
|
|
1208
|
+
const sampleValidity = strictSampleValidity.add(seedValidity.mul(seedWeight));
|
|
1209
|
+
const surfaceAlpha = vec3(
|
|
1210
|
+
frameMember(a, "surfaceAlpha"),
|
|
1211
|
+
frameMember(b, "surfaceAlpha"),
|
|
1212
|
+
frameMember(c, "surfaceAlpha")
|
|
1213
|
+
);
|
|
1214
|
+
const largestAngularWeight = max(
|
|
1215
|
+
angularWeights.x,
|
|
1216
|
+
max(angularWeights.y, angularWeights.z)
|
|
1217
|
+
);
|
|
1218
|
+
const transition = smoothstep(
|
|
1219
|
+
0,
|
|
1220
|
+
0.5,
|
|
1221
|
+
float(1).sub(largestAngularWeight)
|
|
1222
|
+
).toVar("impostorCoverageTransition");
|
|
1223
|
+
weights.mulAssign(surfaceAlpha).mulAssign(sampleValidity);
|
|
1224
|
+
weights.divAssign(max(dot(weights, vec3(1)), 1e-4));
|
|
1225
|
+
const weighted3 = (field) => frameMember(a, field).mul(weights.x).add(frameMember(b, field).mul(weights.y)).add(frameMember(c, field).mul(weights.z));
|
|
1226
|
+
const weighted4 = (field) => frameMember(a, field).mul(weights.x).add(frameMember(b, field).mul(weights.y)).add(frameMember(c, field).mul(weights.z));
|
|
1227
|
+
const contourSharpness = impostorContourUnionSharpness(descriptor.frames);
|
|
1228
|
+
const effectiveContourSharpness = mix(
|
|
1229
|
+
float(0.01),
|
|
1230
|
+
float(contourSharpness),
|
|
1231
|
+
transition
|
|
1232
|
+
);
|
|
1233
|
+
const coverageContributions = visibilityWeights.mul(sampleValidity).toVar("impostorCoverageContributions");
|
|
1234
|
+
const coverageSupport = dot(coverageContributions, vec3(1)).toVar("impostorCoverageSupport");
|
|
1235
|
+
const coverageWeights = coverageContributions.div(max(coverageSupport, 1e-4)).toVar("impostorCoverageWeights");
|
|
1236
|
+
const perspectiveCapturedCoverage = dot(
|
|
1237
|
+
coverageContributions,
|
|
1238
|
+
surfaceAlpha
|
|
1239
|
+
);
|
|
1240
|
+
const coverageDistanceSamples = vec3(
|
|
1241
|
+
frameMember(a, "contour"),
|
|
1242
|
+
frameMember(b, "contour"),
|
|
1243
|
+
frameMember(c, "contour")
|
|
1244
|
+
);
|
|
1245
|
+
const unionDistanceCoverage = max(
|
|
1246
|
+
dot(coverageWeights, exp(coverageDistanceSamples.mul(effectiveContourSharpness))),
|
|
1247
|
+
1e-4
|
|
1248
|
+
).log().div(effectiveContourSharpness);
|
|
1249
|
+
const perspectiveDistanceCoverage = min(
|
|
1250
|
+
unionDistanceCoverage,
|
|
1251
|
+
perspectiveCapturedCoverage
|
|
1252
|
+
);
|
|
1253
|
+
const coverageDistanceMean = dot(coverageWeights, coverageDistanceSamples);
|
|
1254
|
+
const coverageDistanceDeviation = dot(
|
|
1255
|
+
coverageWeights,
|
|
1256
|
+
abs(coverageDistanceSamples.sub(coverageDistanceMean))
|
|
1257
|
+
);
|
|
1258
|
+
return BlendedSample(
|
|
1259
|
+
weighted4("base"),
|
|
1260
|
+
weighted3("normalLocal").normalize(),
|
|
1261
|
+
weighted3("positionLocal"),
|
|
1262
|
+
weighted3("orm"),
|
|
1263
|
+
weighted3("emissive"),
|
|
1264
|
+
perspectiveCapturedCoverage,
|
|
1265
|
+
perspectiveDistanceCoverage,
|
|
1266
|
+
coverageSupport,
|
|
1267
|
+
// View-domain antialiasing complements screen-space fwidth. It widens
|
|
1268
|
+
// only where the contributing captures disagree about occupancy, so a
|
|
1269
|
+
// moving contour crosses pixels over an angular interval rather than in
|
|
1270
|
+
// one frame while stable interiors remain sharp.
|
|
1271
|
+
coverageDistanceDeviation.mul(0.2).mul(transition),
|
|
1272
|
+
transition
|
|
1273
|
+
);
|
|
1274
|
+
};
|
|
1275
|
+
const blendImpostor = Fn(buildBlendedSample);
|
|
1276
|
+
const sample = blendImpostor().toVar("impostorSample");
|
|
1277
|
+
const reconstructedWorldPosition = modelWorldMatrix.mul(vec4(blendedMember(sample, "positionLocal"), 1)).xyz;
|
|
1278
|
+
const reconstructedDepth = Fn(([localPosition], builder) => {
|
|
1279
|
+
const viewZ = modelViewMatrix.mul(vec4(localPosition, 1)).z;
|
|
1280
|
+
const resolvedDepth = builder.camera.isPerspectiveCamera ? viewZToPerspectiveDepth(viewZ, cameraNear, cameraFar) : viewZToOrthographicDepth(viewZ, cameraNear, cameraFar);
|
|
1281
|
+
return builder.material.isShadowPassMaterial ? min(1, resolvedDepth.add(IMPOSTOR_SHADOW_CASTER_DEPTH_BIAS)) : resolvedDepth;
|
|
1282
|
+
}, "float");
|
|
1283
|
+
const material = new MeshStandardNodeMaterial({
|
|
1284
|
+
name: "Needle PBR impostor TSL",
|
|
1285
|
+
side: DoubleSide,
|
|
1286
|
+
transparent: false,
|
|
1287
|
+
depthTest: true,
|
|
1288
|
+
depthWrite: true
|
|
1289
|
+
});
|
|
1290
|
+
const distanceCoverage = blendedMember(sample, "distanceCoverage");
|
|
1291
|
+
const coverageWidth = max(
|
|
1292
|
+
max(fwidth(distanceCoverage), blendedMember(sample, "coverageWidth")),
|
|
1293
|
+
1 / 255
|
|
1294
|
+
);
|
|
1295
|
+
const screenDistanceCoverage = smoothstep(
|
|
1296
|
+
float(0.5).sub(coverageWidth),
|
|
1297
|
+
float(0.5).add(coverageWidth),
|
|
1298
|
+
distanceCoverage
|
|
1299
|
+
);
|
|
1300
|
+
const screenCoverage = screenDistanceCoverage.mul(mappingVisibility);
|
|
1301
|
+
material.positionNode = displayBillboardPosition;
|
|
1302
|
+
material.castShadowPositionNode = displayBillboardPosition;
|
|
1303
|
+
material.colorNode = vec4(blendedMember(sample, "base").rgb, screenCoverage);
|
|
1304
|
+
material.normalNode = transformNormalToView(blendedMember(sample, "normalLocal"));
|
|
1305
|
+
material.aoNode = blendedMember(sample, "orm").r;
|
|
1306
|
+
material.roughnessNode = blendedMember(sample, "orm").g;
|
|
1307
|
+
material.metalnessNode = blendedMember(sample, "orm").b;
|
|
1308
|
+
material.emissiveNode = blendedMember(sample, "emissive");
|
|
1309
|
+
const cropSupported = blendedMember(sample, "cropSupport").greaterThanEqual(0.25);
|
|
1310
|
+
material.maskNode = screenCoverage.greaterThanEqual(0.25).and(
|
|
1311
|
+
screenCoverage.greaterThanEqual(0.5).or(cropSupported)
|
|
1312
|
+
);
|
|
1313
|
+
material.maskShadowNode = distanceCoverage.mul(mappingVisibility).greaterThanEqual(0.5);
|
|
1314
|
+
material.alphaToCoverage = true;
|
|
1315
|
+
material.depthNode = reconstructedDepth(blendedMember(sample, "positionLocal"));
|
|
1316
|
+
material.receivedShadowPositionNode = reconstructedWorldPosition;
|
|
1317
|
+
material.toneMapped = true;
|
|
1318
|
+
material.userData.impostorShaderProfile = profile;
|
|
1319
|
+
return material;
|
|
1320
|
+
}
|
|
1321
|
+
export {
|
|
1322
|
+
DEFAULT_IMPOSTOR_RUNTIME_FEATURES,
|
|
1323
|
+
IMPOSTOR_RUNTIME_SAMPLE_LIMITS,
|
|
1324
|
+
NeedleImpostorLoaderPlugin,
|
|
1325
|
+
createPbrImpostorNodeMaterial,
|
|
1326
|
+
registerNeedleImpostorLoader,
|
|
1327
|
+
rendererSupportsImpostorNodeMaterials,
|
|
1328
|
+
resolveImpostorRuntimeFeatures,
|
|
1329
|
+
upgradeImpostorScene
|
|
1330
|
+
};
|
|
1331
|
+
//# sourceMappingURL=impostor-node-material.js.map
|