@luma.gl/engine 10.0.0-alpha.10 → 10.0.0-alpha.12
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/dist/animation/node-animation-binding.cjs +10 -0
- package/dist/animation/node-animation-binding.d.ts +1 -1
- package/dist/animation/node-animation-binding.d.ts.map +1 -1
- package/dist/animation/node-animation-binding.js +10 -0
- package/dist/animation/node-animation-binding.js.map +1 -1
- package/dist/dist.dev.js +457 -206
- package/dist/dist.min.js +58 -58
- package/dist/gltf/gltf/animations/animations.d.ts +1 -1
- package/dist/gltf/gltf/animations/animations.d.ts.map +1 -1
- package/dist/gltf/gltf/create-scenegraph-from-gltf.cjs +80 -12
- package/dist/gltf/gltf/create-scenegraph-from-gltf.d.ts +8 -1
- package/dist/gltf/gltf/create-scenegraph-from-gltf.d.ts.map +1 -1
- package/dist/gltf/gltf/create-scenegraph-from-gltf.js +82 -14
- package/dist/gltf/gltf/create-scenegraph-from-gltf.js.map +1 -1
- package/dist/gltf/gltf/gltf-billboard.cjs +6 -1
- package/dist/gltf/gltf/gltf-billboard.d.ts.map +1 -1
- package/dist/gltf/gltf/gltf-billboard.js +6 -1
- package/dist/gltf/gltf/gltf-billboard.js.map +1 -1
- package/dist/gltf/gltf/gltf-extension-support.cjs +2 -2
- package/dist/gltf/gltf/gltf-extension-support.js +2 -2
- package/dist/gltf/gltf/gltf-extension-support.js.map +1 -1
- package/dist/gltf/index.cjs +2 -0
- package/dist/gltf/index.cjs.map +3 -3
- package/dist/gltf/index.d.ts +1 -1
- package/dist/gltf/index.d.ts.map +1 -1
- package/dist/gltf/index.js +1 -1
- package/dist/gltf/index.js.map +1 -1
- package/dist/gltf/parsers/parse-gltf-animations.cjs +8 -5
- package/dist/gltf/parsers/parse-gltf-animations.js +20 -5
- package/dist/gltf/parsers/parse-gltf-animations.js.map +1 -1
- package/dist/gltf/parsers/parse-gltf-lights.cjs +36 -72
- package/dist/gltf/parsers/parse-gltf-lights.d.ts +6 -2
- package/dist/gltf/parsers/parse-gltf-lights.d.ts.map +1 -1
- package/dist/gltf/parsers/parse-gltf-lights.js +34 -91
- package/dist/gltf/parsers/parse-gltf-lights.js.map +1 -1
- package/dist/gltf/parsers/parse-gltf-metadata.cjs +60 -0
- package/dist/gltf/parsers/parse-gltf-metadata.d.ts +5 -0
- package/dist/gltf/parsers/parse-gltf-metadata.d.ts.map +1 -0
- package/dist/gltf/parsers/parse-gltf-metadata.js +39 -0
- package/dist/gltf/parsers/parse-gltf-metadata.js.map +1 -0
- package/dist/gltf/parsers/parse-gltf.cjs +4 -1
- package/dist/gltf/parsers/parse-gltf.d.ts +4 -0
- package/dist/gltf/parsers/parse-gltf.d.ts.map +1 -1
- package/dist/gltf/parsers/parse-gltf.js +3 -0
- package/dist/gltf/parsers/parse-gltf.js.map +1 -1
- package/dist/gltf/pbr/pbr-environment.cjs +21 -3
- package/dist/gltf/pbr/pbr-environment.d.ts +3 -0
- package/dist/gltf/pbr/pbr-environment.d.ts.map +1 -1
- package/dist/gltf/pbr/pbr-environment.js +19 -1
- package/dist/gltf/pbr/pbr-environment.js.map +1 -1
- package/dist/graph/models/index.cjs.map +1 -1
- package/dist/graph/models/path/path-attribute-model.cjs +1 -1
- package/dist/graph/models/path/path-attribute-model.js +1 -1
- package/dist/graph/models/path/path-attribute-model.js.map +1 -1
- package/dist/graph/models/path/path-storage-model.cjs +1 -1
- package/dist/graph/models/path/path-storage-model.js +1 -1
- package/dist/graph/models/path/path-storage-model.js.map +1 -1
- package/dist/graph/physics/index.cjs.map +1 -1
- package/dist/graph/tables/index.cjs.map +1 -1
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/lighting/ambient-occlusion-pass.cjs +126 -0
- package/dist/lighting/ambient-occlusion-pass.d.ts +31 -0
- package/dist/lighting/ambient-occlusion-pass.d.ts.map +1 -0
- package/dist/lighting/ambient-occlusion-pass.js +108 -0
- package/dist/lighting/ambient-occlusion-pass.js.map +1 -0
- package/dist/lighting/clustered-light-grid.cjs +378 -0
- package/dist/lighting/clustered-light-grid.d.ts +94 -0
- package/dist/lighting/clustered-light-grid.d.ts.map +1 -0
- package/dist/lighting/clustered-light-grid.js +370 -0
- package/dist/lighting/clustered-light-grid.js.map +1 -0
- package/dist/lighting/contact-shadow-composite.cjs +80 -0
- package/dist/lighting/contact-shadow-composite.d.ts +6 -0
- package/dist/lighting/contact-shadow-composite.d.ts.map +1 -0
- package/dist/lighting/contact-shadow-composite.js +59 -0
- package/dist/lighting/contact-shadow-composite.js.map +1 -0
- package/dist/lighting/contact-shadow-pass.cjs +102 -0
- package/dist/lighting/contact-shadow-pass.d.ts +23 -0
- package/dist/lighting/contact-shadow-pass.d.ts.map +1 -0
- package/dist/lighting/contact-shadow-pass.js +85 -0
- package/dist/lighting/contact-shadow-pass.js.map +1 -0
- package/dist/lighting/index.cjs +118 -0
- package/dist/lighting/index.cjs.map +6 -0
- package/dist/lighting/index.d.ts +31 -0
- package/dist/lighting/index.d.ts.map +1 -0
- package/dist/lighting/index.js +20 -0
- package/dist/lighting/index.js.map +1 -0
- package/dist/lighting/lightmap-baker.cjs +238 -0
- package/dist/lighting/lightmap-baker.d.ts +62 -0
- package/dist/lighting/lightmap-baker.d.ts.map +1 -0
- package/dist/lighting/lightmap-baker.js +236 -0
- package/dist/lighting/lightmap-baker.js.map +1 -0
- package/dist/lighting/lightmap-data.cjs +117 -0
- package/dist/lighting/lightmap-data.d.ts +20 -0
- package/dist/lighting/lightmap-data.d.ts.map +1 -0
- package/dist/lighting/lightmap-data.js +110 -0
- package/dist/lighting/lightmap-data.js.map +1 -0
- package/dist/lighting/lightmap-environment.cjs +61 -0
- package/dist/lighting/lightmap-environment.d.ts +18 -0
- package/dist/lighting/lightmap-environment.d.ts.map +1 -0
- package/dist/lighting/lightmap-environment.js +46 -0
- package/dist/lighting/lightmap-environment.js.map +1 -0
- package/dist/lighting/pbr-environment.cjs +45 -0
- package/dist/lighting/pbr-environment.d.ts +25 -0
- package/dist/lighting/pbr-environment.d.ts.map +1 -0
- package/dist/lighting/pbr-environment.js +30 -0
- package/dist/lighting/pbr-environment.js.map +1 -0
- package/dist/lighting/pbr-material.cjs +75 -0
- package/dist/lighting/pbr-material.d.ts +33 -0
- package/dist/lighting/pbr-material.d.ts.map +1 -0
- package/dist/lighting/pbr-material.js +52 -0
- package/dist/lighting/pbr-material.js.map +1 -0
- package/dist/lighting/pbr-model.cjs +579 -0
- package/dist/lighting/pbr-model.d.ts +45 -0
- package/dist/lighting/pbr-model.d.ts.map +1 -0
- package/dist/lighting/pbr-model.js +545 -0
- package/dist/lighting/pbr-model.js.map +1 -0
- package/dist/lighting/pbr-surface-pass.cjs +116 -0
- package/dist/lighting/pbr-surface-pass.d.ts +35 -0
- package/dist/lighting/pbr-surface-pass.d.ts.map +1 -0
- package/dist/lighting/pbr-surface-pass.js +98 -0
- package/dist/lighting/pbr-surface-pass.js.map +1 -0
- package/dist/lighting/prefiltered-environment.cjs +275 -0
- package/dist/lighting/prefiltered-environment.d.ts +56 -0
- package/dist/lighting/prefiltered-environment.d.ts.map +1 -0
- package/dist/lighting/prefiltered-environment.js +264 -0
- package/dist/lighting/prefiltered-environment.js.map +1 -0
- package/dist/lighting/shadow-map-renderer.cjs +631 -0
- package/dist/lighting/shadow-map-renderer.d.ts +92 -0
- package/dist/lighting/shadow-map-renderer.d.ts.map +1 -0
- package/dist/lighting/shadow-map-renderer.js +622 -0
- package/dist/lighting/shadow-map-renderer.js.map +1 -0
- package/dist/lighting/shadow-quality.cjs +58 -0
- package/dist/lighting/shadow-quality.d.ts +13 -0
- package/dist/lighting/shadow-quality.d.ts.map +1 -0
- package/dist/lighting/shadow-quality.js +37 -0
- package/dist/lighting/shadow-quality.js.map +1 -0
- package/dist/material/material.cjs +3 -0
- package/dist/material/material.d.ts +2 -0
- package/dist/material/material.d.ts.map +1 -1
- package/dist/material/material.js +3 -0
- package/dist/material/material.js.map +1 -1
- package/dist/model/gpu-vector-model.cjs +11 -1
- package/dist/model/gpu-vector-model.d.ts +4 -2
- package/dist/model/gpu-vector-model.d.ts.map +1 -1
- package/dist/model/gpu-vector-model.js +12 -1
- package/dist/model/gpu-vector-model.js.map +1 -1
- package/dist/scenegraph/controllers/punctual-light-controller.cjs +121 -0
- package/dist/scenegraph/controllers/punctual-light-controller.d.ts +31 -0
- package/dist/scenegraph/controllers/punctual-light-controller.d.ts.map +1 -0
- package/dist/scenegraph/controllers/punctual-light-controller.js +117 -0
- package/dist/scenegraph/controllers/punctual-light-controller.js.map +1 -0
- package/dist/scenegraph/scene-metadata.cjs +43 -0
- package/dist/scenegraph/scene-metadata.d.ts +9 -0
- package/dist/scenegraph/scene-metadata.d.ts.map +1 -0
- package/dist/scenegraph/scene-metadata.js +21 -0
- package/dist/scenegraph/scene-metadata.js.map +1 -0
- package/dist/scenegraph/scenegraph-interaction.cjs +99 -0
- package/dist/scenegraph/scenegraph-interaction.d.ts +47 -0
- package/dist/scenegraph/scenegraph-interaction.d.ts.map +1 -0
- package/dist/scenegraph/scenegraph-interaction.js +79 -0
- package/dist/scenegraph/scenegraph-interaction.js.map +1 -0
- package/dist/scenegraph/scenegraph-node.cjs +10 -0
- package/dist/scenegraph/scenegraph-node.d.ts +10 -0
- package/dist/scenegraph/scenegraph-node.d.ts.map +1 -1
- package/dist/scenegraph/scenegraph-node.js +10 -0
- package/dist/scenegraph/scenegraph-node.js.map +1 -1
- package/dist/text/experimental.cjs.map +1 -1
- package/dist/text/fonts/atlas/text-utils.cjs +1 -1
- package/dist/text/fonts/atlas/text-utils.js +1 -1
- package/dist/text/fonts/atlas/text-utils.js.map +1 -1
- package/dist/text/fonts/index.cjs.map +1 -1
- package/dist/text/index.cjs.map +1 -1
- package/dist/text/text-2d/models/text-attribute-model.cjs +1 -1
- package/dist/text/text-2d/models/text-attribute-model.js +1 -1
- package/dist/text/text-2d/models/text-attribute-model.js.map +1 -1
- package/dist/text/text-2d/models/text-dictionary-model.cjs +1 -1
- package/dist/text/text-2d/models/text-dictionary-model.js +1 -1
- package/dist/text/text-2d/models/text-dictionary-model.js.map +1 -1
- package/dist/text/text-2d/models/text-storage-model.cjs +1 -1
- package/dist/text/text-2d/models/text-storage-model.js +1 -1
- package/dist/text/text-2d/models/text-storage-model.js.map +1 -1
- package/package.json +13 -8
- package/src/animation/node-animation-binding.ts +18 -1
- package/src/gltf/gltf/animations/animations.ts +8 -1
- package/src/gltf/gltf/create-scenegraph-from-gltf.ts +98 -17
- package/src/gltf/gltf/gltf-billboard.ts +6 -1
- package/src/gltf/gltf/gltf-extension-support.ts +2 -2
- package/src/gltf/index.ts +1 -0
- package/src/gltf/parsers/parse-gltf-animations.ts +22 -8
- package/src/gltf/parsers/parse-gltf-lights.ts +42 -130
- package/src/gltf/parsers/parse-gltf-metadata.ts +40 -0
- package/src/gltf/parsers/parse-gltf.ts +5 -0
- package/src/gltf/pbr/pbr-environment.ts +27 -1
- package/src/graph/models/path/path-attribute-model.ts +1 -1
- package/src/graph/models/path/path-storage-model.ts +1 -1
- package/src/index.ts +16 -0
- package/src/lighting/ambient-occlusion-pass.ts +130 -0
- package/src/lighting/clustered-light-grid.ts +481 -0
- package/src/lighting/contact-shadow-composite.ts +67 -0
- package/src/lighting/contact-shadow-pass.ts +107 -0
- package/src/lighting/index.ts +107 -0
- package/src/lighting/lightmap-baker.ts +306 -0
- package/src/lighting/lightmap-data.ts +138 -0
- package/src/lighting/lightmap-environment.ts +70 -0
- package/src/lighting/pbr-environment.ts +55 -0
- package/src/lighting/pbr-material.ts +85 -0
- package/src/lighting/pbr-model.ts +608 -0
- package/src/lighting/pbr-surface-pass.ts +122 -0
- package/src/lighting/prefiltered-environment.ts +354 -0
- package/src/lighting/shadow-map-renderer.ts +918 -0
- package/src/lighting/shadow-quality.ts +50 -0
- package/src/material/material.ts +3 -0
- package/src/model/gpu-vector-model.ts +18 -3
- package/src/scenegraph/controllers/punctual-light-controller.ts +172 -0
- package/src/scenegraph/scene-metadata.ts +23 -0
- package/src/scenegraph/scenegraph-interaction.ts +131 -0
- package/src/scenegraph/scenegraph-node.ts +12 -0
- package/src/text/fonts/atlas/text-utils.ts +1 -1
- package/src/text/text-2d/models/text-attribute-model.ts +1 -1
- package/src/text/text-2d/models/text-dictionary-model.ts +1 -1
- package/src/text/text-2d/models/text-storage-model.ts +1 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {Binding, Device, Texture} from '@luma.gl/core';
|
|
6
|
+
import type {Material} from '../material/material';
|
|
7
|
+
import {MaterialFactory} from '../material/material-factory';
|
|
8
|
+
import {type TextureBindingSource} from '@luma.gl/core/dynamic';
|
|
9
|
+
import {type PBRMaterialBindings, type PBRMaterialUniforms, pbrMaterial} from '@luma.gl/shaders';
|
|
10
|
+
|
|
11
|
+
/** Concrete or deferred texture resources accepted by the shared PBR material factory. */
|
|
12
|
+
export type PBRMaterialResources = Record<string, Binding | TextureBindingSource>;
|
|
13
|
+
|
|
14
|
+
/** Typed material instance backed by the canonical shaders PBR module. */
|
|
15
|
+
export type PBRMaterial = Material<{pbrMaterial: PBRMaterialUniforms}, PBRMaterialResources>;
|
|
16
|
+
|
|
17
|
+
/** Options used to create a reusable physically based material. */
|
|
18
|
+
export type CreatePBRMaterialOptions = {
|
|
19
|
+
/** Optional application-provided material identifier. */
|
|
20
|
+
id?: string;
|
|
21
|
+
/** Canonical glTF-compatible physically based material uniforms. */
|
|
22
|
+
uniforms?: PBRMaterialUniforms;
|
|
23
|
+
/** Material-scoped borrowed texture bindings; absent maps do not allocate fallback resources. */
|
|
24
|
+
bindings?: Partial<PBRMaterialBindings<Texture>> | PBRMaterialResources;
|
|
25
|
+
/** Existing compatible factory reused across materials in the same scene. */
|
|
26
|
+
factory?: MaterialFactory<{pbrMaterial: PBRMaterialUniforms}, PBRMaterialResources>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Creates a material factory that owns the canonical PBR group-3 binding schema. */
|
|
30
|
+
export function createPBRMaterialFactory(
|
|
31
|
+
device: Device
|
|
32
|
+
): MaterialFactory<{pbrMaterial: PBRMaterialUniforms}, PBRMaterialResources> {
|
|
33
|
+
return new MaterialFactory(device, {modules: [pbrMaterial]});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Creates a typed shared PBR material without introducing a format-specific material runtime. */
|
|
37
|
+
export function createPBRMaterial(
|
|
38
|
+
device: Device,
|
|
39
|
+
options: CreatePBRMaterialOptions = {}
|
|
40
|
+
): PBRMaterial {
|
|
41
|
+
const materialFactory = options.factory || createPBRMaterialFactory(device);
|
|
42
|
+
const bindings: PBRMaterialResources = {};
|
|
43
|
+
|
|
44
|
+
for (const [bindingName, binding] of Object.entries(options.bindings || {})) {
|
|
45
|
+
if (binding && materialFactory.ownsBinding(bindingName)) {
|
|
46
|
+
bindings[bindingName] = binding;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const material = materialFactory.createMaterial({id: options.id, bindings});
|
|
51
|
+
material.setProps({
|
|
52
|
+
pbrMaterial: {
|
|
53
|
+
...getPBRMaterialMapUniforms(bindings),
|
|
54
|
+
...options.uniforms
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return material;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Returns texture-presence flags matching the canonical PBR shader bindings. */
|
|
61
|
+
export function getPBRMaterialMapUniforms(
|
|
62
|
+
bindings: Partial<PBRMaterialBindings<Texture>> | PBRMaterialResources
|
|
63
|
+
): Partial<PBRMaterialUniforms> {
|
|
64
|
+
return {
|
|
65
|
+
baseColorMapEnabled: Boolean(bindings.pbr_baseColorSampler),
|
|
66
|
+
normalMapEnabled: Boolean(bindings.pbr_normalSampler),
|
|
67
|
+
lightmapMapEnabled: Boolean(bindings.pbr_lightmapSampler),
|
|
68
|
+
emissiveMapEnabled: Boolean(bindings.pbr_emissiveSampler),
|
|
69
|
+
metallicRoughnessMapEnabled: Boolean(bindings.pbr_metallicRoughnessSampler),
|
|
70
|
+
occlusionMapEnabled: Boolean(bindings.pbr_occlusionSampler),
|
|
71
|
+
specularColorMapEnabled: Boolean(bindings.pbr_specularColorSampler),
|
|
72
|
+
specularIntensityMapEnabled: Boolean(bindings.pbr_specularIntensitySampler),
|
|
73
|
+
transmissionMapEnabled: Boolean(bindings.pbr_transmissionSampler),
|
|
74
|
+
clearcoatMapEnabled: Boolean(bindings.pbr_clearcoatSampler),
|
|
75
|
+
clearcoatRoughnessMapEnabled: Boolean(bindings.pbr_clearcoatRoughnessSampler),
|
|
76
|
+
sheenColorMapEnabled: Boolean(bindings.pbr_sheenColorSampler),
|
|
77
|
+
sheenRoughnessMapEnabled: Boolean(bindings.pbr_sheenRoughnessSampler),
|
|
78
|
+
iridescenceMapEnabled: Boolean(bindings.pbr_iridescenceSampler),
|
|
79
|
+
anisotropyMapEnabled: Boolean(bindings.pbr_anisotropySampler),
|
|
80
|
+
bumpMapEnabled: Boolean(bindings.pbr_bumpSampler),
|
|
81
|
+
diffuseTransmissionMapEnabled: Boolean(bindings.pbr_diffuseTransmissionSampler),
|
|
82
|
+
diffuseTransmissionColorMapEnabled: Boolean(bindings.pbr_diffuseTransmissionColorSampler),
|
|
83
|
+
multiscatterColorMapEnabled: Boolean(bindings.pbr_multiscatterColorSampler)
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
3
|
+
|
|
4
|
+
import {assert, type Device} from '@luma.gl/core';
|
|
5
|
+
import type {Material} from '../material/material';
|
|
6
|
+
import {Model, type ModelProps} from '../model/model';
|
|
7
|
+
import {
|
|
8
|
+
pbrMaterial,
|
|
9
|
+
lightmapBake,
|
|
10
|
+
pbrScene,
|
|
11
|
+
pbrShadow,
|
|
12
|
+
pbrSurface,
|
|
13
|
+
pbrContactShadow,
|
|
14
|
+
pbrAmbientOcclusion,
|
|
15
|
+
skin
|
|
16
|
+
} from '@luma.gl/shaders';
|
|
17
|
+
|
|
18
|
+
/** Optional WGSL extensions for adapters building specialized mesh passes. */
|
|
19
|
+
export type PBRModelShaderHooks = {
|
|
20
|
+
vertexInputs?: string;
|
|
21
|
+
fragmentInputs?: string;
|
|
22
|
+
instanceTransform?: string;
|
|
23
|
+
vertexOutput?: string;
|
|
24
|
+
fragmentDeclarations?: string;
|
|
25
|
+
fragmentReturnType?: string;
|
|
26
|
+
fragmentBody?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Builds mesh entry points; default source has no scene-specific resources or identifiers. */
|
|
30
|
+
export function getPBRModelWGSL(hooks: PBRModelShaderHooks = {}): string {
|
|
31
|
+
return /* wgsl */ `
|
|
32
|
+
struct PBRModelVertexInputs {
|
|
33
|
+
@location(0) positions: vec3f,
|
|
34
|
+
#ifdef HAS_NORMALS
|
|
35
|
+
@location(1) normals: vec3f,
|
|
36
|
+
#endif
|
|
37
|
+
#ifdef HAS_TANGENTS
|
|
38
|
+
@location(2) TANGENT: vec4f,
|
|
39
|
+
#endif
|
|
40
|
+
#ifdef HAS_UV
|
|
41
|
+
@location(3) texCoords: vec2f,
|
|
42
|
+
#endif
|
|
43
|
+
#ifdef HAS_UV_1
|
|
44
|
+
@location(4) texCoords1: vec2f,
|
|
45
|
+
#endif
|
|
46
|
+
#ifdef HAS_SKIN
|
|
47
|
+
@location(5) JOINTS_0: vec4u,
|
|
48
|
+
@location(6) WEIGHTS_0: vec4f,
|
|
49
|
+
#endif
|
|
50
|
+
#ifdef HAS_COLORS
|
|
51
|
+
#ifdef HAS_RGBA_COLORS
|
|
52
|
+
@location(7) colors: vec4f,
|
|
53
|
+
#else
|
|
54
|
+
@location(7) colors: vec3f,
|
|
55
|
+
#endif
|
|
56
|
+
#endif
|
|
57
|
+
${hooks.vertexInputs ?? ''}
|
|
58
|
+
#ifdef HAS_INSTANCING
|
|
59
|
+
@location(8) instanceModelMatrixCol0: vec4f,
|
|
60
|
+
@location(9) instanceModelMatrixCol1: vec4f,
|
|
61
|
+
@location(10) instanceModelMatrixCol2: vec4f,
|
|
62
|
+
@location(11) instanceModelMatrixCol3: vec4f,
|
|
63
|
+
#endif
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
struct PBRModelFragmentInputs {
|
|
67
|
+
@builtin(position) position: vec4f,
|
|
68
|
+
@location(0) pbrPosition: vec3f,
|
|
69
|
+
@location(1) pbrUV0: vec2f,
|
|
70
|
+
@location(2) pbrUV1: vec2f,
|
|
71
|
+
@location(3) pbrNormal: vec3f,
|
|
72
|
+
@location(4) pbrColor: vec4f,
|
|
73
|
+
${hooks.fragmentInputs ?? ''}
|
|
74
|
+
#ifdef HAS_TANGENTS
|
|
75
|
+
@location(5) pbrTangent: vec4f,
|
|
76
|
+
#endif
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
fn getPBRInstanceNormalMatrix(matrix: mat3x3f) -> mat3x3f {
|
|
80
|
+
let firstCofactor = cross(matrix[1], matrix[2]);
|
|
81
|
+
let inverseDeterminant = 1.0 / dot(matrix[0], firstCofactor);
|
|
82
|
+
return mat3x3f(
|
|
83
|
+
firstCofactor,
|
|
84
|
+
cross(matrix[2], matrix[0]),
|
|
85
|
+
cross(matrix[0], matrix[1])
|
|
86
|
+
) * inverseDeterminant;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@vertex
|
|
90
|
+
fn vertexMain(inputs: PBRModelVertexInputs) -> PBRModelFragmentInputs {
|
|
91
|
+
var outputs: PBRModelFragmentInputs;
|
|
92
|
+
var position = vec4f(inputs.positions, 1.0);
|
|
93
|
+
var normal = vec3f(0.0, 0.0, 1.0);
|
|
94
|
+
var tangent = vec4f(1.0, 0.0, 0.0, 1.0);
|
|
95
|
+
var textureCoordinates = vec2f(0.0);
|
|
96
|
+
var secondTextureCoordinates = vec2f(0.0);
|
|
97
|
+
var vertexColor = vec4f(1.0);
|
|
98
|
+
|
|
99
|
+
#ifdef HAS_NORMALS
|
|
100
|
+
normal = inputs.normals;
|
|
101
|
+
#endif
|
|
102
|
+
#ifdef HAS_UV
|
|
103
|
+
textureCoordinates = inputs.texCoords;
|
|
104
|
+
#endif
|
|
105
|
+
#ifdef HAS_UV_1
|
|
106
|
+
secondTextureCoordinates = inputs.texCoords1;
|
|
107
|
+
#endif
|
|
108
|
+
#ifdef HAS_TANGENTS
|
|
109
|
+
tangent = inputs.TANGENT;
|
|
110
|
+
#endif
|
|
111
|
+
#ifdef HAS_COLORS
|
|
112
|
+
#ifdef HAS_RGBA_COLORS
|
|
113
|
+
vertexColor = inputs.colors;
|
|
114
|
+
#else
|
|
115
|
+
vertexColor = vec4f(inputs.colors, 1.0);
|
|
116
|
+
#endif
|
|
117
|
+
#endif
|
|
118
|
+
|
|
119
|
+
#ifdef HAS_SKIN
|
|
120
|
+
let skinMatrix = getSkinMatrix(inputs.WEIGHTS_0, inputs.JOINTS_0);
|
|
121
|
+
position = skinMatrix * position;
|
|
122
|
+
normal = normalize(getPBRInstanceNormalMatrix(mat3x3f(skinMatrix[0].xyz, skinMatrix[1].xyz, skinMatrix[2].xyz)) * normal);
|
|
123
|
+
#ifdef HAS_TANGENTS
|
|
124
|
+
tangent = vec4f(normalize((skinMatrix * vec4f(tangent.xyz, 0.0)).xyz), tangent.w * sign(determinant(mat3x3f(skinMatrix[0].xyz, skinMatrix[1].xyz, skinMatrix[2].xyz))));
|
|
125
|
+
#endif
|
|
126
|
+
#endif
|
|
127
|
+
|
|
128
|
+
#ifdef HAS_INSTANCING
|
|
129
|
+
var instanceMatrix = mat4x4f(
|
|
130
|
+
inputs.instanceModelMatrixCol0,
|
|
131
|
+
inputs.instanceModelMatrixCol1,
|
|
132
|
+
inputs.instanceModelMatrixCol2,
|
|
133
|
+
inputs.instanceModelMatrixCol3
|
|
134
|
+
);
|
|
135
|
+
${hooks.instanceTransform ?? ''} position = instanceMatrix * position;
|
|
136
|
+
let instanceNormalMatrix = getPBRInstanceNormalMatrix(mat3x3f(
|
|
137
|
+
instanceMatrix[0].xyz, instanceMatrix[1].xyz, instanceMatrix[2].xyz
|
|
138
|
+
));
|
|
139
|
+
normal = normalize(instanceNormalMatrix * normal);
|
|
140
|
+
#ifdef HAS_TANGENTS
|
|
141
|
+
tangent = vec4f(normalize((instanceMatrix * vec4f(tangent.xyz, 0.0)).xyz), tangent.w * sign(determinant(mat3x3f(instanceMatrix[0].xyz, instanceMatrix[1].xyz, instanceMatrix[2].xyz))));
|
|
142
|
+
#endif
|
|
143
|
+
#endif
|
|
144
|
+
let worldPosition = pbrProjection.modelMatrix * position;
|
|
145
|
+
normal = normalize((pbrProjection.normalMatrix * vec4f(normal, 0.0)).xyz);
|
|
146
|
+
#ifdef HAS_TANGENTS
|
|
147
|
+
tangent = vec4f(normalize((pbrProjection.modelMatrix * vec4f(tangent.xyz, 0.0)).xyz), tangent.w * sign(determinant(mat3x3f(pbrProjection.modelMatrix[0].xyz, pbrProjection.modelMatrix[1].xyz, pbrProjection.modelMatrix[2].xyz))));
|
|
148
|
+
#endif
|
|
149
|
+
outputs.position = pbrProjection.modelViewProjectionMatrix * position;
|
|
150
|
+
#ifdef PBR_LIGHTMAP_PASS
|
|
151
|
+
let atlasUV = (lightmapBake.uvTransform * vec3f(select(textureCoordinates, secondTextureCoordinates, lightmapBake.uvSet == 1), 1.0)).xy;
|
|
152
|
+
outputs.position = vec4f(atlasUV.x * 2.0 - 1.0, 1.0 - atlasUV.y * 2.0, 0.0, 1.0);
|
|
153
|
+
#endif
|
|
154
|
+
${hooks.vertexOutput ?? ''}
|
|
155
|
+
outputs.pbrPosition = worldPosition.xyz / worldPosition.w;
|
|
156
|
+
outputs.pbrUV0 = textureCoordinates;
|
|
157
|
+
outputs.pbrUV1 = secondTextureCoordinates;
|
|
158
|
+
outputs.pbrNormal = normal;
|
|
159
|
+
outputs.pbrColor = vertexColor;
|
|
160
|
+
#ifdef HAS_TANGENTS
|
|
161
|
+
outputs.pbrTangent = tangent;
|
|
162
|
+
#endif
|
|
163
|
+
return outputs;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
${hooks.fragmentDeclarations ?? ''}
|
|
167
|
+
@fragment
|
|
168
|
+
fn fragmentMain(inputs: PBRModelFragmentInputs, @builtin(front_facing) frontFacing: bool)
|
|
169
|
+
${
|
|
170
|
+
hooks.fragmentReturnType ??
|
|
171
|
+
`#ifndef PBR_SHADOW_PASS
|
|
172
|
+
-> @location(0) vec4f
|
|
173
|
+
#endif`
|
|
174
|
+
}
|
|
175
|
+
{
|
|
176
|
+
#ifdef DOUBLE_SIDED
|
|
177
|
+
pbr_frontFacing = frontFacing;
|
|
178
|
+
#endif
|
|
179
|
+
fragmentInputs.pbr_vPosition = inputs.pbrPosition;
|
|
180
|
+
fragmentInputs.pbr_vUV0 = inputs.pbrUV0;
|
|
181
|
+
fragmentInputs.pbr_vUV1 = inputs.pbrUV1;
|
|
182
|
+
fragmentInputs.pbr_vNormal = inputs.pbrNormal;
|
|
183
|
+
#ifdef HAS_TANGENTS
|
|
184
|
+
let tangent = normalize(inputs.pbrTangent.xyz);
|
|
185
|
+
let bitangent = normalize(cross(inputs.pbrNormal, tangent)) * inputs.pbrTangent.w;
|
|
186
|
+
fragmentInputs.pbr_vTBN = mat3x3f(tangent, bitangent, inputs.pbrNormal);
|
|
187
|
+
#endif
|
|
188
|
+
${
|
|
189
|
+
hooks.fragmentBody ??
|
|
190
|
+
`
|
|
191
|
+
#ifdef PBR_LIGHTMAP_PASS
|
|
192
|
+
pbr_discardMaskedFragment(inputs.pbrColor);
|
|
193
|
+
var directionalVisibility = 1.0;
|
|
194
|
+
var pointVisibility = vec4f(1.0);
|
|
195
|
+
var spotVisibility = vec4f(1.0);
|
|
196
|
+
let normal = normalize(inputs.pbrNormal);
|
|
197
|
+
#ifdef PBR_RECEIVE_SHADOWS
|
|
198
|
+
if (pbrShadow.enabled != 0) {
|
|
199
|
+
let viewDepth = -(pbrShadow.viewMatrix * vec4f(inputs.pbrPosition, 1.0)).z;
|
|
200
|
+
directionalVisibility = shadow_getDirectionalFactor(inputs.pbrPosition, normal, viewDepth);
|
|
201
|
+
for (var index = 0; index < 4; index++) {
|
|
202
|
+
pointVisibility[index] = shadow_getPointFactor(index, inputs.pbrPosition, normal);
|
|
203
|
+
spotVisibility[index] = shadow_getSpotFactor(index, inputs.pbrPosition, normal);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
#endif
|
|
207
|
+
return vec4f(lightmap_getIrradiance(inputs.pbrPosition, normal, directionalVisibility, pointVisibility, spotVisibility), lightmapBake.chartId);
|
|
208
|
+
#else
|
|
209
|
+
#ifdef PBR_SHADOW_PASS
|
|
210
|
+
pbr_discardMaskedFragment(inputs.pbrColor);
|
|
211
|
+
#else
|
|
212
|
+
#ifdef PBR_SURFACE_PASS
|
|
213
|
+
pbr_discardMaskedFragment(inputs.pbrColor);
|
|
214
|
+
return pbrSurface_getColor();
|
|
215
|
+
#else
|
|
216
|
+
#ifdef DEBUG_NORMALS
|
|
217
|
+
return vec4f(normalize(inputs.pbrNormal) * 0.5 + vec3f(0.5), 1.0);
|
|
218
|
+
#else
|
|
219
|
+
#ifdef DEBUG_DEPTH
|
|
220
|
+
return vec4f(vec3f(inputs.position.z), 1.0);
|
|
221
|
+
#else
|
|
222
|
+
#ifdef PBR_RECEIVE_AMBIENT_OCCLUSION
|
|
223
|
+
pbrAmbientOcclusion_setFactor(inputs.position.xy);
|
|
224
|
+
#endif
|
|
225
|
+
#ifdef PBR_RECEIVE_SHADOWS
|
|
226
|
+
#ifdef HAS_NORMALS
|
|
227
|
+
let shadowNormal = inputs.pbrNormal;
|
|
228
|
+
#else
|
|
229
|
+
let shadowNormal = normalize(cross(dpdy(inputs.pbrPosition), dpdx(inputs.pbrPosition)));
|
|
230
|
+
#endif
|
|
231
|
+
#ifdef PBR_RECEIVE_CONTACT_SHADOWS
|
|
232
|
+
return pbrShadow_filterColorWithDirectionalContact(inputs.pbrColor, inputs.pbrPosition, shadowNormal, pbrContactShadow_getFactor(inputs.position.xy));
|
|
233
|
+
#else
|
|
234
|
+
return pbrShadow_filterColor(inputs.pbrColor, inputs.pbrPosition, shadowNormal);
|
|
235
|
+
#endif
|
|
236
|
+
#else
|
|
237
|
+
#ifdef PBR_RECEIVE_CONTACT_SHADOWS
|
|
238
|
+
return pbrContactShadow_filterColor(inputs.pbrColor, inputs.position.xy);
|
|
239
|
+
#else
|
|
240
|
+
return pbr_filterColor(inputs.pbrColor);
|
|
241
|
+
#endif
|
|
242
|
+
#endif
|
|
243
|
+
#endif
|
|
244
|
+
#endif
|
|
245
|
+
#endif
|
|
246
|
+
#endif
|
|
247
|
+
#endif
|
|
248
|
+
`
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
`;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export const PBR_MODEL_WGSL_SHADER = /* @__PURE__ */ getPBRModelWGSL();
|
|
255
|
+
|
|
256
|
+
const PBR_VERTEX_SHADER = /* glsl */ `#version 300 es
|
|
257
|
+
in vec3 positions;
|
|
258
|
+
#ifdef HAS_NORMALS
|
|
259
|
+
in vec3 normals;
|
|
260
|
+
#endif
|
|
261
|
+
#ifdef HAS_TANGENTS
|
|
262
|
+
in vec4 TANGENT;
|
|
263
|
+
#endif
|
|
264
|
+
#ifdef HAS_UV
|
|
265
|
+
in vec2 texCoords;
|
|
266
|
+
#endif
|
|
267
|
+
#ifdef HAS_UV_1
|
|
268
|
+
in vec2 texCoords1;
|
|
269
|
+
#endif
|
|
270
|
+
#ifdef HAS_SKIN
|
|
271
|
+
in uvec4 JOINTS_0;
|
|
272
|
+
in vec4 WEIGHTS_0;
|
|
273
|
+
#endif
|
|
274
|
+
#ifdef HAS_COLORS
|
|
275
|
+
#ifdef HAS_RGBA_COLORS
|
|
276
|
+
in vec4 colors;
|
|
277
|
+
#else
|
|
278
|
+
in vec3 colors;
|
|
279
|
+
#endif
|
|
280
|
+
#endif
|
|
281
|
+
#ifdef HAS_INSTANCING
|
|
282
|
+
in vec4 instanceModelMatrixCol0;
|
|
283
|
+
in vec4 instanceModelMatrixCol1;
|
|
284
|
+
in vec4 instanceModelMatrixCol2;
|
|
285
|
+
in vec4 instanceModelMatrixCol3;
|
|
286
|
+
#endif
|
|
287
|
+
out vec4 sceneVertexColor;
|
|
288
|
+
|
|
289
|
+
void main(void) {
|
|
290
|
+
vec4 position = vec4(positions, 1.0);
|
|
291
|
+
vec4 normal = vec4(0.0, 0.0, 1.0, 0.0);
|
|
292
|
+
vec4 tangent = vec4(1.0, 0.0, 0.0, 1.0);
|
|
293
|
+
vec2 textureCoordinates = vec2(0.0);
|
|
294
|
+
vec2 secondTextureCoordinates = vec2(0.0);
|
|
295
|
+
sceneVertexColor = vec4(1.0);
|
|
296
|
+
|
|
297
|
+
#ifdef HAS_NORMALS
|
|
298
|
+
normal = vec4(normals, 0.0);
|
|
299
|
+
#endif
|
|
300
|
+
#ifdef HAS_TANGENTS
|
|
301
|
+
tangent = TANGENT;
|
|
302
|
+
#endif
|
|
303
|
+
#ifdef HAS_UV
|
|
304
|
+
textureCoordinates = texCoords;
|
|
305
|
+
#endif
|
|
306
|
+
#ifdef HAS_UV_1
|
|
307
|
+
secondTextureCoordinates = texCoords1;
|
|
308
|
+
#endif
|
|
309
|
+
#ifdef HAS_COLORS
|
|
310
|
+
#ifdef HAS_RGBA_COLORS
|
|
311
|
+
sceneVertexColor = colors;
|
|
312
|
+
#else
|
|
313
|
+
sceneVertexColor = vec4(colors, 1.0);
|
|
314
|
+
#endif
|
|
315
|
+
#endif
|
|
316
|
+
|
|
317
|
+
#ifdef HAS_SKIN
|
|
318
|
+
mat4 skinMatrix = getSkinMatrix(WEIGHTS_0, JOINTS_0);
|
|
319
|
+
position = skinMatrix * position;
|
|
320
|
+
normal = vec4(transpose(inverse(mat3(skinMatrix))) * normal.xyz, 0.0);
|
|
321
|
+
tangent = vec4((skinMatrix * vec4(tangent.xyz, 0.0)).xyz, tangent.w * sign(determinant(mat3(skinMatrix))));
|
|
322
|
+
#endif
|
|
323
|
+
|
|
324
|
+
#ifdef HAS_INSTANCING
|
|
325
|
+
mat4 instanceMatrix = mat4(
|
|
326
|
+
instanceModelMatrixCol0,
|
|
327
|
+
instanceModelMatrixCol1,
|
|
328
|
+
instanceModelMatrixCol2,
|
|
329
|
+
instanceModelMatrixCol3
|
|
330
|
+
);
|
|
331
|
+
position = instanceMatrix * position;
|
|
332
|
+
normal = vec4(normalize(transpose(inverse(mat3(instanceMatrix))) * normal.xyz), 0.0);
|
|
333
|
+
tangent = vec4(normalize(mat3(instanceMatrix) * tangent.xyz), tangent.w * sign(determinant(mat3(instanceMatrix))));
|
|
334
|
+
#endif
|
|
335
|
+
|
|
336
|
+
tangent.w *= sign(determinant(mat3(pbrProjection.modelMatrix)));
|
|
337
|
+
pbr_setPositionNormalTangentUV(
|
|
338
|
+
position,
|
|
339
|
+
normal,
|
|
340
|
+
tangent,
|
|
341
|
+
textureCoordinates,
|
|
342
|
+
secondTextureCoordinates
|
|
343
|
+
);
|
|
344
|
+
gl_Position = pbrProjection.modelViewProjectionMatrix * position;
|
|
345
|
+
#ifdef PBR_LIGHTMAP_PASS
|
|
346
|
+
vec2 atlasUV = (lightmapBake.uvTransform * vec3(lightmapBake.uvSet == 1 ? secondTextureCoordinates : textureCoordinates, 1.0)).xy;
|
|
347
|
+
gl_Position = vec4(atlasUV * 2.0 - 1.0, 0.0, 1.0);
|
|
348
|
+
#endif
|
|
349
|
+
#ifdef PBR_SHADOW_PASS
|
|
350
|
+
// ShadowMapRenderer light projections use [0, 1] depth on both backends.
|
|
351
|
+
gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;
|
|
352
|
+
#endif
|
|
353
|
+
}
|
|
354
|
+
`;
|
|
355
|
+
|
|
356
|
+
const PBR_FRAGMENT_SHADER = /* glsl */ `#version 300 es
|
|
357
|
+
in vec4 sceneVertexColor;
|
|
358
|
+
#ifndef PBR_SHADOW_PASS
|
|
359
|
+
out vec4 fragmentColor;
|
|
360
|
+
#endif
|
|
361
|
+
|
|
362
|
+
void main(void) {
|
|
363
|
+
#ifdef PBR_LIGHTMAP_PASS
|
|
364
|
+
pbr_discardMaskedFragment(sceneVertexColor);
|
|
365
|
+
#ifdef HAS_TANGENTS
|
|
366
|
+
vec3 normal = normalize(pbr_vTBN[2]);
|
|
367
|
+
#else
|
|
368
|
+
vec3 normal = normalize(pbr_vNormal);
|
|
369
|
+
#endif
|
|
370
|
+
float directionalVisibility = 1.0;
|
|
371
|
+
vec4 pointVisibility = vec4(1.0);
|
|
372
|
+
vec4 spotVisibility = vec4(1.0);
|
|
373
|
+
#ifdef PBR_RECEIVE_SHADOWS
|
|
374
|
+
if (pbrShadow.enabled != 0) {
|
|
375
|
+
float viewDepth = -(pbrShadow.viewMatrix * vec4(pbr_vPosition, 1.0)).z;
|
|
376
|
+
directionalVisibility = shadow_getDirectionalFactor(pbr_vPosition, normal, viewDepth);
|
|
377
|
+
for (int index = 0; index < 4; index++) {
|
|
378
|
+
pointVisibility[index] = shadow_getPointFactor(index, pbr_vPosition, normal);
|
|
379
|
+
spotVisibility[index] = shadow_getSpotFactor(index, pbr_vPosition, normal);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
#endif
|
|
383
|
+
fragmentColor = vec4(lightmap_getIrradiance(pbr_vPosition, normal, directionalVisibility, pointVisibility, spotVisibility), lightmapBake.chartId);
|
|
384
|
+
#else
|
|
385
|
+
#ifdef PBR_SHADOW_PASS
|
|
386
|
+
pbr_discardMaskedFragment(sceneVertexColor);
|
|
387
|
+
#else
|
|
388
|
+
#ifdef PBR_SURFACE_PASS
|
|
389
|
+
pbr_discardMaskedFragment(sceneVertexColor);
|
|
390
|
+
fragmentColor = pbrSurface_getColor();
|
|
391
|
+
#else
|
|
392
|
+
#ifdef DEBUG_NORMALS
|
|
393
|
+
#ifdef HAS_TANGENTS
|
|
394
|
+
fragmentColor = vec4(normalize(pbr_vTBN[2]) * 0.5 + 0.5, 1.0);
|
|
395
|
+
#else
|
|
396
|
+
#ifdef HAS_NORMALS
|
|
397
|
+
fragmentColor = vec4(normalize(pbr_vNormal) * 0.5 + 0.5, 1.0);
|
|
398
|
+
#else
|
|
399
|
+
fragmentColor = vec4(0.5, 0.5, 1.0, 1.0);
|
|
400
|
+
#endif
|
|
401
|
+
#endif
|
|
402
|
+
#else
|
|
403
|
+
#ifdef DEBUG_DEPTH
|
|
404
|
+
fragmentColor = vec4(vec3(gl_FragCoord.z), 1.0);
|
|
405
|
+
#else
|
|
406
|
+
#ifdef PBR_RECEIVE_AMBIENT_OCCLUSION
|
|
407
|
+
pbrAmbientOcclusion_setFactor(gl_FragCoord.xy);
|
|
408
|
+
#endif
|
|
409
|
+
#ifdef PBR_RECEIVE_SHADOWS
|
|
410
|
+
#ifdef HAS_NORMALS
|
|
411
|
+
#ifdef HAS_TANGENTS
|
|
412
|
+
vec3 shadowNormal = pbr_vTBN[2];
|
|
413
|
+
#else
|
|
414
|
+
vec3 shadowNormal = pbr_vNormal;
|
|
415
|
+
#endif
|
|
416
|
+
#else
|
|
417
|
+
vec3 shadowNormal = normalize(cross(dFdx(pbr_vPosition), dFdy(pbr_vPosition)));
|
|
418
|
+
#endif
|
|
419
|
+
#ifdef PBR_RECEIVE_CONTACT_SHADOWS
|
|
420
|
+
fragmentColor = pbrShadow_filterColorWithDirectionalContact(sceneVertexColor, pbr_vPosition, shadowNormal, pbrContactShadow_getFactor(gl_FragCoord.xy));
|
|
421
|
+
#else
|
|
422
|
+
fragmentColor = pbrShadow_filterColor(sceneVertexColor, pbr_vPosition, shadowNormal);
|
|
423
|
+
#endif
|
|
424
|
+
#else
|
|
425
|
+
#ifdef PBR_RECEIVE_CONTACT_SHADOWS
|
|
426
|
+
fragmentColor = pbrContactShadow_filterColor(sceneVertexColor, gl_FragCoord.xy);
|
|
427
|
+
#else
|
|
428
|
+
fragmentColor = pbr_filterColor(sceneVertexColor);
|
|
429
|
+
#endif
|
|
430
|
+
#endif
|
|
431
|
+
#endif
|
|
432
|
+
#endif
|
|
433
|
+
#endif
|
|
434
|
+
#endif
|
|
435
|
+
#endif
|
|
436
|
+
}
|
|
437
|
+
`;
|
|
438
|
+
|
|
439
|
+
/** Model properties accepted by the shared physically based model factory. */
|
|
440
|
+
export type CreatePBRModelOptions = ModelProps & {
|
|
441
|
+
/** Shared PBR material that owns the canonical material binding group. */
|
|
442
|
+
material: Material;
|
|
443
|
+
/** Include shadow sampling for directional, point and spot direct lights. */
|
|
444
|
+
receiveShadows?: boolean;
|
|
445
|
+
/** Include screen-space visibility for the first directional light. */
|
|
446
|
+
receiveContactShadows?: boolean;
|
|
447
|
+
/** Attenuate indirect lighting using screen-space ambient visibility. */
|
|
448
|
+
receiveAmbientOcclusion?: boolean;
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
/** Creates a standalone WebGPU/WebGL PBR model with a borrowed material. */
|
|
452
|
+
export function createPBRModel(device: Device, options: CreatePBRModelOptions): Model {
|
|
453
|
+
const {
|
|
454
|
+
receiveShadows = false,
|
|
455
|
+
receiveContactShadows = false,
|
|
456
|
+
receiveAmbientOcclusion = false,
|
|
457
|
+
...modelOptions
|
|
458
|
+
} = options;
|
|
459
|
+
const shaderModules = [
|
|
460
|
+
pbrScene,
|
|
461
|
+
pbrMaterial,
|
|
462
|
+
...(receiveShadows ? [pbrShadow] : []),
|
|
463
|
+
...(receiveContactShadows ? [pbrContactShadow] : []),
|
|
464
|
+
...(receiveAmbientOcclusion ? [pbrAmbientOcclusion] : []),
|
|
465
|
+
...(options.defines?.['PBR_SURFACE_PASS'] ? [pbrSurface] : []),
|
|
466
|
+
...(options.modules || [])
|
|
467
|
+
];
|
|
468
|
+
const modules = shaderModules.filter(
|
|
469
|
+
(module, moduleIndex) =>
|
|
470
|
+
shaderModules.findIndex(candidate => candidate.name === module.name) === moduleIndex
|
|
471
|
+
);
|
|
472
|
+
const geometryDefines = getPBRGeometryDefines(options.geometry);
|
|
473
|
+
const hasSkin = Boolean(options.defines?.['HAS_SKIN'] ?? geometryDefines['HAS_SKIN']);
|
|
474
|
+
if (hasSkin && !modules.some(module => module.name === skin.name)) {
|
|
475
|
+
modules.push(skin);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const model = new Model(device, {
|
|
479
|
+
source: PBR_MODEL_WGSL_SHADER,
|
|
480
|
+
vs: PBR_VERTEX_SHADER,
|
|
481
|
+
fs: PBR_FRAGMENT_SHADER,
|
|
482
|
+
...modelOptions,
|
|
483
|
+
modules,
|
|
484
|
+
defines: {
|
|
485
|
+
USE_SCENE_COLOR_MANAGEMENT: true,
|
|
486
|
+
PBR_RECEIVE_SHADOWS: receiveShadows,
|
|
487
|
+
PBR_RECEIVE_CONTACT_SHADOWS: receiveContactShadows,
|
|
488
|
+
PBR_RECEIVE_AMBIENT_OCCLUSION: receiveAmbientOcclusion,
|
|
489
|
+
...geometryDefines,
|
|
490
|
+
...getPBRTextureDefines(options.material.getResourceBindings()),
|
|
491
|
+
...options.defines
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
if (!options.shaderInputs) {
|
|
495
|
+
const identity = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
496
|
+
model.shaderInputs.setProps({
|
|
497
|
+
pbrProjection: {
|
|
498
|
+
modelMatrix: identity.slice(),
|
|
499
|
+
modelViewProjectionMatrix: identity.slice(),
|
|
500
|
+
normalMatrix: identity.slice(),
|
|
501
|
+
camera: [0, 0, 1]
|
|
502
|
+
},
|
|
503
|
+
pbrScene: {toneMapMode: 0, outputEncoding: 0}
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
return model;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/** Creates a depth-only caster sharing PBR geometry, transforms, skinning and alpha cutouts.
|
|
510
|
+
* Supply the zero-to-one light projection returned by ShadowMapRenderer on both backends.
|
|
511
|
+
* The material is borrowed; caster and visible models may share it.
|
|
512
|
+
*/
|
|
513
|
+
export function createPBRShadowModel(device: Device, options: CreatePBRModelOptions): Model {
|
|
514
|
+
return createPBRModel(device, {
|
|
515
|
+
colorAttachmentFormats: [],
|
|
516
|
+
depthStencilAttachmentFormat: 'depth32float',
|
|
517
|
+
...options,
|
|
518
|
+
receiveShadows: false,
|
|
519
|
+
receiveContactShadows: false,
|
|
520
|
+
receiveAmbientOcclusion: false,
|
|
521
|
+
defines: {...options.defines, PBR_SHADOW_PASS: true},
|
|
522
|
+
parameters: {depthWriteEnabled: true, depthCompare: 'less-equal', ...options.parameters}
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** Creates a camera-space depth/normal model with the shared PBR vertex and alpha-cutout paths. */
|
|
527
|
+
export function createPBRSurfaceModel(device: Device, options: CreatePBRModelOptions): Model {
|
|
528
|
+
return createPBRModel(device, {
|
|
529
|
+
colorAttachmentFormats: ['rgba8unorm'],
|
|
530
|
+
depthStencilAttachmentFormat: 'depth32float',
|
|
531
|
+
...options,
|
|
532
|
+
receiveShadows: false,
|
|
533
|
+
receiveContactShadows: false,
|
|
534
|
+
receiveAmbientOcclusion: false,
|
|
535
|
+
defines: {...options.defines, PBR_SHADOW_PASS: false, PBR_SURFACE_PASS: true},
|
|
536
|
+
parameters: {depthWriteEnabled: true, depthCompare: 'less-equal', ...options.parameters}
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/** Returns shader features implied by canonical CPU geometry attributes. */
|
|
541
|
+
export function getPBRGeometryDefines(geometry: ModelProps['geometry']): Record<string, boolean> {
|
|
542
|
+
const attributes = geometry && 'attributes' in geometry ? geometry.attributes : {};
|
|
543
|
+
const colors = attributes['COLOR_0'] || attributes['colors'];
|
|
544
|
+
|
|
545
|
+
return {
|
|
546
|
+
HAS_NORMALS: Boolean(attributes['NORMAL'] || attributes['normals']),
|
|
547
|
+
HAS_TANGENTS: Boolean(attributes['TANGENT'] || attributes['tangents']),
|
|
548
|
+
HAS_UV: Boolean(attributes['TEXCOORD_0'] || attributes['texCoords']),
|
|
549
|
+
HAS_UV_1: Boolean(attributes['TEXCOORD_1'] || attributes['texCoords1']),
|
|
550
|
+
HAS_SKIN: Boolean(attributes['JOINTS_0'] && attributes['WEIGHTS_0']),
|
|
551
|
+
HAS_COLORS: Boolean(colors),
|
|
552
|
+
HAS_RGBA_COLORS: Boolean(colors && 'size' in colors && colors.size === 4)
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/** Returns feature-specialized shader defines for texture bindings present on one PBR material. */
|
|
557
|
+
export function getPBRTextureDefines(bindings: Record<string, unknown>): Record<string, boolean> {
|
|
558
|
+
return {
|
|
559
|
+
HAS_BASECOLORMAP: Boolean(bindings['pbr_baseColorSampler']),
|
|
560
|
+
HAS_NORMALMAP: Boolean(bindings['pbr_normalSampler']),
|
|
561
|
+
HAS_LIGHTMAP: Boolean(bindings['pbr_lightmapSampler']),
|
|
562
|
+
HAS_EMISSIVEMAP: Boolean(bindings['pbr_emissiveSampler']),
|
|
563
|
+
HAS_METALROUGHNESSMAP: Boolean(bindings['pbr_metallicRoughnessSampler']),
|
|
564
|
+
HAS_OCCLUSIONMAP: Boolean(bindings['pbr_occlusionSampler']),
|
|
565
|
+
HAS_SPECULARCOLORMAP: Boolean(bindings['pbr_specularColorSampler']),
|
|
566
|
+
HAS_SPECULARINTENSITYMAP: Boolean(bindings['pbr_specularIntensitySampler']),
|
|
567
|
+
HAS_TRANSMISSIONMAP: Boolean(bindings['pbr_transmissionSampler']),
|
|
568
|
+
HAS_THICKNESSMAP: Boolean(bindings['pbr_thicknessSampler']),
|
|
569
|
+
HAS_CLEARCOATMAP: Boolean(bindings['pbr_clearcoatSampler']),
|
|
570
|
+
HAS_CLEARCOATROUGHNESSMAP: Boolean(bindings['pbr_clearcoatRoughnessSampler']),
|
|
571
|
+
HAS_CLEARCOATNORMALMAP: Boolean(bindings['pbr_clearcoatNormalSampler']),
|
|
572
|
+
HAS_SHEENCOLORMAP: Boolean(bindings['pbr_sheenColorSampler']),
|
|
573
|
+
HAS_SHEENROUGHNESSMAP: Boolean(bindings['pbr_sheenRoughnessSampler']),
|
|
574
|
+
HAS_IRIDESCENCEMAP: Boolean(bindings['pbr_iridescenceSampler']),
|
|
575
|
+
HAS_IRIDESCENCETHICKNESSMAP: Boolean(bindings['pbr_iridescenceThicknessSampler']),
|
|
576
|
+
HAS_ANISOTROPYMAP: Boolean(bindings['pbr_anisotropySampler']),
|
|
577
|
+
HAS_BUMPMAP: Boolean(bindings['pbr_bumpSampler']),
|
|
578
|
+
HAS_DIFFUSETRANSMISSIONMAP: Boolean(bindings['pbr_diffuseTransmissionSampler']),
|
|
579
|
+
HAS_DIFFUSETRANSMISSIONCOLORMAP: Boolean(bindings['pbr_diffuseTransmissionColorSampler']),
|
|
580
|
+
HAS_MULTISCATTERCOLORMAP: Boolean(bindings['pbr_multiscatterColorSampler'])
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/** Options for a UV-space diffuse irradiance model. Supply normals and nonoverlapping atlas UVs. */
|
|
585
|
+
export type CreatePBRLightmapModelOptions = CreatePBRModelOptions;
|
|
586
|
+
|
|
587
|
+
/** Creates a borrowed-material bake model; output is linear irradiance E with chart ID in alpha. */
|
|
588
|
+
export function createPBRLightmapModel(
|
|
589
|
+
device: Device,
|
|
590
|
+
options: CreatePBRLightmapModelOptions
|
|
591
|
+
): Model {
|
|
592
|
+
const features = {...getPBRGeometryDefines(options.geometry), ...options.defines};
|
|
593
|
+
// Bake surfaces require geometric normals and the selected atlas UV attributes.
|
|
594
|
+
assert(features['HAS_NORMALS'] && (features['HAS_UV'] || features['HAS_UV_1']));
|
|
595
|
+
return createPBRModel(device, {
|
|
596
|
+
colorAttachmentFormats: ['rgba16float'],
|
|
597
|
+
...options,
|
|
598
|
+
modules: [lightmapBake, ...(options.modules || [])],
|
|
599
|
+
receiveContactShadows: false,
|
|
600
|
+
receiveAmbientOcclusion: false,
|
|
601
|
+
defines: {...options.defines, HAS_LIGHTMAP: false, USE_IBL: false, PBR_LIGHTMAP_PASS: true},
|
|
602
|
+
parameters: {
|
|
603
|
+
...options.parameters,
|
|
604
|
+
depthWriteEnabled: false,
|
|
605
|
+
cullMode: 'none'
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
}
|