@onerjs/core 8.51.4 → 8.51.5
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/Animations/animatorAvatar.d.ts +2 -0
- package/Animations/animatorAvatar.js +163 -94
- package/Animations/animatorAvatar.js.map +1 -1
- package/Engines/constants.d.ts +10 -0
- package/Engines/constants.js +10 -0
- package/Engines/constants.js.map +1 -1
- package/Materials/Node/Blocks/Fragment/perturbNormalBlock.js +6 -3
- package/Materials/Node/Blocks/Fragment/perturbNormalBlock.js.map +1 -1
- package/Materials/PBR/openpbrMaterial.d.ts +8 -0
- package/Materials/PBR/openpbrMaterial.js +16 -0
- package/Materials/PBR/openpbrMaterial.js.map +1 -1
- package/Materials/PBR/pbrBaseMaterial.d.ts +1 -0
- package/Materials/PBR/pbrBaseMaterial.js +8 -0
- package/Materials/PBR/pbrBaseMaterial.js.map +1 -1
- package/Materials/Textures/Procedurals/proceduralTexture.d.ts +6 -0
- package/Materials/Textures/Procedurals/proceduralTexture.js +3 -1
- package/Materials/Textures/Procedurals/proceduralTexture.js.map +1 -1
- package/Materials/Textures/index.d.ts +1 -0
- package/Materials/Textures/index.js +1 -0
- package/Materials/Textures/index.js.map +1 -1
- package/Materials/Textures/textureMerger.js +1 -0
- package/Materials/Textures/textureMerger.js.map +1 -1
- package/Materials/Textures/textureProcessor.d.ts +315 -0
- package/Materials/Textures/textureProcessor.js +792 -0
- package/Materials/Textures/textureProcessor.js.map +1 -0
- package/Materials/material.d.ts +24 -0
- package/Materials/material.js +39 -0
- package/Materials/material.js.map +1 -1
- package/Materials/standardMaterial.d.ts +1 -0
- package/Materials/standardMaterial.js +6 -0
- package/Materials/standardMaterial.js.map +1 -1
- package/Physics/v2/characterController.d.ts +104 -7
- package/Physics/v2/characterController.js +355 -7
- package/Physics/v2/characterController.js.map +1 -1
- package/Shaders/ShadersInclude/bumpFragment.js +3 -3
- package/Shaders/ShadersInclude/bumpFragment.js.map +1 -1
- package/Shaders/ShadersInclude/bumpFragmentMainFunctions.js +5 -1
- package/Shaders/ShadersInclude/bumpFragmentMainFunctions.js.map +1 -1
- package/Shaders/ShadersInclude/defaultFragmentDeclaration.js +3 -0
- package/Shaders/ShadersInclude/defaultFragmentDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/defaultUboDeclaration.js +1 -1
- package/Shaders/ShadersInclude/defaultUboDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/defaultVertexDeclaration.js +1 -1
- package/Shaders/ShadersInclude/defaultVertexDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrAmbientOcclusionData.js +1 -1
- package/Shaders/ShadersInclude/openpbrAmbientOcclusionData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrBackgroundTransmission.js +1 -1
- package/Shaders/ShadersInclude/openpbrBackgroundTransmission.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrBaseLayerData.js +14 -14
- package/Shaders/ShadersInclude/openpbrBaseLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrCoatLayerData.js +6 -6
- package/Shaders/ShadersInclude/openpbrCoatLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrDirectLighting.js +1 -1
- package/Shaders/ShadersInclude/openpbrDirectLighting.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrEnvironmentLighting.js +1 -1
- package/Shaders/ShadersInclude/openpbrEnvironmentLighting.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrFragmentDeclaration.js +3 -0
- package/Shaders/ShadersInclude/openpbrFragmentDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrFuzzLayerData.js +3 -3
- package/Shaders/ShadersInclude/openpbrFuzzLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrNormalMapFragment.js +4 -4
- package/Shaders/ShadersInclude/openpbrNormalMapFragment.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrSubsurfaceLayerData.js +7 -3
- package/Shaders/ShadersInclude/openpbrSubsurfaceLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrThinFilmLayerData.js +2 -2
- package/Shaders/ShadersInclude/openpbrThinFilmLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrTransmissionLayerData.js +5 -5
- package/Shaders/ShadersInclude/openpbrTransmissionLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrUboDeclaration.js +1 -1
- package/Shaders/ShadersInclude/openpbrUboDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrVertexDeclaration.js +1 -1
- package/Shaders/ShadersInclude/openpbrVertexDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/pbrBlockFinalUnlitComponents.js +1 -1
- package/Shaders/ShadersInclude/pbrBlockFinalUnlitComponents.js.map +1 -1
- package/Shaders/ShadersInclude/pbrBlockLightmapInit.js +1 -1
- package/Shaders/ShadersInclude/pbrBlockLightmapInit.js.map +1 -1
- package/Shaders/ShadersInclude/pbrFragmentDeclaration.js +3 -0
- package/Shaders/ShadersInclude/pbrFragmentDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/pbrHelperFunctions.js +4 -0
- package/Shaders/ShadersInclude/pbrHelperFunctions.js.map +1 -1
- package/Shaders/ShadersInclude/pbrUboDeclaration.js +1 -1
- package/Shaders/ShadersInclude/pbrUboDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/pbrVertexDeclaration.js +1 -1
- package/Shaders/ShadersInclude/pbrVertexDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/textureRepetitionFunctions.d.ts +5 -0
- package/Shaders/ShadersInclude/textureRepetitionFunctions.js +52 -0
- package/Shaders/ShadersInclude/textureRepetitionFunctions.js.map +1 -0
- package/Shaders/default.fragment.d.ts +1 -0
- package/Shaders/default.fragment.js +8 -6
- package/Shaders/default.fragment.js.map +1 -1
- package/Shaders/geometry.fragment.js +3 -3
- package/Shaders/geometry.fragment.js.map +1 -1
- package/Shaders/openpbr.fragment.d.ts +1 -0
- package/Shaders/openpbr.fragment.js +4 -2
- package/Shaders/openpbr.fragment.js.map +1 -1
- package/Shaders/pbr.fragment.d.ts +1 -0
- package/Shaders/pbr.fragment.js +24 -22
- package/Shaders/pbr.fragment.js.map +1 -1
- package/Shaders/textureProcessor.fragment.d.ts +5 -0
- package/Shaders/textureProcessor.fragment.js +156 -0
- package/Shaders/textureProcessor.fragment.js.map +1 -0
- package/ShadersWGSL/ShadersInclude/bumpFragment.js +3 -3
- package/ShadersWGSL/ShadersInclude/bumpFragment.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/bumpFragmentMainFunctions.js +5 -1
- package/ShadersWGSL/ShadersInclude/bumpFragmentMainFunctions.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/defaultUboDeclaration.js +1 -1
- package/ShadersWGSL/ShadersInclude/defaultUboDeclaration.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrAmbientOcclusionData.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrAmbientOcclusionData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrBackgroundTransmission.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrBackgroundTransmission.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrBaseLayerData.js +15 -15
- package/ShadersWGSL/ShadersInclude/openpbrBaseLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrCoatLayerData.js +7 -7
- package/ShadersWGSL/ShadersInclude/openpbrCoatLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrDirectLighting.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrDirectLighting.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrEnvironmentLighting.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrEnvironmentLighting.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrFuzzLayerData.js +4 -4
- package/ShadersWGSL/ShadersInclude/openpbrFuzzLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrIblFunctions.js +3 -3
- package/ShadersWGSL/ShadersInclude/openpbrIblFunctions.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrNormalMapFragment.js +4 -4
- package/ShadersWGSL/ShadersInclude/openpbrNormalMapFragment.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrSubsurfaceLayerData.js +7 -3
- package/ShadersWGSL/ShadersInclude/openpbrSubsurfaceLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrThinFilmLayerData.js +2 -2
- package/ShadersWGSL/ShadersInclude/openpbrThinFilmLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrTransmissionLayerData.js +6 -6
- package/ShadersWGSL/ShadersInclude/openpbrTransmissionLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrUboDeclaration.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrUboDeclaration.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/pbrBlockFinalUnlitComponents.js +1 -1
- package/ShadersWGSL/ShadersInclude/pbrBlockFinalUnlitComponents.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/pbrBlockLightmapInit.js +1 -1
- package/ShadersWGSL/ShadersInclude/pbrBlockLightmapInit.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/pbrHelperFunctions.js +4 -0
- package/ShadersWGSL/ShadersInclude/pbrHelperFunctions.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/pbrUboDeclaration.js +1 -1
- package/ShadersWGSL/ShadersInclude/pbrUboDeclaration.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/textureRepetitionFunctions.d.ts +5 -0
- package/ShadersWGSL/ShadersInclude/textureRepetitionFunctions.js +52 -0
- package/ShadersWGSL/ShadersInclude/textureRepetitionFunctions.js.map +1 -0
- package/ShadersWGSL/default.fragment.d.ts +1 -0
- package/ShadersWGSL/default.fragment.js +8 -6
- package/ShadersWGSL/default.fragment.js.map +1 -1
- package/ShadersWGSL/geometry.fragment.js +3 -3
- package/ShadersWGSL/geometry.fragment.js.map +1 -1
- package/ShadersWGSL/openpbr.fragment.d.ts +1 -0
- package/ShadersWGSL/openpbr.fragment.js +5 -3
- package/ShadersWGSL/openpbr.fragment.js.map +1 -1
- package/ShadersWGSL/openpbr.vertex.js +1 -1
- package/ShadersWGSL/openpbr.vertex.js.map +1 -1
- package/ShadersWGSL/pbr.fragment.d.ts +1 -0
- package/ShadersWGSL/pbr.fragment.js +24 -22
- package/ShadersWGSL/pbr.fragment.js.map +1 -1
- package/ShadersWGSL/textureProcessor.fragment.d.ts +5 -0
- package/ShadersWGSL/textureProcessor.fragment.js +161 -0
- package/ShadersWGSL/textureProcessor.fragment.js.map +1 -0
- package/SmartAssets/index.d.ts +2 -0
- package/SmartAssets/index.js +2 -0
- package/SmartAssets/index.js.map +1 -0
- package/SmartAssets/smartAssetManager.d.ts +156 -0
- package/SmartAssets/smartAssetManager.js +531 -0
- package/SmartAssets/smartAssetManager.js.map +1 -0
- package/SmartAssets/smartAssetSerializer.d.ts +61 -0
- package/SmartAssets/smartAssetSerializer.js +97 -0
- package/SmartAssets/smartAssetSerializer.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,792 @@
|
|
|
1
|
+
import { ProceduralTexture } from "./Procedurals/proceduralTexture.js";
|
|
2
|
+
|
|
3
|
+
import { Color4 } from "../../Maths/math.color.js";
|
|
4
|
+
const _ShaderName = "textureProcessor";
|
|
5
|
+
/**
|
|
6
|
+
* Specifies the color space of a texture operand.
|
|
7
|
+
* When `sRGB` is set the sampled RGB values are converted to linear space before any channel
|
|
8
|
+
* swizzle, factor multiplication, or arithmetic operation. Alpha is always treated as linear.
|
|
9
|
+
*/
|
|
10
|
+
export var TextureColorSpace;
|
|
11
|
+
(function (TextureColorSpace) {
|
|
12
|
+
/** Texture data is already in linear space (default). No conversion applied. */
|
|
13
|
+
TextureColorSpace[TextureColorSpace["Linear"] = 0] = "Linear";
|
|
14
|
+
/** Texture data is in sRGB (gamma) space. RGB channels are linearized (IEC 61966-2-1) before use. */
|
|
15
|
+
TextureColorSpace[TextureColorSpace["SRGB"] = 1] = "SRGB";
|
|
16
|
+
})(TextureColorSpace || (TextureColorSpace = {}));
|
|
17
|
+
/**
|
|
18
|
+
* Bitmask controlling which channels are written to the output texture by a processing operation.
|
|
19
|
+
* Channels excluded from the mask receive a sensible default: `0.0` for RGB channels, `1.0` for alpha.
|
|
20
|
+
* Use `ChannelMask.RGBA` (or omit the parameter) to pass all channels through unchanged.
|
|
21
|
+
*
|
|
22
|
+
* | Flag | Channels written | Excluded channels |
|
|
23
|
+
* |------|-----------------|-------------------|
|
|
24
|
+
* | R | red | G=0, B=0, A=1 |
|
|
25
|
+
* | G | green | R=0, B=0, A=1 |
|
|
26
|
+
* | B | blue | R=0, G=0, A=1 |
|
|
27
|
+
* | A | alpha | R=0, G=0, B=0 |
|
|
28
|
+
* | RGB | red, green, blue | A=1 |
|
|
29
|
+
* | RGBA | all four | (none) |
|
|
30
|
+
*/
|
|
31
|
+
export var ChannelMask;
|
|
32
|
+
(function (ChannelMask) {
|
|
33
|
+
/** Pass only the red channel; G=0, B=0, A=1. */
|
|
34
|
+
ChannelMask[ChannelMask["R"] = 1] = "R";
|
|
35
|
+
/** Pass only the green channel; R=0, B=0, A=1. */
|
|
36
|
+
ChannelMask[ChannelMask["G"] = 2] = "G";
|
|
37
|
+
/** Pass only the blue channel; R=0, G=0, A=1. */
|
|
38
|
+
ChannelMask[ChannelMask["B"] = 4] = "B";
|
|
39
|
+
/** Pass only the alpha channel; R=0, G=0, B=0. */
|
|
40
|
+
ChannelMask[ChannelMask["A"] = 8] = "A";
|
|
41
|
+
/** Pass red, green, and blue; alpha is forced to 1.0. */
|
|
42
|
+
ChannelMask[ChannelMask["RGB"] = 7] = "RGB";
|
|
43
|
+
/** Pass all four channels unchanged (default — no masking). */
|
|
44
|
+
ChannelMask[ChannelMask["RGBA"] = 15] = "RGBA";
|
|
45
|
+
})(ChannelMask || (ChannelMask = {}));
|
|
46
|
+
/**
|
|
47
|
+
* Specifies which channel of a texture to read for an operation.
|
|
48
|
+
* When a single channel is selected its scalar value is broadcast to RGB; alpha
|
|
49
|
+
* is either preserved from the original sample or replicated when `A` is chosen.
|
|
50
|
+
*
|
|
51
|
+
* | Value | Swizzle |
|
|
52
|
+
* |-------|---------|
|
|
53
|
+
* | RGBA | (r, g, b, a) — no swizzle (default) |
|
|
54
|
+
* | R | (r, r, r, a) |
|
|
55
|
+
* | G | (g, g, g, a) |
|
|
56
|
+
* | B | (b, b, b, a) |
|
|
57
|
+
* | A | (a, a, a, a) |
|
|
58
|
+
*/
|
|
59
|
+
export var TextureChannel;
|
|
60
|
+
(function (TextureChannel) {
|
|
61
|
+
/** Use all four channels as sampled (default). */
|
|
62
|
+
TextureChannel[TextureChannel["RGBA"] = 0] = "RGBA";
|
|
63
|
+
/** Broadcast the red channel to RGB; preserve alpha: RRRA. */
|
|
64
|
+
TextureChannel[TextureChannel["R"] = 1] = "R";
|
|
65
|
+
/** Broadcast the green channel to RGB; preserve alpha: GGGA. */
|
|
66
|
+
TextureChannel[TextureChannel["G"] = 2] = "G";
|
|
67
|
+
/** Broadcast the blue channel to RGB; preserve alpha: BBBA. */
|
|
68
|
+
TextureChannel[TextureChannel["B"] = 3] = "B";
|
|
69
|
+
/** Broadcast the alpha channel to all four components: AAAA. */
|
|
70
|
+
TextureChannel[TextureChannel["A"] = 4] = "A";
|
|
71
|
+
})(TextureChannel || (TextureChannel = {}));
|
|
72
|
+
/**
|
|
73
|
+
* Create an operand from a texture alone (no constant factor scaling).
|
|
74
|
+
* @param texture - The texture to sample, or null to produce an identity (1,1,1,1) constant operand
|
|
75
|
+
* @param channel - Optional channel selection. When set, the sampled value is swizzled before use
|
|
76
|
+
* (e.g. `TextureChannel.R` → RRRA). Defaults to `TextureChannel.RGBA` (no swizzle).
|
|
77
|
+
* @param colorSpace - Optional color space. When `TextureColorSpace.SRGB`, the sampled RGB channels
|
|
78
|
+
* are linearized before use. Defaults to `TextureColorSpace.Linear`.
|
|
79
|
+
* @returns An operand that evaluates to the sampled texture value
|
|
80
|
+
*/
|
|
81
|
+
export function CreateTextureOperand(texture, channel, colorSpace) {
|
|
82
|
+
const op = { texture };
|
|
83
|
+
if (channel) {
|
|
84
|
+
op.channel = channel;
|
|
85
|
+
}
|
|
86
|
+
if (colorSpace) {
|
|
87
|
+
op.colorSpace = colorSpace;
|
|
88
|
+
}
|
|
89
|
+
return op;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Create an operand from a constant RGBA factor with no texture.
|
|
93
|
+
* @param factor - The constant RGBA value
|
|
94
|
+
* @returns An operand that evaluates to the constant factor
|
|
95
|
+
*/
|
|
96
|
+
export function CreateFactorOperand(factor) {
|
|
97
|
+
return { texture: null, factor };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Create an operand from a texture multiplied by a constant RGBA factor.
|
|
101
|
+
* This is the standard glTF pattern (e.g. baseColorTexture * baseColorFactor).
|
|
102
|
+
* If `texture` is null, returns a factor-only operand.
|
|
103
|
+
* @param texture - The texture to sample, or null to use the factor alone
|
|
104
|
+
* @param factor - The constant factor to multiply by
|
|
105
|
+
* @param channel - Optional channel selection. When set, the sampled value is swizzled before
|
|
106
|
+
* factor multiplication (e.g. `TextureChannel.G` → GGGA, then multiplied by factor).
|
|
107
|
+
* Defaults to `TextureChannel.RGBA` (no swizzle).
|
|
108
|
+
* @param colorSpace - Optional color space. When `TextureColorSpace.SRGB`, the sampled RGB channels
|
|
109
|
+
* are linearized before factor multiplication. Defaults to `TextureColorSpace.Linear`.
|
|
110
|
+
* @returns An operand that evaluates to `sample(texture) * factor`, or `factor` if texture is null
|
|
111
|
+
*/
|
|
112
|
+
export function CreateTextureWithFactorOperand(texture, factor, channel, colorSpace) {
|
|
113
|
+
const op = { texture, factor };
|
|
114
|
+
if (channel) {
|
|
115
|
+
op.channel = channel;
|
|
116
|
+
}
|
|
117
|
+
if (colorSpace) {
|
|
118
|
+
op.colorSpace = colorSpace;
|
|
119
|
+
}
|
|
120
|
+
return op;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* @internal
|
|
124
|
+
* Evaluate the effective constant Color4 of an operand.
|
|
125
|
+
* When a texture-only operand omits factor, the implicit value is (1, 1, 1, 1).
|
|
126
|
+
*/
|
|
127
|
+
function _EvalConstant(op) {
|
|
128
|
+
return op.factor ?? new Color4(1, 1, 1, 1);
|
|
129
|
+
}
|
|
130
|
+
/** @internal */
|
|
131
|
+
function _MultiplyConstants(a, b) {
|
|
132
|
+
return new Color4(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);
|
|
133
|
+
}
|
|
134
|
+
/** @internal */
|
|
135
|
+
function _MaxConstants(a, b) {
|
|
136
|
+
return new Color4(Math.max(a.r, b.r), Math.max(a.g, b.g), Math.max(a.b, b.b), Math.max(a.a, b.a));
|
|
137
|
+
}
|
|
138
|
+
/** @internal */
|
|
139
|
+
function _LerpConstants(a, b, t) {
|
|
140
|
+
return new Color4(a.r + (b.r - a.r) * t.r, a.g + (b.g - a.g) * t.g, a.b + (b.b - a.b) * t.b, a.a + (b.a - a.a) * t.a);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* @internal
|
|
144
|
+
* Determine the output texture size from a list of operands, using the largest input texture.
|
|
145
|
+
*/
|
|
146
|
+
function _ResolveOutputSize(operands) {
|
|
147
|
+
let maxDim = 0;
|
|
148
|
+
let result = 512;
|
|
149
|
+
for (const op of operands) {
|
|
150
|
+
if (op.texture) {
|
|
151
|
+
const size = op.texture.getSize();
|
|
152
|
+
const dim = Math.max(size.width, size.height);
|
|
153
|
+
if (dim > maxDim) {
|
|
154
|
+
maxDim = dim;
|
|
155
|
+
result = size.width === size.height ? dim : size;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* @internal
|
|
163
|
+
* Returns true when the texture has a non-identity UV transform (offset, scale, or rotation).
|
|
164
|
+
*/
|
|
165
|
+
function _HasNonIdentityTransform(texture) {
|
|
166
|
+
return !texture.getTextureMatrix().isIdentity();
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* @internal
|
|
170
|
+
* Returns true when every texture in the list shares the same UV transform matrix.
|
|
171
|
+
* A single texture (or empty list) trivially satisfies this.
|
|
172
|
+
*/
|
|
173
|
+
function _AllTransformsMatch(textures) {
|
|
174
|
+
if (textures.length <= 1) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
const ref = textures[0].getTextureMatrix();
|
|
178
|
+
for (let i = 1; i < textures.length; i++) {
|
|
179
|
+
if (!ref.equals(textures[i].getTextureMatrix())) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* @internal
|
|
187
|
+
* Copy sampling metadata from a source texture onto the output ProceduralTexture.
|
|
188
|
+
* `coordinatesIndex` and wrap modes are always copied.
|
|
189
|
+
* When `includeTransform` is true the UV offset/scale/rotation are also copied
|
|
190
|
+
* (used when all inputs share the same transform and it is propagated rather than baked).
|
|
191
|
+
*/
|
|
192
|
+
function _CopyTextureMetadata(from, to, includeTransform) {
|
|
193
|
+
to.coordinatesIndex = from.coordinatesIndex;
|
|
194
|
+
to.wrapU = from.wrapU;
|
|
195
|
+
to.wrapV = from.wrapV;
|
|
196
|
+
if (includeTransform) {
|
|
197
|
+
const src = from;
|
|
198
|
+
to.uOffset = src.uOffset ?? 0;
|
|
199
|
+
to.vOffset = src.vOffset ?? 0;
|
|
200
|
+
to.uScale = src.uScale ?? 1;
|
|
201
|
+
to.vScale = src.vScale ?? 1;
|
|
202
|
+
to.wAng = src.wAng ?? 0;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* @internal
|
|
207
|
+
* Return the shader define suffix for a TextureChannel (e.g. TextureChannel.R → "R").
|
|
208
|
+
* Returns an empty string for RGBA (no swizzle needed).
|
|
209
|
+
*/
|
|
210
|
+
function _ChannelDefine(channel) {
|
|
211
|
+
switch (channel) {
|
|
212
|
+
case TextureChannel.R:
|
|
213
|
+
return "R";
|
|
214
|
+
case TextureChannel.G:
|
|
215
|
+
return "G";
|
|
216
|
+
case TextureChannel.B:
|
|
217
|
+
return "B";
|
|
218
|
+
case TextureChannel.A:
|
|
219
|
+
return "A";
|
|
220
|
+
default:
|
|
221
|
+
return "";
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @internal
|
|
226
|
+
* Apply a channel swizzle to a constant Color4, matching the GPU behaviour for TextureChannel.
|
|
227
|
+
*/
|
|
228
|
+
function _ApplyChannelSwizzle(c, channel) {
|
|
229
|
+
switch (channel) {
|
|
230
|
+
case TextureChannel.R:
|
|
231
|
+
return new Color4(c.r, c.r, c.r, c.a);
|
|
232
|
+
case TextureChannel.G:
|
|
233
|
+
return new Color4(c.g, c.g, c.g, c.a);
|
|
234
|
+
case TextureChannel.B:
|
|
235
|
+
return new Color4(c.b, c.b, c.b, c.a);
|
|
236
|
+
case TextureChannel.A:
|
|
237
|
+
return new Color4(c.a, c.a, c.a, c.a);
|
|
238
|
+
default:
|
|
239
|
+
return c;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* @internal
|
|
244
|
+
* Build the OP_INVERT define plus per-channel INVERT_R/G/B/A defines from an ChannelMask bitmask.
|
|
245
|
+
*/
|
|
246
|
+
function _BuildInvertDefines(channels) {
|
|
247
|
+
const defines = ["OP_INVERT"];
|
|
248
|
+
if (channels & ChannelMask.R) {
|
|
249
|
+
defines.push("INVERT_R");
|
|
250
|
+
}
|
|
251
|
+
if (channels & ChannelMask.G) {
|
|
252
|
+
defines.push("INVERT_G");
|
|
253
|
+
}
|
|
254
|
+
if (channels & ChannelMask.B) {
|
|
255
|
+
defines.push("INVERT_B");
|
|
256
|
+
}
|
|
257
|
+
if (channels & ChannelMask.A) {
|
|
258
|
+
defines.push("INVERT_A");
|
|
259
|
+
}
|
|
260
|
+
return defines;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* @internal
|
|
264
|
+
* Build OUTPUT_MASK_X_ZERO / OUTPUT_MASK_A_ONE defines for excluded channels.
|
|
265
|
+
* Channels present in the mask pass through; excluded channels get defaults (0.0 for RGB, 1.0 for A).
|
|
266
|
+
* Returns an empty array for ChannelMask.RGBA (no masking needed).
|
|
267
|
+
*/
|
|
268
|
+
function _BuildOutputChannelMaskDefines(mask) {
|
|
269
|
+
const defines = [];
|
|
270
|
+
if (!(mask & ChannelMask.R)) {
|
|
271
|
+
defines.push("OUTPUT_MASK_R_ZERO");
|
|
272
|
+
}
|
|
273
|
+
if (!(mask & ChannelMask.G)) {
|
|
274
|
+
defines.push("OUTPUT_MASK_G_ZERO");
|
|
275
|
+
}
|
|
276
|
+
if (!(mask & ChannelMask.B)) {
|
|
277
|
+
defines.push("OUTPUT_MASK_B_ZERO");
|
|
278
|
+
}
|
|
279
|
+
if (!(mask & ChannelMask.A)) {
|
|
280
|
+
defines.push("OUTPUT_MASK_A_ONE");
|
|
281
|
+
}
|
|
282
|
+
return defines;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* @internal
|
|
286
|
+
* Apply a ChannelMask to a constant Color4: included channels pass through,
|
|
287
|
+
* excluded color channels become 0, excluded alpha becomes 1.
|
|
288
|
+
*/
|
|
289
|
+
function _ApplyOutputChannelMask(c, mask) {
|
|
290
|
+
return new Color4(mask & ChannelMask.R ? c.r : 0, mask & ChannelMask.G ? c.g : 0, mask & ChannelMask.B ? c.b : 0, mask & ChannelMask.A ? c.a : 1);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* @internal
|
|
294
|
+
* Build shader defines for a standard A/B operand.
|
|
295
|
+
* When `bakeTransform` is true and the texture has a non-identity UV transform,
|
|
296
|
+
* the OPERAND_X_MATRIX define is emitted so the shader applies the matrix when sampling.
|
|
297
|
+
*/
|
|
298
|
+
function _BuildOperandDefines(operand, prefix, bakeTransform) {
|
|
299
|
+
const defines = [];
|
|
300
|
+
if (operand.texture) {
|
|
301
|
+
defines.push(`OPERAND_${prefix}_TEXTURE`);
|
|
302
|
+
if (bakeTransform && _HasNonIdentityTransform(operand.texture)) {
|
|
303
|
+
defines.push(`OPERAND_${prefix}_MATRIX`);
|
|
304
|
+
}
|
|
305
|
+
if (operand.colorSpace) {
|
|
306
|
+
defines.push(`OPERAND_${prefix}_SRGB`);
|
|
307
|
+
}
|
|
308
|
+
if (operand.channel) {
|
|
309
|
+
defines.push(`OPERAND_${prefix}_CHANNEL_${_ChannelDefine(operand.channel)}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (operand.factor !== undefined || !operand.texture) {
|
|
313
|
+
defines.push(`OPERAND_${prefix}_FACTOR`);
|
|
314
|
+
}
|
|
315
|
+
return defines;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* @internal
|
|
319
|
+
* Build shader defines for the lerp blend operand.
|
|
320
|
+
* When `bakeTransform` is true and the texture has a non-identity UV transform,
|
|
321
|
+
* the LERP_T_MATRIX define is emitted.
|
|
322
|
+
*/
|
|
323
|
+
function _BuildLerpBlendDefines(t, bakeTransform) {
|
|
324
|
+
const defines = [];
|
|
325
|
+
if (t.texture) {
|
|
326
|
+
defines.push("LERP_T_TEXTURE");
|
|
327
|
+
if (bakeTransform && _HasNonIdentityTransform(t.texture)) {
|
|
328
|
+
defines.push("LERP_T_MATRIX");
|
|
329
|
+
}
|
|
330
|
+
if (t.factor !== undefined) {
|
|
331
|
+
defines.push("LERP_T_FACTOR");
|
|
332
|
+
}
|
|
333
|
+
if (t.colorSpace) {
|
|
334
|
+
defines.push("LERP_T_SRGB");
|
|
335
|
+
}
|
|
336
|
+
if (t.channel) {
|
|
337
|
+
defines.push(`LERP_T_CHANNEL_${_ChannelDefine(t.channel)}`);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
// factor-only: no additional defines needed; the shader uses factorT when LERP_T_TEXTURE is absent.
|
|
341
|
+
return defines;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* @internal
|
|
345
|
+
* Set uniforms and textures for a standard A/B operand on a procedural texture.
|
|
346
|
+
* When `bakeTransform` is true and the texture has a non-identity UV matrix,
|
|
347
|
+
* that matrix is uploaded as `<textureName>Matrix` for the shader to apply when sampling.
|
|
348
|
+
*/
|
|
349
|
+
function _SetOperandUniforms(pt, operand, textureName, factorName, bakeTransform) {
|
|
350
|
+
if (operand.texture) {
|
|
351
|
+
pt.setTexture(textureName, operand.texture);
|
|
352
|
+
if (bakeTransform && _HasNonIdentityTransform(operand.texture)) {
|
|
353
|
+
pt.setMatrix(`${textureName}Matrix`, operand.texture.getTextureMatrix());
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const needsFactor = operand.factor !== undefined || !operand.texture;
|
|
357
|
+
if (needsFactor) {
|
|
358
|
+
pt.setColor4(factorName, _EvalConstant(operand));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* @internal
|
|
363
|
+
* Set uniforms and textures for the lerp blend operand.
|
|
364
|
+
* When `bakeTransform` is true and the texture has a non-identity UV matrix,
|
|
365
|
+
* that matrix is uploaded as `textureTMatrix`.
|
|
366
|
+
*/
|
|
367
|
+
function _SetLerpBlendUniforms(pt, t, bakeTransform) {
|
|
368
|
+
if (t.texture) {
|
|
369
|
+
pt.setTexture("textureT", t.texture);
|
|
370
|
+
if (bakeTransform && _HasNonIdentityTransform(t.texture)) {
|
|
371
|
+
pt.setMatrix("textureTMatrix", t.texture.getTextureMatrix());
|
|
372
|
+
}
|
|
373
|
+
if (t.factor !== undefined) {
|
|
374
|
+
pt.setColor4("factorT", t.factor);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
pt.setColor4("factorT", _EvalConstant(t));
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* @internal
|
|
383
|
+
* Create a textureProcessor procedural texture with the given defines. The returned texture
|
|
384
|
+
* is not yet rendered — uniforms must be set on it before calling _RenderAsync.
|
|
385
|
+
*/
|
|
386
|
+
function _CreateProcessorTexture(name, defines, outputSize, scene, outputColorSpace = TextureColorSpace.Linear) {
|
|
387
|
+
const options = {
|
|
388
|
+
type: 0,
|
|
389
|
+
format: 5,
|
|
390
|
+
samplingMode: 2,
|
|
391
|
+
generateDepthBuffer: false,
|
|
392
|
+
generateMipMaps: false,
|
|
393
|
+
gammaSpace: outputColorSpace === TextureColorSpace.SRGB,
|
|
394
|
+
shaderLanguage: scene.getEngine().isWebGPU ? 1 /* ShaderLanguage.WGSL */ : 0 /* ShaderLanguage.GLSL */,
|
|
395
|
+
extraInitializationsAsync: async () => {
|
|
396
|
+
if (scene.getEngine().isWebGPU) {
|
|
397
|
+
await Promise.all([import("../../ShadersWGSL/textureProcessor.fragment.js")]);
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
await Promise.all([import("../../Shaders/textureProcessor.fragment.js")]);
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
// Opt out of scene-managed rendering. _shouldRender() would re-render the texture
|
|
404
|
+
// on the first scene frame regardless of refreshRate (because _currentRefreshId starts
|
|
405
|
+
// at -1 and is only advanced by _shouldRender() itself, not by a direct render() call).
|
|
406
|
+
// That re-render would sample already-disposed input textures, producing blank output.
|
|
407
|
+
skipSceneRegistration: true,
|
|
408
|
+
};
|
|
409
|
+
const pt = new ProceduralTexture(name, outputSize, _ShaderName, scene, options);
|
|
410
|
+
pt.refreshRate = -1; // render on demand only
|
|
411
|
+
pt.defines = defines.length > 0 ? "#define " + defines.join("\n#define ") + "\n" : "";
|
|
412
|
+
return pt;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* @internal
|
|
416
|
+
* Wait for a procedural texture's shader to compile then render it. Uniforms must be set
|
|
417
|
+
* on the texture before calling this.
|
|
418
|
+
*/
|
|
419
|
+
async function _RenderAsync(pt) {
|
|
420
|
+
return await new Promise((resolve, reject) => {
|
|
421
|
+
pt.executeWhenReady(() => {
|
|
422
|
+
try {
|
|
423
|
+
pt.render();
|
|
424
|
+
resolve();
|
|
425
|
+
}
|
|
426
|
+
catch (error) {
|
|
427
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Multiply two texture operands together, component-wise: `result = a * b`.
|
|
434
|
+
*
|
|
435
|
+
* Each operand can be a texture, a constant factor, or a texture scaled by a factor.
|
|
436
|
+
* This is useful for applying glTF-style factors to textures (e.g. `baseColorTexture * baseColorFactor`),
|
|
437
|
+
* or for modulating one texture by another.
|
|
438
|
+
*
|
|
439
|
+
* If both operands are constant (no textures), the multiplication is performed on the CPU and
|
|
440
|
+
* the result is returned as a factor-only operand with no texture allocated.
|
|
441
|
+
*
|
|
442
|
+
* When operands are results of previous operations (i.e. they carry a `dispose` function),
|
|
443
|
+
* their intermediate textures are automatically released after the GPU pass completes.
|
|
444
|
+
*
|
|
445
|
+
* @param name - Name for the resulting procedural texture (used only when a GPU pass is needed)
|
|
446
|
+
* @param a - First operand
|
|
447
|
+
* @param b - Second operand
|
|
448
|
+
* @param scene - Scene to create the texture in (used only when a GPU pass is needed)
|
|
449
|
+
* @param outputColorSpace - Optional output color space. When `TextureColorSpace.SRGB`, the linear
|
|
450
|
+
* result is converted to sRGB (IEC 61966-2-1) before being written. Defaults to `TextureColorSpace.Linear`.
|
|
451
|
+
* @param outputChannelMask - Optional bitmask of channels to write. Excluded color channels are set to
|
|
452
|
+
* `0.0`; excluded alpha is set to `1.0`. Defaults to `ChannelMask.RGBA` (all channels written).
|
|
453
|
+
* @returns An operand whose `texture` holds the GPU result, or whose `factor` holds the CPU-folded constant
|
|
454
|
+
*/
|
|
455
|
+
export async function MultiplyTexturesAsync(name, a, b, scene, outputColorSpace, outputChannelMask) {
|
|
456
|
+
if (!a.texture && !b.texture) {
|
|
457
|
+
const factor = _MultiplyConstants(_EvalConstant(a), _EvalConstant(b));
|
|
458
|
+
return { texture: null, factor: outputChannelMask ? _ApplyOutputChannelMask(factor, outputChannelMask) : factor };
|
|
459
|
+
}
|
|
460
|
+
const allTextures = [];
|
|
461
|
+
if (a.texture) {
|
|
462
|
+
allTextures.push(a.texture);
|
|
463
|
+
}
|
|
464
|
+
if (b.texture) {
|
|
465
|
+
allTextures.push(b.texture);
|
|
466
|
+
}
|
|
467
|
+
const canPropagate = _AllTransformsMatch(allTextures);
|
|
468
|
+
const bakeTransform = !canPropagate;
|
|
469
|
+
const defines = [
|
|
470
|
+
..._BuildOperandDefines(a, "A", bakeTransform),
|
|
471
|
+
..._BuildOperandDefines(b, "B", bakeTransform),
|
|
472
|
+
...(outputChannelMask ? _BuildOutputChannelMaskDefines(outputChannelMask) : []),
|
|
473
|
+
];
|
|
474
|
+
if (outputColorSpace) {
|
|
475
|
+
defines.push("OUTPUT_SRGB");
|
|
476
|
+
}
|
|
477
|
+
const pt = _CreateProcessorTexture(name, defines, _ResolveOutputSize([a, b]), scene, outputColorSpace);
|
|
478
|
+
_SetOperandUniforms(pt, a, "textureA", "factorA", bakeTransform);
|
|
479
|
+
_SetOperandUniforms(pt, b, "textureB", "factorB", bakeTransform);
|
|
480
|
+
try {
|
|
481
|
+
await _RenderAsync(pt);
|
|
482
|
+
}
|
|
483
|
+
catch (error) {
|
|
484
|
+
a.dispose?.();
|
|
485
|
+
b.dispose?.();
|
|
486
|
+
throw error;
|
|
487
|
+
}
|
|
488
|
+
a.dispose?.();
|
|
489
|
+
b.dispose?.();
|
|
490
|
+
_CopyTextureMetadata(allTextures[0], pt, canPropagate);
|
|
491
|
+
const result = { texture: pt, dispose: () => pt.dispose() };
|
|
492
|
+
if (outputColorSpace) {
|
|
493
|
+
result.colorSpace = outputColorSpace;
|
|
494
|
+
}
|
|
495
|
+
return result;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Take the component-wise maximum of two texture operands: `result = max(a, b)`.
|
|
499
|
+
*
|
|
500
|
+
* Each operand can be a texture, a constant factor, or a texture scaled by a factor.
|
|
501
|
+
* Useful for operations such as combining ambient occlusion maps or taking the
|
|
502
|
+
* brightest contribution from two sources.
|
|
503
|
+
*
|
|
504
|
+
* If both operands are constant (no textures), the operation is performed on the CPU and
|
|
505
|
+
* the result is returned as a factor-only operand with no texture allocated.
|
|
506
|
+
*
|
|
507
|
+
* When operands are results of previous operations (i.e. they carry a `dispose` function),
|
|
508
|
+
* their intermediate textures are automatically released after the GPU pass completes.
|
|
509
|
+
*
|
|
510
|
+
* @param name - Name for the resulting procedural texture (used only when a GPU pass is needed)
|
|
511
|
+
* @param a - First operand
|
|
512
|
+
* @param b - Second operand
|
|
513
|
+
* @param scene - Scene to create the texture in (used only when a GPU pass is needed)
|
|
514
|
+
* @param outputColorSpace - Optional output color space. When `TextureColorSpace.SRGB`, the linear
|
|
515
|
+
* result is converted to sRGB (IEC 61966-2-1) before being written. Defaults to `TextureColorSpace.Linear`.
|
|
516
|
+
* @param outputChannelMask - Optional bitmask of channels to write. Excluded color channels are set to
|
|
517
|
+
* `0.0`; excluded alpha is set to `1.0`. Defaults to `ChannelMask.RGBA` (all channels written).
|
|
518
|
+
* @returns An operand whose `texture` holds the GPU result, or whose `factor` holds the CPU-folded constant
|
|
519
|
+
*/
|
|
520
|
+
export async function MaxTexturesAsync(name, a, b, scene, outputColorSpace, outputChannelMask) {
|
|
521
|
+
if (!a.texture && !b.texture) {
|
|
522
|
+
const factor = _MaxConstants(_EvalConstant(a), _EvalConstant(b));
|
|
523
|
+
return { texture: null, factor: outputChannelMask ? _ApplyOutputChannelMask(factor, outputChannelMask) : factor };
|
|
524
|
+
}
|
|
525
|
+
const allTextures = [];
|
|
526
|
+
if (a.texture) {
|
|
527
|
+
allTextures.push(a.texture);
|
|
528
|
+
}
|
|
529
|
+
if (b.texture) {
|
|
530
|
+
allTextures.push(b.texture);
|
|
531
|
+
}
|
|
532
|
+
const canPropagate = _AllTransformsMatch(allTextures);
|
|
533
|
+
const bakeTransform = !canPropagate;
|
|
534
|
+
const defines = [
|
|
535
|
+
"OP_MAX",
|
|
536
|
+
..._BuildOperandDefines(a, "A", bakeTransform),
|
|
537
|
+
..._BuildOperandDefines(b, "B", bakeTransform),
|
|
538
|
+
...(outputChannelMask ? _BuildOutputChannelMaskDefines(outputChannelMask) : []),
|
|
539
|
+
];
|
|
540
|
+
if (outputColorSpace) {
|
|
541
|
+
defines.push("OUTPUT_SRGB");
|
|
542
|
+
}
|
|
543
|
+
const pt = _CreateProcessorTexture(name, defines, _ResolveOutputSize([a, b]), scene, outputColorSpace);
|
|
544
|
+
_SetOperandUniforms(pt, a, "textureA", "factorA", bakeTransform);
|
|
545
|
+
_SetOperandUniforms(pt, b, "textureB", "factorB", bakeTransform);
|
|
546
|
+
try {
|
|
547
|
+
await _RenderAsync(pt);
|
|
548
|
+
}
|
|
549
|
+
catch (error) {
|
|
550
|
+
a.dispose?.();
|
|
551
|
+
b.dispose?.();
|
|
552
|
+
throw error;
|
|
553
|
+
}
|
|
554
|
+
a.dispose?.();
|
|
555
|
+
b.dispose?.();
|
|
556
|
+
_CopyTextureMetadata(allTextures[0], pt, canPropagate);
|
|
557
|
+
const result = { texture: pt, dispose: () => pt.dispose() };
|
|
558
|
+
if (outputColorSpace) {
|
|
559
|
+
result.colorSpace = outputColorSpace;
|
|
560
|
+
}
|
|
561
|
+
return result;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Linearly interpolate between two texture operands: `result = mix(a, b, t)`.
|
|
565
|
+
*
|
|
566
|
+
* Each operand can be a texture, a constant factor, or a texture scaled by a factor.
|
|
567
|
+
* The `t` operand controls the blend weight per texel, per channel — a value of 0 returns `a`,
|
|
568
|
+
* a value of 1 returns `b`. Use a grayscale texture or a scalar `Color4(v, v, v, v)` for
|
|
569
|
+
* uniform blending across all channels.
|
|
570
|
+
*
|
|
571
|
+
* If all three operands are constant (no textures), the interpolation is performed on the CPU and
|
|
572
|
+
* the result is returned as a factor-only operand with no texture allocated.
|
|
573
|
+
*
|
|
574
|
+
* When operands are results of previous operations (i.e. they carry a `dispose` function),
|
|
575
|
+
* their intermediate textures are automatically released after the GPU pass completes.
|
|
576
|
+
*
|
|
577
|
+
* @param name - Name for the resulting procedural texture (used only when a GPU pass is needed)
|
|
578
|
+
* @param a - Start value operand (returned when t = 0)
|
|
579
|
+
* @param b - End value operand (returned when t = 1)
|
|
580
|
+
* @param t - Blend weight operand. Each channel independently controls the blend for the corresponding output channel.
|
|
581
|
+
* @param scene - Scene to create the texture in (used only when a GPU pass is needed)
|
|
582
|
+
* @param outputColorSpace - Optional output color space. When `TextureColorSpace.SRGB`, the linear
|
|
583
|
+
* result is converted to sRGB (IEC 61966-2-1) before being written. Defaults to `TextureColorSpace.Linear`.
|
|
584
|
+
* @param outputChannelMask - Optional bitmask of channels to write. Excluded color channels are set to
|
|
585
|
+
* `0.0`; excluded alpha is set to `1.0`. Defaults to `ChannelMask.RGBA` (all channels written).
|
|
586
|
+
* @returns An operand whose `texture` holds the GPU result, or whose `factor` holds the CPU-folded constant
|
|
587
|
+
*/
|
|
588
|
+
export async function LerpTexturesAsync(name, a, b, t, scene, outputColorSpace, outputChannelMask) {
|
|
589
|
+
if (!a.texture && !b.texture && !t.texture) {
|
|
590
|
+
const factor = _LerpConstants(_EvalConstant(a), _EvalConstant(b), _EvalConstant(t));
|
|
591
|
+
return { texture: null, factor: outputChannelMask ? _ApplyOutputChannelMask(factor, outputChannelMask) : factor };
|
|
592
|
+
}
|
|
593
|
+
const allTextures = [];
|
|
594
|
+
if (a.texture) {
|
|
595
|
+
allTextures.push(a.texture);
|
|
596
|
+
}
|
|
597
|
+
if (b.texture) {
|
|
598
|
+
allTextures.push(b.texture);
|
|
599
|
+
}
|
|
600
|
+
if (t.texture) {
|
|
601
|
+
allTextures.push(t.texture);
|
|
602
|
+
}
|
|
603
|
+
const canPropagate = _AllTransformsMatch(allTextures);
|
|
604
|
+
const bakeTransform = !canPropagate;
|
|
605
|
+
const defines = [
|
|
606
|
+
"OP_LERP",
|
|
607
|
+
..._BuildOperandDefines(a, "A", bakeTransform),
|
|
608
|
+
..._BuildOperandDefines(b, "B", bakeTransform),
|
|
609
|
+
..._BuildLerpBlendDefines(t, bakeTransform),
|
|
610
|
+
...(outputChannelMask ? _BuildOutputChannelMaskDefines(outputChannelMask) : []),
|
|
611
|
+
];
|
|
612
|
+
if (outputColorSpace) {
|
|
613
|
+
defines.push("OUTPUT_SRGB");
|
|
614
|
+
}
|
|
615
|
+
const pt = _CreateProcessorTexture(name, defines, _ResolveOutputSize([a, b, t]), scene, outputColorSpace);
|
|
616
|
+
_SetOperandUniforms(pt, a, "textureA", "factorA", bakeTransform);
|
|
617
|
+
_SetOperandUniforms(pt, b, "textureB", "factorB", bakeTransform);
|
|
618
|
+
_SetLerpBlendUniforms(pt, t, bakeTransform);
|
|
619
|
+
try {
|
|
620
|
+
await _RenderAsync(pt);
|
|
621
|
+
}
|
|
622
|
+
catch (error) {
|
|
623
|
+
a.dispose?.();
|
|
624
|
+
b.dispose?.();
|
|
625
|
+
t.dispose?.();
|
|
626
|
+
throw error;
|
|
627
|
+
}
|
|
628
|
+
a.dispose?.();
|
|
629
|
+
b.dispose?.();
|
|
630
|
+
t.dispose?.();
|
|
631
|
+
_CopyTextureMetadata(allTextures[0], pt, canPropagate);
|
|
632
|
+
const result = { texture: pt, dispose: () => pt.dispose() };
|
|
633
|
+
if (outputColorSpace) {
|
|
634
|
+
result.colorSpace = outputColorSpace;
|
|
635
|
+
}
|
|
636
|
+
return result;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Invert selected channels of a texture operand: `result[ch] = 1 - input[ch]`.
|
|
640
|
+
*
|
|
641
|
+
* The `channels` bitmask selects which channels are inverted; unselected channels pass through
|
|
642
|
+
* unchanged. Use `ChannelMask.RGB` for the common roughness↔smoothness conversion, or
|
|
643
|
+
* `ChannelMask.RGBA` (the default) to invert the entire texture.
|
|
644
|
+
*
|
|
645
|
+
* This is a unary operation — only operand A is used. Any `colorSpace` or `channel` properties
|
|
646
|
+
* on the input operand are honoured (sRGB linearization and channel swizzle applied before
|
|
647
|
+
* the invert).
|
|
648
|
+
*
|
|
649
|
+
* If the input is constant (no texture), the invert is performed on the CPU.
|
|
650
|
+
*
|
|
651
|
+
* When the input is the result of a previous operation (i.e. it carries a `dispose` function),
|
|
652
|
+
* its intermediate texture is automatically released after the GPU pass completes.
|
|
653
|
+
*
|
|
654
|
+
* @param name - Name for the resulting procedural texture (used only when a GPU pass is needed)
|
|
655
|
+
* @param input - Operand to invert
|
|
656
|
+
* @param scene - Scene to create the texture in (used only when a GPU pass is needed)
|
|
657
|
+
* @param channels - Bitmask of channels to invert. Defaults to `ChannelMask.RGBA`.
|
|
658
|
+
* @param outputColorSpace - Optional output color space. When `TextureColorSpace.SRGB`, the linear
|
|
659
|
+
* result is converted to sRGB (IEC 61966-2-1) before being written. Defaults to `TextureColorSpace.Linear`.
|
|
660
|
+
* @param outputChannelMask - Optional bitmask of channels to write. Excluded color channels are set to
|
|
661
|
+
* `0.0`; excluded alpha is set to `1.0`. Defaults to `ChannelMask.RGBA` (all channels written).
|
|
662
|
+
* @returns An operand whose `texture` holds the GPU result, or whose `factor` holds the CPU-folded constant
|
|
663
|
+
*/
|
|
664
|
+
export async function InvertTextureAsync(name, input, scene, channels = ChannelMask.RGBA, outputColorSpace, outputChannelMask) {
|
|
665
|
+
if (!input.texture) {
|
|
666
|
+
const c = _EvalConstant(input);
|
|
667
|
+
const factor = new Color4(channels & ChannelMask.R ? 1 - c.r : c.r, channels & ChannelMask.G ? 1 - c.g : c.g, channels & ChannelMask.B ? 1 - c.b : c.b, channels & ChannelMask.A ? 1 - c.a : c.a);
|
|
668
|
+
return { texture: null, factor: outputChannelMask ? _ApplyOutputChannelMask(factor, outputChannelMask) : factor };
|
|
669
|
+
}
|
|
670
|
+
// Single input: UV transform is always propagated (no bake needed).
|
|
671
|
+
const defines = [..._BuildOperandDefines(input, "A", false), ..._BuildInvertDefines(channels), ...(outputChannelMask ? _BuildOutputChannelMaskDefines(outputChannelMask) : [])];
|
|
672
|
+
if (outputColorSpace) {
|
|
673
|
+
defines.push("OUTPUT_SRGB");
|
|
674
|
+
}
|
|
675
|
+
const pt = _CreateProcessorTexture(name, defines, _ResolveOutputSize([input]), scene, outputColorSpace);
|
|
676
|
+
_SetOperandUniforms(pt, input, "textureA", "factorA", false);
|
|
677
|
+
try {
|
|
678
|
+
await _RenderAsync(pt);
|
|
679
|
+
}
|
|
680
|
+
catch (error) {
|
|
681
|
+
input.dispose?.();
|
|
682
|
+
throw error;
|
|
683
|
+
}
|
|
684
|
+
input.dispose?.();
|
|
685
|
+
_CopyTextureMetadata(input.texture, pt, true);
|
|
686
|
+
const result = { texture: pt, dispose: () => pt.dispose() };
|
|
687
|
+
if (outputColorSpace) {
|
|
688
|
+
result.colorSpace = outputColorSpace;
|
|
689
|
+
}
|
|
690
|
+
return result;
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Extract the per-texel maximum channel value from a texture and broadcast it to all output
|
|
694
|
+
* channels, producing a single-value (greyscale) texture in a single GPU pass.
|
|
695
|
+
*
|
|
696
|
+
* For each texel, computes `max(r, g, b)` — or `max(r, g, b, a)` when `includeAlpha` is true —
|
|
697
|
+
* and writes that scalar to the output:
|
|
698
|
+
* - `includeAlpha = false` (default): output is `(m, m, m, a)` where `m = max(r, g, b)`
|
|
699
|
+
* - `includeAlpha = true`: output is `(m, m, m, m)` where `m = max(r, g, b, a)`
|
|
700
|
+
*
|
|
701
|
+
* This is more efficient than chaining `ExtractChannelAsync` calls through `MaxTexturesAsync`,
|
|
702
|
+
* which would require multiple intermediate textures and GPU passes.
|
|
703
|
+
*
|
|
704
|
+
* Any `colorSpace` or `channel` properties on the input operand are honoured (sRGB linearization
|
|
705
|
+
* and channel swizzle applied before the max reduction).
|
|
706
|
+
*
|
|
707
|
+
* If the input is constant (no texture), the reduction is performed on the CPU.
|
|
708
|
+
*
|
|
709
|
+
* When the input is the result of a previous operation (i.e. it carries a `dispose` function),
|
|
710
|
+
* its intermediate texture is automatically released after the GPU pass completes.
|
|
711
|
+
*
|
|
712
|
+
* @param name - Name for the resulting procedural texture (used only when a GPU pass is needed)
|
|
713
|
+
* @param input - Operand to reduce
|
|
714
|
+
* @param scene - Scene to create the texture in (used only when a GPU pass is needed)
|
|
715
|
+
* @param includeAlpha - When true, alpha participates in the max and is also set to the result.
|
|
716
|
+
* Defaults to false (alpha is preserved from the input).
|
|
717
|
+
* @param outputColorSpace - Optional output color space. When `TextureColorSpace.SRGB`, the linear
|
|
718
|
+
* result is converted to sRGB (IEC 61966-2-1) before being written. Defaults to `TextureColorSpace.Linear`.
|
|
719
|
+
* @param outputChannelMask - Optional bitmask of channels to write. Excluded color channels are set to
|
|
720
|
+
* `0.0`; excluded alpha is set to `1.0`. Defaults to `ChannelMask.RGBA` (all channels written).
|
|
721
|
+
* @returns An operand whose `texture` holds the GPU result, or whose `factor` holds the CPU-folded constant
|
|
722
|
+
*/
|
|
723
|
+
export async function ExtractMaxChannelAsync(name, input, scene, includeAlpha = false, outputColorSpace, outputChannelMask) {
|
|
724
|
+
if (!input.texture) {
|
|
725
|
+
const c = _EvalConstant(input);
|
|
726
|
+
const m = includeAlpha ? Math.max(c.r, c.g, c.b, c.a) : Math.max(c.r, c.g, c.b);
|
|
727
|
+
const factor = new Color4(m, m, m, includeAlpha ? m : c.a);
|
|
728
|
+
return { texture: null, factor: outputChannelMask ? _ApplyOutputChannelMask(factor, outputChannelMask) : factor };
|
|
729
|
+
}
|
|
730
|
+
// Single input: UV transform is always propagated (no bake needed).
|
|
731
|
+
const defines = [..._BuildOperandDefines(input, "A", false), "OP_CHANNEL_MAX", ...(outputChannelMask ? _BuildOutputChannelMaskDefines(outputChannelMask) : [])];
|
|
732
|
+
if (includeAlpha) {
|
|
733
|
+
defines.push("CHANNEL_MAX_INCLUDE_ALPHA");
|
|
734
|
+
}
|
|
735
|
+
if (outputColorSpace) {
|
|
736
|
+
defines.push("OUTPUT_SRGB");
|
|
737
|
+
}
|
|
738
|
+
const pt = _CreateProcessorTexture(name, defines, _ResolveOutputSize([input]), scene, outputColorSpace);
|
|
739
|
+
_SetOperandUniforms(pt, input, "textureA", "factorA", false);
|
|
740
|
+
try {
|
|
741
|
+
await _RenderAsync(pt);
|
|
742
|
+
}
|
|
743
|
+
catch (error) {
|
|
744
|
+
input.dispose?.();
|
|
745
|
+
throw error;
|
|
746
|
+
}
|
|
747
|
+
input.dispose?.();
|
|
748
|
+
_CopyTextureMetadata(input.texture, pt, true);
|
|
749
|
+
const result = { texture: pt, dispose: () => pt.dispose() };
|
|
750
|
+
if (outputColorSpace) {
|
|
751
|
+
result.colorSpace = outputColorSpace;
|
|
752
|
+
}
|
|
753
|
+
return result;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Extract a single channel from a texture and broadcast it to RGB (or all four components for
|
|
757
|
+
* `TextureChannel.A`), producing a new texture. This is a convenience wrapper over
|
|
758
|
+
* `MultiplyTexturesAsync` with a `(1,1,1,1)` factor and the requested channel swizzle applied
|
|
759
|
+
* to the input.
|
|
760
|
+
*
|
|
761
|
+
* Swizzle results per channel:
|
|
762
|
+
* - `TextureChannel.R` → (r, r, r, a)
|
|
763
|
+
* - `TextureChannel.G` → (g, g, g, a)
|
|
764
|
+
* - `TextureChannel.B` → (b, b, b, a)
|
|
765
|
+
* - `TextureChannel.A` → (a, a, a, a)
|
|
766
|
+
*
|
|
767
|
+
* If the input is constant (no texture), the swizzle is applied on the CPU.
|
|
768
|
+
*
|
|
769
|
+
* Any `colorSpace` property on the input operand is honoured (sRGB linearization applied before
|
|
770
|
+
* the swizzle). Any existing `channel` on the input is replaced by the `channel` argument.
|
|
771
|
+
*
|
|
772
|
+
* When the input is the result of a previous operation (i.e. it carries a `dispose` function),
|
|
773
|
+
* its intermediate texture is automatically released after the GPU pass completes.
|
|
774
|
+
*
|
|
775
|
+
* @param name - Name for the resulting procedural texture (used only when a GPU pass is needed)
|
|
776
|
+
* @param input - Operand to extract the channel from
|
|
777
|
+
* @param channel - The channel to extract and broadcast
|
|
778
|
+
* @param scene - Scene to create the texture in (used only when a GPU pass is needed)
|
|
779
|
+
* @param outputColorSpace - Optional output color space. When `TextureColorSpace.SRGB`, the linear
|
|
780
|
+
* result is converted to sRGB (IEC 61966-2-1) before being written. Defaults to `TextureColorSpace.Linear`.
|
|
781
|
+
* @param outputChannelMask - Optional bitmask of channels to write. Excluded color channels are set to
|
|
782
|
+
* `0.0`; excluded alpha is set to `1.0`. Defaults to `ChannelMask.RGBA` (all channels written).
|
|
783
|
+
* @returns An operand whose `texture` holds the GPU result, or whose `factor` holds the CPU-folded constant
|
|
784
|
+
*/
|
|
785
|
+
export async function ExtractChannelAsync(name, input, channel, scene, outputColorSpace, outputChannelMask) {
|
|
786
|
+
if (!input.texture) {
|
|
787
|
+
const swizzled = _ApplyChannelSwizzle(_EvalConstant(input), channel);
|
|
788
|
+
return { texture: null, factor: outputChannelMask ? _ApplyOutputChannelMask(swizzled, outputChannelMask) : swizzled };
|
|
789
|
+
}
|
|
790
|
+
return await MultiplyTexturesAsync(name, { ...input, channel }, CreateFactorOperand(new Color4(1, 1, 1, 1)), scene, outputColorSpace, outputChannelMask);
|
|
791
|
+
}
|
|
792
|
+
//# sourceMappingURL=textureProcessor.js.map
|