@aura3d/rendering 1.4.0 → 1.4.2
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/package.json +38 -4
- package/AGENTS.md +0 -46
- package/src/ArchitecturalLightingFixtures.ts +0 -241
- package/src/ArchitecturalMaterialCatalog.ts +0 -130
- package/src/ArchitecturalMeasurementFixtures.ts +0 -155
- package/src/BRDFLut.ts +0 -47
- package/src/CameraFraming.ts +0 -200
- package/src/CanonicalSceneFixtures.ts +0 -95
- package/src/CascadedShadowMaps.ts +0 -449
- package/src/ColorManagement.ts +0 -127
- package/src/CullingFixtures.ts +0 -280
- package/src/DecalGeometry.ts +0 -26
- package/src/DepthPass.ts +0 -110
- package/src/EnvironmentBackgroundPass.ts +0 -194
- package/src/EnvironmentBackgroundResources.ts +0 -309
- package/src/EnvironmentLighting.ts +0 -96
- package/src/EnvironmentMapResources.ts +0 -1026
- package/src/EnvironmentPipeline.ts +0 -69
- package/src/EnvironmentPlatform.ts +0 -1306
- package/src/EnvironmentPreset.ts +0 -155
- package/src/Exposure.ts +0 -66
- package/src/ExternalParityRenderPreset.ts +0 -725
- package/src/ForwardPass.ts +0 -1668
- package/src/FrameVisualMetrics.ts +0 -301
- package/src/Geometry.ts +0 -461
- package/src/GeometryPrimitives.ts +0 -2
- package/src/HDRRenderPipeline.ts +0 -94
- package/src/IBL.ts +0 -86
- package/src/IndexBuffer.ts +0 -57
- package/src/InstancedPBRMaterial.ts +0 -186
- package/src/InstancedUnlitMaterial.ts +0 -60
- package/src/Instancing.ts +0 -19
- package/src/LightCollector.ts +0 -82
- package/src/LightUniforms.ts +0 -47
- package/src/LightingDebug.ts +0 -80
- package/src/LightingDefaults.ts +0 -79
- package/src/LightingRig.ts +0 -301
- package/src/LineGeometry.ts +0 -2
- package/src/Material.ts +0 -209
- package/src/MaterialBinding.ts +0 -131
- package/src/MaterialInstance.ts +0 -59
- package/src/MaterialPresets.ts +0 -272
- package/src/MorphTarget.ts +0 -124
- package/src/MorphTargetPlan.ts +0 -178
- package/src/MorphUnlitMaterial.ts +0 -60
- package/src/NormalMappedPBRMaterial.ts +0 -188
- package/src/OceanFixtures.ts +0 -318
- package/src/PBRLightingDefaults.ts +0 -12
- package/src/PBRMaterial.ts +0 -442
- package/src/PMREM.ts +0 -53
- package/src/PbrReference.ts +0 -681
- package/src/PostProcessPass.ts +0 -2507
- package/src/ProceduralTextureFixtures.ts +0 -393
- package/src/ProductTurntableFixtures.ts +0 -815
- package/src/Raycaster.ts +0 -21
- package/src/ReflectionProbe.ts +0 -13
- package/src/ReflectionSurfaces.ts +0 -219
- package/src/RenderBackend.ts +0 -43
- package/src/RenderDebugViews.ts +0 -138
- package/src/RenderDevice.ts +0 -836
- package/src/RenderGraph.ts +0 -194
- package/src/RenderPass.ts +0 -27
- package/src/RenderPipeline.ts +0 -101
- package/src/RenderQueue.ts +0 -12
- package/src/RenderState.ts +0 -12
- package/src/Renderer.ts +0 -2631
- package/src/RendererDebugOverlay.ts +0 -80
- package/src/RendererFeatureGates.ts +0 -150
- package/src/RendererPostprocessPlan.ts +0 -279
- package/src/RendererTiming.ts +0 -251
- package/src/RendererVisualPipelineReport.ts +0 -836
- package/src/ResourceLifecycle.ts +0 -21
- package/src/Sampler.ts +0 -37
- package/src/SceneOptimization.ts +0 -518
- package/src/ShaderChunks.ts +0 -393
- package/src/ShaderLibrary.ts +0 -2766
- package/src/ShaderModule.ts +0 -36
- package/src/ShaderPreprocessor.ts +0 -260
- package/src/ShaderReflection.ts +0 -201
- package/src/ShadowMap.ts +0 -237
- package/src/ShadowPass.ts +0 -198
- package/src/ShadowProjection.ts +0 -115
- package/src/SkinnedLitMaterial.ts +0 -397
- package/src/SkinnedUnlitMaterial.ts +0 -63
- package/src/SkinningBounds.ts +0 -131
- package/src/SpaceEnvironmentFixtures.ts +0 -200
- package/src/SpriteGeometry.ts +0 -21
- package/src/StereoCameraRig.ts +0 -127
- package/src/StereoEffects.ts +0 -274
- package/src/TerrainFixtures.ts +0 -180
- package/src/Texture.ts +0 -307
- package/src/TextureBinding.ts +0 -91
- package/src/TexturedPBRMaterial.ts +0 -1112
- package/src/TexturedUnlitMaterial.ts +0 -67
- package/src/ToneMapping.ts +0 -105
- package/src/UniformBinder.ts +0 -16
- package/src/UniformLayout.ts +0 -129
- package/src/UnlitMaterial.ts +0 -75
- package/src/VegetationFixtures.ts +0 -314
- package/src/VertexBuffer.ts +0 -120
- package/src/VertexFormat.ts +0 -171
- package/src/VoxelWorldFixtures.ts +0 -280
- package/src/WeatherFixtures.ts +0 -223
- package/src/WebGL2Device.ts +0 -2280
- package/src/WebGL2StateCache.ts +0 -330
- package/src/WebGPUDevice.ts +0 -2877
- package/src/WebGPURenderToTextureProof.ts +0 -139
- package/src/advanced-runtime/AdvancedRenderer.ts +0 -73
- package/src/advanced-runtime/index.ts +0 -9
- package/src/animation/AnimationMaterialStyle.ts +0 -32
- package/src/animation/AnimationRenderPreset.ts +0 -45
- package/src/animation/AnimationToonMaterial.ts +0 -387
- package/src/animation/AnimationVisualQuality.ts +0 -71
- package/src/animation/applyAnimationRenderPreset.ts +0 -180
- package/src/animation/index.ts +0 -5
- package/src/cinematic/BloomPass.ts +0 -26
- package/src/cinematic/CinematicDepthComposition.ts +0 -30
- package/src/cinematic/CinematicEvidence.ts +0 -123
- package/src/cinematic/CinematicLightingRig.ts +0 -144
- package/src/cinematic/CinematicMaterialPresets.ts +0 -130
- package/src/cinematic/CinematicPostProcess.ts +0 -83
- package/src/cinematic/DepthHazePass.ts +0 -30
- package/src/cinematic/EmissivePracticalLightSystem.ts +0 -45
- package/src/cinematic/FilmGrainPass.ts +0 -24
- package/src/cinematic/FogVolumeSystem.ts +0 -30
- package/src/cinematic/GlowCardSystem.ts +0 -44
- package/src/cinematic/RainParticleSystem.ts +0 -151
- package/src/cinematic/VignettePass.ts +0 -25
- package/src/cinematic/WetReflectionApproximation.ts +0 -27
- package/src/cinematic/index.ts +0 -14
- package/src/effects/CollisionModule.ts +0 -64
- package/src/effects/ColorModule.ts +0 -60
- package/src/effects/ForceModule.ts +0 -25
- package/src/effects/GPUParticleBackend.ts +0 -569
- package/src/effects/Particle.ts +0 -108
- package/src/effects/ParticleDiagnostics.ts +0 -224
- package/src/effects/ParticleEffectPresets.ts +0 -147
- package/src/effects/ParticleEmitter.ts +0 -326
- package/src/effects/ParticleModule.ts +0 -26
- package/src/effects/ParticleRenderPass.ts +0 -117
- package/src/effects/ParticleRenderer.ts +0 -132
- package/src/effects/ParticleSystem.ts +0 -333
- package/src/effects/SizeModule.ts +0 -56
- package/src/effects/TrailModule.ts +0 -72
- package/src/effects/VelocityModule.ts +0 -75
- package/src/effects/index.ts +0 -15
- package/src/index.ts +0 -925
- package/src/materials/AlphaSorting.ts +0 -30
- package/src/materials/MaterialExtensions.ts +0 -42
- package/src/materials/PhysicalMaterial.ts +0 -133
- package/src/materials/TransmissionPass.ts +0 -51
- package/src/performance/BVH.ts +0 -18
- package/src/performance/Batcher.ts +0 -2
- package/src/performance/FrustumCuller.ts +0 -9
- package/src/performance/LOD.ts +0 -20
- package/src/performance/Octree.ts +0 -10
- package/src/performance/RenderItemSorting.ts +0 -170
- package/src/performance/RendererStats.ts +0 -43
- package/src/performance/ResourceBudget.ts +0 -44
- package/src/postprocess/BloomPass.ts +0 -28
- package/src/postprocess/CinematicDiagnostics.ts +0 -642
- package/src/postprocess/ColorGradingPass.ts +0 -24
- package/src/postprocess/DepthOfFieldPass.ts +0 -12
- package/src/postprocess/EffectComposer.ts +0 -349
- package/src/postprocess/SSAOPass.ts +0 -20
- package/src/postprocess/index.ts +0 -34
- package/src/production-runtime/AnimationControlsPipeline.ts +0 -77
- package/src/production-runtime/PBRHDRPipeline.ts +0 -381
- package/src/production-runtime/ProductionEffectsPipeline.ts +0 -85
- package/src/production-runtime/ProductionRendererTypes.ts +0 -136
- package/src/production-runtime/ProductionRuntimeRenderer.ts +0 -172
- package/src/production-runtime/ProductionWebGL2Renderer.ts +0 -313
- package/src/production-runtime/ProductionWebGPURenderer.ts +0 -396
- package/src/production-runtime/TransmissionBackdropCapture.ts +0 -130
- package/src/production-runtime/animation/MorphTargetRenderer.ts +0 -2
- package/src/production-runtime/animation/SkinningRenderer.ts +0 -2
- package/src/production-runtime/backends/RendererBackend.ts +0 -17
- package/src/production-runtime/backends/WebGL2RendererBackend.ts +0 -30
- package/src/production-runtime/backends/WebGPURendererBackend.ts +0 -24
- package/src/production-runtime/backends/webgl2/WebGL2Buffer.ts +0 -2
- package/src/production-runtime/backends/webgl2/WebGL2Capabilities.ts +0 -2
- package/src/production-runtime/backends/webgl2/WebGL2RenderTarget.ts +0 -2
- package/src/production-runtime/backends/webgl2/WebGL2Shader.ts +0 -2
- package/src/production-runtime/backends/webgl2/WebGL2StateCache.ts +0 -1
- package/src/production-runtime/backends/webgl2/WebGL2Texture.ts +0 -2
- package/src/production-runtime/backends/webgpu/WebGPUBuffer.ts +0 -2
- package/src/production-runtime/backends/webgpu/WebGPUCapabilities.ts +0 -2
- package/src/production-runtime/backends/webgpu/WebGPURenderTarget.ts +0 -2
- package/src/production-runtime/backends/webgpu/WebGPUShader.ts +0 -2
- package/src/production-runtime/backends/webgpu/WebGPUTexture.ts +0 -2
- package/src/production-runtime/color/ColorManagement.ts +0 -3
- package/src/production-runtime/color/ToneMapping.ts +0 -7
- package/src/production-runtime/diagnostics/FrameCapture.ts +0 -2
- package/src/production-runtime/diagnostics/GPUCapabilities.ts +0 -2
- package/src/production-runtime/diagnostics/RendererStats.ts +0 -2
- package/src/production-runtime/environment/EnvironmentMap.ts +0 -1
- package/src/production-runtime/environment/HDRLoader.ts +0 -57
- package/src/production-runtime/environment/PMREMGenerator.ts +0 -713
- package/src/production-runtime/framegraph/FrameGraph.ts +0 -18
- package/src/production-runtime/framegraph/RenderPass.ts +0 -15
- package/src/production-runtime/geometry/ProjectedDecalGeometry.ts +0 -439
- package/src/production-runtime/index.ts +0 -127
- package/src/production-runtime/lights/LightManager.ts +0 -7
- package/src/production-runtime/lights/ShadowMapRenderer.ts +0 -5
- package/src/production-runtime/materials/GLTFMaterialAdapter.ts +0 -6
- package/src/production-runtime/materials/GLTFPBRMaterialAdapter.ts +0 -2
- package/src/production-runtime/materials/MaterialCompiler.ts +0 -7
- package/src/production-runtime/materials/MaterialTextureBindings.ts +0 -5
- package/src/production-runtime/materials/PBRMaterial.ts +0 -2
- package/src/production-runtime/materials/PBRShaderFeatures.ts +0 -2
- package/src/production-runtime/passes/ContactShadowPass.ts +0 -184
- package/src/production-runtime/passes/DepthPrepass.ts +0 -7
- package/src/production-runtime/passes/OpaquePass.ts +0 -7
- package/src/production-runtime/passes/ShadowPass.ts +0 -7
- package/src/production-runtime/passes/SkyboxPass.ts +0 -7
- package/src/production-runtime/passes/ToneMappingPass.ts +0 -7
- package/src/production-runtime/passes/TransparentPass.ts +0 -7
- package/src/production-runtime/postprocess/BloomPass.ts +0 -51
- package/src/production-runtime/postprocess/ColorGradingPass.ts +0 -49
- package/src/production-runtime/postprocess/DOFPass.ts +0 -47
- package/src/production-runtime/postprocess/FXAAPass.ts +0 -53
- package/src/production-runtime/postprocess/ProductionEffectComposer.ts +0 -78
- package/src/production-runtime/postprocess/ProductionPostProcessTypes.ts +0 -174
- package/src/production-runtime/postprocess/SSAOPass.ts +0 -45
- package/src/production-runtime/resources/GPUBuffer.ts +0 -2
- package/src/production-runtime/resources/GPUTexture.ts +0 -2
- package/src/production-runtime/resources/RenderTarget.ts +0 -3
- package/src/production-runtime/resources/ResourceCache.ts +0 -7
- package/src/production-runtime/scene/Camera.ts +0 -1
- package/src/production-runtime/scene/Lights.ts +0 -2
- package/src/production-runtime/scene/RenderableMesh.ts +0 -2
- package/src/production-runtime/scene/RenderablePrimitive.ts +0 -1
- package/src/production-runtime/scene/RenderableScene.ts +0 -4
- package/src/production-runtime/shaders/ShaderProgramLibrary.ts +0 -7
- package/src/production-runtime/shaders/chunks/brdf.glsl +0 -2
- package/src/production-runtime/shaders/chunks/ibl.glsl +0 -3
- package/src/production-runtime/shaders/chunks/pbr.frag.glsl +0 -47
- package/src/production-runtime/shaders/chunks/pbr.vert.glsl +0 -8
- package/src/production-runtime/shaders/chunks/shadows.glsl +0 -2
- package/src/production-runtime/shaders/wgsl/pbr.wgsl +0 -3
- package/src/production-runtime/shaders/wgsl/postprocess.wgsl +0 -2
- package/src/production-runtime/shaders/wgsl/skybox.wgsl +0 -2
- package/src/shaders/depth.frag.glsl +0 -5
- package/src/shaders/depth.vert.glsl +0 -8
- package/src/shaders/pbr-direct.frag.glsl +0 -687
- package/src/shaders/pbr-direct.vert.glsl +0 -18
- package/src/shaders/unlit.frag.glsl +0 -18
- package/src/shaders/unlit.vert.glsl +0 -13
- package/src/shadows/CascadedShadowPipeline.ts +0 -53
- package/src/shadows/ContactShadows.ts +0 -106
- package/src/shadows/ShadowDebugViews.ts +0 -46
- package/src/threejs-compatibility/InstancingSystem.ts +0 -17
- package/src/threejs-compatibility/LightingSystem.ts +0 -22
- package/src/threejs-compatibility/MaterialSystem.ts +0 -9
- package/src/threejs-compatibility/RenderTargetSystem.ts +0 -35
- package/src/threejs-compatibility/RendererDiagnostics.ts +0 -61
- package/src/threejs-compatibility/SceneRenderer.ts +0 -34
- package/src/threejs-compatibility/ShadowSystem.ts +0 -19
- package/src/threejs-compatibility/TextureSystem.ts +0 -19
- package/src/threejs-compatibility/ThreeCompatRenderer.ts +0 -90
- package/src/threejs-compatibility/TransparencySystem.ts +0 -19
- package/src/threejs-compatibility/index.ts +0 -29
- package/src/threejs-compatibility/performance/BVH.ts +0 -14
- package/src/threejs-compatibility/performance/FrustumCulling.ts +0 -10
- package/src/threejs-compatibility/performance/Instancing.ts +0 -4
- package/src/threejs-compatibility/performance/LODSystem.ts +0 -16
- package/src/threejs-compatibility/performance/OcclusionCulling.ts +0 -6
- package/src/threejs-compatibility/performance/RaycastAcceleration.ts +0 -12
- package/src/threejs-compatibility/performance/RendererProfiler.ts +0 -19
- package/src/threejs-compatibility/performance/TextureStreaming.ts +0 -4
- package/src/threejs-compatibility/performance/index.ts +0 -12
- package/src/threejs-compatibility/postprocess/BloomPass.ts +0 -16
- package/src/threejs-compatibility/postprocess/ColorGradingPass.ts +0 -16
- package/src/threejs-compatibility/postprocess/DepthOfFieldPass.ts +0 -21
- package/src/threejs-compatibility/postprocess/EffectComposer.ts +0 -14
- package/src/threejs-compatibility/postprocess/FXAAPass.ts +0 -15
- package/src/threejs-compatibility/postprocess/MotionBlurPass.ts +0 -20
- package/src/threejs-compatibility/postprocess/OutlinePass.ts +0 -21
- package/src/threejs-compatibility/postprocess/PostProcessTypes.ts +0 -169
- package/src/threejs-compatibility/postprocess/RenderPass.ts +0 -9
- package/src/threejs-compatibility/postprocess/SMAAPass.ts +0 -15
- package/src/threejs-compatibility/postprocess/SSAOPass.ts +0 -21
- package/src/threejs-compatibility/postprocess/ShaderPass.ts +0 -7
- package/src/threejs-compatibility/postprocess/TAAPass.ts +0 -15
- package/src/threejs-compatibility/postprocess/VignettePass.ts +0 -16
- package/src/threejs-compatibility/postprocess/index.ts +0 -15
- package/src/threejs-compatibility/shaders/NodeMaterial.ts +0 -21
- package/src/threejs-compatibility/shaders/RawShaderMaterial.ts +0 -6
- package/src/threejs-compatibility/shaders/ShaderDiagnostics.ts +0 -17
- package/src/threejs-compatibility/shaders/ShaderMaterial.ts +0 -18
- package/src/threejs-compatibility/shaders/ThreeCompatShaderChunks.ts +0 -6
- package/src/threejs-compatibility/shaders/Uniforms.ts +0 -18
- package/src/threejs-compatibility/shaders/index.ts +0 -9
- package/src/threejs-compatibility/vfx/GPUPointCloud.ts +0 -7
- package/src/threejs-compatibility/vfx/LineRenderer.ts +0 -13
- package/src/threejs-compatibility/vfx/ParticleSystem.ts +0 -23
- package/src/threejs-compatibility/vfx/SpriteSystem.ts +0 -14
- package/src/threejs-compatibility/vfx/TrailRenderer.ts +0 -8
- package/src/threejs-compatibility/vfx/VFXDiagnostics.ts +0 -23
- package/src/threejs-compatibility/vfx/index.ts +0 -9
- package/src/threejs-example-parity/index.ts +0 -205
- package/src/webgpu/WebGPUBuffer.ts +0 -1
- package/src/webgpu/WebGPUCompute.ts +0 -2
- package/src/webgpu/WebGPUPipelineCache.ts +0 -15
- package/src/webgpu/WebGPUPostProcess.ts +0 -2
- package/src/webgpu/WebGPUTexture.ts +0 -2
- package/tsconfig.json +0 -26
- package/vitest.config.ts +0 -20
|
@@ -1,1026 +0,0 @@
|
|
|
1
|
-
import { type TextureMipLevelDescriptor } from "./Texture";
|
|
2
|
-
|
|
3
|
-
export type EnvironmentColorSpace = "linear" | "srgb";
|
|
4
|
-
export type EnvironmentInputEncoding = "rgba8-linear" | "rgba8-srgb" | "rgbe";
|
|
5
|
-
export type EnvironmentTextureEncoding = "rgba8-linear" | "rgba8-srgb" | "rgbe";
|
|
6
|
-
export type EnvironmentToneMappingOperator = "linear" | "reinhard";
|
|
7
|
-
|
|
8
|
-
const A3D_EPSILON_NUMBER = 0.00001;
|
|
9
|
-
|
|
10
|
-
export interface Rgba8EnvironmentMapSource {
|
|
11
|
-
readonly width: number;
|
|
12
|
-
readonly height: number;
|
|
13
|
-
readonly data: Uint8Array | Uint8ClampedArray;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface RgbeEnvironmentMapSource {
|
|
17
|
-
readonly width: number;
|
|
18
|
-
readonly height: number;
|
|
19
|
-
readonly data: Uint8Array | Uint8ClampedArray;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface LinearHdrEnvironmentMapSource {
|
|
23
|
-
readonly width: number;
|
|
24
|
-
readonly height: number;
|
|
25
|
-
readonly data: Float32Array | readonly number[];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type EnvironmentMapResourceInput =
|
|
29
|
-
| (Rgba8EnvironmentMapSource & { readonly encoding?: "rgba8-linear" | "rgba8-srgb" })
|
|
30
|
-
| (RgbeEnvironmentMapSource & { readonly encoding: "rgbe" })
|
|
31
|
-
| (LinearHdrEnvironmentMapSource & { readonly encoding: "linear-hdr" });
|
|
32
|
-
|
|
33
|
-
export interface EnvironmentMipGenerationOptions {
|
|
34
|
-
readonly levels?: number;
|
|
35
|
-
readonly blurRadius?: number;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface EnvironmentSpecularPrefilterGenerationOptions extends EnvironmentMipGenerationOptions {
|
|
39
|
-
readonly sampleCount?: number;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface EnvironmentHdrEncodeOptions {
|
|
43
|
-
readonly exposure?: number;
|
|
44
|
-
readonly toneMapping?: EnvironmentToneMappingOperator;
|
|
45
|
-
readonly outputColorSpace?: EnvironmentColorSpace;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface DiffuseIrradianceGenerationOptions {
|
|
49
|
-
readonly width?: number;
|
|
50
|
-
readonly height?: number;
|
|
51
|
-
readonly blurRadius?: number;
|
|
52
|
-
readonly sampleCount?: number;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface EnvironmentResourceSetOptions {
|
|
56
|
-
readonly inputEncoding?: EnvironmentInputEncoding | "linear-hdr";
|
|
57
|
-
readonly textureEncoding?: EnvironmentTextureEncoding;
|
|
58
|
-
readonly outputColorSpace?: EnvironmentColorSpace;
|
|
59
|
-
readonly exposure?: number;
|
|
60
|
-
readonly toneMapping?: EnvironmentToneMappingOperator;
|
|
61
|
-
readonly specularLevels?: number;
|
|
62
|
-
readonly specularBlurRadius?: number;
|
|
63
|
-
readonly irradianceWidth?: number;
|
|
64
|
-
readonly irradianceHeight?: number;
|
|
65
|
-
readonly irradianceBlurRadius?: number;
|
|
66
|
-
readonly brdfLutSize?: number;
|
|
67
|
-
readonly brdfLutSampleCount?: number;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface EnvironmentMapResourceSet {
|
|
71
|
-
readonly base: Rgba8EnvironmentMapSource;
|
|
72
|
-
readonly diffuseIrradiance: Rgba8EnvironmentMapSource;
|
|
73
|
-
readonly specularMipLevels: readonly TextureMipLevelDescriptor[];
|
|
74
|
-
readonly brdfLut: Rgba8EnvironmentMapSource;
|
|
75
|
-
readonly diagnostics: {
|
|
76
|
-
readonly inputEncoding: EnvironmentInputEncoding | "linear-hdr";
|
|
77
|
-
readonly textureEncoding: EnvironmentTextureEncoding;
|
|
78
|
-
readonly outputColorSpace: EnvironmentColorSpace;
|
|
79
|
-
readonly hdrSource: boolean;
|
|
80
|
-
readonly maxLinearValue: number;
|
|
81
|
-
readonly specularMipCount: number;
|
|
82
|
-
readonly diffuseIrradianceSize: readonly [number, number];
|
|
83
|
-
readonly brdfLutSize: readonly [number, number];
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface BrdfLutDescriptor {
|
|
88
|
-
readonly width?: number;
|
|
89
|
-
readonly height?: number;
|
|
90
|
-
readonly sampleCount?: number;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function srgbChannelToLinear(value: number): number {
|
|
94
|
-
const channel = validateNormalizedChannel(value, "sRGB channel");
|
|
95
|
-
return channel <= 0.04045 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function linearChannelToSrgb(value: number): number {
|
|
99
|
-
const channel = clampFinite(value, 0, 1, "linear channel");
|
|
100
|
-
return channel <= 0.0031308 ? channel * 12.92 : 1.055 * Math.pow(channel, 1 / 2.4) - 0.055;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function decodeRgbeEnvironmentMap(source: RgbeEnvironmentMapSource): LinearHdrEnvironmentMapSource {
|
|
104
|
-
validateSource(source);
|
|
105
|
-
const data = new Float32Array(source.width * source.height * 4);
|
|
106
|
-
for (let index = 0; index < source.width * source.height; index += 1) {
|
|
107
|
-
const sourceOffset = index * 4;
|
|
108
|
-
const exponent = source.data[sourceOffset + 3] ?? 0;
|
|
109
|
-
const outputOffset = sourceOffset;
|
|
110
|
-
if (exponent === 0) {
|
|
111
|
-
data[outputOffset] = 0;
|
|
112
|
-
data[outputOffset + 1] = 0;
|
|
113
|
-
data[outputOffset + 2] = 0;
|
|
114
|
-
data[outputOffset + 3] = 1;
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
const scale = Math.pow(2, exponent - 128) / 256;
|
|
118
|
-
data[outputOffset] = (source.data[sourceOffset] ?? 0) * scale;
|
|
119
|
-
data[outputOffset + 1] = (source.data[sourceOffset + 1] ?? 0) * scale;
|
|
120
|
-
data[outputOffset + 2] = (source.data[sourceOffset + 2] ?? 0) * scale;
|
|
121
|
-
data[outputOffset + 3] = 1;
|
|
122
|
-
}
|
|
123
|
-
return { width: source.width, height: source.height, data };
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export function decodeRgba8EnvironmentToLinear(
|
|
127
|
-
source: Rgba8EnvironmentMapSource,
|
|
128
|
-
inputColorSpace: EnvironmentColorSpace = "srgb"
|
|
129
|
-
): LinearHdrEnvironmentMapSource {
|
|
130
|
-
validateSource(source);
|
|
131
|
-
const data = new Float32Array(source.width * source.height * 4);
|
|
132
|
-
for (let index = 0; index < source.width * source.height; index += 1) {
|
|
133
|
-
const offset = index * 4;
|
|
134
|
-
data[offset] = decodeColorByte(source.data[offset] ?? 0, inputColorSpace);
|
|
135
|
-
data[offset + 1] = decodeColorByte(source.data[offset + 1] ?? 0, inputColorSpace);
|
|
136
|
-
data[offset + 2] = decodeColorByte(source.data[offset + 2] ?? 0, inputColorSpace);
|
|
137
|
-
data[offset + 3] = (source.data[offset + 3] ?? 255) / 255;
|
|
138
|
-
}
|
|
139
|
-
return { width: source.width, height: source.height, data };
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function encodeLinearHdrEnvironmentToRgba8(
|
|
143
|
-
source: LinearHdrEnvironmentMapSource,
|
|
144
|
-
options: EnvironmentHdrEncodeOptions = {}
|
|
145
|
-
): Rgba8EnvironmentMapSource {
|
|
146
|
-
validateHdrSource(source);
|
|
147
|
-
const exposure = options.exposure ?? 1;
|
|
148
|
-
if (!Number.isFinite(exposure) || exposure < 0) {
|
|
149
|
-
throw new RangeError("Environment HDR exposure must be finite and non-negative");
|
|
150
|
-
}
|
|
151
|
-
const toneMapping = options.toneMapping ?? "reinhard";
|
|
152
|
-
const outputColorSpace = options.outputColorSpace ?? "srgb";
|
|
153
|
-
const data = new Uint8Array(source.width * source.height * 4);
|
|
154
|
-
for (let index = 0; index < source.width * source.height; index += 1) {
|
|
155
|
-
const offset = index * 4;
|
|
156
|
-
data[offset] = encodeColorByte(toneMap((source.data[offset] ?? 0) * exposure, toneMapping), outputColorSpace);
|
|
157
|
-
data[offset + 1] = encodeColorByte(toneMap((source.data[offset + 1] ?? 0) * exposure, toneMapping), outputColorSpace);
|
|
158
|
-
data[offset + 2] = encodeColorByte(toneMap((source.data[offset + 2] ?? 0) * exposure, toneMapping), outputColorSpace);
|
|
159
|
-
data[offset + 3] = Math.round(clampFinite(source.data[offset + 3] ?? 1, 0, 1, "HDR alpha") * 255);
|
|
160
|
-
}
|
|
161
|
-
return { width: source.width, height: source.height, data };
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export function encodeLinearHdrEnvironmentToRgbe(source: LinearHdrEnvironmentMapSource): RgbeEnvironmentMapSource {
|
|
165
|
-
validateHdrSource(source);
|
|
166
|
-
const data = new Uint8Array(source.width * source.height * 4);
|
|
167
|
-
for (let index = 0; index < source.width * source.height; index += 1) {
|
|
168
|
-
const offset = index * 4;
|
|
169
|
-
encodeLinearRgbePixel(
|
|
170
|
-
source.data[offset] ?? 0,
|
|
171
|
-
source.data[offset + 1] ?? 0,
|
|
172
|
-
source.data[offset + 2] ?? 0,
|
|
173
|
-
source.data[offset + 3] ?? 1,
|
|
174
|
-
data,
|
|
175
|
-
offset
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
return { width: source.width, height: source.height, data };
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export function encodeLinearHdrEnvironmentToRgba16f(source: LinearHdrEnvironmentMapSource): TextureMipLevelDescriptor {
|
|
182
|
-
validateHdrSource(source);
|
|
183
|
-
const data = new Uint16Array(source.width * source.height * 4);
|
|
184
|
-
for (let index = 0; index < source.width * source.height; index += 1) {
|
|
185
|
-
const offset = index * 4;
|
|
186
|
-
data[offset] = numberToHalfFloat(Math.max(0, source.data[offset] ?? 0));
|
|
187
|
-
data[offset + 1] = numberToHalfFloat(Math.max(0, source.data[offset + 1] ?? 0));
|
|
188
|
-
data[offset + 2] = numberToHalfFloat(Math.max(0, source.data[offset + 2] ?? 0));
|
|
189
|
-
data[offset + 3] = numberToHalfFloat(clampFinite(source.data[offset + 3] ?? 1, 0, 1, "HDR alpha"));
|
|
190
|
-
}
|
|
191
|
-
return { width: source.width, height: source.height, data };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export function generateRgba8EnvironmentMipLevels(
|
|
195
|
-
source: Rgba8EnvironmentMapSource,
|
|
196
|
-
options: EnvironmentMipGenerationOptions = {}
|
|
197
|
-
): readonly TextureMipLevelDescriptor[] {
|
|
198
|
-
validateSource(source);
|
|
199
|
-
const maxLevels = maxMipLevels(source.width, source.height);
|
|
200
|
-
const requestedLevels = options.levels ?? maxLevels;
|
|
201
|
-
if (!Number.isInteger(requestedLevels) || requestedLevels < 1) {
|
|
202
|
-
throw new RangeError("Environment mip levels must be a positive integer");
|
|
203
|
-
}
|
|
204
|
-
const blurRadius = options.blurRadius ?? 1;
|
|
205
|
-
if (!Number.isInteger(blurRadius) || blurRadius < 0) {
|
|
206
|
-
throw new RangeError("Environment mip blurRadius must be a non-negative integer");
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const levels: TextureMipLevelDescriptor[] = [{
|
|
210
|
-
width: source.width,
|
|
211
|
-
height: source.height,
|
|
212
|
-
data: new Uint8Array(source.data)
|
|
213
|
-
}];
|
|
214
|
-
while (levels.length < Math.min(requestedLevels, maxLevels)) {
|
|
215
|
-
const previous = levels[levels.length - 1]!;
|
|
216
|
-
levels.push(downsampleEnvironmentLevel(previous, blurRadius + levels.length - 1));
|
|
217
|
-
}
|
|
218
|
-
return levels;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export function generateSpecularPrefilterMipLevels(
|
|
222
|
-
source: Rgba8EnvironmentMapSource,
|
|
223
|
-
options: EnvironmentMipGenerationOptions = {}
|
|
224
|
-
): readonly TextureMipLevelDescriptor[] {
|
|
225
|
-
return generateRgba8EnvironmentMipLevels(source, {
|
|
226
|
-
levels: options.levels,
|
|
227
|
-
blurRadius: options.blurRadius ?? 2
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export function generateRgbeEnvironmentMipLevels(
|
|
232
|
-
source: LinearHdrEnvironmentMapSource,
|
|
233
|
-
options: EnvironmentMipGenerationOptions = {}
|
|
234
|
-
): readonly TextureMipLevelDescriptor[] {
|
|
235
|
-
validateHdrSource(source);
|
|
236
|
-
const maxLevels = maxMipLevels(source.width, source.height);
|
|
237
|
-
const requestedLevels = options.levels ?? maxLevels;
|
|
238
|
-
if (!Number.isInteger(requestedLevels) || requestedLevels < 1) {
|
|
239
|
-
throw new RangeError("Environment RGBe mip levels must be a positive integer");
|
|
240
|
-
}
|
|
241
|
-
const blurRadius = options.blurRadius ?? 2;
|
|
242
|
-
if (!Number.isInteger(blurRadius) || blurRadius < 0) {
|
|
243
|
-
throw new RangeError("Environment RGBe mip blurRadius must be a non-negative integer");
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const levels: TextureMipLevelDescriptor[] = [];
|
|
247
|
-
let linearLevel: LinearHdrEnvironmentMapSource = {
|
|
248
|
-
width: source.width,
|
|
249
|
-
height: source.height,
|
|
250
|
-
data: new Float32Array(source.data)
|
|
251
|
-
};
|
|
252
|
-
levels.push(encodeLinearHdrEnvironmentToRgbe(linearLevel));
|
|
253
|
-
while (levels.length < Math.min(requestedLevels, maxLevels)) {
|
|
254
|
-
linearLevel = downsampleLinearHdrEnvironmentLevel(linearLevel, blurRadius + levels.length - 1);
|
|
255
|
-
levels.push(encodeLinearHdrEnvironmentToRgbe(linearLevel));
|
|
256
|
-
}
|
|
257
|
-
return levels;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
export function generateRgba16fEnvironmentMipLevels(
|
|
261
|
-
source: LinearHdrEnvironmentMapSource,
|
|
262
|
-
options: EnvironmentMipGenerationOptions = {}
|
|
263
|
-
): readonly TextureMipLevelDescriptor[] {
|
|
264
|
-
validateHdrSource(source);
|
|
265
|
-
const maxLevels = maxMipLevels(source.width, source.height);
|
|
266
|
-
const requestedLevels = options.levels ?? maxLevels;
|
|
267
|
-
if (!Number.isInteger(requestedLevels) || requestedLevels < 1) {
|
|
268
|
-
throw new RangeError("Environment RGBA16F mip levels must be a positive integer");
|
|
269
|
-
}
|
|
270
|
-
const blurRadius = options.blurRadius ?? 2;
|
|
271
|
-
if (!Number.isInteger(blurRadius) || blurRadius < 0) {
|
|
272
|
-
throw new RangeError("Environment RGBA16F mip blurRadius must be a non-negative integer");
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const levels: TextureMipLevelDescriptor[] = [];
|
|
276
|
-
let linearLevel: LinearHdrEnvironmentMapSource = {
|
|
277
|
-
width: source.width,
|
|
278
|
-
height: source.height,
|
|
279
|
-
data: new Float32Array(source.data)
|
|
280
|
-
};
|
|
281
|
-
levels.push(encodeLinearHdrEnvironmentToRgba16f(linearLevel));
|
|
282
|
-
while (levels.length < Math.min(requestedLevels, maxLevels)) {
|
|
283
|
-
linearLevel = downsampleLinearHdrEnvironmentLevel(linearLevel, blurRadius + levels.length - 1);
|
|
284
|
-
levels.push(encodeLinearHdrEnvironmentToRgba16f(linearLevel));
|
|
285
|
-
}
|
|
286
|
-
return levels;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
export function generateRgba16fSpecularPrefilterMipLevels(
|
|
290
|
-
source: LinearHdrEnvironmentMapSource,
|
|
291
|
-
options: EnvironmentSpecularPrefilterGenerationOptions = {}
|
|
292
|
-
): readonly TextureMipLevelDescriptor[] {
|
|
293
|
-
validateHdrSource(source);
|
|
294
|
-
const maxLevels = maxMipLevels(source.width, source.height);
|
|
295
|
-
const requestedLevels = options.levels ?? maxLevels;
|
|
296
|
-
if (!Number.isInteger(requestedLevels) || requestedLevels < 1) {
|
|
297
|
-
throw new RangeError("Environment RGBA16F specular prefilter mip levels must be a positive integer");
|
|
298
|
-
}
|
|
299
|
-
const sampleCount = options.sampleCount ?? 16;
|
|
300
|
-
if (!Number.isInteger(sampleCount) || sampleCount <= 0) {
|
|
301
|
-
throw new RangeError("Environment RGBA16F specular prefilter sampleCount must be a positive integer");
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const mipCount = Math.min(requestedLevels, maxLevels);
|
|
305
|
-
const levels: TextureMipLevelDescriptor[] = [
|
|
306
|
-
encodeLinearHdrEnvironmentToRgba16f({
|
|
307
|
-
width: source.width,
|
|
308
|
-
height: source.height,
|
|
309
|
-
data: new Float32Array(source.data)
|
|
310
|
-
})
|
|
311
|
-
];
|
|
312
|
-
for (let mipIndex = 1; mipIndex < mipCount; mipIndex += 1) {
|
|
313
|
-
const roughness = clamp(mipIndex / Math.max(1, mipCount - 1), 0, 1);
|
|
314
|
-
const effectiveSamples = Math.max(8, Math.round(sampleCount * lerp(0.5, 1, roughness)));
|
|
315
|
-
const level = prefilterLinearHdrEnvironmentLevel(source, mipIndex, roughness, effectiveSamples);
|
|
316
|
-
levels.push(encodeLinearHdrEnvironmentToRgba16f(level));
|
|
317
|
-
}
|
|
318
|
-
if (levels.length > 1) {
|
|
319
|
-
const diffuseLevel = levels[levels.length - 1]!;
|
|
320
|
-
levels[levels.length - 1] = generateRgba16fDiffuseIrradianceMipLevel(source, {
|
|
321
|
-
width: diffuseLevel.width,
|
|
322
|
-
height: diffuseLevel.height,
|
|
323
|
-
sampleCount: Math.max(32, sampleCount * 2)
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
return levels;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
export function generateRgba16fDiffuseIrradianceMipLevel(
|
|
330
|
-
source: LinearHdrEnvironmentMapSource,
|
|
331
|
-
options: DiffuseIrradianceGenerationOptions = {}
|
|
332
|
-
): TextureMipLevelDescriptor {
|
|
333
|
-
validateHdrSource(source);
|
|
334
|
-
const width = options.width ?? Math.min(32, source.width);
|
|
335
|
-
const height = options.height ?? Math.min(16, source.height);
|
|
336
|
-
if (!Number.isInteger(width) || width <= 0 || !Number.isInteger(height) || height <= 0) {
|
|
337
|
-
throw new RangeError("Environment RGBA16F diffuse irradiance dimensions must be positive integers");
|
|
338
|
-
}
|
|
339
|
-
const sampleCount = options.sampleCount ?? 64;
|
|
340
|
-
if (!Number.isInteger(sampleCount) || sampleCount <= 0) {
|
|
341
|
-
throw new RangeError("Environment RGBA16F diffuse irradiance sampleCount must be a positive integer");
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
const data = new Float32Array(width * height * 4);
|
|
345
|
-
for (let y = 0; y < height; y += 1) {
|
|
346
|
-
for (let x = 0; x < width; x += 1) {
|
|
347
|
-
const normal = equirectPixelDirection(x, y, width, height);
|
|
348
|
-
const color = integrateDiffuseIrradianceOverHemisphere(source, normal, sampleCount);
|
|
349
|
-
const index = (y * width + x) * 4;
|
|
350
|
-
data[index] = color[0];
|
|
351
|
-
data[index + 1] = color[1];
|
|
352
|
-
data[index + 2] = color[2];
|
|
353
|
-
data[index + 3] = color[3];
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
return encodeLinearHdrEnvironmentToRgba16f({ width, height, data });
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
export function generateDiffuseIrradianceRgba8(
|
|
360
|
-
source: Rgba8EnvironmentMapSource,
|
|
361
|
-
options: DiffuseIrradianceGenerationOptions = {}
|
|
362
|
-
): Rgba8EnvironmentMapSource {
|
|
363
|
-
validateSource(source);
|
|
364
|
-
const width = options.width ?? Math.min(16, source.width);
|
|
365
|
-
const height = options.height ?? Math.min(8, source.height);
|
|
366
|
-
if (!Number.isInteger(width) || width <= 0 || !Number.isInteger(height) || height <= 0) {
|
|
367
|
-
throw new RangeError("Environment diffuse irradiance dimensions must be positive integers");
|
|
368
|
-
}
|
|
369
|
-
const blurRadius = options.blurRadius ?? Math.max(2, Math.ceil(Math.max(source.width / width, source.height / height)));
|
|
370
|
-
if (!Number.isInteger(blurRadius) || blurRadius < 0) {
|
|
371
|
-
throw new RangeError("Environment diffuse irradiance blurRadius must be a non-negative integer");
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
const data = new Uint8Array(width * height * 4);
|
|
375
|
-
for (let y = 0; y < height; y += 1) {
|
|
376
|
-
for (let x = 0; x < width; x += 1) {
|
|
377
|
-
const sourceX = Math.round((x / Math.max(1, width - 1)) * (source.width - 1));
|
|
378
|
-
const sourceY = Math.round((y / Math.max(1, height - 1)) * (source.height - 1));
|
|
379
|
-
const color = sampleBox(source, sourceX, sourceY, blurRadius);
|
|
380
|
-
const index = (y * width + x) * 4;
|
|
381
|
-
data[index] = color[0];
|
|
382
|
-
data[index + 1] = color[1];
|
|
383
|
-
data[index + 2] = color[2];
|
|
384
|
-
data[index + 3] = color[3];
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return { width, height, data };
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
export function generateDiffuseIrradianceRgbe(
|
|
391
|
-
source: LinearHdrEnvironmentMapSource,
|
|
392
|
-
options: DiffuseIrradianceGenerationOptions = {}
|
|
393
|
-
): RgbeEnvironmentMapSource {
|
|
394
|
-
validateHdrSource(source);
|
|
395
|
-
const width = options.width ?? Math.min(16, source.width);
|
|
396
|
-
const height = options.height ?? Math.min(8, source.height);
|
|
397
|
-
if (!Number.isInteger(width) || width <= 0 || !Number.isInteger(height) || height <= 0) {
|
|
398
|
-
throw new RangeError("Environment diffuse irradiance dimensions must be positive integers");
|
|
399
|
-
}
|
|
400
|
-
const blurRadius = options.blurRadius ?? Math.max(2, Math.ceil(Math.max(source.width / width, source.height / height)));
|
|
401
|
-
if (!Number.isInteger(blurRadius) || blurRadius < 0) {
|
|
402
|
-
throw new RangeError("Environment diffuse irradiance blurRadius must be a non-negative integer");
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
const data = new Uint8Array(width * height * 4);
|
|
406
|
-
for (let y = 0; y < height; y += 1) {
|
|
407
|
-
for (let x = 0; x < width; x += 1) {
|
|
408
|
-
const sourceX = Math.round((x / Math.max(1, width - 1)) * (source.width - 1));
|
|
409
|
-
const sourceY = Math.round((y / Math.max(1, height - 1)) * (source.height - 1));
|
|
410
|
-
const color = sampleLinearHdrBox(source, sourceX, sourceY, blurRadius);
|
|
411
|
-
encodeLinearRgbePixel(color[0], color[1], color[2], color[3], data, (y * width + x) * 4);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
return { width, height, data };
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
export function createEnvironmentMapResourceSet(
|
|
418
|
-
input: EnvironmentMapResourceInput,
|
|
419
|
-
options: EnvironmentResourceSetOptions = {}
|
|
420
|
-
): EnvironmentMapResourceSet {
|
|
421
|
-
const inputEncoding = options.inputEncoding ?? input.encoding ?? "rgba8-srgb";
|
|
422
|
-
const outputColorSpace = options.outputColorSpace ?? "srgb";
|
|
423
|
-
const textureEncoding = options.textureEncoding ?? (outputColorSpace === "linear" ? "rgba8-linear" : "rgba8-srgb");
|
|
424
|
-
const linear = toLinearHdrEnvironment(input, inputEncoding);
|
|
425
|
-
const linearForTexture = textureEncoding === "rgbe"
|
|
426
|
-
? scaleLinearHdrEnvironment(linear, 1)
|
|
427
|
-
: scaleLinearHdrEnvironment(linear, options.exposure ?? 1);
|
|
428
|
-
const base = textureEncoding === "rgbe"
|
|
429
|
-
? encodeLinearHdrEnvironmentToRgbe(linearForTexture)
|
|
430
|
-
: encodeLinearHdrEnvironmentToRgba8(linear, {
|
|
431
|
-
exposure: options.exposure ?? 1,
|
|
432
|
-
toneMapping: options.toneMapping ?? (inputEncoding === "rgba8-linear" || inputEncoding === "rgba8-srgb" ? "linear" : "reinhard"),
|
|
433
|
-
outputColorSpace: textureEncoding === "rgba8-linear" ? "linear" : "srgb"
|
|
434
|
-
});
|
|
435
|
-
const specularMipLevels = textureEncoding === "rgbe"
|
|
436
|
-
? generateRgbeEnvironmentMipLevels(linearForTexture, {
|
|
437
|
-
levels: options.specularLevels,
|
|
438
|
-
blurRadius: options.specularBlurRadius ?? 2
|
|
439
|
-
})
|
|
440
|
-
: generateSpecularPrefilterMipLevels(base, {
|
|
441
|
-
levels: options.specularLevels,
|
|
442
|
-
blurRadius: options.specularBlurRadius ?? 2
|
|
443
|
-
});
|
|
444
|
-
const diffuseIrradiance = textureEncoding === "rgbe"
|
|
445
|
-
? generateDiffuseIrradianceRgbe(linearForTexture, {
|
|
446
|
-
width: options.irradianceWidth,
|
|
447
|
-
height: options.irradianceHeight,
|
|
448
|
-
blurRadius: options.irradianceBlurRadius
|
|
449
|
-
})
|
|
450
|
-
: generateDiffuseIrradianceRgba8(base, {
|
|
451
|
-
width: options.irradianceWidth,
|
|
452
|
-
height: options.irradianceHeight,
|
|
453
|
-
blurRadius: options.irradianceBlurRadius
|
|
454
|
-
});
|
|
455
|
-
const brdfLut = generateApproximateBrdfLutPixels({
|
|
456
|
-
width: options.brdfLutSize ?? 32,
|
|
457
|
-
height: options.brdfLutSize ?? 32,
|
|
458
|
-
sampleCount: options.brdfLutSampleCount
|
|
459
|
-
});
|
|
460
|
-
return {
|
|
461
|
-
base,
|
|
462
|
-
diffuseIrradiance,
|
|
463
|
-
specularMipLevels,
|
|
464
|
-
brdfLut,
|
|
465
|
-
diagnostics: {
|
|
466
|
-
inputEncoding,
|
|
467
|
-
textureEncoding,
|
|
468
|
-
outputColorSpace,
|
|
469
|
-
hdrSource: inputEncoding === "rgbe" || inputEncoding === "linear-hdr",
|
|
470
|
-
maxLinearValue: Number(maxLinearValue(linear).toFixed(6)),
|
|
471
|
-
specularMipCount: specularMipLevels.length,
|
|
472
|
-
diffuseIrradianceSize: [diffuseIrradiance.width, diffuseIrradiance.height],
|
|
473
|
-
brdfLutSize: [brdfLut.width, brdfLut.height]
|
|
474
|
-
}
|
|
475
|
-
};
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
export function generateApproximateBrdfLutPixels(descriptor: BrdfLutDescriptor = {}): Rgba8EnvironmentMapSource {
|
|
479
|
-
const width = descriptor.width ?? 32;
|
|
480
|
-
const height = descriptor.height ?? 32;
|
|
481
|
-
if (!Number.isInteger(width) || width <= 0 || !Number.isInteger(height) || height <= 0) {
|
|
482
|
-
throw new RangeError("Environment BRDF LUT dimensions must be positive integers");
|
|
483
|
-
}
|
|
484
|
-
const sampleCount = descriptor.sampleCount ?? 128;
|
|
485
|
-
if (!Number.isInteger(sampleCount) || sampleCount <= 0) {
|
|
486
|
-
throw new RangeError("Environment BRDF LUT sampleCount must be a positive integer");
|
|
487
|
-
}
|
|
488
|
-
const data = new Uint8Array(width * height * 4);
|
|
489
|
-
for (let y = 0; y < height; y += 1) {
|
|
490
|
-
const roughness = clamp(y / Math.max(1, height - 1), 0, 1);
|
|
491
|
-
for (let x = 0; x < width; x += 1) {
|
|
492
|
-
const nDotV = clamp(x / Math.max(1, width - 1), 0.001, 1);
|
|
493
|
-
const brdf = integrateGgxEnvironmentBrdf(nDotV, roughness, sampleCount);
|
|
494
|
-
const index = (y * width + x) * 4;
|
|
495
|
-
data[index] = encodeColorByte(brdf[0], "linear");
|
|
496
|
-
data[index + 1] = encodeColorByte(brdf[1], "linear");
|
|
497
|
-
data[index + 2] = 0;
|
|
498
|
-
data[index + 3] = 255;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
return { width, height, data };
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
function integrateGgxEnvironmentBrdf(
|
|
505
|
-
nDotV: number,
|
|
506
|
-
roughness: number,
|
|
507
|
-
sampleCount: number
|
|
508
|
-
): readonly [number, number] {
|
|
509
|
-
const view: readonly [number, number, number] = [
|
|
510
|
-
Math.sqrt(Math.max(0, 1 - nDotV * nDotV)),
|
|
511
|
-
0,
|
|
512
|
-
nDotV
|
|
513
|
-
];
|
|
514
|
-
let scale = 0;
|
|
515
|
-
let bias = 0;
|
|
516
|
-
for (let index = 0; index < sampleCount; index += 1) {
|
|
517
|
-
const xi = hammersley2d(index, sampleCount);
|
|
518
|
-
const halfVector = importanceSampleGgx(xi, roughness);
|
|
519
|
-
const vDotH = Math.max(dot3(view, halfVector), 0);
|
|
520
|
-
const light = normalize3([
|
|
521
|
-
2 * vDotH * halfVector[0] - view[0],
|
|
522
|
-
2 * vDotH * halfVector[1] - view[1],
|
|
523
|
-
2 * vDotH * halfVector[2] - view[2]
|
|
524
|
-
]);
|
|
525
|
-
const nDotL = Math.max(light[2], 0);
|
|
526
|
-
const nDotH = Math.max(halfVector[2], 0);
|
|
527
|
-
if (nDotL <= 0 || nDotH <= 0 || vDotH <= 0) {
|
|
528
|
-
continue;
|
|
529
|
-
}
|
|
530
|
-
const visibility = ggxGeometrySmithCorrelated(nDotV, nDotL, roughness) * vDotH / Math.max(nDotH * nDotV, A3D_EPSILON_NUMBER);
|
|
531
|
-
const fresnel = Math.pow(1 - vDotH, 5);
|
|
532
|
-
scale += (1 - fresnel) * visibility;
|
|
533
|
-
bias += fresnel * visibility;
|
|
534
|
-
}
|
|
535
|
-
return [
|
|
536
|
-
clamp(scale / sampleCount, 0, 1),
|
|
537
|
-
clamp(bias / sampleCount, 0, 1)
|
|
538
|
-
];
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
function hammersley2d(index: number, count: number): readonly [number, number] {
|
|
542
|
-
return [index / count, radicalInverseVdc(index)];
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
function radicalInverseVdc(bits: number): number {
|
|
546
|
-
let value = bits >>> 0;
|
|
547
|
-
value = ((value << 16) | (value >>> 16)) >>> 0;
|
|
548
|
-
value = (((value & 0x55555555) << 1) | ((value & 0xaaaaaaaa) >>> 1)) >>> 0;
|
|
549
|
-
value = (((value & 0x33333333) << 2) | ((value & 0xcccccccc) >>> 2)) >>> 0;
|
|
550
|
-
value = (((value & 0x0f0f0f0f) << 4) | ((value & 0xf0f0f0f0) >>> 4)) >>> 0;
|
|
551
|
-
value = (((value & 0x00ff00ff) << 8) | ((value & 0xff00ff00) >>> 8)) >>> 0;
|
|
552
|
-
return value * 2.3283064365386963e-10;
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
function importanceSampleGgx(
|
|
556
|
-
xi: readonly [number, number],
|
|
557
|
-
roughness: number
|
|
558
|
-
): readonly [number, number, number] {
|
|
559
|
-
const alpha = Math.max(roughness, 0.001) * Math.max(roughness, 0.001);
|
|
560
|
-
const alpha2 = alpha * alpha;
|
|
561
|
-
const phi = 2 * Math.PI * xi[0];
|
|
562
|
-
const cosTheta = Math.sqrt((1 - xi[1]) / Math.max(1 + (alpha2 - 1) * xi[1], A3D_EPSILON_NUMBER));
|
|
563
|
-
const sinTheta = Math.sqrt(Math.max(0, 1 - cosTheta * cosTheta));
|
|
564
|
-
return [
|
|
565
|
-
Math.cos(phi) * sinTheta,
|
|
566
|
-
Math.sin(phi) * sinTheta,
|
|
567
|
-
cosTheta
|
|
568
|
-
];
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
function ggxGeometrySmithCorrelated(nDotV: number, nDotL: number, roughness: number): number {
|
|
572
|
-
const alpha = Math.max(roughness, 0.045);
|
|
573
|
-
const alpha2 = alpha * alpha * alpha * alpha;
|
|
574
|
-
const lambdaV = nDotL * Math.sqrt(Math.max((nDotV - alpha2 * nDotV) * nDotV + alpha2, A3D_EPSILON_NUMBER));
|
|
575
|
-
const lambdaL = nDotV * Math.sqrt(Math.max((nDotL - alpha2 * nDotL) * nDotL + alpha2, A3D_EPSILON_NUMBER));
|
|
576
|
-
return 0.5 / Math.max(lambdaV + lambdaL, A3D_EPSILON_NUMBER);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
function normalize3(value: readonly [number, number, number]): readonly [number, number, number] {
|
|
580
|
-
const length = Math.hypot(value[0], value[1], value[2]);
|
|
581
|
-
if (length <= A3D_EPSILON_NUMBER) return [0, 0, 1];
|
|
582
|
-
return [value[0] / length, value[1] / length, value[2] / length];
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
function dot3(left: readonly [number, number, number], right: readonly [number, number, number]): number {
|
|
586
|
-
return left[0] * right[0] + left[1] * right[1] + left[2] * right[2];
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
function downsampleEnvironmentLevel(level: TextureMipLevelDescriptor, blurRadius: number): TextureMipLevelDescriptor {
|
|
590
|
-
const width = Math.max(1, Math.floor(level.width / 2));
|
|
591
|
-
const height = Math.max(1, Math.floor(level.height / 2));
|
|
592
|
-
const data = new Uint8Array(width * height * 4);
|
|
593
|
-
for (let y = 0; y < height; y += 1) {
|
|
594
|
-
for (let x = 0; x < width; x += 1) {
|
|
595
|
-
const color = sampleBox(level, x * 2, y * 2, Math.max(1, blurRadius));
|
|
596
|
-
const index = (y * width + x) * 4;
|
|
597
|
-
data[index] = color[0];
|
|
598
|
-
data[index + 1] = color[1];
|
|
599
|
-
data[index + 2] = color[2];
|
|
600
|
-
data[index + 3] = color[3];
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
return { width, height, data };
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
function downsampleLinearHdrEnvironmentLevel(source: LinearHdrEnvironmentMapSource, blurRadius: number): LinearHdrEnvironmentMapSource {
|
|
607
|
-
const width = Math.max(1, Math.floor(source.width / 2));
|
|
608
|
-
const height = Math.max(1, Math.floor(source.height / 2));
|
|
609
|
-
const data = new Float32Array(width * height * 4);
|
|
610
|
-
for (let y = 0; y < height; y += 1) {
|
|
611
|
-
for (let x = 0; x < width; x += 1) {
|
|
612
|
-
const color = sampleLinearHdrBox(source, x * 2, y * 2, Math.max(1, blurRadius));
|
|
613
|
-
const index = (y * width + x) * 4;
|
|
614
|
-
data[index] = color[0];
|
|
615
|
-
data[index + 1] = color[1];
|
|
616
|
-
data[index + 2] = color[2];
|
|
617
|
-
data[index + 3] = color[3];
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
return { width, height, data };
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
function prefilterLinearHdrEnvironmentLevel(
|
|
624
|
-
source: LinearHdrEnvironmentMapSource,
|
|
625
|
-
mipIndex: number,
|
|
626
|
-
roughness: number,
|
|
627
|
-
sampleCount: number
|
|
628
|
-
): LinearHdrEnvironmentMapSource {
|
|
629
|
-
const width = Math.max(1, Math.floor(source.width / 2 ** mipIndex));
|
|
630
|
-
const height = Math.max(1, Math.floor(source.height / 2 ** mipIndex));
|
|
631
|
-
const data = new Float32Array(width * height * 4);
|
|
632
|
-
for (let y = 0; y < height; y += 1) {
|
|
633
|
-
for (let x = 0; x < width; x += 1) {
|
|
634
|
-
const direction = equirectPixelDirection(x, y, width, height);
|
|
635
|
-
const color = prefilterLinearHdrEnvironmentDirection(source, direction, roughness, sampleCount);
|
|
636
|
-
const index = (y * width + x) * 4;
|
|
637
|
-
data[index] = color[0];
|
|
638
|
-
data[index + 1] = color[1];
|
|
639
|
-
data[index + 2] = color[2];
|
|
640
|
-
data[index + 3] = color[3];
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
return { width, height, data };
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
function prefilterLinearHdrEnvironmentDirection(
|
|
647
|
-
source: LinearHdrEnvironmentMapSource,
|
|
648
|
-
reflectionDirection: readonly [number, number, number],
|
|
649
|
-
roughness: number,
|
|
650
|
-
sampleCount: number
|
|
651
|
-
): readonly [number, number, number, number] {
|
|
652
|
-
if (roughness <= A3D_EPSILON_NUMBER) {
|
|
653
|
-
return sampleLinearHdrEquirect(source, reflectionDirection);
|
|
654
|
-
}
|
|
655
|
-
const normal = reflectionDirection;
|
|
656
|
-
const view = reflectionDirection;
|
|
657
|
-
let red = 0;
|
|
658
|
-
let green = 0;
|
|
659
|
-
let blue = 0;
|
|
660
|
-
let alpha = 0;
|
|
661
|
-
let totalWeight = 0;
|
|
662
|
-
for (let sampleIndex = 0; sampleIndex < sampleCount; sampleIndex += 1) {
|
|
663
|
-
const xi = hammersley2d(sampleIndex, sampleCount);
|
|
664
|
-
const halfVector = tangentToWorld(importanceSampleGgx(xi, roughness), normal);
|
|
665
|
-
const vDotH = Math.max(dot3(view, halfVector), 0);
|
|
666
|
-
const light = normalize3([
|
|
667
|
-
2 * vDotH * halfVector[0] - view[0],
|
|
668
|
-
2 * vDotH * halfVector[1] - view[1],
|
|
669
|
-
2 * vDotH * halfVector[2] - view[2]
|
|
670
|
-
]);
|
|
671
|
-
const nDotL = Math.max(dot3(normal, light), 0);
|
|
672
|
-
if (nDotL <= A3D_EPSILON_NUMBER) {
|
|
673
|
-
continue;
|
|
674
|
-
}
|
|
675
|
-
const sample = sampleLinearHdrEquirect(source, light);
|
|
676
|
-
red += sample[0] * nDotL;
|
|
677
|
-
green += sample[1] * nDotL;
|
|
678
|
-
blue += sample[2] * nDotL;
|
|
679
|
-
alpha += sample[3] * nDotL;
|
|
680
|
-
totalWeight += nDotL;
|
|
681
|
-
}
|
|
682
|
-
if (totalWeight <= A3D_EPSILON_NUMBER) {
|
|
683
|
-
return sampleLinearHdrEquirect(source, reflectionDirection);
|
|
684
|
-
}
|
|
685
|
-
return [
|
|
686
|
-
red / totalWeight,
|
|
687
|
-
green / totalWeight,
|
|
688
|
-
blue / totalWeight,
|
|
689
|
-
alpha / totalWeight
|
|
690
|
-
];
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
function integrateDiffuseIrradianceOverHemisphere(
|
|
694
|
-
source: LinearHdrEnvironmentMapSource,
|
|
695
|
-
normal: readonly [number, number, number],
|
|
696
|
-
sampleCount: number
|
|
697
|
-
): readonly [number, number, number, number] {
|
|
698
|
-
let red = 0;
|
|
699
|
-
let green = 0;
|
|
700
|
-
let blue = 0;
|
|
701
|
-
let alpha = 0;
|
|
702
|
-
for (let sampleIndex = 0; sampleIndex < sampleCount; sampleIndex += 1) {
|
|
703
|
-
const direction = tangentToWorld(cosineSampleHemisphere(hammersley2d(sampleIndex, sampleCount)), normal);
|
|
704
|
-
const sample = sampleLinearHdrEquirect(source, direction);
|
|
705
|
-
red += sample[0];
|
|
706
|
-
green += sample[1];
|
|
707
|
-
blue += sample[2];
|
|
708
|
-
alpha += sample[3];
|
|
709
|
-
}
|
|
710
|
-
return [
|
|
711
|
-
red / sampleCount,
|
|
712
|
-
green / sampleCount,
|
|
713
|
-
blue / sampleCount,
|
|
714
|
-
alpha / sampleCount
|
|
715
|
-
];
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
function cosineSampleHemisphere(xi: readonly [number, number]): readonly [number, number, number] {
|
|
719
|
-
const radius = Math.sqrt(clamp(xi[0], 0, 1));
|
|
720
|
-
const phi = 2 * Math.PI * xi[1];
|
|
721
|
-
return [
|
|
722
|
-
radius * Math.cos(phi),
|
|
723
|
-
radius * Math.sin(phi),
|
|
724
|
-
Math.sqrt(Math.max(0, 1 - xi[0]))
|
|
725
|
-
];
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
function equirectPixelDirection(x: number, y: number, width: number, height: number): readonly [number, number, number] {
|
|
729
|
-
const u = (x + 0.5) / width;
|
|
730
|
-
const v = (y + 0.5) / height;
|
|
731
|
-
const phi = (u - 0.5) * Math.PI * 2;
|
|
732
|
-
const theta = v * Math.PI;
|
|
733
|
-
const sinTheta = Math.sin(theta);
|
|
734
|
-
return normalize3([
|
|
735
|
-
Math.cos(phi) * sinTheta,
|
|
736
|
-
Math.cos(theta),
|
|
737
|
-
Math.sin(phi) * sinTheta
|
|
738
|
-
]);
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
function directionToEquirectUv(direction: readonly [number, number, number]): readonly [number, number] {
|
|
742
|
-
const d = normalize3(direction);
|
|
743
|
-
return [
|
|
744
|
-
modulo(Math.atan2(d[2], d[0]) / (Math.PI * 2) + 0.5, 1),
|
|
745
|
-
clamp(Math.acos(clamp(d[1], -1, 1)) / Math.PI, 0, 1)
|
|
746
|
-
];
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
function sampleLinearHdrEquirect(
|
|
750
|
-
source: LinearHdrEnvironmentMapSource,
|
|
751
|
-
direction: readonly [number, number, number]
|
|
752
|
-
): readonly [number, number, number, number] {
|
|
753
|
-
const uv = directionToEquirectUv(direction);
|
|
754
|
-
const fx = uv[0] * source.width - 0.5;
|
|
755
|
-
const fy = uv[1] * source.height - 0.5;
|
|
756
|
-
const x0 = Math.floor(fx);
|
|
757
|
-
const y0 = Math.floor(fy);
|
|
758
|
-
const tx = fx - x0;
|
|
759
|
-
const ty = fy - y0;
|
|
760
|
-
const c00 = sampleLinearHdrPixel(source, x0, y0);
|
|
761
|
-
const c10 = sampleLinearHdrPixel(source, x0 + 1, y0);
|
|
762
|
-
const c01 = sampleLinearHdrPixel(source, x0, y0 + 1);
|
|
763
|
-
const c11 = sampleLinearHdrPixel(source, x0 + 1, y0 + 1);
|
|
764
|
-
const top = mixColor4(c00, c10, tx);
|
|
765
|
-
const bottom = mixColor4(c01, c11, tx);
|
|
766
|
-
return mixColor4(top, bottom, ty);
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
function sampleLinearHdrPixel(
|
|
770
|
-
source: LinearHdrEnvironmentMapSource,
|
|
771
|
-
x: number,
|
|
772
|
-
y: number
|
|
773
|
-
): readonly [number, number, number, number] {
|
|
774
|
-
const wrappedX = modulo(x, source.width);
|
|
775
|
-
const wrappedY = clamp(y, 0, source.height - 1);
|
|
776
|
-
const index = (wrappedY * source.width + wrappedX) * 4;
|
|
777
|
-
return [
|
|
778
|
-
Math.max(0, source.data[index] ?? 0),
|
|
779
|
-
Math.max(0, source.data[index + 1] ?? 0),
|
|
780
|
-
Math.max(0, source.data[index + 2] ?? 0),
|
|
781
|
-
clampFinite(source.data[index + 3] ?? 1, 0, 1, "HDR alpha")
|
|
782
|
-
];
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
function mixColor4(
|
|
786
|
-
left: readonly [number, number, number, number],
|
|
787
|
-
right: readonly [number, number, number, number],
|
|
788
|
-
amount: number
|
|
789
|
-
): readonly [number, number, number, number] {
|
|
790
|
-
return [
|
|
791
|
-
lerp(left[0], right[0], amount),
|
|
792
|
-
lerp(left[1], right[1], amount),
|
|
793
|
-
lerp(left[2], right[2], amount),
|
|
794
|
-
lerp(left[3], right[3], amount)
|
|
795
|
-
];
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
function tangentToWorld(
|
|
799
|
-
local: readonly [number, number, number],
|
|
800
|
-
normal: readonly [number, number, number]
|
|
801
|
-
): readonly [number, number, number] {
|
|
802
|
-
const up: readonly [number, number, number] = Math.abs(normal[1]) < 0.999 ? [0, 1, 0] : [1, 0, 0];
|
|
803
|
-
const tangent = normalize3(cross3(up, normal));
|
|
804
|
-
const bitangent = cross3(normal, tangent);
|
|
805
|
-
return normalize3([
|
|
806
|
-
tangent[0] * local[0] + bitangent[0] * local[1] + normal[0] * local[2],
|
|
807
|
-
tangent[1] * local[0] + bitangent[1] * local[1] + normal[1] * local[2],
|
|
808
|
-
tangent[2] * local[0] + bitangent[2] * local[1] + normal[2] * local[2]
|
|
809
|
-
]);
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
function cross3(left: readonly [number, number, number], right: readonly [number, number, number]): readonly [number, number, number] {
|
|
813
|
-
return [
|
|
814
|
-
left[1] * right[2] - left[2] * right[1],
|
|
815
|
-
left[2] * right[0] - left[0] * right[2],
|
|
816
|
-
left[0] * right[1] - left[1] * right[0]
|
|
817
|
-
];
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
function sampleBox(level: Rgba8EnvironmentMapSource | TextureMipLevelDescriptor, centerX: number, centerY: number, radius: number): readonly [number, number, number, number] {
|
|
821
|
-
let r = 0;
|
|
822
|
-
let g = 0;
|
|
823
|
-
let b = 0;
|
|
824
|
-
let a = 0;
|
|
825
|
-
let count = 0;
|
|
826
|
-
for (let y = centerY - radius; y <= centerY + radius; y += 1) {
|
|
827
|
-
const wrappedY = clamp(y, 0, level.height - 1);
|
|
828
|
-
for (let x = centerX - radius; x <= centerX + radius; x += 1) {
|
|
829
|
-
const wrappedX = modulo(x, level.width);
|
|
830
|
-
const index = (wrappedY * level.width + wrappedX) * 4;
|
|
831
|
-
r += level.data[index] ?? 0;
|
|
832
|
-
g += level.data[index + 1] ?? 0;
|
|
833
|
-
b += level.data[index + 2] ?? 0;
|
|
834
|
-
a += level.data[index + 3] ?? 255;
|
|
835
|
-
count += 1;
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
return [
|
|
839
|
-
Math.round(r / count),
|
|
840
|
-
Math.round(g / count),
|
|
841
|
-
Math.round(b / count),
|
|
842
|
-
Math.round(a / count)
|
|
843
|
-
];
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
function sampleLinearHdrBox(
|
|
847
|
-
level: LinearHdrEnvironmentMapSource,
|
|
848
|
-
centerX: number,
|
|
849
|
-
centerY: number,
|
|
850
|
-
radius: number
|
|
851
|
-
): readonly [number, number, number, number] {
|
|
852
|
-
let r = 0;
|
|
853
|
-
let g = 0;
|
|
854
|
-
let b = 0;
|
|
855
|
-
let a = 0;
|
|
856
|
-
let count = 0;
|
|
857
|
-
for (let y = centerY - radius; y <= centerY + radius; y += 1) {
|
|
858
|
-
const wrappedY = clamp(y, 0, level.height - 1);
|
|
859
|
-
for (let x = centerX - radius; x <= centerX + radius; x += 1) {
|
|
860
|
-
const wrappedX = modulo(x, level.width);
|
|
861
|
-
const index = (wrappedY * level.width + wrappedX) * 4;
|
|
862
|
-
r += level.data[index] ?? 0;
|
|
863
|
-
g += level.data[index + 1] ?? 0;
|
|
864
|
-
b += level.data[index + 2] ?? 0;
|
|
865
|
-
a += level.data[index + 3] ?? 1;
|
|
866
|
-
count += 1;
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
return [r / count, g / count, b / count, a / count];
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
function scaleLinearHdrEnvironment(source: LinearHdrEnvironmentMapSource, exposure: number): LinearHdrEnvironmentMapSource {
|
|
873
|
-
if (!Number.isFinite(exposure) || exposure < 0) {
|
|
874
|
-
throw new RangeError("Environment HDR exposure must be finite and non-negative");
|
|
875
|
-
}
|
|
876
|
-
if (exposure === 1) {
|
|
877
|
-
return { width: source.width, height: source.height, data: new Float32Array(source.data) };
|
|
878
|
-
}
|
|
879
|
-
const data = new Float32Array(source.width * source.height * 4);
|
|
880
|
-
for (let index = 0; index < source.width * source.height; index += 1) {
|
|
881
|
-
const offset = index * 4;
|
|
882
|
-
data[offset] = Math.max(0, source.data[offset] ?? 0) * exposure;
|
|
883
|
-
data[offset + 1] = Math.max(0, source.data[offset + 1] ?? 0) * exposure;
|
|
884
|
-
data[offset + 2] = Math.max(0, source.data[offset + 2] ?? 0) * exposure;
|
|
885
|
-
data[offset + 3] = clampFinite(source.data[offset + 3] ?? 1, 0, 1, "HDR alpha");
|
|
886
|
-
}
|
|
887
|
-
return { width: source.width, height: source.height, data };
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
function encodeLinearRgbePixel(
|
|
891
|
-
red: number,
|
|
892
|
-
green: number,
|
|
893
|
-
blue: number,
|
|
894
|
-
alpha: number,
|
|
895
|
-
output: Uint8Array,
|
|
896
|
-
offset: number
|
|
897
|
-
): void {
|
|
898
|
-
const r = Math.max(0, red);
|
|
899
|
-
const g = Math.max(0, green);
|
|
900
|
-
const b = Math.max(0, blue);
|
|
901
|
-
const maxChannel = Math.max(r, g, b);
|
|
902
|
-
if (maxChannel < 1e-32) {
|
|
903
|
-
output[offset] = 0;
|
|
904
|
-
output[offset + 1] = 0;
|
|
905
|
-
output[offset + 2] = 0;
|
|
906
|
-
output[offset + 3] = 0;
|
|
907
|
-
return;
|
|
908
|
-
}
|
|
909
|
-
const exponent = Math.ceil(Math.log2(maxChannel));
|
|
910
|
-
const scale = 256 / Math.pow(2, exponent);
|
|
911
|
-
output[offset] = clampByte(Math.round(r * scale));
|
|
912
|
-
output[offset + 1] = clampByte(Math.round(g * scale));
|
|
913
|
-
output[offset + 2] = clampByte(Math.round(b * scale));
|
|
914
|
-
output[offset + 3] = clampByte(exponent + 128);
|
|
915
|
-
if (alpha <= 0) {
|
|
916
|
-
output[offset + 3] = 0;
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
function numberToHalfFloat(value: number): number {
|
|
921
|
-
if (Number.isNaN(value)) return 0x7e00;
|
|
922
|
-
if (value === Infinity) return 0x7c00;
|
|
923
|
-
if (value === -Infinity) return 0xfc00;
|
|
924
|
-
const sign = value < 0 ? 0x8000 : 0;
|
|
925
|
-
const absolute = Math.abs(value);
|
|
926
|
-
if (absolute === 0) return sign;
|
|
927
|
-
if (absolute >= 65504) return sign | 0x7bff;
|
|
928
|
-
if (absolute < 2 ** -14) {
|
|
929
|
-
return sign | Math.round(absolute / 2 ** -24);
|
|
930
|
-
}
|
|
931
|
-
const exponent = Math.floor(Math.log2(absolute));
|
|
932
|
-
const mantissa = Math.round((absolute / 2 ** exponent - 1) * 1024);
|
|
933
|
-
return sign | ((exponent + 15) << 10) | (mantissa & 0x03ff);
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
function toLinearHdrEnvironment(input: EnvironmentMapResourceInput, inputEncoding: EnvironmentInputEncoding | "linear-hdr"): LinearHdrEnvironmentMapSource {
|
|
937
|
-
switch (inputEncoding) {
|
|
938
|
-
case "rgba8-linear":
|
|
939
|
-
return decodeRgba8EnvironmentToLinear(input as Rgba8EnvironmentMapSource, "linear");
|
|
940
|
-
case "rgba8-srgb":
|
|
941
|
-
return decodeRgba8EnvironmentToLinear(input as Rgba8EnvironmentMapSource, "srgb");
|
|
942
|
-
case "rgbe":
|
|
943
|
-
return decodeRgbeEnvironmentMap(input as RgbeEnvironmentMapSource);
|
|
944
|
-
case "linear-hdr":
|
|
945
|
-
validateHdrSource(input as LinearHdrEnvironmentMapSource);
|
|
946
|
-
return { width: input.width, height: input.height, data: new Float32Array((input as LinearHdrEnvironmentMapSource).data) };
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
function validateSource(source: Rgba8EnvironmentMapSource): void {
|
|
951
|
-
if (!Number.isInteger(source.width) || source.width <= 0 || !Number.isInteger(source.height) || source.height <= 0) {
|
|
952
|
-
throw new RangeError("Environment map dimensions must be positive integers");
|
|
953
|
-
}
|
|
954
|
-
if (source.data.byteLength !== source.width * source.height * 4) {
|
|
955
|
-
throw new RangeError("Environment map data must contain exactly width * height * 4 RGBA8 bytes");
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
function validateHdrSource(source: LinearHdrEnvironmentMapSource): void {
|
|
960
|
-
if (!Number.isInteger(source.width) || source.width <= 0 || !Number.isInteger(source.height) || source.height <= 0) {
|
|
961
|
-
throw new RangeError("Environment HDR dimensions must be positive integers");
|
|
962
|
-
}
|
|
963
|
-
if (source.data.length !== source.width * source.height * 4) {
|
|
964
|
-
throw new RangeError("Environment HDR data must contain exactly width * height * 4 linear RGBA values");
|
|
965
|
-
}
|
|
966
|
-
for (const value of source.data) {
|
|
967
|
-
if (!Number.isFinite(value)) {
|
|
968
|
-
throw new RangeError("Environment HDR data must contain only finite values");
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
function maxMipLevels(width: number, height: number): number {
|
|
974
|
-
return Math.floor(Math.log2(Math.max(width, height))) + 1;
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
function clamp(value: number, min: number, max: number): number {
|
|
978
|
-
return Math.max(min, Math.min(max, value));
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
function clampByte(value: number): number {
|
|
982
|
-
return Math.max(0, Math.min(255, value));
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
function modulo(value: number, divisor: number): number {
|
|
986
|
-
return ((value % divisor) + divisor) % divisor;
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
function lerp(a: number, b: number, t: number): number {
|
|
990
|
-
return a + (b - a) * t;
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
function validateNormalizedChannel(value: number, label: string): number {
|
|
994
|
-
return clampFinite(value, 0, 1, label);
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
function clampFinite(value: number, min: number, max: number, label: string): number {
|
|
998
|
-
if (!Number.isFinite(value)) {
|
|
999
|
-
throw new RangeError(`${label} must be finite`);
|
|
1000
|
-
}
|
|
1001
|
-
return Math.max(min, Math.min(max, value));
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
function decodeColorByte(byte: number, colorSpace: EnvironmentColorSpace): number {
|
|
1005
|
-
const normalized = byte / 255;
|
|
1006
|
-
return colorSpace === "srgb" ? srgbChannelToLinear(normalized) : normalized;
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
function encodeColorByte(linear: number, colorSpace: EnvironmentColorSpace): number {
|
|
1010
|
-
const normalized = colorSpace === "srgb" ? linearChannelToSrgb(linear) : clampFinite(linear, 0, 1, "linear color");
|
|
1011
|
-
return Math.round(normalized * 255);
|
|
1012
|
-
}
|
|
1013
|
-
|
|
1014
|
-
function toneMap(value: number, operator: EnvironmentToneMappingOperator): number {
|
|
1015
|
-
const linear = Math.max(0, value);
|
|
1016
|
-
return operator === "linear" ? clamp(linear, 0, 1) : linear / (1 + linear);
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
|
-
function maxLinearValue(source: LinearHdrEnvironmentMapSource): number {
|
|
1020
|
-
let max = 0;
|
|
1021
|
-
for (let index = 0; index < source.width * source.height; index += 1) {
|
|
1022
|
-
const offset = index * 4;
|
|
1023
|
-
max = Math.max(max, source.data[offset] ?? 0, source.data[offset + 1] ?? 0, source.data[offset + 2] ?? 0);
|
|
1024
|
-
}
|
|
1025
|
-
return max;
|
|
1026
|
-
}
|