@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.
Files changed (308) hide show
  1. package/package.json +38 -4
  2. package/AGENTS.md +0 -46
  3. package/src/ArchitecturalLightingFixtures.ts +0 -241
  4. package/src/ArchitecturalMaterialCatalog.ts +0 -130
  5. package/src/ArchitecturalMeasurementFixtures.ts +0 -155
  6. package/src/BRDFLut.ts +0 -47
  7. package/src/CameraFraming.ts +0 -200
  8. package/src/CanonicalSceneFixtures.ts +0 -95
  9. package/src/CascadedShadowMaps.ts +0 -449
  10. package/src/ColorManagement.ts +0 -127
  11. package/src/CullingFixtures.ts +0 -280
  12. package/src/DecalGeometry.ts +0 -26
  13. package/src/DepthPass.ts +0 -110
  14. package/src/EnvironmentBackgroundPass.ts +0 -194
  15. package/src/EnvironmentBackgroundResources.ts +0 -309
  16. package/src/EnvironmentLighting.ts +0 -96
  17. package/src/EnvironmentMapResources.ts +0 -1026
  18. package/src/EnvironmentPipeline.ts +0 -69
  19. package/src/EnvironmentPlatform.ts +0 -1306
  20. package/src/EnvironmentPreset.ts +0 -155
  21. package/src/Exposure.ts +0 -66
  22. package/src/ExternalParityRenderPreset.ts +0 -725
  23. package/src/ForwardPass.ts +0 -1668
  24. package/src/FrameVisualMetrics.ts +0 -301
  25. package/src/Geometry.ts +0 -461
  26. package/src/GeometryPrimitives.ts +0 -2
  27. package/src/HDRRenderPipeline.ts +0 -94
  28. package/src/IBL.ts +0 -86
  29. package/src/IndexBuffer.ts +0 -57
  30. package/src/InstancedPBRMaterial.ts +0 -186
  31. package/src/InstancedUnlitMaterial.ts +0 -60
  32. package/src/Instancing.ts +0 -19
  33. package/src/LightCollector.ts +0 -82
  34. package/src/LightUniforms.ts +0 -47
  35. package/src/LightingDebug.ts +0 -80
  36. package/src/LightingDefaults.ts +0 -79
  37. package/src/LightingRig.ts +0 -301
  38. package/src/LineGeometry.ts +0 -2
  39. package/src/Material.ts +0 -209
  40. package/src/MaterialBinding.ts +0 -131
  41. package/src/MaterialInstance.ts +0 -59
  42. package/src/MaterialPresets.ts +0 -272
  43. package/src/MorphTarget.ts +0 -124
  44. package/src/MorphTargetPlan.ts +0 -178
  45. package/src/MorphUnlitMaterial.ts +0 -60
  46. package/src/NormalMappedPBRMaterial.ts +0 -188
  47. package/src/OceanFixtures.ts +0 -318
  48. package/src/PBRLightingDefaults.ts +0 -12
  49. package/src/PBRMaterial.ts +0 -442
  50. package/src/PMREM.ts +0 -53
  51. package/src/PbrReference.ts +0 -681
  52. package/src/PostProcessPass.ts +0 -2507
  53. package/src/ProceduralTextureFixtures.ts +0 -393
  54. package/src/ProductTurntableFixtures.ts +0 -815
  55. package/src/Raycaster.ts +0 -21
  56. package/src/ReflectionProbe.ts +0 -13
  57. package/src/ReflectionSurfaces.ts +0 -219
  58. package/src/RenderBackend.ts +0 -43
  59. package/src/RenderDebugViews.ts +0 -138
  60. package/src/RenderDevice.ts +0 -836
  61. package/src/RenderGraph.ts +0 -194
  62. package/src/RenderPass.ts +0 -27
  63. package/src/RenderPipeline.ts +0 -101
  64. package/src/RenderQueue.ts +0 -12
  65. package/src/RenderState.ts +0 -12
  66. package/src/Renderer.ts +0 -2631
  67. package/src/RendererDebugOverlay.ts +0 -80
  68. package/src/RendererFeatureGates.ts +0 -150
  69. package/src/RendererPostprocessPlan.ts +0 -279
  70. package/src/RendererTiming.ts +0 -251
  71. package/src/RendererVisualPipelineReport.ts +0 -836
  72. package/src/ResourceLifecycle.ts +0 -21
  73. package/src/Sampler.ts +0 -37
  74. package/src/SceneOptimization.ts +0 -518
  75. package/src/ShaderChunks.ts +0 -393
  76. package/src/ShaderLibrary.ts +0 -2766
  77. package/src/ShaderModule.ts +0 -36
  78. package/src/ShaderPreprocessor.ts +0 -260
  79. package/src/ShaderReflection.ts +0 -201
  80. package/src/ShadowMap.ts +0 -237
  81. package/src/ShadowPass.ts +0 -198
  82. package/src/ShadowProjection.ts +0 -115
  83. package/src/SkinnedLitMaterial.ts +0 -397
  84. package/src/SkinnedUnlitMaterial.ts +0 -63
  85. package/src/SkinningBounds.ts +0 -131
  86. package/src/SpaceEnvironmentFixtures.ts +0 -200
  87. package/src/SpriteGeometry.ts +0 -21
  88. package/src/StereoCameraRig.ts +0 -127
  89. package/src/StereoEffects.ts +0 -274
  90. package/src/TerrainFixtures.ts +0 -180
  91. package/src/Texture.ts +0 -307
  92. package/src/TextureBinding.ts +0 -91
  93. package/src/TexturedPBRMaterial.ts +0 -1112
  94. package/src/TexturedUnlitMaterial.ts +0 -67
  95. package/src/ToneMapping.ts +0 -105
  96. package/src/UniformBinder.ts +0 -16
  97. package/src/UniformLayout.ts +0 -129
  98. package/src/UnlitMaterial.ts +0 -75
  99. package/src/VegetationFixtures.ts +0 -314
  100. package/src/VertexBuffer.ts +0 -120
  101. package/src/VertexFormat.ts +0 -171
  102. package/src/VoxelWorldFixtures.ts +0 -280
  103. package/src/WeatherFixtures.ts +0 -223
  104. package/src/WebGL2Device.ts +0 -2280
  105. package/src/WebGL2StateCache.ts +0 -330
  106. package/src/WebGPUDevice.ts +0 -2877
  107. package/src/WebGPURenderToTextureProof.ts +0 -139
  108. package/src/advanced-runtime/AdvancedRenderer.ts +0 -73
  109. package/src/advanced-runtime/index.ts +0 -9
  110. package/src/animation/AnimationMaterialStyle.ts +0 -32
  111. package/src/animation/AnimationRenderPreset.ts +0 -45
  112. package/src/animation/AnimationToonMaterial.ts +0 -387
  113. package/src/animation/AnimationVisualQuality.ts +0 -71
  114. package/src/animation/applyAnimationRenderPreset.ts +0 -180
  115. package/src/animation/index.ts +0 -5
  116. package/src/cinematic/BloomPass.ts +0 -26
  117. package/src/cinematic/CinematicDepthComposition.ts +0 -30
  118. package/src/cinematic/CinematicEvidence.ts +0 -123
  119. package/src/cinematic/CinematicLightingRig.ts +0 -144
  120. package/src/cinematic/CinematicMaterialPresets.ts +0 -130
  121. package/src/cinematic/CinematicPostProcess.ts +0 -83
  122. package/src/cinematic/DepthHazePass.ts +0 -30
  123. package/src/cinematic/EmissivePracticalLightSystem.ts +0 -45
  124. package/src/cinematic/FilmGrainPass.ts +0 -24
  125. package/src/cinematic/FogVolumeSystem.ts +0 -30
  126. package/src/cinematic/GlowCardSystem.ts +0 -44
  127. package/src/cinematic/RainParticleSystem.ts +0 -151
  128. package/src/cinematic/VignettePass.ts +0 -25
  129. package/src/cinematic/WetReflectionApproximation.ts +0 -27
  130. package/src/cinematic/index.ts +0 -14
  131. package/src/effects/CollisionModule.ts +0 -64
  132. package/src/effects/ColorModule.ts +0 -60
  133. package/src/effects/ForceModule.ts +0 -25
  134. package/src/effects/GPUParticleBackend.ts +0 -569
  135. package/src/effects/Particle.ts +0 -108
  136. package/src/effects/ParticleDiagnostics.ts +0 -224
  137. package/src/effects/ParticleEffectPresets.ts +0 -147
  138. package/src/effects/ParticleEmitter.ts +0 -326
  139. package/src/effects/ParticleModule.ts +0 -26
  140. package/src/effects/ParticleRenderPass.ts +0 -117
  141. package/src/effects/ParticleRenderer.ts +0 -132
  142. package/src/effects/ParticleSystem.ts +0 -333
  143. package/src/effects/SizeModule.ts +0 -56
  144. package/src/effects/TrailModule.ts +0 -72
  145. package/src/effects/VelocityModule.ts +0 -75
  146. package/src/effects/index.ts +0 -15
  147. package/src/index.ts +0 -925
  148. package/src/materials/AlphaSorting.ts +0 -30
  149. package/src/materials/MaterialExtensions.ts +0 -42
  150. package/src/materials/PhysicalMaterial.ts +0 -133
  151. package/src/materials/TransmissionPass.ts +0 -51
  152. package/src/performance/BVH.ts +0 -18
  153. package/src/performance/Batcher.ts +0 -2
  154. package/src/performance/FrustumCuller.ts +0 -9
  155. package/src/performance/LOD.ts +0 -20
  156. package/src/performance/Octree.ts +0 -10
  157. package/src/performance/RenderItemSorting.ts +0 -170
  158. package/src/performance/RendererStats.ts +0 -43
  159. package/src/performance/ResourceBudget.ts +0 -44
  160. package/src/postprocess/BloomPass.ts +0 -28
  161. package/src/postprocess/CinematicDiagnostics.ts +0 -642
  162. package/src/postprocess/ColorGradingPass.ts +0 -24
  163. package/src/postprocess/DepthOfFieldPass.ts +0 -12
  164. package/src/postprocess/EffectComposer.ts +0 -349
  165. package/src/postprocess/SSAOPass.ts +0 -20
  166. package/src/postprocess/index.ts +0 -34
  167. package/src/production-runtime/AnimationControlsPipeline.ts +0 -77
  168. package/src/production-runtime/PBRHDRPipeline.ts +0 -381
  169. package/src/production-runtime/ProductionEffectsPipeline.ts +0 -85
  170. package/src/production-runtime/ProductionRendererTypes.ts +0 -136
  171. package/src/production-runtime/ProductionRuntimeRenderer.ts +0 -172
  172. package/src/production-runtime/ProductionWebGL2Renderer.ts +0 -313
  173. package/src/production-runtime/ProductionWebGPURenderer.ts +0 -396
  174. package/src/production-runtime/TransmissionBackdropCapture.ts +0 -130
  175. package/src/production-runtime/animation/MorphTargetRenderer.ts +0 -2
  176. package/src/production-runtime/animation/SkinningRenderer.ts +0 -2
  177. package/src/production-runtime/backends/RendererBackend.ts +0 -17
  178. package/src/production-runtime/backends/WebGL2RendererBackend.ts +0 -30
  179. package/src/production-runtime/backends/WebGPURendererBackend.ts +0 -24
  180. package/src/production-runtime/backends/webgl2/WebGL2Buffer.ts +0 -2
  181. package/src/production-runtime/backends/webgl2/WebGL2Capabilities.ts +0 -2
  182. package/src/production-runtime/backends/webgl2/WebGL2RenderTarget.ts +0 -2
  183. package/src/production-runtime/backends/webgl2/WebGL2Shader.ts +0 -2
  184. package/src/production-runtime/backends/webgl2/WebGL2StateCache.ts +0 -1
  185. package/src/production-runtime/backends/webgl2/WebGL2Texture.ts +0 -2
  186. package/src/production-runtime/backends/webgpu/WebGPUBuffer.ts +0 -2
  187. package/src/production-runtime/backends/webgpu/WebGPUCapabilities.ts +0 -2
  188. package/src/production-runtime/backends/webgpu/WebGPURenderTarget.ts +0 -2
  189. package/src/production-runtime/backends/webgpu/WebGPUShader.ts +0 -2
  190. package/src/production-runtime/backends/webgpu/WebGPUTexture.ts +0 -2
  191. package/src/production-runtime/color/ColorManagement.ts +0 -3
  192. package/src/production-runtime/color/ToneMapping.ts +0 -7
  193. package/src/production-runtime/diagnostics/FrameCapture.ts +0 -2
  194. package/src/production-runtime/diagnostics/GPUCapabilities.ts +0 -2
  195. package/src/production-runtime/diagnostics/RendererStats.ts +0 -2
  196. package/src/production-runtime/environment/EnvironmentMap.ts +0 -1
  197. package/src/production-runtime/environment/HDRLoader.ts +0 -57
  198. package/src/production-runtime/environment/PMREMGenerator.ts +0 -713
  199. package/src/production-runtime/framegraph/FrameGraph.ts +0 -18
  200. package/src/production-runtime/framegraph/RenderPass.ts +0 -15
  201. package/src/production-runtime/geometry/ProjectedDecalGeometry.ts +0 -439
  202. package/src/production-runtime/index.ts +0 -127
  203. package/src/production-runtime/lights/LightManager.ts +0 -7
  204. package/src/production-runtime/lights/ShadowMapRenderer.ts +0 -5
  205. package/src/production-runtime/materials/GLTFMaterialAdapter.ts +0 -6
  206. package/src/production-runtime/materials/GLTFPBRMaterialAdapter.ts +0 -2
  207. package/src/production-runtime/materials/MaterialCompiler.ts +0 -7
  208. package/src/production-runtime/materials/MaterialTextureBindings.ts +0 -5
  209. package/src/production-runtime/materials/PBRMaterial.ts +0 -2
  210. package/src/production-runtime/materials/PBRShaderFeatures.ts +0 -2
  211. package/src/production-runtime/passes/ContactShadowPass.ts +0 -184
  212. package/src/production-runtime/passes/DepthPrepass.ts +0 -7
  213. package/src/production-runtime/passes/OpaquePass.ts +0 -7
  214. package/src/production-runtime/passes/ShadowPass.ts +0 -7
  215. package/src/production-runtime/passes/SkyboxPass.ts +0 -7
  216. package/src/production-runtime/passes/ToneMappingPass.ts +0 -7
  217. package/src/production-runtime/passes/TransparentPass.ts +0 -7
  218. package/src/production-runtime/postprocess/BloomPass.ts +0 -51
  219. package/src/production-runtime/postprocess/ColorGradingPass.ts +0 -49
  220. package/src/production-runtime/postprocess/DOFPass.ts +0 -47
  221. package/src/production-runtime/postprocess/FXAAPass.ts +0 -53
  222. package/src/production-runtime/postprocess/ProductionEffectComposer.ts +0 -78
  223. package/src/production-runtime/postprocess/ProductionPostProcessTypes.ts +0 -174
  224. package/src/production-runtime/postprocess/SSAOPass.ts +0 -45
  225. package/src/production-runtime/resources/GPUBuffer.ts +0 -2
  226. package/src/production-runtime/resources/GPUTexture.ts +0 -2
  227. package/src/production-runtime/resources/RenderTarget.ts +0 -3
  228. package/src/production-runtime/resources/ResourceCache.ts +0 -7
  229. package/src/production-runtime/scene/Camera.ts +0 -1
  230. package/src/production-runtime/scene/Lights.ts +0 -2
  231. package/src/production-runtime/scene/RenderableMesh.ts +0 -2
  232. package/src/production-runtime/scene/RenderablePrimitive.ts +0 -1
  233. package/src/production-runtime/scene/RenderableScene.ts +0 -4
  234. package/src/production-runtime/shaders/ShaderProgramLibrary.ts +0 -7
  235. package/src/production-runtime/shaders/chunks/brdf.glsl +0 -2
  236. package/src/production-runtime/shaders/chunks/ibl.glsl +0 -3
  237. package/src/production-runtime/shaders/chunks/pbr.frag.glsl +0 -47
  238. package/src/production-runtime/shaders/chunks/pbr.vert.glsl +0 -8
  239. package/src/production-runtime/shaders/chunks/shadows.glsl +0 -2
  240. package/src/production-runtime/shaders/wgsl/pbr.wgsl +0 -3
  241. package/src/production-runtime/shaders/wgsl/postprocess.wgsl +0 -2
  242. package/src/production-runtime/shaders/wgsl/skybox.wgsl +0 -2
  243. package/src/shaders/depth.frag.glsl +0 -5
  244. package/src/shaders/depth.vert.glsl +0 -8
  245. package/src/shaders/pbr-direct.frag.glsl +0 -687
  246. package/src/shaders/pbr-direct.vert.glsl +0 -18
  247. package/src/shaders/unlit.frag.glsl +0 -18
  248. package/src/shaders/unlit.vert.glsl +0 -13
  249. package/src/shadows/CascadedShadowPipeline.ts +0 -53
  250. package/src/shadows/ContactShadows.ts +0 -106
  251. package/src/shadows/ShadowDebugViews.ts +0 -46
  252. package/src/threejs-compatibility/InstancingSystem.ts +0 -17
  253. package/src/threejs-compatibility/LightingSystem.ts +0 -22
  254. package/src/threejs-compatibility/MaterialSystem.ts +0 -9
  255. package/src/threejs-compatibility/RenderTargetSystem.ts +0 -35
  256. package/src/threejs-compatibility/RendererDiagnostics.ts +0 -61
  257. package/src/threejs-compatibility/SceneRenderer.ts +0 -34
  258. package/src/threejs-compatibility/ShadowSystem.ts +0 -19
  259. package/src/threejs-compatibility/TextureSystem.ts +0 -19
  260. package/src/threejs-compatibility/ThreeCompatRenderer.ts +0 -90
  261. package/src/threejs-compatibility/TransparencySystem.ts +0 -19
  262. package/src/threejs-compatibility/index.ts +0 -29
  263. package/src/threejs-compatibility/performance/BVH.ts +0 -14
  264. package/src/threejs-compatibility/performance/FrustumCulling.ts +0 -10
  265. package/src/threejs-compatibility/performance/Instancing.ts +0 -4
  266. package/src/threejs-compatibility/performance/LODSystem.ts +0 -16
  267. package/src/threejs-compatibility/performance/OcclusionCulling.ts +0 -6
  268. package/src/threejs-compatibility/performance/RaycastAcceleration.ts +0 -12
  269. package/src/threejs-compatibility/performance/RendererProfiler.ts +0 -19
  270. package/src/threejs-compatibility/performance/TextureStreaming.ts +0 -4
  271. package/src/threejs-compatibility/performance/index.ts +0 -12
  272. package/src/threejs-compatibility/postprocess/BloomPass.ts +0 -16
  273. package/src/threejs-compatibility/postprocess/ColorGradingPass.ts +0 -16
  274. package/src/threejs-compatibility/postprocess/DepthOfFieldPass.ts +0 -21
  275. package/src/threejs-compatibility/postprocess/EffectComposer.ts +0 -14
  276. package/src/threejs-compatibility/postprocess/FXAAPass.ts +0 -15
  277. package/src/threejs-compatibility/postprocess/MotionBlurPass.ts +0 -20
  278. package/src/threejs-compatibility/postprocess/OutlinePass.ts +0 -21
  279. package/src/threejs-compatibility/postprocess/PostProcessTypes.ts +0 -169
  280. package/src/threejs-compatibility/postprocess/RenderPass.ts +0 -9
  281. package/src/threejs-compatibility/postprocess/SMAAPass.ts +0 -15
  282. package/src/threejs-compatibility/postprocess/SSAOPass.ts +0 -21
  283. package/src/threejs-compatibility/postprocess/ShaderPass.ts +0 -7
  284. package/src/threejs-compatibility/postprocess/TAAPass.ts +0 -15
  285. package/src/threejs-compatibility/postprocess/VignettePass.ts +0 -16
  286. package/src/threejs-compatibility/postprocess/index.ts +0 -15
  287. package/src/threejs-compatibility/shaders/NodeMaterial.ts +0 -21
  288. package/src/threejs-compatibility/shaders/RawShaderMaterial.ts +0 -6
  289. package/src/threejs-compatibility/shaders/ShaderDiagnostics.ts +0 -17
  290. package/src/threejs-compatibility/shaders/ShaderMaterial.ts +0 -18
  291. package/src/threejs-compatibility/shaders/ThreeCompatShaderChunks.ts +0 -6
  292. package/src/threejs-compatibility/shaders/Uniforms.ts +0 -18
  293. package/src/threejs-compatibility/shaders/index.ts +0 -9
  294. package/src/threejs-compatibility/vfx/GPUPointCloud.ts +0 -7
  295. package/src/threejs-compatibility/vfx/LineRenderer.ts +0 -13
  296. package/src/threejs-compatibility/vfx/ParticleSystem.ts +0 -23
  297. package/src/threejs-compatibility/vfx/SpriteSystem.ts +0 -14
  298. package/src/threejs-compatibility/vfx/TrailRenderer.ts +0 -8
  299. package/src/threejs-compatibility/vfx/VFXDiagnostics.ts +0 -23
  300. package/src/threejs-compatibility/vfx/index.ts +0 -9
  301. package/src/threejs-example-parity/index.ts +0 -205
  302. package/src/webgpu/WebGPUBuffer.ts +0 -1
  303. package/src/webgpu/WebGPUCompute.ts +0 -2
  304. package/src/webgpu/WebGPUPipelineCache.ts +0 -15
  305. package/src/webgpu/WebGPUPostProcess.ts +0 -2
  306. package/src/webgpu/WebGPUTexture.ts +0 -2
  307. package/tsconfig.json +0 -26
  308. package/vitest.config.ts +0 -20
@@ -1,2766 +0,0 @@
1
- import { ShaderPreprocessor, type ShaderPreprocessOptions } from "./ShaderPreprocessor";
2
- import { DEFAULT_PBR_SHADER_MARKER, DEFAULT_PBR_SHADER_NAME } from "./PBRMaterial";
3
- import { SHADER_CHUNKS, validateShaderChunks } from "./ShaderChunks";
4
- import { registerAnimationToonShader } from "./animation/AnimationToonMaterial.js";
5
-
6
- export interface ShaderSourcePair {
7
- readonly name: string;
8
- readonly marker: string;
9
- readonly vertex: string;
10
- readonly fragment: string;
11
- readonly variants?: readonly ShaderVariantDescriptor[];
12
- }
13
-
14
- export interface ShaderVariantDescriptor {
15
- readonly name: string;
16
- readonly defines?: Readonly<Record<string, string | number | boolean>>;
17
- }
18
-
19
- export interface CompiledShaderSource {
20
- readonly label: string;
21
- readonly marker: string;
22
- readonly vertex: string;
23
- readonly fragment: string;
24
- }
25
-
26
- export class ShaderLibrary {
27
- private readonly shaders = new Map<string, ShaderSourcePair>();
28
- private readonly chunks = new Map<string, string>();
29
- private readonly preprocessor = new ShaderPreprocessor();
30
- private readonly variantCache = new Map<string, CompiledShaderSource>();
31
-
32
- register(shader: ShaderSourcePair): void {
33
- if (this.shaders.has(shader.name)) {
34
- throw new Error(`Shader is already registered: ${shader.name}`);
35
- }
36
- this.assertMarker(shader);
37
- this.assertVariants(shader);
38
- this.shaders.set(shader.name, shader);
39
- this.variantCache.clear();
40
- }
41
-
42
- registerChunk(name: string, source: string): void {
43
- if (this.chunks.has(name)) {
44
- throw new Error(`Shader chunk is already registered: ${name}`);
45
- }
46
- this.chunks.set(name, source);
47
- this.variantCache.clear();
48
- }
49
-
50
- get(name: string): ShaderSourcePair {
51
- const shader = this.shaders.get(name);
52
- if (!shader) {
53
- throw new Error(`Shader is not registered: ${name}`);
54
- }
55
- return shader;
56
- }
57
-
58
- compileSource(name: string, options: ShaderPreprocessOptions = {}): CompiledShaderSource {
59
- const shader = this.get(name);
60
- const includes = new Map([...this.chunks, ...(options.includes ?? new Map())]);
61
- const preprocessOptions = { ...options, includes };
62
- const vertex = this.preprocessor.preprocess(shader.vertex, preprocessOptions).source;
63
- const fragment = this.preprocessor.preprocess(shader.fragment, preprocessOptions).source;
64
- if (!vertex.includes(shader.marker) || !fragment.includes(shader.marker)) {
65
- throw new Error(`Shader marker ${shader.marker} was not preserved for ${name}`);
66
- }
67
- return { label: name, marker: shader.marker, vertex, fragment };
68
- }
69
-
70
- compileVariant(name: string, variantName: string, options: ShaderPreprocessOptions = {}): CompiledShaderSource {
71
- const shader = this.get(name);
72
- const variant = shader.variants?.find((candidate) => candidate.name === variantName);
73
- if (!variant) {
74
- throw new Error(`Shader variant is not registered: ${name}:${variantName}`);
75
- }
76
- const compileOptions: ShaderPreprocessOptions = {
77
- ...options,
78
- defines: {
79
- ...(variant.defines ?? {}),
80
- ...(options.defines ?? {})
81
- }
82
- };
83
- const cacheKey = variantCacheKey(name, variantName, compileOptions);
84
- const cached = this.variantCache.get(cacheKey);
85
- if (cached) {
86
- return cached;
87
- }
88
- const compiled = this.compileSource(name, compileOptions);
89
- const labeled = { ...compiled, label: `${name}:${variantName}` };
90
- this.variantCache.set(cacheKey, labeled);
91
- return labeled;
92
- }
93
-
94
- names(): readonly string[] {
95
- return [...this.shaders.keys()];
96
- }
97
-
98
- private assertMarker(shader: ShaderSourcePair): void {
99
- if (!shader.marker.trim()) {
100
- throw new Error(`Shader ${shader.name} must declare a non-empty source marker`);
101
- }
102
- if (!shader.vertex.includes(shader.marker) || !shader.fragment.includes(shader.marker)) {
103
- throw new Error(`Shader ${shader.name} must include marker ${shader.marker} in both stages`);
104
- }
105
- }
106
-
107
- private assertVariants(shader: ShaderSourcePair): void {
108
- const seen = new Set<string>();
109
- for (const variant of shader.variants ?? []) {
110
- if (!variant.name.trim()) {
111
- throw new Error(`Shader ${shader.name} has a variant with an empty name`);
112
- }
113
- if (seen.has(variant.name)) {
114
- throw new Error(`Shader variant is already registered: ${shader.name}:${variant.name}`);
115
- }
116
- seen.add(variant.name);
117
- }
118
- }
119
- }
120
-
121
- function variantCacheKey(name: string, variantName: string, options: ShaderPreprocessOptions): string {
122
- return JSON.stringify({
123
- name,
124
- variantName,
125
- defines: Object.entries(options.defines ?? {}).sort(([left], [right]) => left.localeCompare(right)),
126
- includes: [...(options.includes ?? new Map()).entries()].sort(([left], [right]) => left.localeCompare(right))
127
- });
128
- }
129
-
130
- export const DEFAULT_UNLIT_SHADER_NAME = "aura3d/unlit";
131
- export const DEFAULT_UNLIT_SHADER_MARKER = "@aura3d-shader:unlit";
132
- export const DEFAULT_INSTANCED_UNLIT_SHADER_NAME = "aura3d/instanced-unlit";
133
- export const DEFAULT_INSTANCED_UNLIT_SHADER_MARKER = "@aura3d-shader:instanced-unlit";
134
- export const DEFAULT_INSTANCED_PBR_SHADER_NAME = "aura3d/instanced-pbr";
135
- export const DEFAULT_INSTANCED_PBR_SHADER_MARKER = "@aura3d-shader:instanced-pbr";
136
- export const DEFAULT_TEXTURED_UNLIT_SHADER_NAME = "aura3d/textured-unlit";
137
- export const DEFAULT_TEXTURED_UNLIT_SHADER_MARKER = "@aura3d-shader:textured-unlit";
138
- export const DEFAULT_SKINNED_UNLIT_SHADER_NAME = "aura3d/skinned-unlit";
139
- export const DEFAULT_SKINNED_UNLIT_SHADER_MARKER = "@aura3d-shader:skinned-unlit";
140
- export const DEFAULT_SKINNED_LIT_SHADER_NAME = "aura3d/skinned-lit";
141
- export const DEFAULT_SKINNED_LIT_SHADER_MARKER = "@aura3d-shader:skinned-lit";
142
- export const DEFAULT_MORPH_UNLIT_SHADER_NAME = "aura3d/morph-unlit";
143
- export const DEFAULT_MORPH_UNLIT_SHADER_MARKER = "@aura3d-shader:morph-unlit";
144
- export const DEFAULT_NORMAL_MAPPED_PBR_SHADER_NAME = "aura3d/pbr-normal-map";
145
- export const DEFAULT_NORMAL_MAPPED_PBR_SHADER_MARKER = "@aura3d-shader:pbr-normal-map";
146
- export const DEFAULT_TEXTURED_PBR_SHADER_NAME = "aura3d/pbr-textured";
147
- export const DEFAULT_TEXTURED_PBR_SHADER_MARKER = "@aura3d-shader:pbr-textured";
148
- export const DEFAULT_TEXTURED_PBR_CLEARCOAT_TEXTURES_VARIANT = "clearcoat-textures";
149
- export const DEFAULT_TEXTURED_PBR_TRANSMISSION_VOLUME_TEXTURES_VARIANT = "transmission-volume-textures";
150
- export const DEFAULT_TEXTURED_PBR_SPECULAR_SHEEN_ANISOTROPY_TEXTURES_VARIANT = "specular-sheen-anisotropy-textures";
151
- export const DEFAULT_TEXTURED_PBR_IRIDESCENCE_TEXTURES_VARIANT = "iridescence-textures";
152
- export const DEFAULT_TEXTURED_PBR_CLEARCOAT_TRANSMISSION_VOLUME_TEXTURES_VARIANT = "clearcoat-transmission-volume-textures";
153
- export const DEFAULT_TEXTURED_PBR_CLEARCOAT_SPECULAR_TEXTURES_VARIANT = "clearcoat-specular-textures";
154
- export const DEFAULT_TEXTURED_PBR_SPECULAR_SHEEN_ANISOTROPY_IRIDESCENCE_TEXTURES_VARIANT = "specular-sheen-anisotropy-iridescence-textures";
155
- export const DEFAULT_DEPTH_SHADER_NAME = "aura3d/depth";
156
- export const DEFAULT_DEPTH_SHADER_MARKER = "@aura3d-shader:depth";
157
- export const DEFAULT_ENVIRONMENT_BACKGROUND_SHADER_NAME = "aura3d/environment-background";
158
- export const DEFAULT_ENVIRONMENT_BACKGROUND_SHADER_MARKER = "@aura3d-shader:environment-background";
159
-
160
- export function createDefaultShaderLibrary(): ShaderLibrary {
161
- const library = new ShaderLibrary();
162
- validateShaderChunks();
163
- for (const chunk of SHADER_CHUNKS) {
164
- library.registerChunk(chunk.name, chunk.source);
165
- }
166
- library.register({
167
- name: DEFAULT_UNLIT_SHADER_NAME,
168
- marker: DEFAULT_UNLIT_SHADER_MARKER,
169
- vertex: `#version 300 es
170
- // ${DEFAULT_UNLIT_SHADER_MARKER}
171
- precision highp float;
172
- layout(location = 0) in vec3 a_position;
173
- layout(location = 4) in vec4 a_color;
174
- uniform mat4 u_modelViewProjection;
175
- uniform float u_pointSize;
176
- out vec4 v_vertexColor;
177
- void main() {
178
- v_vertexColor = a_color;
179
- gl_PointSize = max(u_pointSize, 1.0);
180
- gl_Position = u_modelViewProjection * vec4(a_position, 1.0);
181
- }
182
- `,
183
- fragment: `#version 300 es
184
- // ${DEFAULT_UNLIT_SHADER_MARKER}
185
- precision highp float;
186
- uniform vec4 u_baseColor;
187
- uniform float u_alphaCutoff;
188
- uniform float u_roundPoints;
189
- in vec4 v_vertexColor;
190
- out vec4 outColor;
191
- void main() {
192
- if (u_roundPoints > 0.5) {
193
- vec2 pointUv = gl_PointCoord * 2.0 - 1.0;
194
- float radius = dot(pointUv, pointUv);
195
- if (radius > 1.0) discard;
196
- }
197
- vec4 base = u_baseColor * v_vertexColor;
198
- if (base.a < u_alphaCutoff) discard;
199
- outColor = base;
200
- }
201
- `
202
- });
203
- library.register({
204
- name: DEFAULT_INSTANCED_UNLIT_SHADER_NAME,
205
- marker: DEFAULT_INSTANCED_UNLIT_SHADER_MARKER,
206
- vertex: `#version 300 es
207
- // ${DEFAULT_INSTANCED_UNLIT_SHADER_MARKER}
208
- precision highp float;
209
- layout(location = 0) in vec3 a_position;
210
- layout(location = 4) in vec4 a_color;
211
- uniform mat4 u_modelViewProjection;
212
- uniform mat4 u_instanceMatrices[64];
213
- uniform float u_instanceCount;
214
- uniform float u_instanceAttributeMode;
215
- layout(location = 8) in vec4 a_instanceMatrix0;
216
- layout(location = 9) in vec4 a_instanceMatrix1;
217
- layout(location = 10) in vec4 a_instanceMatrix2;
218
- layout(location = 11) in vec4 a_instanceMatrix3;
219
- layout(location = 12) in vec4 a_instanceColor;
220
- out vec4 v_vertexColor;
221
- void main() {
222
- int instanceIndex = clamp(gl_InstanceID, 0, max(int(u_instanceCount) - 1, 0));
223
- mat4 attributeMatrix = mat4(a_instanceMatrix0, a_instanceMatrix1, a_instanceMatrix2, a_instanceMatrix3);
224
- mat4 instanceMatrix = u_instanceAttributeMode > 0.5 ? attributeMatrix : u_instanceMatrices[instanceIndex];
225
- v_vertexColor = a_color * a_instanceColor;
226
- gl_Position = u_modelViewProjection * instanceMatrix * vec4(a_position, 1.0);
227
- }
228
- `,
229
- fragment: `#version 300 es
230
- // ${DEFAULT_INSTANCED_UNLIT_SHADER_MARKER}
231
- precision highp float;
232
- uniform vec4 u_baseColor;
233
- uniform float u_alphaCutoff;
234
- in vec4 v_vertexColor;
235
- out vec4 outColor;
236
- void main() {
237
- vec4 base = u_baseColor * v_vertexColor;
238
- if (base.a < u_alphaCutoff) discard;
239
- outColor = base;
240
- }
241
- `
242
- });
243
- library.register({
244
- name: DEFAULT_PBR_SHADER_NAME,
245
- marker: DEFAULT_PBR_SHADER_MARKER,
246
- vertex: `#version 300 es
247
- // ${DEFAULT_PBR_SHADER_MARKER}
248
- precision highp float;
249
- layout(location = 0) in vec3 a_position;
250
- layout(location = 1) in vec3 a_normal;
251
- layout(location = 4) in vec4 a_color;
252
- uniform mat4 u_modelViewProjection;
253
- uniform mat4 u_modelMatrix;
254
- uniform mat4 u_normalMatrix;
255
- out vec3 v_normal;
256
- out vec3 v_worldPosition;
257
- out vec4 v_vertexColor;
258
- void main() {
259
- v_normal = mat3(u_normalMatrix) * a_normal;
260
- v_worldPosition = (u_modelMatrix * vec4(a_position, 1.0)).xyz;
261
- v_vertexColor = a_color;
262
- gl_Position = u_modelViewProjection * vec4(a_position, 1.0);
263
- }
264
- `,
265
- fragment: `#version 300 es
266
- // ${DEFAULT_PBR_SHADER_MARKER}
267
- precision highp float;
268
- #include <lighting_common>
269
- #include <pbr_common>
270
- uniform vec4 u_baseColor;
271
- uniform float u_alphaCutoff;
272
- uniform float u_metallic;
273
- uniform float u_roughness;
274
- uniform vec3 u_environmentColor;
275
- uniform float u_environmentIntensity;
276
- uniform vec3 u_environmentSkyColor;
277
- uniform vec3 u_environmentHorizonColor;
278
- uniform vec3 u_environmentGroundColor;
279
- uniform vec3 u_environmentSpecularColor;
280
- uniform float u_environmentMapIntensity;
281
- uniform float u_environmentSpecularIntensity;
282
- uniform sampler2D u_environmentMapTexture;
283
- uniform samplerCube u_environmentCubeMapTexture;
284
- uniform float u_environmentCubeMapTextureEnabled;
285
- uniform float u_environmentMapTextureEnabled;
286
- uniform float u_environmentMapTextureIntensity;
287
- uniform float u_environmentMapTextureSpecularIntensity;
288
- uniform float u_environmentMapTextureRotation;
289
- uniform float u_environmentMapTextureMipCount;
290
- uniform float u_environmentMapTextureEncoding;
291
- uniform sampler2D u_environmentBrdfLutTexture;
292
- uniform float u_environmentBrdfLutEnabled;
293
- uniform vec3 u_emissiveColor;
294
- uniform float u_emissiveStrength;
295
- uniform float u_clearcoatFactor;
296
- uniform float u_clearcoatRoughnessFactor;
297
- uniform float u_transmissionFactor;
298
- uniform float u_diffuseTransmissionFactor;
299
- uniform vec3 u_diffuseTransmissionColorFactor;
300
- uniform float u_transmissionFallbackEnergy;
301
- uniform float u_volumeThicknessFactor;
302
- uniform float u_volumeAttenuationDistance;
303
- uniform vec3 u_volumeAttenuationColor;
304
- uniform float u_transmissionParallaxStrength;
305
- uniform vec3 u_transmissionParallaxBoxMin;
306
- uniform vec3 u_transmissionParallaxBoxMax;
307
- uniform float u_transmissionBounceCount;
308
- uniform float u_transmissionCausticStrength;
309
- uniform float u_ior;
310
- uniform float u_specularFactor;
311
- uniform vec3 u_specularColorFactor;
312
- uniform vec3 u_sheenColorFactor;
313
- uniform float u_sheenRoughnessFactor;
314
- uniform float u_anisotropyStrength;
315
- uniform float u_anisotropyRotation;
316
- uniform float u_iridescenceFactor;
317
- uniform float u_iridescenceIor;
318
- uniform float u_iridescenceThicknessMinimum;
319
- uniform float u_iridescenceThicknessMaximum;
320
- uniform float u_dispersion;
321
- uniform float u_lightCount;
322
- uniform vec4 u_lightData[64];
323
- uniform sampler2D u_shadowMapTexture;
324
- uniform float u_shadowMapEnabled;
325
- uniform mat4 u_shadowMapMatrix;
326
- uniform float u_shadowMapStrength;
327
- uniform float u_shadowMapBias;
328
- uniform float u_shadowMapSlopeBias;
329
- uniform vec2 u_shadowMapTexelSize;
330
- uniform float u_shadowPcfSampleCount;
331
- uniform vec4 u_shadowPcfSamples[32];
332
- uniform sampler2D u_pointShadowMapTexture;
333
- uniform float u_pointShadowMapEnabled;
334
- uniform vec3 u_pointShadowLightPosition;
335
- uniform float u_pointShadowRange;
336
- uniform mat4 u_pointShadowFaceMatrices[6];
337
- uniform vec4 u_pointShadowFaceRects[6];
338
- uniform float u_pointShadowStrength;
339
- uniform float u_pointShadowBias;
340
- uniform float u_pointShadowSlopeBias;
341
- uniform vec2 u_pointShadowTexelSize;
342
- uniform float u_pointShadowPcfSampleCount;
343
- uniform vec4 u_pointShadowPcfSamples[32];
344
- uniform float u_outputColorSpace;
345
- uniform vec3 u_cameraPosition;
346
- uniform float u_environmentFogEnabled;
347
- uniform float u_environmentFogMode;
348
- uniform vec3 u_environmentFogColor;
349
- uniform float u_environmentFogNear;
350
- uniform float u_environmentFogFar;
351
- uniform float u_environmentFogDensity;
352
- uniform float u_environmentFogHeightFalloff;
353
- uniform float u_environmentFogHeightReference;
354
- uniform float u_environmentFogMaxOpacity;
355
- in vec3 v_normal;
356
- in vec3 v_worldPosition;
357
- in vec4 v_vertexColor;
358
- out vec4 outColor;
359
- #include <environment_fog_common>
360
- vec2 a3dEnvironmentEquirectUv(vec3 direction, float rotation) {
361
- vec3 d = normalize(direction);
362
- float u = atan(d.z, d.x) / 6.28318530718 + 0.5 + rotation;
363
- float v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
364
- return vec2(fract(u), clamp(v, 0.0, 1.0));
365
- }
366
- vec3 a3dEnvironmentCubeDirection(vec3 direction, float rotation) {
367
- float angle = rotation * 6.28318530718;
368
- float c = cos(angle);
369
- float s = sin(angle);
370
- vec3 d = normalize(direction);
371
- return normalize(vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z));
372
- }
373
- vec3 a3dPbrDecodeEnvironmentSrgb(vec3 encodedColor) {
374
- return max(encodedColor, vec3(0.0));
375
- }
376
- vec3 a3dPbrDecodeEnvironmentRgbe(vec4 encodedSample) {
377
- float exponent = encodedSample.a * 255.0;
378
- float scale = exponent <= 0.0 ? 0.0 : exp2(exponent - 128.0) * (255.0 / 256.0);
379
- return max(encodedSample.rgb * scale, vec3(0.0));
380
- }
381
- vec3 a3dPbrDecodeEnvironmentSample(vec4 encodedSample) {
382
- if (u_environmentMapTextureEncoding > 1.5) return max(encodedSample.rgb, vec3(0.0));
383
- if (u_environmentMapTextureEncoding > 0.5) return a3dPbrDecodeEnvironmentRgbe(encodedSample);
384
- return a3dPbrDecodeEnvironmentSrgb(encodedSample.rgb);
385
- }
386
- vec3 a3dPbrBoundHdrTransmissionRadiance(vec3 radiance) {
387
- vec3 nonNegative = max(radiance, vec3(0.0));
388
- return nonNegative / (vec3(1.0) + max(nonNegative - vec3(1.0), vec3(0.0)));
389
- }
390
- vec3 a3dPbrBoundHdrSpecularRadiance(vec3 radiance) {
391
- vec3 nonNegative = max(radiance, vec3(0.0));
392
- vec3 softKnee = nonNegative / (vec3(1.0) + max(nonNegative - vec3(1.0), vec3(0.0)) * 0.58);
393
- float maxChannel = max(max(softKnee.r, softKnee.g), softKnee.b);
394
- return maxChannel > 1.65 ? softKnee * (1.65 / maxChannel) : softKnee;
395
- }
396
- vec3 a3dPbrClampSampledSpecularEdgeEnergy(vec3 radiance, float nDotV, float roughness) {
397
- float faceOn = smoothstep(0.12, 0.55, clamp(nDotV, 0.0, 1.0));
398
- float roughEnergy = mix(0.6, 1.0, clamp(roughness, 0.0, 1.0));
399
- float edgeCap = mix(0.12, 1.2, faceOn) * roughEnergy;
400
- float edgeScale = mix(0.14, 0.92, faceOn);
401
- vec3 bounded = min(radiance * edgeScale, vec3(edgeCap));
402
- return max(bounded, vec3(0.0));
403
- }
404
- vec4 a3dPbrEnvironmentSampleRaw(vec3 direction, float lod) {
405
- vec4 equirectSample = textureLod(u_environmentMapTexture, a3dEnvironmentEquirectUv(direction, u_environmentMapTextureRotation), lod);
406
- vec4 cubeSample = textureLod(u_environmentCubeMapTexture, a3dEnvironmentCubeDirection(direction, u_environmentMapTextureRotation), lod);
407
- return mix(equirectSample, cubeSample, step(0.5, u_environmentCubeMapTextureEnabled));
408
- }
409
- vec3 a3dPbrBoxProjectedDirection(vec3 worldPosition, vec3 direction, vec3 boxMin, vec3 boxMax) {
410
- vec3 safeDirection = normalize(direction);
411
- vec3 invDirection = 1.0 / max(abs(safeDirection), vec3(0.0001)) * sign(safeDirection);
412
- vec3 firstPlane = mix(boxMin, boxMax, step(vec3(0.0), safeDirection));
413
- vec3 distances = (firstPlane - worldPosition) * invDirection;
414
- float travel = min(min(
415
- distances.x > 0.0 ? distances.x : 100000.0,
416
- distances.y > 0.0 ? distances.y : 100000.0
417
- ), distances.z > 0.0 ? distances.z : 100000.0);
418
- vec3 hitPosition = worldPosition + safeDirection * travel;
419
- vec3 boxCenter = (boxMin + boxMax) * 0.5;
420
- return normalize(hitPosition - boxCenter);
421
- }
422
- float a3dForwardShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
423
- if (u_shadowMapEnabled < 0.5) return 1.0;
424
- vec4 lightPosition = u_shadowMapMatrix * vec4(worldPosition, 1.0);
425
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
426
- vec2 uv = projected.xy * 0.5 + 0.5;
427
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) return 1.0;
428
- vec3 receiverNormal = normalize(normal);
429
- vec3 receiverLightDirection = lightDirection / max(length(lightDirection), 0.0001);
430
- float normalDotLight = clamp(abs(dot(receiverNormal, receiverLightDirection)), 0.0, 1.0);
431
- float slopeReceiverBias = (1.0 - normalDotLight) * u_shadowMapSlopeBias * max(u_shadowMapTexelSize.x, u_shadowMapTexelSize.y);
432
- float receiverDepth = projected.z * 0.5 + 0.5 - u_shadowMapBias - slopeReceiverBias;
433
- float shadowed = 0.0;
434
- float totalWeight = 0.0;
435
- int sampleCount = clamp(int(u_shadowPcfSampleCount), 1, 32);
436
- for (int i = 0; i < 32; ++i) {
437
- if (i >= sampleCount) break;
438
- vec4 sampleData = u_shadowPcfSamples[i];
439
- float weight = max(sampleData.z, 0.0);
440
- vec2 offset = sampleData.xy * u_shadowMapTexelSize;
441
- float storedDepth = texture(u_shadowMapTexture, uv + offset).r;
442
- shadowed += (receiverDepth > storedDepth ? 1.0 : 0.0) * weight;
443
- totalWeight += weight;
444
- }
445
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
446
- return mix(1.0, 1.0 - occlusion, clamp(u_shadowMapStrength, 0.0, 1.0));
447
- }
448
- float a3dPointShadowFaceIndex(vec3 direction) {
449
- vec3 absoluteDirection = abs(direction);
450
- if (absoluteDirection.x >= absoluteDirection.y && absoluteDirection.x >= absoluteDirection.z) return direction.x >= 0.0 ? 0.0 : 1.0;
451
- if (absoluteDirection.y >= absoluteDirection.x && absoluteDirection.y >= absoluteDirection.z) return direction.y >= 0.0 ? 2.0 : 3.0;
452
- return direction.z >= 0.0 ? 4.0 : 5.0;
453
- }
454
- float a3dPointShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
455
- if (u_pointShadowMapEnabled < 0.5) return 1.0;
456
- vec3 lightToFragment = worldPosition - u_pointShadowLightPosition;
457
- float distanceToLight = length(lightToFragment);
458
- if (distanceToLight > u_pointShadowRange) return 1.0;
459
- int faceIndex = int(a3dPointShadowFaceIndex(lightToFragment));
460
- vec4 lightPosition = u_pointShadowFaceMatrices[faceIndex] * vec4(worldPosition, 1.0);
461
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
462
- vec2 localUv = projected.xy * 0.5 + 0.5;
463
- if (localUv.x < 0.0 || localUv.x > 1.0 || localUv.y < 0.0 || localUv.y > 1.0) return 1.0;
464
- vec4 rect = u_pointShadowFaceRects[faceIndex];
465
- vec2 uv = rect.xy + localUv * rect.zw;
466
- vec3 receiverNormal = normalize(normal);
467
- vec3 receiverLightDirection = lightDirection / max(length(lightDirection), 0.0001);
468
- float normalDotLight = clamp(abs(dot(receiverNormal, receiverLightDirection)), 0.0, 1.0);
469
- float slopeReceiverBias = (1.0 - normalDotLight) * u_pointShadowSlopeBias * max(u_pointShadowTexelSize.x, u_pointShadowTexelSize.y);
470
- float receiverDepth = projected.z * 0.5 + 0.5 - u_pointShadowBias - slopeReceiverBias;
471
- float shadowed = 0.0;
472
- float totalWeight = 0.0;
473
- int sampleCount = clamp(int(u_pointShadowPcfSampleCount), 1, 32);
474
- for (int i = 0; i < 32; ++i) {
475
- if (i >= sampleCount) break;
476
- vec4 sampleData = u_pointShadowPcfSamples[i];
477
- float weight = max(sampleData.z, 0.0);
478
- vec2 offset = sampleData.xy * u_pointShadowTexelSize;
479
- float storedDepth = texture(u_pointShadowMapTexture, uv + offset).r;
480
- shadowed += (receiverDepth > storedDepth ? 1.0 : 0.0) * weight;
481
- totalWeight += weight;
482
- }
483
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
484
- return mix(1.0, 1.0 - occlusion, clamp(u_pointShadowStrength, 0.0, 1.0));
485
- }
486
- vec3 a3dPbrEncodeOutput(vec3 linearColor) {
487
- vec3 color = max(linearColor, vec3(0.0));
488
- vec3 filmic = clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), vec3(0.0), vec3(1.0));
489
- vec3 srgb = pow(filmic, vec3(1.0 / 2.2));
490
- return mix(color, srgb, step(0.5, u_outputColorSpace));
491
- }
492
- void main() {
493
- vec3 normal = normalize(v_normal);
494
- if (!gl_FrontFacing) normal = -normal;
495
- vec3 viewDirection = normalize(u_cameraPosition - v_worldPosition);
496
- vec3 materialBase = a3dApplyAdvancedPbrLobes(
497
- u_baseColor.rgb * v_vertexColor.rgb,
498
- u_clearcoatFactor,
499
- u_clearcoatRoughnessFactor,
500
- u_transmissionFactor,
501
- u_diffuseTransmissionFactor,
502
- u_diffuseTransmissionColorFactor,
503
- u_transmissionFallbackEnergy,
504
- u_volumeThicknessFactor,
505
- u_volumeAttenuationDistance,
506
- u_volumeAttenuationColor,
507
- u_ior,
508
- u_specularFactor,
509
- u_specularColorFactor,
510
- u_sheenColorFactor,
511
- u_sheenRoughnessFactor,
512
- u_anisotropyStrength,
513
- u_anisotropyRotation,
514
- u_iridescenceFactor,
515
- u_iridescenceIor,
516
- u_iridescenceThicknessMinimum,
517
- u_iridescenceThicknessMaximum,
518
- u_dispersion
519
- );
520
- float environmentHemi = mix(0.35, 1.0, clamp(normal.y * 0.5 + 0.5, 0.0, 1.0));
521
- vec3 ambientEnvironment = u_environmentColor * u_environmentIntensity * environmentHemi;
522
- float proceduralEnvironmentWeight = step(0.0001, u_environmentMapIntensity);
523
- float skyBlend = clamp(normal.y * 0.5 + 0.5, 0.0, 1.0);
524
- float horizonBlend = 1.0 - abs(normal.y);
525
- vec3 proceduralDiffuse = mix(u_environmentGroundColor, u_environmentSkyColor, skyBlend);
526
- proceduralDiffuse = mix(proceduralDiffuse, u_environmentHorizonColor, clamp(horizonBlend, 0.0, 1.0) * 0.55);
527
- vec3 environmentDiffuse = mix(ambientEnvironment, proceduralDiffuse * u_environmentMapIntensity, proceduralEnvironmentWeight);
528
- float sampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureIntensity);
529
- float diffuseEnvironmentLod = max(u_environmentMapTextureMipCount - 1.0, 0.0);
530
- vec3 sampledDiffuse = a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(normal, diffuseEnvironmentLod));
531
- environmentDiffuse = mix(environmentDiffuse, environmentDiffuse * 0.18 + sampledDiffuse * u_environmentMapTextureIntensity * 0.92, sampledEnvironmentWeight);
532
- vec3 reflectionDirection = reflect(-viewDirection, normal);
533
- float roughness = clamp(u_roughness, 0.0, 1.0);
534
- float reflectionBand = pow(clamp(reflectionDirection.y * 0.5 + 0.5, 0.0, 1.0), mix(18.0, 2.0, roughness));
535
- float roughEnvironmentFloor = mix(0.04, 0.38, roughness);
536
- float proceduralSpecularResponse = max(reflectionBand, roughEnvironmentFloor);
537
- vec3 proceduralSpecular = u_environmentSpecularColor * u_environmentSpecularIntensity * proceduralSpecularResponse * proceduralEnvironmentWeight;
538
- float environmentLod = roughness * max(u_environmentMapTextureMipCount - 1.0, 0.0);
539
- vec3 sampledSpecular = a3dPbrBoundHdrSpecularRadiance(a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(reflectionDirection, environmentLod)));
540
- float nDotV = clamp(dot(normal, viewDirection), 0.0, 1.0);
541
- vec2 brdfLut = texture(u_environmentBrdfLutTexture, vec2(nDotV, roughness)).rg;
542
- sampledSpecular = a3dPbrClampSampledSpecularEdgeEnergy(sampledSpecular, nDotV, roughness);
543
- sampledSpecular *= u_environmentMapTextureSpecularIntensity * sampledEnvironmentWeight * mix(1.1, 0.85, roughness);
544
- vec3 shaded = a3dPbrEnvironmentLightSplitSum(
545
- normal,
546
- viewDirection,
547
- environmentDiffuse,
548
- proceduralSpecular + sampledSpecular,
549
- mix(vec2(1.0, 0.0), brdfLut, step(0.0001, u_environmentBrdfLutEnabled)),
550
- materialBase,
551
- u_metallic,
552
- u_roughness,
553
- u_specularFactor,
554
- u_specularColorFactor
555
- ) + u_emissiveColor * u_emissiveStrength;
556
- shaded += a3dPbrExtensionEnvironmentLight(
557
- normal,
558
- viewDirection,
559
- proceduralSpecular + sampledSpecular,
560
- u_clearcoatFactor,
561
- u_clearcoatRoughnessFactor,
562
- u_sheenColorFactor,
563
- u_sheenRoughnessFactor,
564
- u_anisotropyStrength,
565
- u_anisotropyRotation,
566
- u_iridescenceFactor,
567
- u_iridescenceIor,
568
- u_iridescenceThicknessMinimum,
569
- u_iridescenceThicknessMaximum
570
- );
571
- float transmissionAmount = clamp(max(u_transmissionFactor, u_diffuseTransmissionFactor), 0.0, 1.0);
572
- if (transmissionAmount > 0.0001 && sampledEnvironmentWeight > 0.0001) {
573
- vec3 refractionDirection = refract(-viewDirection, normal, 1.0 / max(u_ior, 1.0));
574
- refractionDirection = length(refractionDirection) > 0.0001 ? normalize(refractionDirection) : -reflectionDirection;
575
- float parallaxStrength = clamp(u_transmissionParallaxStrength, 0.0, 1.0);
576
- if (parallaxStrength > 0.0001) {
577
- vec3 parallaxDirection = a3dPbrBoxProjectedDirection(v_worldPosition, refractionDirection, u_transmissionParallaxBoxMin, u_transmissionParallaxBoxMax);
578
- refractionDirection = normalize(mix(refractionDirection, parallaxDirection, parallaxStrength));
579
- }
580
- float bounceCount = clamp(u_transmissionBounceCount, 0.0, 4.0);
581
- float refractionLod = clamp(roughness + u_volumeThicknessFactor * 0.12 + bounceCount * 0.04 * parallaxStrength, 0.0, 1.0) * max(u_environmentMapTextureMipCount - 1.0, 0.0);
582
- vec3 refractedEnvironment = a3dPbrBoundHdrTransmissionRadiance(a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(refractionDirection, refractionLod)));
583
- float volumeTravel = clamp((max(u_volumeThicknessFactor, 0.0) * (1.0 + bounceCount * 0.18 * parallaxStrength)) / max(u_volumeAttenuationDistance, 0.0001), 0.0, 16.0);
584
- vec3 volumeTint = pow(clamp(u_volumeAttenuationColor, vec3(0.0001), vec3(1.0)), vec3(volumeTravel));
585
- float causticEnergy = u_transmissionCausticStrength * parallaxStrength * transmissionAmount * pow(1.0 - roughness, 2.0) / (1.0 + bounceCount * 0.35);
586
- float roughVolumeIorLift = mix(1.0, 1.45, clamp((u_ior - 1.0) / 1.5, 0.0, 1.0) * smoothstep(0.35, 0.95, roughness + u_volumeThicknessFactor * 0.2));
587
- float fallbackEnvironmentTransmissionEnergy = mix(1.0, clamp(u_transmissionFallbackEnergy, 0.0, 1.0), transmissionAmount);
588
- vec3 refractionRadiance = (refractedEnvironment + vec3(causticEnergy)) * volumeTint * u_environmentMapTextureIntensity * transmissionAmount * fallbackEnvironmentTransmissionEnergy * mix(0.9, 0.55, roughness) * roughVolumeIorLift;
589
- shaded = mix(shaded, shaded * 0.72 + refractionRadiance, transmissionAmount * mix(0.08, 0.58, fallbackEnvironmentTransmissionEnergy));
590
- }
591
- int count = min(int(u_lightCount), 16);
592
- for (int i = 0; i < count; ++i) {
593
- int baseIndex = i * 4;
594
- vec4 colorIntensity = u_lightData[baseIndex];
595
- vec4 positionRange = u_lightData[baseIndex + 1];
596
- vec4 directionKind = u_lightData[baseIndex + 2];
597
- vec4 spotShadowLayer = u_lightData[baseIndex + 3];
598
- float kind = directionKind.w;
599
- vec3 lightDirection = -directionKind.xyz;
600
- float attenuation = 1.0;
601
- if (kind > 0.5) {
602
- vec3 toLight = positionRange.xyz - v_worldPosition;
603
- float distanceToLight = length(toLight);
604
- lightDirection = distanceToLight > 0.0001 ? toLight / distanceToLight : -directionKind.xyz;
605
- float range = max(positionRange.w, 0.0001);
606
- float rangeFalloff = clamp(1.0 - pow(distanceToLight / range, 4.0), 0.0, 1.0);
607
- rangeFalloff *= rangeFalloff;
608
- attenuation = rangeFalloff / max(distanceToLight * distanceToLight, 1.0);
609
- }
610
- if (kind > 1.5) {
611
- vec3 lightToFragment = normalize(v_worldPosition - positionRange.xyz);
612
- float cone = dot(normalize(directionKind.xyz), lightToFragment);
613
- float outer = cos(spotShadowLayer.x);
614
- float inner = cos(spotShadowLayer.x * max(1.0 - spotShadowLayer.y, 0.001));
615
- attenuation *= smoothstep(outer, inner, cone);
616
- }
617
- float directLightIntensity = colorIntensity.a * attenuation * mix(1.0, kind > 0.5 && kind < 1.5 ? a3dPointShadowFactor(v_worldPosition, normal, lightDirection) : a3dForwardShadowFactor(v_worldPosition, normal, lightDirection), step(0.5, spotShadowLayer.z));
618
- shaded += a3dPbrDirectLight(
619
- normal,
620
- viewDirection,
621
- lightDirection,
622
- colorIntensity.rgb,
623
- directLightIntensity,
624
- materialBase,
625
- u_metallic,
626
- u_roughness,
627
- u_specularFactor,
628
- u_specularColorFactor
629
- );
630
- shaded += a3dPbrExtensionDirectLight(
631
- normal,
632
- viewDirection,
633
- lightDirection,
634
- colorIntensity.rgb,
635
- directLightIntensity,
636
- u_clearcoatFactor,
637
- u_clearcoatRoughnessFactor,
638
- u_sheenColorFactor,
639
- u_sheenRoughnessFactor,
640
- u_anisotropyStrength,
641
- u_anisotropyRotation,
642
- u_iridescenceFactor,
643
- u_iridescenceIor,
644
- u_iridescenceThicknessMinimum,
645
- u_iridescenceThicknessMaximum
646
- );
647
- }
648
- float alpha = u_baseColor.a * v_vertexColor.a;
649
- if (alpha < u_alphaCutoff) discard;
650
- vec3 fogged = a3dApplyEnvironmentFog(max(shaded, vec3(0.0)), v_worldPosition);
651
- outColor = vec4(a3dPbrEncodeOutput(fogged), alpha);
652
- }
653
- `
654
- });
655
- library.register({
656
- name: DEFAULT_INSTANCED_PBR_SHADER_NAME,
657
- marker: DEFAULT_INSTANCED_PBR_SHADER_MARKER,
658
- vertex: `#version 300 es
659
- // ${DEFAULT_INSTANCED_PBR_SHADER_MARKER}
660
- precision highp float;
661
- layout(location = 0) in vec3 a_position;
662
- layout(location = 1) in vec3 a_normal;
663
- uniform mat4 u_modelViewProjection;
664
- uniform mat4 u_modelMatrix;
665
- uniform mat4 u_normalMatrix;
666
- uniform mat4 u_instanceMatrices[64];
667
- uniform float u_instanceCount;
668
- uniform float u_instanceAttributeMode;
669
- layout(location = 8) in vec4 a_instanceMatrix0;
670
- layout(location = 9) in vec4 a_instanceMatrix1;
671
- layout(location = 10) in vec4 a_instanceMatrix2;
672
- layout(location = 11) in vec4 a_instanceMatrix3;
673
- layout(location = 12) in vec4 a_instanceColor;
674
- out vec3 v_normal;
675
- out vec3 v_worldPosition;
676
- out vec4 v_instanceColor;
677
- void main() {
678
- int instanceIndex = clamp(gl_InstanceID, 0, max(int(u_instanceCount) - 1, 0));
679
- mat4 attributeMatrix = mat4(a_instanceMatrix0, a_instanceMatrix1, a_instanceMatrix2, a_instanceMatrix3);
680
- mat4 instanceMatrix = u_instanceAttributeMode > 0.5 ? attributeMatrix : u_instanceMatrices[instanceIndex];
681
- vec4 worldPosition = u_modelMatrix * instanceMatrix * vec4(a_position, 1.0);
682
- v_normal = mat3(u_normalMatrix) * transpose(inverse(mat3(instanceMatrix))) * a_normal;
683
- v_worldPosition = worldPosition.xyz;
684
- v_instanceColor = a_instanceColor;
685
- gl_Position = u_modelViewProjection * instanceMatrix * vec4(a_position, 1.0);
686
- }
687
- `,
688
- fragment: `#version 300 es
689
- // ${DEFAULT_INSTANCED_PBR_SHADER_MARKER}
690
- precision highp float;
691
- #include <lighting_common>
692
- #include <pbr_common>
693
- uniform vec4 u_baseColor;
694
- uniform float u_alphaCutoff;
695
- uniform float u_metallic;
696
- uniform float u_roughness;
697
- uniform vec3 u_environmentColor;
698
- uniform float u_environmentIntensity;
699
- uniform vec3 u_environmentSkyColor;
700
- uniform vec3 u_environmentHorizonColor;
701
- uniform vec3 u_environmentGroundColor;
702
- uniform vec3 u_environmentSpecularColor;
703
- uniform float u_environmentMapIntensity;
704
- uniform float u_environmentSpecularIntensity;
705
- uniform sampler2D u_environmentMapTexture;
706
- uniform samplerCube u_environmentCubeMapTexture;
707
- uniform float u_environmentCubeMapTextureEnabled;
708
- uniform float u_environmentMapTextureEnabled;
709
- uniform float u_environmentMapTextureIntensity;
710
- uniform float u_environmentMapTextureSpecularIntensity;
711
- uniform float u_environmentMapTextureRotation;
712
- uniform float u_environmentMapTextureMipCount;
713
- uniform float u_environmentMapTextureEncoding;
714
- uniform sampler2D u_environmentBrdfLutTexture;
715
- uniform float u_environmentBrdfLutEnabled;
716
- uniform vec3 u_emissiveColor;
717
- uniform float u_emissiveStrength;
718
- uniform float u_lightCount;
719
- uniform vec4 u_lightData[64];
720
- uniform sampler2D u_shadowMapTexture;
721
- uniform float u_shadowMapEnabled;
722
- uniform mat4 u_shadowMapMatrix;
723
- uniform float u_shadowMapStrength;
724
- uniform float u_shadowMapBias;
725
- uniform float u_shadowMapSlopeBias;
726
- uniform vec2 u_shadowMapTexelSize;
727
- uniform float u_shadowPcfSampleCount;
728
- uniform vec4 u_shadowPcfSamples[32];
729
- uniform sampler2D u_pointShadowMapTexture;
730
- uniform float u_pointShadowMapEnabled;
731
- uniform vec3 u_pointShadowLightPosition;
732
- uniform float u_pointShadowRange;
733
- uniform mat4 u_pointShadowFaceMatrices[6];
734
- uniform vec4 u_pointShadowFaceRects[6];
735
- uniform float u_pointShadowStrength;
736
- uniform float u_pointShadowBias;
737
- uniform float u_pointShadowSlopeBias;
738
- uniform vec2 u_pointShadowTexelSize;
739
- uniform float u_pointShadowPcfSampleCount;
740
- uniform vec4 u_pointShadowPcfSamples[32];
741
- uniform float u_outputColorSpace;
742
- uniform vec3 u_cameraPosition;
743
- uniform float u_environmentFogEnabled;
744
- uniform float u_environmentFogMode;
745
- uniform vec3 u_environmentFogColor;
746
- uniform float u_environmentFogNear;
747
- uniform float u_environmentFogFar;
748
- uniform float u_environmentFogDensity;
749
- uniform float u_environmentFogHeightFalloff;
750
- uniform float u_environmentFogHeightReference;
751
- uniform float u_environmentFogMaxOpacity;
752
- in vec3 v_normal;
753
- in vec3 v_worldPosition;
754
- in vec4 v_instanceColor;
755
- out vec4 outColor;
756
- #include <environment_fog_common>
757
- float a3dForwardShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
758
- if (u_shadowMapEnabled < 0.5) return 1.0;
759
- vec4 lightPosition = u_shadowMapMatrix * vec4(worldPosition, 1.0);
760
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
761
- vec2 uv = projected.xy * 0.5 + 0.5;
762
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) return 1.0;
763
- vec3 receiverNormal = normalize(normal);
764
- vec3 receiverLightDirection = lightDirection / max(length(lightDirection), 0.0001);
765
- float normalDotLight = clamp(abs(dot(receiverNormal, receiverLightDirection)), 0.0, 1.0);
766
- float slopeReceiverBias = (1.0 - normalDotLight) * u_shadowMapSlopeBias * max(u_shadowMapTexelSize.x, u_shadowMapTexelSize.y);
767
- float receiverDepth = projected.z * 0.5 + 0.5 - u_shadowMapBias - slopeReceiverBias;
768
- float shadowed = 0.0;
769
- float totalWeight = 0.0;
770
- int sampleCount = clamp(int(u_shadowPcfSampleCount), 1, 32);
771
- for (int i = 0; i < 32; ++i) {
772
- if (i >= sampleCount) break;
773
- vec4 sampleData = u_shadowPcfSamples[i];
774
- float weight = max(sampleData.z, 0.0);
775
- vec2 offset = sampleData.xy * u_shadowMapTexelSize;
776
- float storedDepth = texture(u_shadowMapTexture, uv + offset).r;
777
- shadowed += (receiverDepth > storedDepth ? 1.0 : 0.0) * weight;
778
- totalWeight += weight;
779
- }
780
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
781
- return mix(1.0, 1.0 - occlusion, clamp(u_shadowMapStrength, 0.0, 1.0));
782
- }
783
- float a3dPointShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
784
- if (u_pointShadowMapEnabled < 0.5) return 1.0;
785
- vec3 lightToFragment = worldPosition - u_pointShadowLightPosition;
786
- vec3 absoluteDirection = abs(lightToFragment);
787
- int faceIndex = absoluteDirection.x >= absoluteDirection.y && absoluteDirection.x >= absoluteDirection.z ? (lightToFragment.x >= 0.0 ? 0 : 1) : (absoluteDirection.y >= absoluteDirection.z ? (lightToFragment.y >= 0.0 ? 2 : 3) : (lightToFragment.z >= 0.0 ? 4 : 5));
788
- vec4 lightPosition = u_pointShadowFaceMatrices[faceIndex] * vec4(worldPosition, 1.0);
789
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
790
- vec2 localUv = projected.xy * 0.5 + 0.5;
791
- if (localUv.x < 0.0 || localUv.x > 1.0 || localUv.y < 0.0 || localUv.y > 1.0 || length(lightToFragment) > u_pointShadowRange) return 1.0;
792
- vec4 rect = u_pointShadowFaceRects[faceIndex];
793
- vec2 uv = rect.xy + localUv * rect.zw;
794
- float normalDotLight = clamp(abs(dot(normalize(normal), normalize(lightDirection))), 0.0, 1.0);
795
- float receiverDepth = projected.z * 0.5 + 0.5 - u_pointShadowBias - (1.0 - normalDotLight) * u_pointShadowSlopeBias * max(u_pointShadowTexelSize.x, u_pointShadowTexelSize.y);
796
- float shadowed = 0.0;
797
- float totalWeight = 0.0;
798
- int sampleCount = clamp(int(u_pointShadowPcfSampleCount), 1, 32);
799
- for (int i = 0; i < 32; ++i) {
800
- if (i >= sampleCount) break;
801
- vec4 sampleData = u_pointShadowPcfSamples[i];
802
- float weight = max(sampleData.z, 0.0);
803
- shadowed += (receiverDepth > texture(u_pointShadowMapTexture, uv + sampleData.xy * u_pointShadowTexelSize).r ? 1.0 : 0.0) * weight;
804
- totalWeight += weight;
805
- }
806
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
807
- return mix(1.0, 1.0 - occlusion, clamp(u_pointShadowStrength, 0.0, 1.0));
808
- }
809
- vec2 a3dEnvironmentEquirectUv(vec3 direction, float rotation) {
810
- vec3 d = normalize(direction);
811
- float u = atan(d.z, d.x) / 6.28318530718 + 0.5 + rotation;
812
- float v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
813
- return vec2(fract(u), clamp(v, 0.0, 1.0));
814
- }
815
- vec3 a3dEnvironmentCubeDirection(vec3 direction, float rotation) {
816
- float angle = rotation * 6.28318530718;
817
- float c = cos(angle);
818
- float s = sin(angle);
819
- vec3 d = normalize(direction);
820
- return normalize(vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z));
821
- }
822
- vec3 a3dPbrDecodeEnvironmentSrgb(vec3 encodedColor) {
823
- return max(encodedColor, vec3(0.0));
824
- }
825
- vec3 a3dPbrDecodeEnvironmentRgbe(vec4 encodedSample) {
826
- float exponent = encodedSample.a * 255.0;
827
- float scale = exponent <= 0.0 ? 0.0 : exp2(exponent - 128.0) * (255.0 / 256.0);
828
- return max(encodedSample.rgb * scale, vec3(0.0));
829
- }
830
- vec3 a3dPbrDecodeEnvironmentSample(vec4 encodedSample) {
831
- if (u_environmentMapTextureEncoding > 1.5) return max(encodedSample.rgb, vec3(0.0));
832
- if (u_environmentMapTextureEncoding > 0.5) return a3dPbrDecodeEnvironmentRgbe(encodedSample);
833
- return a3dPbrDecodeEnvironmentSrgb(encodedSample.rgb);
834
- }
835
- vec3 a3dPbrBoundHdrSpecularRadiance(vec3 radiance) {
836
- vec3 nonNegative = max(radiance, vec3(0.0));
837
- vec3 softKnee = nonNegative / (vec3(1.0) + max(nonNegative - vec3(1.0), vec3(0.0)) * 0.58);
838
- float maxChannel = max(max(softKnee.r, softKnee.g), softKnee.b);
839
- return maxChannel > 1.65 ? softKnee * (1.65 / maxChannel) : softKnee;
840
- }
841
- vec3 a3dPbrClampSampledSpecularEdgeEnergy(vec3 radiance, float nDotV, float roughness) {
842
- float faceOn = smoothstep(0.12, 0.55, clamp(nDotV, 0.0, 1.0));
843
- float roughEnergy = mix(0.6, 1.0, clamp(roughness, 0.0, 1.0));
844
- float edgeCap = mix(0.12, 1.2, faceOn) * roughEnergy;
845
- float edgeScale = mix(0.14, 0.92, faceOn);
846
- vec3 bounded = min(radiance * edgeScale, vec3(edgeCap));
847
- return max(bounded, vec3(0.0));
848
- }
849
- vec4 a3dPbrEnvironmentSampleRaw(vec3 direction, float lod) {
850
- vec4 equirectSample = textureLod(u_environmentMapTexture, a3dEnvironmentEquirectUv(direction, u_environmentMapTextureRotation), lod);
851
- vec4 cubeSample = textureLod(u_environmentCubeMapTexture, a3dEnvironmentCubeDirection(direction, u_environmentMapTextureRotation), lod);
852
- return mix(equirectSample, cubeSample, step(0.5, u_environmentCubeMapTextureEnabled));
853
- }
854
- vec3 a3dPbrEnvironmentDiffuseInput(vec3 normal) {
855
- float environmentHemi = mix(0.35, 1.0, clamp(normal.y * 0.5 + 0.5, 0.0, 1.0));
856
- vec3 ambientEnvironment = u_environmentColor * u_environmentIntensity * environmentHemi;
857
- float proceduralEnvironmentWeight = step(0.0001, u_environmentMapIntensity);
858
- float skyBlend = clamp(normal.y * 0.5 + 0.5, 0.0, 1.0);
859
- float horizonBlend = 1.0 - abs(normal.y);
860
- vec3 proceduralDiffuse = mix(u_environmentGroundColor, u_environmentSkyColor, skyBlend);
861
- proceduralDiffuse = mix(proceduralDiffuse, u_environmentHorizonColor, clamp(horizonBlend, 0.0, 1.0) * 0.55);
862
- vec3 environmentDiffuse = mix(ambientEnvironment, proceduralDiffuse * u_environmentMapIntensity, proceduralEnvironmentWeight);
863
- float sampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureIntensity);
864
- float diffuseEnvironmentLod = max(u_environmentMapTextureMipCount - 1.0, 0.0);
865
- vec3 sampledDiffuse = a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(normal, diffuseEnvironmentLod));
866
- return mix(environmentDiffuse, environmentDiffuse * 0.18 + sampledDiffuse * u_environmentMapTextureIntensity * 0.92, sampledEnvironmentWeight);
867
- }
868
- vec3 a3dPbrEnvironmentSpecularInput(vec3 normal, vec3 viewDirection, float roughness) {
869
- float proceduralEnvironmentWeight = step(0.0001, u_environmentMapIntensity);
870
- vec3 reflectionDirection = reflect(-viewDirection, normal);
871
- float clampedRoughness = clamp(roughness, 0.0, 1.0);
872
- float reflectionBand = pow(clamp(reflectionDirection.y * 0.5 + 0.5, 0.0, 1.0), mix(18.0, 2.0, clampedRoughness));
873
- float roughEnvironmentFloor = mix(0.04, 0.38, clampedRoughness);
874
- float proceduralSpecularResponse = max(reflectionBand, roughEnvironmentFloor);
875
- float horizonStripe = pow(clamp(1.0 - abs(reflectionDirection.y), 0.0, 1.0), mix(14.0, 3.0, clampedRoughness));
876
- float sideStripe = pow(clamp(abs(reflectionDirection.x), 0.0, 1.0), mix(10.0, 3.0, clampedRoughness)) * smoothstep(0.0, 0.72, 1.0 - abs(reflectionDirection.y));
877
- float overheadStripe = pow(clamp(reflectionDirection.y * 0.5 + 0.5, 0.0, 1.0), mix(22.0, 4.0, clampedRoughness));
878
- vec3 proceduralSpecularColor = u_environmentSpecularColor * proceduralSpecularResponse
879
- + u_environmentHorizonColor * horizonStripe * 0.42
880
- + u_environmentSkyColor * overheadStripe * 0.28
881
- + u_environmentGroundColor * sideStripe * 0.22;
882
- vec3 proceduralSpecular = proceduralSpecularColor * u_environmentSpecularIntensity * proceduralEnvironmentWeight;
883
- float sampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureSpecularIntensity);
884
- float environmentLod = clampedRoughness * max(u_environmentMapTextureMipCount - 1.0, 0.0);
885
- vec3 sampledSpecular = a3dPbrBoundHdrSpecularRadiance(a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(reflectionDirection, environmentLod)));
886
- float nDotV = clamp(dot(normal, viewDirection), 0.0, 1.0);
887
- vec2 brdfLut = texture(u_environmentBrdfLutTexture, vec2(nDotV, clampedRoughness)).rg;
888
- sampledSpecular = a3dPbrClampSampledSpecularEdgeEnergy(sampledSpecular, nDotV, clampedRoughness);
889
- sampledSpecular *= u_environmentMapTextureSpecularIntensity * sampledEnvironmentWeight * mix(0.84, 0.58, clampedRoughness);
890
- return proceduralSpecular + sampledSpecular;
891
- }
892
- vec3 a3dPbrEncodeOutput(vec3 linearColor) {
893
- vec3 color = max(linearColor, vec3(0.0));
894
- vec3 filmic = clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), vec3(0.0), vec3(1.0));
895
- vec3 srgb = pow(filmic, vec3(1.0 / 2.2));
896
- return mix(color, srgb, step(0.5, u_outputColorSpace));
897
- }
898
- void main() {
899
- vec3 normal = normalize(v_normal);
900
- if (!gl_FrontFacing) normal = -normal;
901
- vec3 viewDirection = normalize(u_cameraPosition - v_worldPosition);
902
- vec3 base = u_baseColor.rgb * v_instanceColor.rgb;
903
- vec3 shaded = u_emissiveColor * u_emissiveStrength + a3dPbrEnvironmentLight(
904
- normal,
905
- viewDirection,
906
- a3dPbrEnvironmentDiffuseInput(normal),
907
- a3dPbrEnvironmentSpecularInput(normal, viewDirection, u_roughness),
908
- base,
909
- u_metallic,
910
- u_roughness,
911
- 1.0,
912
- vec3(1.0)
913
- );
914
- int count = min(int(u_lightCount), 16);
915
- for (int i = 0; i < count; ++i) {
916
- int baseIndex = i * 4;
917
- vec4 colorIntensity = u_lightData[baseIndex];
918
- vec4 positionRange = u_lightData[baseIndex + 1];
919
- vec4 directionKind = u_lightData[baseIndex + 2];
920
- vec4 spotShadowLayer = u_lightData[baseIndex + 3];
921
- float kind = directionKind.w;
922
- vec3 lightDirection = -directionKind.xyz;
923
- float attenuation = 1.0;
924
- if (kind > 0.5) {
925
- vec3 toLight = positionRange.xyz - v_worldPosition;
926
- float distanceToLight = length(toLight);
927
- lightDirection = distanceToLight > 0.0001 ? toLight / distanceToLight : -directionKind.xyz;
928
- float range = max(positionRange.w, 0.0001);
929
- float rangeFalloff = clamp(1.0 - pow(distanceToLight / range, 4.0), 0.0, 1.0);
930
- rangeFalloff *= rangeFalloff;
931
- attenuation = rangeFalloff / max(distanceToLight * distanceToLight, 1.0);
932
- }
933
- if (kind > 1.5) {
934
- vec3 lightToFragment = normalize(v_worldPosition - positionRange.xyz);
935
- float cone = dot(normalize(directionKind.xyz), lightToFragment);
936
- float outer = cos(spotShadowLayer.x);
937
- float inner = cos(spotShadowLayer.x * max(1.0 - spotShadowLayer.y, 0.001));
938
- attenuation *= smoothstep(outer, inner, cone);
939
- }
940
- float shadowFactor = mix(1.0, kind > 0.5 && kind < 1.5 ? a3dPointShadowFactor(v_worldPosition, normal, lightDirection) : a3dForwardShadowFactor(v_worldPosition, normal, lightDirection), step(0.5, spotShadowLayer.z));
941
- shaded += a3dPbrDirectLight(normal, viewDirection, lightDirection, colorIntensity.rgb, colorIntensity.a * attenuation * shadowFactor, base, u_metallic, u_roughness, 1.0, vec3(1.0));
942
- }
943
- float alpha = u_baseColor.a * v_instanceColor.a;
944
- if (alpha < u_alphaCutoff) discard;
945
- vec3 fogged = a3dApplyEnvironmentFog(max(shaded, vec3(0.0)), v_worldPosition);
946
- outColor = vec4(a3dPbrEncodeOutput(fogged), alpha);
947
- }
948
- `
949
- });
950
- library.register({
951
- name: DEFAULT_TEXTURED_UNLIT_SHADER_NAME,
952
- marker: DEFAULT_TEXTURED_UNLIT_SHADER_MARKER,
953
- vertex: `#version 300 es
954
- // ${DEFAULT_TEXTURED_UNLIT_SHADER_MARKER}
955
- precision highp float;
956
- layout(location = 0) in vec3 a_position;
957
- layout(location = 2) in vec2 a_uv;
958
- layout(location = 4) in vec4 a_color;
959
- uniform mat4 u_modelViewProjection;
960
- uniform vec2 u_baseColorTextureOffset;
961
- uniform vec2 u_baseColorTextureScale;
962
- uniform float u_baseColorTextureRotation;
963
- out vec2 v_uv;
964
- out vec4 v_vertexColor;
965
- void main() {
966
- vec2 scaledUv = a_uv * u_baseColorTextureScale;
967
- float c = cos(u_baseColorTextureRotation);
968
- float s = sin(u_baseColorTextureRotation);
969
- v_uv = vec2(scaledUv.x * c - scaledUv.y * s, scaledUv.x * s + scaledUv.y * c) + u_baseColorTextureOffset;
970
- v_vertexColor = a_color;
971
- gl_Position = u_modelViewProjection * vec4(a_position, 1.0);
972
- }
973
- `,
974
- fragment: `#version 300 es
975
- // ${DEFAULT_TEXTURED_UNLIT_SHADER_MARKER}
976
- precision highp float;
977
- uniform vec4 u_baseColor;
978
- uniform float u_alphaCutoff;
979
- uniform sampler2D u_baseColorTexture;
980
- in vec2 v_uv;
981
- in vec4 v_vertexColor;
982
- out vec4 outColor;
983
- void main() {
984
- vec4 base = texture(u_baseColorTexture, v_uv) * u_baseColor * v_vertexColor;
985
- if (base.a < u_alphaCutoff) discard;
986
- outColor = base;
987
- }
988
- `
989
- });
990
- library.register({
991
- name: DEFAULT_SKINNED_UNLIT_SHADER_NAME,
992
- marker: DEFAULT_SKINNED_UNLIT_SHADER_MARKER,
993
- vertex: `#version 300 es
994
- // ${DEFAULT_SKINNED_UNLIT_SHADER_MARKER}
995
- precision highp float;
996
- layout(location = 0) in vec3 a_position;
997
- layout(location = 5) in vec4 a_joints;
998
- layout(location = 6) in vec4 a_weights;
999
- uniform mat4 u_modelViewProjection;
1000
- uniform mat4 u_jointMatrices[96];
1001
- uniform float u_jointCount;
1002
- void main() {
1003
- float maxJoint = max(u_jointCount - 1.0, 0.0);
1004
- int jointX = int(clamp(a_joints.x, 0.0, maxJoint));
1005
- int jointY = int(clamp(a_joints.y, 0.0, maxJoint));
1006
- int jointZ = int(clamp(a_joints.z, 0.0, maxJoint));
1007
- int jointW = int(clamp(a_joints.w, 0.0, maxJoint));
1008
- mat4 skin =
1009
- u_jointMatrices[jointX] * a_weights.x +
1010
- u_jointMatrices[jointY] * a_weights.y +
1011
- u_jointMatrices[jointZ] * a_weights.z +
1012
- u_jointMatrices[jointW] * a_weights.w;
1013
- float weightSum = a_weights.x + a_weights.y + a_weights.z + a_weights.w;
1014
- vec4 skinnedPosition = weightSum > 0.0001 ? skin * vec4(a_position, 1.0) : vec4(a_position, 1.0);
1015
- gl_Position = u_modelViewProjection * skinnedPosition;
1016
- }
1017
- `,
1018
- fragment: `#version 300 es
1019
- // ${DEFAULT_SKINNED_UNLIT_SHADER_MARKER}
1020
- precision highp float;
1021
- uniform vec4 u_baseColor;
1022
- out vec4 outColor;
1023
- void main() {
1024
- outColor = u_baseColor;
1025
- }
1026
- `
1027
- });
1028
- library.register({
1029
- name: DEFAULT_SKINNED_LIT_SHADER_NAME,
1030
- marker: DEFAULT_SKINNED_LIT_SHADER_MARKER,
1031
- vertex: `#version 300 es
1032
- // ${DEFAULT_SKINNED_LIT_SHADER_MARKER}
1033
- precision highp float;
1034
- layout(location = 0) in vec3 a_position;
1035
- layout(location = 1) in vec3 a_normal;
1036
- layout(location = 2) in vec2 a_uv;
1037
- layout(location = 3) in vec4 a_tangent;
1038
- layout(location = 4) in vec4 a_color;
1039
- layout(location = 5) in vec4 a_joints;
1040
- layout(location = 6) in vec4 a_weights;
1041
- uniform mat4 u_modelViewProjection;
1042
- uniform mat4 u_modelMatrix;
1043
- uniform mat4 u_normalMatrix;
1044
- uniform vec2 u_baseColorTextureOffset;
1045
- uniform vec2 u_baseColorTextureScale;
1046
- uniform float u_baseColorTextureRotation;
1047
- uniform mat4 u_jointMatrices[96];
1048
- uniform float u_jointCount;
1049
- out vec3 v_normal;
1050
- out vec4 v_tangent;
1051
- out vec3 v_worldPosition;
1052
- out vec2 v_uv;
1053
- out vec4 v_vertexColor;
1054
- out float v_weightSum;
1055
- void main() {
1056
- vec2 scaledUv = a_uv * u_baseColorTextureScale;
1057
- float c = cos(u_baseColorTextureRotation);
1058
- float s = sin(u_baseColorTextureRotation);
1059
- v_uv = vec2(scaledUv.x * c - scaledUv.y * s, scaledUv.x * s + scaledUv.y * c) + u_baseColorTextureOffset;
1060
- v_vertexColor = a_color;
1061
- float maxJoint = max(u_jointCount - 1.0, 0.0);
1062
- int jointX = int(clamp(a_joints.x, 0.0, maxJoint));
1063
- int jointY = int(clamp(a_joints.y, 0.0, maxJoint));
1064
- int jointZ = int(clamp(a_joints.z, 0.0, maxJoint));
1065
- int jointW = int(clamp(a_joints.w, 0.0, maxJoint));
1066
- mat4 skin =
1067
- u_jointMatrices[jointX] * a_weights.x +
1068
- u_jointMatrices[jointY] * a_weights.y +
1069
- u_jointMatrices[jointZ] * a_weights.z +
1070
- u_jointMatrices[jointW] * a_weights.w;
1071
- v_weightSum = a_weights.x + a_weights.y + a_weights.z + a_weights.w;
1072
- vec4 skinnedPosition = v_weightSum > 0.0001 ? skin * vec4(a_position, 1.0) : vec4(a_position, 1.0);
1073
- vec3 skinnedNormal = (v_weightSum > 0.0001 ? skin * vec4(a_normal, 0.0) : vec4(a_normal, 0.0)).xyz;
1074
- vec3 skinnedTangent = (v_weightSum > 0.0001 ? skin * vec4(a_tangent.xyz, 0.0) : vec4(a_tangent.xyz, 0.0)).xyz;
1075
- v_normal = mat3(u_normalMatrix) * skinnedNormal;
1076
- v_tangent = vec4(mat3(u_normalMatrix) * skinnedTangent, a_tangent.w);
1077
- v_worldPosition = (u_modelMatrix * skinnedPosition).xyz;
1078
- gl_Position = u_modelViewProjection * skinnedPosition;
1079
- }
1080
- `,
1081
- fragment: `#version 300 es
1082
- // ${DEFAULT_SKINNED_LIT_SHADER_MARKER}
1083
- precision highp float;
1084
- #include <lighting_common>
1085
- #include <pbr_common>
1086
- uniform vec4 u_baseColor;
1087
- uniform sampler2D u_baseColorTexture;
1088
- uniform float u_baseColorTextureEnabled;
1089
- uniform sampler2D u_normalTexture;
1090
- uniform float u_normalTextureEnabled;
1091
- uniform float u_normalScale;
1092
- uniform sampler2D u_metallicRoughnessTexture;
1093
- uniform float u_metallicRoughnessTextureEnabled;
1094
- uniform sampler2D u_occlusionTexture;
1095
- uniform float u_occlusionTextureEnabled;
1096
- uniform float u_occlusionStrength;
1097
- uniform sampler2D u_emissiveTexture;
1098
- uniform float u_emissiveTextureEnabled;
1099
- uniform float u_alphaCutoff;
1100
- uniform float u_metallic;
1101
- uniform float u_roughness;
1102
- uniform vec3 u_environmentColor;
1103
- uniform float u_environmentIntensity;
1104
- uniform vec3 u_environmentSkyColor;
1105
- uniform vec3 u_environmentHorizonColor;
1106
- uniform vec3 u_environmentGroundColor;
1107
- uniform vec3 u_environmentSpecularColor;
1108
- uniform float u_environmentMapIntensity;
1109
- uniform float u_environmentSpecularIntensity;
1110
- uniform sampler2D u_environmentMapTexture;
1111
- uniform samplerCube u_environmentCubeMapTexture;
1112
- uniform float u_environmentCubeMapTextureEnabled;
1113
- uniform float u_environmentMapTextureEnabled;
1114
- uniform float u_environmentMapTextureIntensity;
1115
- uniform float u_environmentMapTextureSpecularIntensity;
1116
- uniform float u_environmentMapTextureRotation;
1117
- uniform float u_environmentMapTextureMipCount;
1118
- uniform float u_environmentMapTextureEncoding;
1119
- uniform sampler2D u_environmentBrdfLutTexture;
1120
- uniform float u_environmentBrdfLutEnabled;
1121
- uniform vec3 u_emissiveColor;
1122
- uniform float u_emissiveStrength;
1123
- uniform float u_clearcoatFactor;
1124
- uniform float u_clearcoatRoughnessFactor;
1125
- uniform float u_transmissionFactor;
1126
- uniform float u_diffuseTransmissionFactor;
1127
- uniform vec3 u_diffuseTransmissionColorFactor;
1128
- uniform float u_transmissionFallbackEnergy;
1129
- uniform float u_volumeThicknessFactor;
1130
- uniform float u_volumeAttenuationDistance;
1131
- uniform vec3 u_volumeAttenuationColor;
1132
- uniform float u_transmissionParallaxStrength;
1133
- uniform vec3 u_transmissionParallaxBoxMin;
1134
- uniform vec3 u_transmissionParallaxBoxMax;
1135
- uniform float u_transmissionBounceCount;
1136
- uniform float u_transmissionCausticStrength;
1137
- uniform float u_ior;
1138
- uniform float u_specularFactor;
1139
- uniform vec3 u_specularColorFactor;
1140
- uniform vec3 u_sheenColorFactor;
1141
- uniform float u_sheenRoughnessFactor;
1142
- uniform float u_anisotropyStrength;
1143
- uniform float u_anisotropyRotation;
1144
- uniform float u_iridescenceFactor;
1145
- uniform float u_iridescenceIor;
1146
- uniform float u_iridescenceThicknessMinimum;
1147
- uniform float u_iridescenceThicknessMaximum;
1148
- uniform float u_dispersion;
1149
- uniform float u_lightCount;
1150
- uniform vec4 u_lightData[64];
1151
- uniform sampler2D u_shadowMapTexture;
1152
- uniform float u_shadowMapEnabled;
1153
- uniform mat4 u_shadowMapMatrix;
1154
- uniform float u_shadowMapStrength;
1155
- uniform float u_shadowMapBias;
1156
- uniform float u_shadowMapSlopeBias;
1157
- uniform vec2 u_shadowMapTexelSize;
1158
- uniform float u_shadowPcfSampleCount;
1159
- uniform vec4 u_shadowPcfSamples[32];
1160
- uniform sampler2D u_pointShadowMapTexture;
1161
- uniform float u_pointShadowMapEnabled;
1162
- uniform vec3 u_pointShadowLightPosition;
1163
- uniform float u_pointShadowRange;
1164
- uniform mat4 u_pointShadowFaceMatrices[6];
1165
- uniform vec4 u_pointShadowFaceRects[6];
1166
- uniform float u_pointShadowStrength;
1167
- uniform float u_pointShadowBias;
1168
- uniform float u_pointShadowSlopeBias;
1169
- uniform vec2 u_pointShadowTexelSize;
1170
- uniform float u_pointShadowPcfSampleCount;
1171
- uniform vec4 u_pointShadowPcfSamples[32];
1172
- uniform float u_outputColorSpace;
1173
- uniform vec3 u_cameraPosition;
1174
- uniform float u_environmentFogEnabled;
1175
- uniform float u_environmentFogMode;
1176
- uniform vec3 u_environmentFogColor;
1177
- uniform float u_environmentFogNear;
1178
- uniform float u_environmentFogFar;
1179
- uniform float u_environmentFogDensity;
1180
- uniform float u_environmentFogHeightFalloff;
1181
- uniform float u_environmentFogHeightReference;
1182
- uniform float u_environmentFogMaxOpacity;
1183
- in vec3 v_normal;
1184
- in vec4 v_tangent;
1185
- in vec3 v_worldPosition;
1186
- in vec2 v_uv;
1187
- in vec4 v_vertexColor;
1188
- in float v_weightSum;
1189
- out vec4 outColor;
1190
- #include <environment_fog_common>
1191
- vec2 a3dEnvironmentEquirectUv(vec3 direction, float rotation) {
1192
- vec3 d = normalize(direction);
1193
- float u = atan(d.z, d.x) / 6.28318530718 + 0.5 + rotation;
1194
- float v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
1195
- return vec2(fract(u), clamp(v, 0.0, 1.0));
1196
- }
1197
- vec3 a3dEnvironmentCubeDirection(vec3 direction, float rotation) {
1198
- float angle = rotation * 6.28318530718;
1199
- float c = cos(angle);
1200
- float s = sin(angle);
1201
- vec3 d = normalize(direction);
1202
- return normalize(vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z));
1203
- }
1204
- vec3 a3dPbrDecodeEnvironmentSrgb(vec3 encodedColor) {
1205
- return max(encodedColor, vec3(0.0));
1206
- }
1207
- vec3 a3dPbrDecodeEnvironmentRgbe(vec4 encodedSample) {
1208
- float exponent = encodedSample.a * 255.0;
1209
- float scale = exponent <= 0.0 ? 0.0 : exp2(exponent - 128.0) * (255.0 / 256.0);
1210
- return max(encodedSample.rgb * scale, vec3(0.0));
1211
- }
1212
- vec3 a3dPbrDecodeEnvironmentSample(vec4 encodedSample) {
1213
- if (u_environmentMapTextureEncoding > 1.5) return max(encodedSample.rgb, vec3(0.0));
1214
- if (u_environmentMapTextureEncoding > 0.5) return a3dPbrDecodeEnvironmentRgbe(encodedSample);
1215
- return a3dPbrDecodeEnvironmentSrgb(encodedSample.rgb);
1216
- }
1217
- vec3 a3dPbrBoundHdrSpecularRadiance(vec3 radiance) {
1218
- vec3 nonNegative = max(radiance, vec3(0.0));
1219
- vec3 softKnee = nonNegative / (vec3(1.0) + max(nonNegative - vec3(1.0), vec3(0.0)) * 0.58);
1220
- float maxChannel = max(max(softKnee.r, softKnee.g), softKnee.b);
1221
- return maxChannel > 1.65 ? softKnee * (1.65 / maxChannel) : softKnee;
1222
- }
1223
- vec3 a3dPbrClampSampledSpecularEdgeEnergy(vec3 radiance, float nDotV, float roughness) {
1224
- float faceOn = smoothstep(0.12, 0.55, clamp(nDotV, 0.0, 1.0));
1225
- float roughEnergy = mix(0.6, 1.0, clamp(roughness, 0.0, 1.0));
1226
- float edgeCap = mix(0.12, 1.2, faceOn) * roughEnergy;
1227
- float edgeScale = mix(0.14, 0.92, faceOn);
1228
- vec3 bounded = min(radiance * edgeScale, vec3(edgeCap));
1229
- return max(bounded, vec3(0.0));
1230
- }
1231
- vec4 a3dPbrEnvironmentSampleRaw(vec3 direction, float lod) {
1232
- vec4 equirectSample = textureLod(u_environmentMapTexture, a3dEnvironmentEquirectUv(direction, u_environmentMapTextureRotation), lod);
1233
- vec4 cubeSample = textureLod(u_environmentCubeMapTexture, a3dEnvironmentCubeDirection(direction, u_environmentMapTextureRotation), lod);
1234
- return mix(equirectSample, cubeSample, step(0.5, u_environmentCubeMapTextureEnabled));
1235
- }
1236
- vec3 a3dSkinnedPbrNormal(vec3 baseNormal, vec4 tangentFrame, vec2 uv) {
1237
- vec3 sampled = texture(u_normalTexture, uv).xyz * 2.0 - 1.0;
1238
- vec3 n = normalize(baseNormal);
1239
- vec3 tangent = normalize(tangentFrame.xyz);
1240
- vec3 bitangent = normalize(cross(n, tangent) * tangentFrame.w);
1241
- return normalize(tangent * sampled.x * u_normalScale + bitangent * sampled.y * u_normalScale + n * max(sampled.z, 0.001));
1242
- }
1243
- float a3dForwardShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
1244
- if (u_shadowMapEnabled < 0.5) return 1.0;
1245
- vec4 lightPosition = u_shadowMapMatrix * vec4(worldPosition, 1.0);
1246
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
1247
- vec2 uv = projected.xy * 0.5 + 0.5;
1248
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) return 1.0;
1249
- vec3 receiverNormal = normalize(normal);
1250
- vec3 receiverLightDirection = lightDirection / max(length(lightDirection), 0.0001);
1251
- float normalDotLight = clamp(abs(dot(receiverNormal, receiverLightDirection)), 0.0, 1.0);
1252
- float slopeReceiverBias = (1.0 - normalDotLight) * u_shadowMapSlopeBias * max(u_shadowMapTexelSize.x, u_shadowMapTexelSize.y);
1253
- float receiverDepth = projected.z * 0.5 + 0.5 - u_shadowMapBias - slopeReceiverBias;
1254
- float shadowed = 0.0;
1255
- float totalWeight = 0.0;
1256
- int sampleCount = clamp(int(u_shadowPcfSampleCount), 1, 32);
1257
- for (int i = 0; i < 32; ++i) {
1258
- if (i >= sampleCount) break;
1259
- vec4 sampleData = u_shadowPcfSamples[i];
1260
- float weight = max(sampleData.z, 0.0);
1261
- vec2 offset = sampleData.xy * u_shadowMapTexelSize;
1262
- float storedDepth = texture(u_shadowMapTexture, uv + offset).r;
1263
- shadowed += (receiverDepth > storedDepth ? 1.0 : 0.0) * weight;
1264
- totalWeight += weight;
1265
- }
1266
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
1267
- return mix(1.0, 1.0 - occlusion, clamp(u_shadowMapStrength, 0.0, 1.0));
1268
- }
1269
- float a3dPointShadowFaceIndex(vec3 direction) {
1270
- vec3 absoluteDirection = abs(direction);
1271
- if (absoluteDirection.x >= absoluteDirection.y && absoluteDirection.x >= absoluteDirection.z) return direction.x >= 0.0 ? 0.0 : 1.0;
1272
- if (absoluteDirection.y >= absoluteDirection.x && absoluteDirection.y >= absoluteDirection.z) return direction.y >= 0.0 ? 2.0 : 3.0;
1273
- return direction.z >= 0.0 ? 4.0 : 5.0;
1274
- }
1275
- float a3dPointShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
1276
- if (u_pointShadowMapEnabled < 0.5) return 1.0;
1277
- vec3 lightToFragment = worldPosition - u_pointShadowLightPosition;
1278
- float distanceToLight = length(lightToFragment);
1279
- if (distanceToLight > u_pointShadowRange) return 1.0;
1280
- int faceIndex = int(a3dPointShadowFaceIndex(lightToFragment));
1281
- vec4 lightPosition = u_pointShadowFaceMatrices[faceIndex] * vec4(worldPosition, 1.0);
1282
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
1283
- vec2 localUv = projected.xy * 0.5 + 0.5;
1284
- if (localUv.x < 0.0 || localUv.x > 1.0 || localUv.y < 0.0 || localUv.y > 1.0) return 1.0;
1285
- vec4 rect = u_pointShadowFaceRects[faceIndex];
1286
- vec2 uv = rect.xy + localUv * rect.zw;
1287
- vec3 receiverNormal = normalize(normal);
1288
- vec3 receiverLightDirection = lightDirection / max(length(lightDirection), 0.0001);
1289
- float normalDotLight = clamp(abs(dot(receiverNormal, receiverLightDirection)), 0.0, 1.0);
1290
- float slopeReceiverBias = (1.0 - normalDotLight) * u_pointShadowSlopeBias * max(u_pointShadowTexelSize.x, u_pointShadowTexelSize.y);
1291
- float receiverDepth = projected.z * 0.5 + 0.5 - u_pointShadowBias - slopeReceiverBias;
1292
- float shadowed = 0.0;
1293
- float totalWeight = 0.0;
1294
- int sampleCount = clamp(int(u_pointShadowPcfSampleCount), 1, 32);
1295
- for (int i = 0; i < 32; ++i) {
1296
- if (i >= sampleCount) break;
1297
- vec4 sampleData = u_pointShadowPcfSamples[i];
1298
- float weight = max(sampleData.z, 0.0);
1299
- vec2 offset = sampleData.xy * u_pointShadowTexelSize;
1300
- float storedDepth = texture(u_pointShadowMapTexture, uv + offset).r;
1301
- shadowed += (receiverDepth > storedDepth ? 1.0 : 0.0) * weight;
1302
- totalWeight += weight;
1303
- }
1304
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
1305
- return mix(1.0, 1.0 - occlusion, clamp(u_pointShadowStrength, 0.0, 1.0));
1306
- }
1307
- vec3 a3dPbrEncodeOutput(vec3 linearColor) {
1308
- vec3 color = max(linearColor, vec3(0.0));
1309
- vec3 filmic = clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), vec3(0.0), vec3(1.0));
1310
- vec3 srgb = pow(filmic, vec3(1.0 / 2.2));
1311
- return mix(color, srgb, step(0.5, u_outputColorSpace));
1312
- }
1313
- void main() {
1314
- vec3 normal = normalize(v_normal);
1315
- normal = mix(normal, a3dSkinnedPbrNormal(v_normal, v_tangent, v_uv), step(0.5, u_normalTextureEnabled));
1316
- if (!gl_FrontFacing) normal = -normal;
1317
- vec3 viewDirection = normalize(u_cameraPosition - v_worldPosition);
1318
- vec4 sampledBaseColor = texture(u_baseColorTexture, v_uv);
1319
- vec4 decodedBaseColor = vec4(a3dPbrDecodeEnvironmentSrgb(sampledBaseColor.rgb), sampledBaseColor.a);
1320
- float baseColorTextureWeight = clamp(u_baseColorTextureEnabled, 0.0, 1.0);
1321
- vec4 baseColor = mix(u_baseColor, u_baseColor * decodedBaseColor, baseColorTextureWeight) * v_vertexColor;
1322
- vec4 metallicRoughnessSample = texture(u_metallicRoughnessTexture, v_uv);
1323
- float metallic = mix(u_metallic, clamp(u_metallic * metallicRoughnessSample.b, 0.0, 1.0), step(0.5, u_metallicRoughnessTextureEnabled));
1324
- float roughness = mix(u_roughness, clamp(u_roughness * metallicRoughnessSample.g, 0.0, 1.0), step(0.5, u_metallicRoughnessTextureEnabled));
1325
- float occlusion = mix(1.0, mix(1.0, texture(u_occlusionTexture, v_uv).r, clamp(u_occlusionStrength, 0.0, 1.0)), step(0.5, u_occlusionTextureEnabled));
1326
- vec3 emissive = u_emissiveColor * u_emissiveStrength * mix(vec3(1.0), a3dPbrDecodeEnvironmentSrgb(texture(u_emissiveTexture, v_uv).rgb), step(0.5, u_emissiveTextureEnabled));
1327
- vec3 materialBase = a3dApplyAdvancedPbrLobes(
1328
- baseColor.rgb,
1329
- u_clearcoatFactor,
1330
- u_clearcoatRoughnessFactor,
1331
- u_transmissionFactor,
1332
- u_diffuseTransmissionFactor,
1333
- u_diffuseTransmissionColorFactor,
1334
- u_transmissionFallbackEnergy,
1335
- u_volumeThicknessFactor,
1336
- u_volumeAttenuationDistance,
1337
- u_volumeAttenuationColor,
1338
- u_ior,
1339
- u_specularFactor,
1340
- u_specularColorFactor,
1341
- u_sheenColorFactor,
1342
- u_sheenRoughnessFactor,
1343
- u_anisotropyStrength,
1344
- u_anisotropyRotation,
1345
- u_iridescenceFactor,
1346
- u_iridescenceIor,
1347
- u_iridescenceThicknessMinimum,
1348
- u_iridescenceThicknessMaximum,
1349
- u_dispersion
1350
- );
1351
- float environmentHemi = mix(0.35, 1.0, clamp(normal.y * 0.5 + 0.5, 0.0, 1.0));
1352
- vec3 ambientEnvironment = u_environmentColor * u_environmentIntensity * environmentHemi;
1353
- float proceduralEnvironmentWeight = step(0.0001, u_environmentMapIntensity);
1354
- float skyBlend = clamp(normal.y * 0.5 + 0.5, 0.0, 1.0);
1355
- float horizonBlend = 1.0 - abs(normal.y);
1356
- vec3 proceduralDiffuse = mix(u_environmentGroundColor, u_environmentSkyColor, skyBlend);
1357
- proceduralDiffuse = mix(proceduralDiffuse, u_environmentHorizonColor, clamp(horizonBlend, 0.0, 1.0) * 0.55);
1358
- vec3 environmentDiffuse = mix(ambientEnvironment, proceduralDiffuse * u_environmentMapIntensity, proceduralEnvironmentWeight);
1359
- vec3 reflectionDirection = reflect(-viewDirection, normal);
1360
- float sampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureIntensity);
1361
- float diffuseEnvironmentLod = max(u_environmentMapTextureMipCount - 1.0, 0.0);
1362
- vec3 sampledDiffuse = a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(normal, diffuseEnvironmentLod));
1363
- float clampedRoughness = clamp(roughness, 0.0, 1.0);
1364
- float reflectionBand = pow(clamp(reflectionDirection.y * 0.5 + 0.5, 0.0, 1.0), mix(18.0, 2.0, clampedRoughness));
1365
- float roughEnvironmentFloor = mix(0.04, 0.38, clampedRoughness);
1366
- float proceduralSpecularResponse = max(reflectionBand, roughEnvironmentFloor);
1367
- float horizonStripe = pow(clamp(1.0 - abs(reflectionDirection.y), 0.0, 1.0), mix(14.0, 3.0, clampedRoughness));
1368
- float sideStripe = pow(clamp(abs(reflectionDirection.x), 0.0, 1.0), mix(10.0, 3.0, clampedRoughness)) * smoothstep(0.0, 0.72, 1.0 - abs(reflectionDirection.y));
1369
- float overheadStripe = pow(clamp(reflectionDirection.y * 0.5 + 0.5, 0.0, 1.0), mix(22.0, 4.0, clampedRoughness));
1370
- vec3 proceduralSpecularColor = u_environmentSpecularColor * proceduralSpecularResponse
1371
- + u_environmentHorizonColor * horizonStripe * 0.42
1372
- + u_environmentSkyColor * overheadStripe * 0.28
1373
- + u_environmentGroundColor * sideStripe * 0.22;
1374
- vec3 proceduralSpecular = proceduralSpecularColor * u_environmentSpecularIntensity * proceduralEnvironmentWeight;
1375
- float environmentLod = clampedRoughness * max(u_environmentMapTextureMipCount - 1.0, 0.0);
1376
- vec3 sampledSpecular = a3dPbrBoundHdrSpecularRadiance(a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(reflectionDirection, environmentLod)));
1377
- float nDotV = clamp(dot(normal, viewDirection), 0.0, 1.0);
1378
- vec2 brdfLut = texture(u_environmentBrdfLutTexture, vec2(nDotV, clampedRoughness)).rg;
1379
- sampledSpecular = a3dPbrClampSampledSpecularEdgeEnergy(sampledSpecular, nDotV, clampedRoughness);
1380
- sampledSpecular *= u_environmentMapTextureSpecularIntensity * sampledEnvironmentWeight * mix(1.1, 0.85, roughness);
1381
- vec3 shaded = a3dPbrEnvironmentLightSplitSum(
1382
- normal,
1383
- viewDirection,
1384
- mix(environmentDiffuse, environmentDiffuse * 0.18 + sampledDiffuse * u_environmentMapTextureIntensity * 0.92, sampledEnvironmentWeight),
1385
- proceduralSpecular + sampledSpecular,
1386
- mix(vec2(1.0, 0.0), brdfLut, step(0.0001, u_environmentBrdfLutEnabled)),
1387
- materialBase,
1388
- metallic,
1389
- roughness,
1390
- u_specularFactor,
1391
- u_specularColorFactor
1392
- ) * occlusion + emissive;
1393
- int count = min(int(u_lightCount), 16);
1394
- for (int i = 0; i < count; ++i) {
1395
- int baseIndex = i * 4;
1396
- vec4 colorIntensity = u_lightData[baseIndex];
1397
- vec4 positionRange = u_lightData[baseIndex + 1];
1398
- vec4 directionKind = u_lightData[baseIndex + 2];
1399
- vec4 spotShadowLayer = u_lightData[baseIndex + 3];
1400
- float kind = directionKind.w;
1401
- vec3 lightDirection = -directionKind.xyz;
1402
- float attenuation = 1.0;
1403
- if (kind > 0.5) {
1404
- vec3 toLight = positionRange.xyz - v_worldPosition;
1405
- float distanceToLight = length(toLight);
1406
- lightDirection = distanceToLight > 0.0001 ? toLight / distanceToLight : -directionKind.xyz;
1407
- float range = max(positionRange.w, 0.0001);
1408
- float rangeFalloff = clamp(1.0 - pow(distanceToLight / range, 4.0), 0.0, 1.0);
1409
- rangeFalloff *= rangeFalloff;
1410
- attenuation = rangeFalloff / max(distanceToLight * distanceToLight, 1.0);
1411
- }
1412
- if (kind > 1.5) {
1413
- vec3 lightToFragment = normalize(v_worldPosition - positionRange.xyz);
1414
- float cone = dot(normalize(directionKind.xyz), lightToFragment);
1415
- float outer = cos(spotShadowLayer.x);
1416
- float inner = cos(spotShadowLayer.x * max(1.0 - spotShadowLayer.y, 0.001));
1417
- attenuation *= smoothstep(outer, inner, cone);
1418
- }
1419
- shaded += a3dPbrDirectLight(
1420
- normal,
1421
- viewDirection,
1422
- lightDirection,
1423
- colorIntensity.rgb,
1424
- colorIntensity.a * attenuation * mix(1.0, kind > 0.5 && kind < 1.5 ? a3dPointShadowFactor(v_worldPosition, normal, lightDirection) : a3dForwardShadowFactor(v_worldPosition, normal, lightDirection), step(0.5, spotShadowLayer.z)),
1425
- materialBase,
1426
- metallic,
1427
- roughness,
1428
- u_specularFactor,
1429
- u_specularColorFactor
1430
- );
1431
- }
1432
- if (baseColor.a < u_alphaCutoff) discard;
1433
- vec3 fogged = a3dApplyEnvironmentFog(max(shaded, vec3(0.0)), v_worldPosition);
1434
- outColor = vec4(a3dPbrEncodeOutput(fogged), baseColor.a);
1435
- }
1436
- `
1437
- });
1438
- library.register({
1439
- name: DEFAULT_MORPH_UNLIT_SHADER_NAME,
1440
- marker: DEFAULT_MORPH_UNLIT_SHADER_MARKER,
1441
- vertex: `#version 300 es
1442
- // ${DEFAULT_MORPH_UNLIT_SHADER_MARKER}
1443
- precision highp float;
1444
- layout(location = 0) in vec3 a_position;
1445
- uniform mat4 u_modelViewProjection;
1446
- // Uniform fast path (<= 4 targets / 64 verts):
1447
- uniform vec4 u_morphPositionDeltas[256];
1448
- uniform vec4 u_morphWeights;
1449
- uniform float u_morphTargetCount;
1450
- // Texture-backed path (lifts the 4/64 cap; see MorphTargetPlan.ts createMorphTargetPlan):
1451
- uniform sampler2D u_morphDeltaTexture;
1452
- uniform float u_morphUsesTexture; // > 0.5 => sample deltas from the texture
1453
- uniform float u_morphTextureWidth; // = vertexCount
1454
- uniform float u_morphRowsPerTarget; // 1 = positions only, 2 = positions + normals
1455
- uniform float u_morphWeightArray[64]; // per-target weights for the texture path
1456
- vec3 sampleMorphTexel(float row, float vertex) {
1457
- // Fetch RGBA float texel at (x=vertex, y=row) using normalized coords + nearest filtering.
1458
- float u = (vertex + 0.5) / max(1.0, u_morphTextureWidth);
1459
- float height = u_morphTargetCount * u_morphRowsPerTarget;
1460
- float v = (row + 0.5) / max(1.0, height);
1461
- return texture(u_morphDeltaTexture, vec2(u, v)).xyz;
1462
- }
1463
- void main() {
1464
- vec3 morphDelta = vec3(0.0);
1465
- if (u_morphUsesTexture > 0.5) {
1466
- float vertex = float(gl_VertexID);
1467
- for (int target = 0; target < 64; ++target) {
1468
- if (float(target) >= u_morphTargetCount) break;
1469
- float row = float(target) * u_morphRowsPerTarget;
1470
- morphDelta += sampleMorphTexel(row, vertex) * u_morphWeightArray[target];
1471
- }
1472
- } else {
1473
- int morphVertexIndex = clamp(gl_VertexID, 0, 63);
1474
- for (int target = 0; target < 4; ++target) {
1475
- if (float(target) < u_morphTargetCount) {
1476
- int morphIndex = target * 64 + morphVertexIndex;
1477
- morphDelta += u_morphPositionDeltas[morphIndex].xyz * u_morphWeights[target];
1478
- }
1479
- }
1480
- }
1481
- vec3 morphedPosition = a_position + morphDelta;
1482
- gl_Position = u_modelViewProjection * vec4(morphedPosition, 1.0);
1483
- }
1484
- `,
1485
- fragment: `#version 300 es
1486
- // ${DEFAULT_MORPH_UNLIT_SHADER_MARKER}
1487
- precision highp float;
1488
- uniform vec4 u_baseColor;
1489
- out vec4 outColor;
1490
- void main() {
1491
- outColor = u_baseColor;
1492
- }
1493
- `
1494
- });
1495
- library.register({
1496
- name: DEFAULT_NORMAL_MAPPED_PBR_SHADER_NAME,
1497
- marker: DEFAULT_NORMAL_MAPPED_PBR_SHADER_MARKER,
1498
- vertex: `#version 300 es
1499
- // ${DEFAULT_NORMAL_MAPPED_PBR_SHADER_MARKER}
1500
- precision highp float;
1501
- layout(location = 0) in vec3 a_position;
1502
- layout(location = 1) in vec3 a_normal;
1503
- layout(location = 2) in vec2 a_uv;
1504
- layout(location = 3) in vec4 a_tangent;
1505
- layout(location = 4) in vec4 a_color;
1506
- layout(location = 7) in vec2 a_uv1;
1507
- uniform mat4 u_modelViewProjection;
1508
- uniform mat4 u_modelMatrix;
1509
- uniform mat4 u_normalMatrix;
1510
- out vec3 v_normal;
1511
- out vec4 v_tangent;
1512
- out vec3 v_worldPosition;
1513
- out vec2 v_uv;
1514
- out vec4 v_vertexColor;
1515
- void main() {
1516
- v_normal = mat3(u_normalMatrix) * a_normal;
1517
- v_tangent = vec4(mat3(u_normalMatrix) * a_tangent.xyz, a_tangent.w);
1518
- v_worldPosition = (u_modelMatrix * vec4(a_position, 1.0)).xyz;
1519
- v_uv = a_uv;
1520
- v_vertexColor = a_color;
1521
- gl_Position = u_modelViewProjection * vec4(a_position, 1.0);
1522
- }
1523
- `,
1524
- fragment: `#version 300 es
1525
- // ${DEFAULT_NORMAL_MAPPED_PBR_SHADER_MARKER}
1526
- precision highp float;
1527
- #include <lighting_common>
1528
- #include <pbr_common>
1529
- uniform vec4 u_baseColor;
1530
- uniform float u_alphaCutoff;
1531
- uniform float u_metallic;
1532
- uniform float u_roughness;
1533
- uniform vec3 u_environmentColor;
1534
- uniform float u_environmentIntensity;
1535
- uniform vec3 u_environmentSkyColor;
1536
- uniform vec3 u_environmentHorizonColor;
1537
- uniform vec3 u_environmentGroundColor;
1538
- uniform vec3 u_environmentSpecularColor;
1539
- uniform float u_environmentMapIntensity;
1540
- uniform float u_environmentSpecularIntensity;
1541
- uniform sampler2D u_environmentMapTexture;
1542
- uniform samplerCube u_environmentCubeMapTexture;
1543
- uniform float u_environmentCubeMapTextureEnabled;
1544
- uniform float u_environmentMapTextureEnabled;
1545
- uniform float u_environmentMapTextureIntensity;
1546
- uniform float u_environmentMapTextureSpecularIntensity;
1547
- uniform float u_environmentMapTextureRotation;
1548
- uniform float u_environmentMapTextureMipCount;
1549
- uniform float u_environmentMapTextureEncoding;
1550
- uniform sampler2D u_environmentBrdfLutTexture;
1551
- uniform float u_environmentBrdfLutEnabled;
1552
- uniform vec3 u_emissiveColor;
1553
- uniform float u_emissiveStrength;
1554
- uniform float u_clearcoatFactor;
1555
- uniform float u_clearcoatRoughnessFactor;
1556
- uniform float u_transmissionFactor;
1557
- uniform float u_diffuseTransmissionFactor;
1558
- uniform vec3 u_diffuseTransmissionColorFactor;
1559
- uniform float u_transmissionFallbackEnergy;
1560
- uniform float u_volumeThicknessFactor;
1561
- uniform float u_volumeAttenuationDistance;
1562
- uniform vec3 u_volumeAttenuationColor;
1563
- uniform float u_transmissionParallaxStrength;
1564
- uniform vec3 u_transmissionParallaxBoxMin;
1565
- uniform vec3 u_transmissionParallaxBoxMax;
1566
- uniform float u_transmissionBounceCount;
1567
- uniform float u_transmissionCausticStrength;
1568
- uniform float u_ior;
1569
- uniform float u_specularFactor;
1570
- uniform vec3 u_specularColorFactor;
1571
- uniform vec3 u_sheenColorFactor;
1572
- uniform float u_sheenRoughnessFactor;
1573
- uniform float u_lightCount;
1574
- uniform vec4 u_lightData[64];
1575
- uniform sampler2D u_normalTexture;
1576
- uniform float u_normalScale;
1577
- uniform sampler2D u_shadowMapTexture;
1578
- uniform float u_shadowMapEnabled;
1579
- uniform mat4 u_shadowMapMatrix;
1580
- uniform float u_shadowMapStrength;
1581
- uniform float u_shadowMapBias;
1582
- uniform float u_shadowMapSlopeBias;
1583
- uniform vec2 u_shadowMapTexelSize;
1584
- uniform float u_shadowPcfSampleCount;
1585
- uniform vec4 u_shadowPcfSamples[32];
1586
- uniform sampler2D u_pointShadowMapTexture;
1587
- uniform float u_pointShadowMapEnabled;
1588
- uniform vec3 u_pointShadowLightPosition;
1589
- uniform float u_pointShadowRange;
1590
- uniform mat4 u_pointShadowFaceMatrices[6];
1591
- uniform vec4 u_pointShadowFaceRects[6];
1592
- uniform float u_pointShadowStrength;
1593
- uniform float u_pointShadowBias;
1594
- uniform float u_pointShadowSlopeBias;
1595
- uniform vec2 u_pointShadowTexelSize;
1596
- uniform float u_pointShadowPcfSampleCount;
1597
- uniform vec4 u_pointShadowPcfSamples[32];
1598
- uniform float u_outputColorSpace;
1599
- uniform vec3 u_cameraPosition;
1600
- uniform float u_environmentFogEnabled;
1601
- uniform float u_environmentFogMode;
1602
- uniform vec3 u_environmentFogColor;
1603
- uniform float u_environmentFogNear;
1604
- uniform float u_environmentFogFar;
1605
- uniform float u_environmentFogDensity;
1606
- uniform float u_environmentFogHeightFalloff;
1607
- uniform float u_environmentFogHeightReference;
1608
- uniform float u_environmentFogMaxOpacity;
1609
- in vec3 v_normal;
1610
- in vec4 v_tangent;
1611
- in vec3 v_worldPosition;
1612
- in vec2 v_uv;
1613
- in vec4 v_vertexColor;
1614
- out vec4 outColor;
1615
- #include <environment_fog_common>
1616
- vec3 a3dNormalMappedNormal(vec3 baseNormal, vec4 tangentFrame, vec2 uv) {
1617
- vec3 sampled = texture(u_normalTexture, uv).xyz * 2.0 - 1.0;
1618
- vec3 n = normalize(baseNormal);
1619
- vec3 tangent = normalize(tangentFrame.xyz);
1620
- vec3 bitangent = normalize(cross(n, tangent) * tangentFrame.w);
1621
- return normalize(tangent * sampled.x * u_normalScale + bitangent * sampled.y * u_normalScale + n * max(sampled.z, 0.001));
1622
- }
1623
- float a3dForwardShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
1624
- if (u_shadowMapEnabled < 0.5) return 1.0;
1625
- vec4 lightPosition = u_shadowMapMatrix * vec4(worldPosition, 1.0);
1626
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
1627
- vec2 uv = projected.xy * 0.5 + 0.5;
1628
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) return 1.0;
1629
- vec3 receiverNormal = normalize(normal);
1630
- vec3 receiverLightDirection = lightDirection / max(length(lightDirection), 0.0001);
1631
- float normalDotLight = clamp(abs(dot(receiverNormal, receiverLightDirection)), 0.0, 1.0);
1632
- float slopeReceiverBias = (1.0 - normalDotLight) * u_shadowMapSlopeBias * max(u_shadowMapTexelSize.x, u_shadowMapTexelSize.y);
1633
- float receiverDepth = projected.z * 0.5 + 0.5 - u_shadowMapBias - slopeReceiverBias;
1634
- float shadowed = 0.0;
1635
- float totalWeight = 0.0;
1636
- int sampleCount = clamp(int(u_shadowPcfSampleCount), 1, 32);
1637
- for (int i = 0; i < 32; ++i) {
1638
- if (i >= sampleCount) break;
1639
- vec4 sampleData = u_shadowPcfSamples[i];
1640
- float weight = max(sampleData.z, 0.0);
1641
- vec2 offset = sampleData.xy * u_shadowMapTexelSize;
1642
- float storedDepth = texture(u_shadowMapTexture, uv + offset).r;
1643
- shadowed += (receiverDepth > storedDepth ? 1.0 : 0.0) * weight;
1644
- totalWeight += weight;
1645
- }
1646
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
1647
- return mix(1.0, 1.0 - occlusion, clamp(u_shadowMapStrength, 0.0, 1.0));
1648
- }
1649
- float a3dPointShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
1650
- if (u_pointShadowMapEnabled < 0.5) return 1.0;
1651
- vec3 lightToFragment = worldPosition - u_pointShadowLightPosition;
1652
- vec3 absoluteDirection = abs(lightToFragment);
1653
- int faceIndex = absoluteDirection.x >= absoluteDirection.y && absoluteDirection.x >= absoluteDirection.z ? (lightToFragment.x >= 0.0 ? 0 : 1) : (absoluteDirection.y >= absoluteDirection.z ? (lightToFragment.y >= 0.0 ? 2 : 3) : (lightToFragment.z >= 0.0 ? 4 : 5));
1654
- vec4 lightPosition = u_pointShadowFaceMatrices[faceIndex] * vec4(worldPosition, 1.0);
1655
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
1656
- vec2 localUv = projected.xy * 0.5 + 0.5;
1657
- if (localUv.x < 0.0 || localUv.x > 1.0 || localUv.y < 0.0 || localUv.y > 1.0 || length(lightToFragment) > u_pointShadowRange) return 1.0;
1658
- vec4 rect = u_pointShadowFaceRects[faceIndex];
1659
- vec2 uv = rect.xy + localUv * rect.zw;
1660
- float normalDotLight = clamp(abs(dot(normalize(normal), normalize(lightDirection))), 0.0, 1.0);
1661
- float receiverDepth = projected.z * 0.5 + 0.5 - u_pointShadowBias - (1.0 - normalDotLight) * u_pointShadowSlopeBias * max(u_pointShadowTexelSize.x, u_pointShadowTexelSize.y);
1662
- float shadowed = 0.0;
1663
- float totalWeight = 0.0;
1664
- int sampleCount = clamp(int(u_pointShadowPcfSampleCount), 1, 32);
1665
- for (int i = 0; i < 32; ++i) {
1666
- if (i >= sampleCount) break;
1667
- vec4 sampleData = u_pointShadowPcfSamples[i];
1668
- float weight = max(sampleData.z, 0.0);
1669
- shadowed += (receiverDepth > texture(u_pointShadowMapTexture, uv + sampleData.xy * u_pointShadowTexelSize).r ? 1.0 : 0.0) * weight;
1670
- totalWeight += weight;
1671
- }
1672
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
1673
- return mix(1.0, 1.0 - occlusion, clamp(u_pointShadowStrength, 0.0, 1.0));
1674
- }
1675
- vec2 a3dEnvironmentEquirectUv(vec3 direction, float rotation) {
1676
- vec3 d = normalize(direction);
1677
- float u = atan(d.z, d.x) / 6.28318530718 + 0.5 + rotation;
1678
- float v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
1679
- return vec2(fract(u), clamp(v, 0.0, 1.0));
1680
- }
1681
- vec3 a3dEnvironmentCubeDirection(vec3 direction, float rotation) {
1682
- float angle = rotation * 6.28318530718;
1683
- float c = cos(angle);
1684
- float s = sin(angle);
1685
- vec3 d = normalize(direction);
1686
- return normalize(vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z));
1687
- }
1688
- vec3 a3dPbrDecodeEnvironmentSrgb(vec3 encodedColor) {
1689
- return max(encodedColor, vec3(0.0));
1690
- }
1691
- vec3 a3dPbrDecodeEnvironmentRgbe(vec4 encodedSample) {
1692
- float exponent = encodedSample.a * 255.0;
1693
- float scale = exponent <= 0.0 ? 0.0 : exp2(exponent - 128.0) * (255.0 / 256.0);
1694
- return max(encodedSample.rgb * scale, vec3(0.0));
1695
- }
1696
- vec3 a3dPbrDecodeEnvironmentSample(vec4 encodedSample) {
1697
- if (u_environmentMapTextureEncoding > 1.5) return max(encodedSample.rgb, vec3(0.0));
1698
- if (u_environmentMapTextureEncoding > 0.5) return a3dPbrDecodeEnvironmentRgbe(encodedSample);
1699
- return a3dPbrDecodeEnvironmentSrgb(encodedSample.rgb);
1700
- }
1701
- vec3 a3dPbrBoundHdrSpecularRadiance(vec3 radiance) {
1702
- vec3 nonNegative = max(radiance, vec3(0.0));
1703
- vec3 softKnee = nonNegative / (vec3(1.0) + max(nonNegative - vec3(1.0), vec3(0.0)) * 0.58);
1704
- float maxChannel = max(max(softKnee.r, softKnee.g), softKnee.b);
1705
- return maxChannel > 1.65 ? softKnee * (1.65 / maxChannel) : softKnee;
1706
- }
1707
- vec3 a3dPbrClampSampledSpecularEdgeEnergy(vec3 radiance, float nDotV, float roughness) {
1708
- float faceOn = smoothstep(0.12, 0.55, clamp(nDotV, 0.0, 1.0));
1709
- float roughEnergy = mix(0.6, 1.0, clamp(roughness, 0.0, 1.0));
1710
- float edgeCap = mix(0.12, 1.2, faceOn) * roughEnergy;
1711
- float edgeScale = mix(0.14, 0.92, faceOn);
1712
- vec3 bounded = min(radiance * edgeScale, vec3(edgeCap));
1713
- return max(bounded, vec3(0.0));
1714
- }
1715
- vec4 a3dPbrEnvironmentSampleRaw(vec3 direction, float lod) {
1716
- vec4 equirectSample = textureLod(u_environmentMapTexture, a3dEnvironmentEquirectUv(direction, u_environmentMapTextureRotation), lod);
1717
- vec4 cubeSample = textureLod(u_environmentCubeMapTexture, a3dEnvironmentCubeDirection(direction, u_environmentMapTextureRotation), lod);
1718
- return mix(equirectSample, cubeSample, step(0.5, u_environmentCubeMapTextureEnabled));
1719
- }
1720
- vec3 a3dPbrEnvironmentDiffuseInput(vec3 normal) {
1721
- float environmentHemi = mix(0.35, 1.0, clamp(normal.y * 0.5 + 0.5, 0.0, 1.0));
1722
- vec3 ambientEnvironment = u_environmentColor * u_environmentIntensity * environmentHemi;
1723
- float proceduralEnvironmentWeight = step(0.0001, u_environmentMapIntensity);
1724
- float skyBlend = clamp(normal.y * 0.5 + 0.5, 0.0, 1.0);
1725
- float horizonBlend = 1.0 - abs(normal.y);
1726
- vec3 proceduralDiffuse = mix(u_environmentGroundColor, u_environmentSkyColor, skyBlend);
1727
- proceduralDiffuse = mix(proceduralDiffuse, u_environmentHorizonColor, clamp(horizonBlend, 0.0, 1.0) * 0.55);
1728
- vec3 environmentDiffuse = mix(ambientEnvironment, proceduralDiffuse * u_environmentMapIntensity, proceduralEnvironmentWeight);
1729
- float sampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureIntensity);
1730
- float diffuseEnvironmentLod = max(u_environmentMapTextureMipCount - 1.0, 0.0);
1731
- vec3 sampledDiffuse = a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(normal, diffuseEnvironmentLod));
1732
- return mix(environmentDiffuse, environmentDiffuse * 0.18 + sampledDiffuse * u_environmentMapTextureIntensity * 0.92, sampledEnvironmentWeight);
1733
- }
1734
- vec3 a3dPbrEnvironmentSpecularInput(vec3 normal, vec3 viewDirection, float roughness) {
1735
- float proceduralEnvironmentWeight = step(0.0001, u_environmentMapIntensity);
1736
- vec3 reflectionDirection = reflect(-viewDirection, normal);
1737
- float clampedRoughness = clamp(roughness, 0.0, 1.0);
1738
- float reflectionBand = pow(clamp(reflectionDirection.y * 0.5 + 0.5, 0.0, 1.0), mix(18.0, 2.0, clampedRoughness));
1739
- float roughEnvironmentFloor = mix(0.04, 0.38, clampedRoughness);
1740
- float proceduralSpecularResponse = max(reflectionBand, roughEnvironmentFloor);
1741
- vec3 proceduralSpecular = u_environmentSpecularColor * u_environmentSpecularIntensity * proceduralSpecularResponse * proceduralEnvironmentWeight;
1742
- float sampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureSpecularIntensity);
1743
- float environmentLod = clampedRoughness * max(u_environmentMapTextureMipCount - 1.0, 0.0);
1744
- vec3 sampledSpecular = a3dPbrBoundHdrSpecularRadiance(a3dPbrDecodeEnvironmentSample(a3dPbrEnvironmentSampleRaw(reflectionDirection, environmentLod)));
1745
- float nDotV = clamp(dot(normal, viewDirection), 0.0, 1.0);
1746
- vec2 brdfLut = texture(u_environmentBrdfLutTexture, vec2(nDotV, clampedRoughness)).rg;
1747
- sampledSpecular = a3dPbrClampSampledSpecularEdgeEnergy(sampledSpecular, nDotV, clampedRoughness);
1748
- sampledSpecular *= u_environmentMapTextureSpecularIntensity * sampledEnvironmentWeight * mix(0.84, 0.58, clampedRoughness);
1749
- return proceduralSpecular + sampledSpecular;
1750
- }
1751
- vec3 a3dPbrEncodeOutput(vec3 linearColor) {
1752
- vec3 color = max(linearColor, vec3(0.0));
1753
- vec3 filmic = clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), vec3(0.0), vec3(1.0));
1754
- vec3 srgb = pow(filmic, vec3(1.0 / 2.2));
1755
- return mix(color, srgb, step(0.5, u_outputColorSpace));
1756
- }
1757
- void main() {
1758
- vec3 mappedNormal = a3dNormalMappedNormal(v_normal, v_tangent, v_uv);
1759
- if (!gl_FrontFacing) mappedNormal = -mappedNormal;
1760
- vec3 viewDirection = normalize(u_cameraPosition - v_worldPosition);
1761
- vec3 materialBase = u_baseColor.rgb * v_vertexColor.rgb;
1762
- vec3 shaded = u_emissiveColor * u_emissiveStrength + a3dPbrEnvironmentLight(
1763
- mappedNormal,
1764
- viewDirection,
1765
- a3dPbrEnvironmentDiffuseInput(mappedNormal),
1766
- a3dPbrEnvironmentSpecularInput(mappedNormal, viewDirection, u_roughness),
1767
- materialBase,
1768
- u_metallic,
1769
- u_roughness,
1770
- u_specularFactor,
1771
- u_specularColorFactor
1772
- );
1773
- int count = min(int(u_lightCount), 16);
1774
- for (int i = 0; i < count; ++i) {
1775
- int baseIndex = i * 4;
1776
- vec4 colorIntensity = u_lightData[baseIndex];
1777
- vec4 positionRange = u_lightData[baseIndex + 1];
1778
- vec4 directionKind = u_lightData[baseIndex + 2];
1779
- vec4 spotShadowLayer = u_lightData[baseIndex + 3];
1780
- float kind = directionKind.w;
1781
- vec3 lightDirection = -directionKind.xyz;
1782
- float attenuation = 1.0;
1783
- if (kind > 0.5) {
1784
- vec3 toLight = positionRange.xyz - v_worldPosition;
1785
- float distanceToLight = length(toLight);
1786
- lightDirection = distanceToLight > 0.0001 ? toLight / distanceToLight : -directionKind.xyz;
1787
- float range = max(positionRange.w, 0.0001);
1788
- float rangeFalloff = clamp(1.0 - pow(distanceToLight / range, 4.0), 0.0, 1.0);
1789
- rangeFalloff *= rangeFalloff;
1790
- attenuation = rangeFalloff / max(distanceToLight * distanceToLight, 1.0);
1791
- }
1792
- if (kind > 1.5) {
1793
- vec3 lightToFragment = normalize(v_worldPosition - positionRange.xyz);
1794
- float cone = dot(normalize(directionKind.xyz), lightToFragment);
1795
- float outer = cos(spotShadowLayer.x);
1796
- float inner = cos(spotShadowLayer.x * max(1.0 - spotShadowLayer.y, 0.001));
1797
- attenuation *= smoothstep(outer, inner, cone);
1798
- }
1799
- float shadowFactor = mix(1.0, kind > 0.5 && kind < 1.5 ? a3dPointShadowFactor(v_worldPosition, mappedNormal, lightDirection) : a3dForwardShadowFactor(v_worldPosition, mappedNormal, lightDirection), step(0.5, spotShadowLayer.z));
1800
- shaded += a3dPbrDirectLight(mappedNormal, viewDirection, lightDirection, colorIntensity.rgb, colorIntensity.a * attenuation * shadowFactor, materialBase, u_metallic, u_roughness, u_specularFactor, u_specularColorFactor);
1801
- }
1802
- float alpha = u_baseColor.a * v_vertexColor.a;
1803
- if (alpha < u_alphaCutoff) discard;
1804
- vec3 fogged = a3dApplyEnvironmentFog(max(shaded, vec3(0.0)), v_worldPosition);
1805
- outColor = vec4(a3dPbrEncodeOutput(fogged), alpha);
1806
- }
1807
- `
1808
- });
1809
- library.register({
1810
- name: DEFAULT_TEXTURED_PBR_SHADER_NAME,
1811
- marker: DEFAULT_TEXTURED_PBR_SHADER_MARKER,
1812
- variants: [
1813
- { name: DEFAULT_TEXTURED_PBR_CLEARCOAT_TEXTURES_VARIANT, defines: { A3D_PBR_CLEARCOAT_TEXTURES: true, A3D_PBR_DISABLE_TRANSMISSION_BACKDROP: true } },
1814
- { name: DEFAULT_TEXTURED_PBR_TRANSMISSION_VOLUME_TEXTURES_VARIANT, defines: { A3D_PBR_TRANSMISSION_VOLUME_TEXTURES: true, A3D_PBR_DISABLE_TRANSMISSION_BACKDROP: true } },
1815
- { name: DEFAULT_TEXTURED_PBR_SPECULAR_SHEEN_ANISOTROPY_TEXTURES_VARIANT, defines: { A3D_PBR_SPECULAR_TEXTURES: true, A3D_PBR_SPECULAR_SHEEN_ANISOTROPY_TEXTURES: true, A3D_PBR_DISABLE_TRANSMISSION_BACKDROP: true } },
1816
- { name: DEFAULT_TEXTURED_PBR_IRIDESCENCE_TEXTURES_VARIANT, defines: { A3D_PBR_IRIDESCENCE_TEXTURES: true, A3D_PBR_DISABLE_TRANSMISSION_BACKDROP: true } },
1817
- { name: DEFAULT_TEXTURED_PBR_CLEARCOAT_TRANSMISSION_VOLUME_TEXTURES_VARIANT, defines: { A3D_PBR_CLEARCOAT_TEXTURES: true, A3D_PBR_TRANSMISSION_VOLUME_TEXTURES: true, A3D_PBR_DISABLE_TRANSMISSION_BACKDROP: true } },
1818
- { name: DEFAULT_TEXTURED_PBR_CLEARCOAT_SPECULAR_TEXTURES_VARIANT, defines: { A3D_PBR_CLEARCOAT_TEXTURES: true, A3D_PBR_SPECULAR_TEXTURES: true, A3D_PBR_DISABLE_TRANSMISSION_BACKDROP: true } },
1819
- { name: DEFAULT_TEXTURED_PBR_SPECULAR_SHEEN_ANISOTROPY_IRIDESCENCE_TEXTURES_VARIANT, defines: { A3D_PBR_SPECULAR_TEXTURES: true, A3D_PBR_SPECULAR_SHEEN_ANISOTROPY_TEXTURES: true, A3D_PBR_IRIDESCENCE_TEXTURES: true, A3D_PBR_DISABLE_TRANSMISSION_BACKDROP: true } }
1820
- ],
1821
- vertex: `#version 300 es
1822
- // ${DEFAULT_TEXTURED_PBR_SHADER_MARKER}
1823
- precision highp float;
1824
- layout(location = 0) in vec3 a_position;
1825
- layout(location = 1) in vec3 a_normal;
1826
- layout(location = 2) in vec2 a_uv;
1827
- layout(location = 3) in vec4 a_tangent;
1828
- layout(location = 4) in vec4 a_color;
1829
- layout(location = 7) in vec2 a_uv1;
1830
- uniform mat4 u_modelViewProjection;
1831
- uniform mat4 u_modelMatrix;
1832
- uniform mat4 u_normalMatrix;
1833
- out vec3 v_normal;
1834
- out vec4 v_tangent;
1835
- out vec3 v_worldPosition;
1836
- out vec2 v_uv;
1837
- out vec2 v_uv1;
1838
- out vec4 v_vertexColor;
1839
- void main() {
1840
- v_uv = a_uv;
1841
- v_uv1 = a_uv1;
1842
- v_normal = mat3(u_normalMatrix) * a_normal;
1843
- v_tangent = vec4(mat3(u_normalMatrix) * a_tangent.xyz, a_tangent.w);
1844
- v_worldPosition = (u_modelMatrix * vec4(a_position, 1.0)).xyz;
1845
- v_vertexColor = a_color;
1846
- gl_Position = u_modelViewProjection * vec4(a_position, 1.0);
1847
- }
1848
- `,
1849
- fragment: `#version 300 es
1850
- // ${DEFAULT_TEXTURED_PBR_SHADER_MARKER}
1851
- precision highp float;
1852
- #include <lighting_common>
1853
- #include <pbr_common>
1854
- uniform vec4 u_baseColor;
1855
- uniform float u_alphaCutoff;
1856
- uniform float u_metallic;
1857
- uniform float u_roughness;
1858
- uniform vec3 u_environmentColor;
1859
- uniform float u_environmentIntensity;
1860
- uniform vec3 u_environmentSkyColor;
1861
- uniform vec3 u_environmentHorizonColor;
1862
- uniform vec3 u_environmentGroundColor;
1863
- uniform vec3 u_environmentSpecularColor;
1864
- uniform float u_environmentMapIntensity;
1865
- uniform float u_environmentSpecularIntensity;
1866
- uniform sampler2D u_environmentMapTexture;
1867
- uniform samplerCube u_environmentCubeMapTexture;
1868
- uniform float u_environmentCubeMapTextureEnabled;
1869
- uniform float u_environmentMapTextureEnabled;
1870
- uniform float u_environmentMapTextureIntensity;
1871
- uniform float u_environmentMapTextureSpecularIntensity;
1872
- uniform float u_materialEnvironmentSpecularScale;
1873
- uniform float u_environmentMapTextureRotation;
1874
- uniform float u_environmentMapTextureMipCount;
1875
- uniform float u_environmentMapTextureEncoding;
1876
- uniform sampler2D u_environmentBrdfLutTexture;
1877
- uniform float u_environmentBrdfLutEnabled;
1878
- uniform vec3 u_emissiveColor;
1879
- uniform float u_emissiveStrength;
1880
- uniform float u_clearcoatFactor;
1881
- uniform float u_clearcoatRoughnessFactor;
1882
- uniform float u_transmissionFactor;
1883
- uniform float u_diffuseTransmissionFactor;
1884
- uniform vec3 u_diffuseTransmissionColorFactor;
1885
- uniform float u_transmissionFallbackEnergy;
1886
- uniform float u_volumeThicknessFactor;
1887
- uniform float u_volumeAttenuationDistance;
1888
- uniform vec3 u_volumeAttenuationColor;
1889
- uniform float u_transmissionParallaxStrength;
1890
- uniform vec3 u_transmissionParallaxBoxMin;
1891
- uniform vec3 u_transmissionParallaxBoxMax;
1892
- uniform float u_transmissionBounceCount;
1893
- uniform float u_transmissionCausticStrength;
1894
- #ifndef A3D_PBR_DISABLE_TRANSMISSION_BACKDROP
1895
- uniform sampler2D u_transmissionBackdropTexture;
1896
- uniform float u_transmissionBackdropEnabled;
1897
- uniform float u_transmissionBackdropStrength;
1898
- uniform vec2 u_transmissionBackdropResolution;
1899
- uniform float u_transmissionBackdropMipCount;
1900
- uniform float u_transmissionBackdropRefractionScale;
1901
- #endif
1902
- uniform float u_ior;
1903
- uniform float u_specularFactor;
1904
- uniform vec3 u_specularColorFactor;
1905
- uniform vec3 u_sheenColorFactor;
1906
- uniform float u_sheenRoughnessFactor;
1907
- uniform float u_anisotropyStrength;
1908
- uniform float u_anisotropyRotation;
1909
- uniform float u_iridescenceFactor;
1910
- uniform float u_iridescenceIor;
1911
- uniform float u_iridescenceThicknessMinimum;
1912
- uniform float u_iridescenceThicknessMaximum;
1913
- uniform float u_dispersion;
1914
- uniform float u_lightCount;
1915
- uniform vec4 u_lightData[64];
1916
- uniform sampler2D u_shadowMapTexture;
1917
- uniform float u_shadowMapEnabled;
1918
- uniform mat4 u_shadowMapMatrix;
1919
- uniform float u_shadowMapStrength;
1920
- uniform float u_shadowMapBias;
1921
- uniform float u_shadowMapSlopeBias;
1922
- uniform vec2 u_shadowMapTexelSize;
1923
- uniform float u_shadowPcfSampleCount;
1924
- uniform vec4 u_shadowPcfSamples[32];
1925
- uniform sampler2D u_pointShadowMapTexture;
1926
- uniform float u_pointShadowMapEnabled;
1927
- uniform vec3 u_pointShadowLightPosition;
1928
- uniform float u_pointShadowRange;
1929
- uniform mat4 u_pointShadowFaceMatrices[6];
1930
- uniform vec4 u_pointShadowFaceRects[6];
1931
- uniform float u_pointShadowStrength;
1932
- uniform float u_pointShadowBias;
1933
- uniform float u_pointShadowSlopeBias;
1934
- uniform vec2 u_pointShadowTexelSize;
1935
- uniform float u_pointShadowPcfSampleCount;
1936
- uniform vec4 u_pointShadowPcfSamples[32];
1937
- uniform sampler2D u_baseColorTexture;
1938
- uniform float u_baseColorTextureEnabled;
1939
- uniform vec2 u_baseColorTextureOffset;
1940
- uniform vec2 u_baseColorTextureScale;
1941
- uniform float u_baseColorTextureRotation;
1942
- uniform float u_baseColorTextureTexCoord;
1943
- uniform vec2 u_baseColorTextureWrap;
1944
- uniform sampler2D u_normalTexture;
1945
- uniform vec2 u_normalTextureOffset;
1946
- uniform vec2 u_normalTextureScale;
1947
- uniform float u_normalTextureRotation;
1948
- uniform float u_normalTextureTexCoord;
1949
- uniform vec2 u_normalTextureWrap;
1950
- uniform float u_normalTextureEnabled;
1951
- uniform sampler2D u_emissiveTexture;
1952
- uniform float u_emissiveTextureEnabled;
1953
- uniform vec2 u_emissiveTextureOffset;
1954
- uniform vec2 u_emissiveTextureScale;
1955
- uniform float u_emissiveTextureRotation;
1956
- uniform float u_emissiveTextureTexCoord;
1957
- uniform vec2 u_emissiveTextureWrap;
1958
- uniform sampler2D u_metallicRoughnessTexture;
1959
- uniform float u_metallicRoughnessTextureEnabled;
1960
- uniform vec2 u_metallicRoughnessTextureOffset;
1961
- uniform vec2 u_metallicRoughnessTextureScale;
1962
- uniform float u_metallicRoughnessTextureRotation;
1963
- uniform float u_metallicRoughnessTextureTexCoord;
1964
- uniform vec2 u_metallicRoughnessTextureWrap;
1965
- uniform sampler2D u_occlusionTexture;
1966
- uniform float u_occlusionTextureEnabled;
1967
- uniform vec2 u_occlusionTextureOffset;
1968
- uniform vec2 u_occlusionTextureScale;
1969
- uniform float u_occlusionTextureRotation;
1970
- uniform float u_occlusionTextureTexCoord;
1971
- uniform vec2 u_occlusionTextureWrap;
1972
- uniform float u_occlusionStrength;
1973
- uniform float u_clearcoatNormalScale;
1974
- #ifdef A3D_PBR_CLEARCOAT_TEXTURES
1975
- uniform sampler2D u_clearcoatTexture;
1976
- uniform float u_clearcoatTextureEnabled;
1977
- uniform vec2 u_clearcoatTextureOffset;
1978
- uniform vec2 u_clearcoatTextureScale;
1979
- uniform float u_clearcoatTextureRotation;
1980
- uniform float u_clearcoatTextureTexCoord;
1981
- uniform vec2 u_clearcoatTextureWrap;
1982
- uniform sampler2D u_clearcoatRoughnessTexture;
1983
- uniform float u_clearcoatRoughnessTextureEnabled;
1984
- uniform vec2 u_clearcoatRoughnessTextureOffset;
1985
- uniform vec2 u_clearcoatRoughnessTextureScale;
1986
- uniform float u_clearcoatRoughnessTextureRotation;
1987
- uniform float u_clearcoatRoughnessTextureTexCoord;
1988
- uniform vec2 u_clearcoatRoughnessTextureWrap;
1989
- uniform sampler2D u_clearcoatNormalTexture;
1990
- uniform float u_clearcoatNormalTextureEnabled;
1991
- uniform vec2 u_clearcoatNormalTextureOffset;
1992
- uniform vec2 u_clearcoatNormalTextureScale;
1993
- uniform float u_clearcoatNormalTextureRotation;
1994
- uniform float u_clearcoatNormalTextureTexCoord;
1995
- uniform vec2 u_clearcoatNormalTextureWrap;
1996
- #endif
1997
- #ifdef A3D_PBR_TRANSMISSION_VOLUME_TEXTURES
1998
- uniform sampler2D u_transmissionTexture;
1999
- uniform float u_transmissionTextureEnabled;
2000
- uniform vec2 u_transmissionTextureOffset;
2001
- uniform vec2 u_transmissionTextureScale;
2002
- uniform float u_transmissionTextureRotation;
2003
- uniform float u_transmissionTextureTexCoord;
2004
- uniform vec2 u_transmissionTextureWrap;
2005
- uniform sampler2D u_diffuseTransmissionTexture;
2006
- uniform float u_diffuseTransmissionTextureEnabled;
2007
- uniform vec2 u_diffuseTransmissionTextureOffset;
2008
- uniform vec2 u_diffuseTransmissionTextureScale;
2009
- uniform float u_diffuseTransmissionTextureRotation;
2010
- uniform float u_diffuseTransmissionTextureTexCoord;
2011
- uniform vec2 u_diffuseTransmissionTextureWrap;
2012
- uniform sampler2D u_diffuseTransmissionColorTexture;
2013
- uniform float u_diffuseTransmissionColorTextureEnabled;
2014
- uniform vec2 u_diffuseTransmissionColorTextureOffset;
2015
- uniform vec2 u_diffuseTransmissionColorTextureScale;
2016
- uniform float u_diffuseTransmissionColorTextureRotation;
2017
- uniform float u_diffuseTransmissionColorTextureTexCoord;
2018
- uniform vec2 u_diffuseTransmissionColorTextureWrap;
2019
- uniform sampler2D u_volumeThicknessTexture;
2020
- uniform float u_volumeThicknessTextureEnabled;
2021
- uniform vec2 u_volumeThicknessTextureOffset;
2022
- uniform vec2 u_volumeThicknessTextureScale;
2023
- uniform float u_volumeThicknessTextureRotation;
2024
- uniform float u_volumeThicknessTextureTexCoord;
2025
- uniform vec2 u_volumeThicknessTextureWrap;
2026
- #endif
2027
- #ifdef A3D_PBR_SPECULAR_TEXTURES
2028
- uniform sampler2D u_specularTexture;
2029
- uniform float u_specularTextureEnabled;
2030
- uniform vec2 u_specularTextureOffset;
2031
- uniform vec2 u_specularTextureScale;
2032
- uniform float u_specularTextureRotation;
2033
- uniform float u_specularTextureTexCoord;
2034
- uniform vec2 u_specularTextureWrap;
2035
- uniform sampler2D u_specularColorTexture;
2036
- uniform float u_specularColorTextureEnabled;
2037
- uniform vec2 u_specularColorTextureOffset;
2038
- uniform vec2 u_specularColorTextureScale;
2039
- uniform float u_specularColorTextureRotation;
2040
- uniform float u_specularColorTextureTexCoord;
2041
- uniform vec2 u_specularColorTextureWrap;
2042
- #endif
2043
- #ifdef A3D_PBR_SPECULAR_SHEEN_ANISOTROPY_TEXTURES
2044
- uniform sampler2D u_sheenColorTexture;
2045
- uniform float u_sheenColorTextureEnabled;
2046
- uniform vec2 u_sheenColorTextureOffset;
2047
- uniform vec2 u_sheenColorTextureScale;
2048
- uniform float u_sheenColorTextureRotation;
2049
- uniform float u_sheenColorTextureTexCoord;
2050
- uniform vec2 u_sheenColorTextureWrap;
2051
- uniform sampler2D u_sheenRoughnessTexture;
2052
- uniform float u_sheenRoughnessTextureEnabled;
2053
- uniform vec2 u_sheenRoughnessTextureOffset;
2054
- uniform vec2 u_sheenRoughnessTextureScale;
2055
- uniform float u_sheenRoughnessTextureRotation;
2056
- uniform float u_sheenRoughnessTextureTexCoord;
2057
- uniform vec2 u_sheenRoughnessTextureWrap;
2058
- uniform sampler2D u_anisotropyTexture;
2059
- uniform float u_anisotropyTextureEnabled;
2060
- uniform vec2 u_anisotropyTextureOffset;
2061
- uniform vec2 u_anisotropyTextureScale;
2062
- uniform float u_anisotropyTextureRotation;
2063
- uniform float u_anisotropyTextureTexCoord;
2064
- uniform vec2 u_anisotropyTextureWrap;
2065
- #endif
2066
- #ifdef A3D_PBR_IRIDESCENCE_TEXTURES
2067
- uniform sampler2D u_iridescenceTexture;
2068
- uniform float u_iridescenceTextureEnabled;
2069
- uniform vec2 u_iridescenceTextureOffset;
2070
- uniform vec2 u_iridescenceTextureScale;
2071
- uniform float u_iridescenceTextureRotation;
2072
- uniform float u_iridescenceTextureTexCoord;
2073
- uniform vec2 u_iridescenceTextureWrap;
2074
- uniform sampler2D u_iridescenceThicknessTexture;
2075
- uniform float u_iridescenceThicknessTextureEnabled;
2076
- uniform vec2 u_iridescenceThicknessTextureOffset;
2077
- uniform vec2 u_iridescenceThicknessTextureScale;
2078
- uniform float u_iridescenceThicknessTextureRotation;
2079
- uniform float u_iridescenceThicknessTextureTexCoord;
2080
- uniform vec2 u_iridescenceThicknessTextureWrap;
2081
- #endif
2082
- uniform float u_normalScale;
2083
- uniform float u_outputColorSpace;
2084
- uniform vec3 u_cameraPosition;
2085
- uniform float u_environmentFogEnabled;
2086
- uniform float u_environmentFogMode;
2087
- uniform vec3 u_environmentFogColor;
2088
- uniform float u_environmentFogNear;
2089
- uniform float u_environmentFogFar;
2090
- uniform float u_environmentFogDensity;
2091
- uniform float u_environmentFogHeightFalloff;
2092
- uniform float u_environmentFogHeightReference;
2093
- uniform float u_environmentFogMaxOpacity;
2094
- in vec3 v_normal;
2095
- in vec4 v_tangent;
2096
- in vec3 v_worldPosition;
2097
- in vec2 v_uv;
2098
- in vec2 v_uv1;
2099
- in vec4 v_vertexColor;
2100
- out vec4 outColor;
2101
- #include <environment_fog_common>
2102
- vec2 a3dTexturedPbrSelectUv(float texCoord) {
2103
- return texCoord > 0.5 ? v_uv1 : v_uv;
2104
- }
2105
- vec2 a3dTexturedPbrUv(float texCoord, vec2 offset, vec2 scale, float rotation) {
2106
- vec2 uv = a3dTexturedPbrSelectUv(texCoord);
2107
- vec2 scaledUv = uv * scale;
2108
- float c = cos(rotation);
2109
- float s = sin(rotation);
2110
- return vec2(scaledUv.x * c - scaledUv.y * s, scaledUv.x * s + scaledUv.y * c) + offset;
2111
- }
2112
- float a3dTexturedPbrWrapCoordinate(float value, float mode) {
2113
- if (mode > 1.5) {
2114
- float repeated = mod(value, 2.0);
2115
- return repeated <= 1.0 ? repeated : 2.0 - repeated;
2116
- }
2117
- if (mode > 0.5) return fract(value);
2118
- return clamp(value, 0.0, 1.0);
2119
- }
2120
- vec2 a3dTexturedPbrWrapUv(vec2 uv, vec2 wrapMode) {
2121
- return vec2(
2122
- a3dTexturedPbrWrapCoordinate(uv.x, wrapMode.x),
2123
- a3dTexturedPbrWrapCoordinate(uv.y, wrapMode.y)
2124
- );
2125
- }
2126
- vec3 a3dTexturedPbrEncodeOutput(vec3 linearColor) {
2127
- vec3 color = max(linearColor, vec3(0.0));
2128
- vec3 filmic = clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), vec3(0.0), vec3(1.0));
2129
- vec3 srgb = pow(filmic, vec3(1.0 / 2.2));
2130
- return mix(color, srgb, step(0.5, u_outputColorSpace));
2131
- }
2132
- vec3 a3dTexturedPbrDecodeSrgb(vec3 encodedColor) {
2133
- return max(encodedColor, vec3(0.0));
2134
- }
2135
- vec3 a3dTexturedPbrDecodeEnvironmentRgbe(vec4 encodedSample) {
2136
- float exponent = encodedSample.a * 255.0;
2137
- float scale = exponent <= 0.0 ? 0.0 : exp2(exponent - 128.0) * (255.0 / 256.0);
2138
- return max(encodedSample.rgb * scale, vec3(0.0));
2139
- }
2140
- vec3 a3dTexturedPbrDecodeEnvironmentSample(vec4 encodedSample) {
2141
- if (u_environmentMapTextureEncoding > 1.5) return max(encodedSample.rgb, vec3(0.0));
2142
- if (u_environmentMapTextureEncoding > 0.5) return a3dTexturedPbrDecodeEnvironmentRgbe(encodedSample);
2143
- return a3dTexturedPbrDecodeSrgb(encodedSample.rgb);
2144
- }
2145
- vec3 a3dTexturedPbrBoundHdrTransmissionRadiance(vec3 radiance) {
2146
- vec3 nonNegative = max(radiance, vec3(0.0));
2147
- return nonNegative / (vec3(1.0) + max(nonNegative - vec3(1.0), vec3(0.0)));
2148
- }
2149
- vec3 a3dTexturedPbrBoundHdrSpecularRadiance(vec3 radiance) {
2150
- vec3 nonNegative = max(radiance, vec3(0.0));
2151
- vec3 softKnee = nonNegative / (vec3(1.0) + max(nonNegative - vec3(1.0), vec3(0.0)) * 0.62);
2152
- float maxChannel = max(max(softKnee.r, softKnee.g), softKnee.b);
2153
- return maxChannel > 1.35 ? softKnee * (1.35 / maxChannel) : softKnee;
2154
- }
2155
- vec3 a3dTexturedPbrClampSampledSpecularEdgeEnergy(vec3 radiance, float nDotV, float roughness) {
2156
- float faceOn = smoothstep(0.2, 0.72, clamp(nDotV, 0.0, 1.0));
2157
- float roughEnergy = mix(0.35, 0.88, clamp(roughness, 0.0, 1.0));
2158
- float edgeCap = mix(0.035, 0.86, faceOn) * roughEnergy;
2159
- float edgeScale = mix(0.045, 0.78, faceOn);
2160
- vec3 bounded = min(radiance * edgeScale, vec3(edgeCap));
2161
- return max(bounded, vec3(0.0));
2162
- }
2163
- vec3 a3dTexturedPbrApplyNormalSample(vec3 baseNormal, vec4 tangentFrame, vec3 sampled, float scale) {
2164
- vec3 n = normalize(baseNormal);
2165
- vec3 tangent = normalize(tangentFrame.xyz);
2166
- vec3 bitangent = normalize(cross(n, tangent) * tangentFrame.w);
2167
- return normalize(tangent * sampled.x * scale + bitangent * sampled.y * scale + n * max(sampled.z, 0.001));
2168
- }
2169
- vec3 a3dTexturedPbrNormal(vec3 baseNormal, vec4 tangentFrame, vec2 uv) {
2170
- vec3 sampled = texture(u_normalTexture, uv).xyz * 2.0 - 1.0;
2171
- return a3dTexturedPbrApplyNormalSample(baseNormal, tangentFrame, sampled, u_normalScale);
2172
- }
2173
- float a3dTexturedPbrShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
2174
- if (u_shadowMapEnabled < 0.5) return 1.0;
2175
- vec4 lightPosition = u_shadowMapMatrix * vec4(worldPosition, 1.0);
2176
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
2177
- vec2 uv = projected.xy * 0.5 + 0.5;
2178
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) return 1.0;
2179
- vec3 receiverNormal = normalize(normal);
2180
- vec3 receiverLightDirection = lightDirection / max(length(lightDirection), 0.0001);
2181
- float normalDotLight = clamp(abs(dot(receiverNormal, receiverLightDirection)), 0.0, 1.0);
2182
- float slopeReceiverBias = (1.0 - normalDotLight) * u_shadowMapSlopeBias * max(u_shadowMapTexelSize.x, u_shadowMapTexelSize.y);
2183
- float receiverDepth = projected.z * 0.5 + 0.5 - u_shadowMapBias - slopeReceiverBias;
2184
- float shadowed = 0.0;
2185
- float totalWeight = 0.0;
2186
- int sampleCount = clamp(int(u_shadowPcfSampleCount), 1, 32);
2187
- for (int i = 0; i < 32; ++i) {
2188
- if (i >= sampleCount) break;
2189
- vec4 sampleData = u_shadowPcfSamples[i];
2190
- float weight = max(sampleData.z, 0.0);
2191
- vec2 offset = sampleData.xy * u_shadowMapTexelSize;
2192
- float storedDepth = texture(u_shadowMapTexture, uv + offset).r;
2193
- shadowed += (receiverDepth > storedDepth ? 1.0 : 0.0) * weight;
2194
- totalWeight += weight;
2195
- }
2196
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
2197
- return mix(1.0, 1.0 - occlusion, clamp(u_shadowMapStrength, 0.0, 1.0));
2198
- }
2199
- float a3dTexturedPbrPointShadowFactor(vec3 worldPosition, vec3 normal, vec3 lightDirection) {
2200
- if (u_pointShadowMapEnabled < 0.5) return 1.0;
2201
- vec3 lightToFragment = worldPosition - u_pointShadowLightPosition;
2202
- vec3 absoluteDirection = abs(lightToFragment);
2203
- int faceIndex = absoluteDirection.x >= absoluteDirection.y && absoluteDirection.x >= absoluteDirection.z ? (lightToFragment.x >= 0.0 ? 0 : 1) : (absoluteDirection.y >= absoluteDirection.z ? (lightToFragment.y >= 0.0 ? 2 : 3) : (lightToFragment.z >= 0.0 ? 4 : 5));
2204
- vec4 lightPosition = u_pointShadowFaceMatrices[faceIndex] * vec4(worldPosition, 1.0);
2205
- vec3 projected = lightPosition.xyz / max(lightPosition.w, 0.0001);
2206
- vec2 localUv = projected.xy * 0.5 + 0.5;
2207
- if (localUv.x < 0.0 || localUv.x > 1.0 || localUv.y < 0.0 || localUv.y > 1.0 || length(lightToFragment) > u_pointShadowRange) return 1.0;
2208
- vec4 rect = u_pointShadowFaceRects[faceIndex];
2209
- vec2 uv = rect.xy + localUv * rect.zw;
2210
- float normalDotLight = clamp(abs(dot(normalize(normal), normalize(lightDirection))), 0.0, 1.0);
2211
- float receiverDepth = projected.z * 0.5 + 0.5 - u_pointShadowBias - (1.0 - normalDotLight) * u_pointShadowSlopeBias * max(u_pointShadowTexelSize.x, u_pointShadowTexelSize.y);
2212
- float shadowed = 0.0;
2213
- float totalWeight = 0.0;
2214
- int sampleCount = clamp(int(u_pointShadowPcfSampleCount), 1, 32);
2215
- for (int i = 0; i < 32; ++i) {
2216
- if (i >= sampleCount) break;
2217
- vec4 sampleData = u_pointShadowPcfSamples[i];
2218
- float weight = max(sampleData.z, 0.0);
2219
- shadowed += (receiverDepth > texture(u_pointShadowMapTexture, uv + sampleData.xy * u_pointShadowTexelSize).r ? 1.0 : 0.0) * weight;
2220
- totalWeight += weight;
2221
- }
2222
- float occlusion = totalWeight > 0.0 ? shadowed / totalWeight : 0.0;
2223
- return mix(1.0, 1.0 - occlusion, clamp(u_pointShadowStrength, 0.0, 1.0));
2224
- }
2225
- vec2 a3dTexturedPbrEnvironmentUv(vec3 direction, float rotation) {
2226
- vec3 d = normalize(direction);
2227
- float u = atan(d.z, d.x) / 6.28318530718 + 0.5 + rotation;
2228
- float v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
2229
- return vec2(fract(u), clamp(v, 0.0, 1.0));
2230
- }
2231
- vec3 a3dTexturedPbrRotateEnvironmentDirection(vec3 direction, float rotation) {
2232
- float angle = rotation * 6.28318530718;
2233
- float c = cos(angle);
2234
- float s = sin(angle);
2235
- vec3 d = normalize(direction);
2236
- return normalize(vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z));
2237
- }
2238
- vec4 a3dTexturedPbrEnvironmentSampleRaw(vec3 direction, float lod) {
2239
- vec4 equirectSample = textureLod(u_environmentMapTexture, a3dTexturedPbrEnvironmentUv(direction, u_environmentMapTextureRotation), lod);
2240
- vec4 cubeSample = textureLod(u_environmentCubeMapTexture, a3dTexturedPbrRotateEnvironmentDirection(direction, u_environmentMapTextureRotation), lod);
2241
- return mix(equirectSample, cubeSample, step(0.5, u_environmentCubeMapTextureEnabled));
2242
- }
2243
- vec3 a3dTexturedPbrBoxProjectedDirection(vec3 worldPosition, vec3 direction, vec3 boxMin, vec3 boxMax) {
2244
- vec3 safeDirection = normalize(direction);
2245
- vec3 invDirection = 1.0 / max(abs(safeDirection), vec3(0.0001)) * sign(safeDirection);
2246
- vec3 firstPlane = mix(boxMin, boxMax, step(vec3(0.0), safeDirection));
2247
- vec3 distances = (firstPlane - worldPosition) * invDirection;
2248
- float travel = min(min(
2249
- distances.x > 0.0 ? distances.x : 100000.0,
2250
- distances.y > 0.0 ? distances.y : 100000.0
2251
- ), distances.z > 0.0 ? distances.z : 100000.0);
2252
- vec3 hitPosition = worldPosition + safeDirection * travel;
2253
- vec3 boxCenter = (boxMin + boxMax) * 0.5;
2254
- return normalize(hitPosition - boxCenter);
2255
- }
2256
- vec3 a3dTexturedPbrEnvironmentDiffuseInput(vec3 normal) {
2257
- float environmentHemi = mix(0.35, 1.0, clamp(normal.y * 0.5 + 0.5, 0.0, 1.0));
2258
- vec3 ambientEnvironment = u_environmentColor * u_environmentIntensity * environmentHemi;
2259
- float proceduralEnvironmentWeight = step(0.0001, u_environmentMapIntensity);
2260
- float skyBlend = clamp(normal.y * 0.5 + 0.5, 0.0, 1.0);
2261
- float horizonBlend = 1.0 - abs(normal.y);
2262
- vec3 proceduralDiffuse = mix(u_environmentGroundColor, u_environmentSkyColor, skyBlend);
2263
- proceduralDiffuse = mix(proceduralDiffuse, u_environmentHorizonColor, clamp(horizonBlend, 0.0, 1.0) * 0.55);
2264
- vec3 environmentDiffuse = mix(ambientEnvironment, proceduralDiffuse * u_environmentMapIntensity, proceduralEnvironmentWeight);
2265
- float sampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureIntensity);
2266
- float diffuseEnvironmentLod = max(u_environmentMapTextureMipCount - 1.0, 0.0);
2267
- vec3 sampledDiffuse = a3dTexturedPbrDecodeEnvironmentSample(a3dTexturedPbrEnvironmentSampleRaw(normal, diffuseEnvironmentLod));
2268
- return mix(environmentDiffuse, environmentDiffuse * 0.18 + sampledDiffuse * u_environmentMapTextureIntensity * 0.92, sampledEnvironmentWeight);
2269
- }
2270
- vec3 a3dTexturedPbrEnvironmentSpecularInput(vec3 normal, vec3 viewDirection, float roughness) {
2271
- float materialEnvironmentSpecularScale = clamp(u_materialEnvironmentSpecularScale, 0.0, 1.0);
2272
- float materialFiniteSpecularScale = sqrt(materialEnvironmentSpecularScale);
2273
- float proceduralEnvironmentWeight = step(0.0001, u_environmentMapIntensity);
2274
- vec3 reflectionDirection = reflect(-viewDirection, normal);
2275
- float clampedRoughness = clamp(roughness, 0.0, 1.0);
2276
- float nDotV = clamp(dot(normal, viewDirection), 0.0, 1.0);
2277
- float faceOnSpecularGate = smoothstep(0.1, 0.42, nDotV);
2278
- float reflectionBand = pow(clamp(reflectionDirection.y * 0.5 + 0.5, 0.0, 1.0), mix(18.0, 2.0, clampedRoughness));
2279
- float roughEnvironmentFloor = mix(0.04, 0.38, clampedRoughness);
2280
- float proceduralSpecularResponse = max(reflectionBand, roughEnvironmentFloor);
2281
- float horizonStripe = pow(clamp(1.0 - abs(reflectionDirection.y), 0.0, 1.0), mix(14.0, 3.0, clampedRoughness));
2282
- float sideStripe = pow(clamp(abs(reflectionDirection.x), 0.0, 1.0), mix(10.0, 3.0, clampedRoughness)) * smoothstep(0.0, 0.72, 1.0 - abs(reflectionDirection.y));
2283
- float overheadStripe = pow(clamp(reflectionDirection.y * 0.5 + 0.5, 0.0, 1.0), mix(22.0, 4.0, clampedRoughness));
2284
- vec3 proceduralSpecularColor = u_environmentSpecularColor * proceduralSpecularResponse
2285
- + u_environmentHorizonColor * horizonStripe * 0.18
2286
- + u_environmentSkyColor * overheadStripe * 0.12
2287
- + u_environmentGroundColor * sideStripe * 0.09;
2288
- vec3 proceduralSpecular = proceduralSpecularColor * u_environmentSpecularIntensity * proceduralEnvironmentWeight;
2289
- float sampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureSpecularIntensity);
2290
- float environmentLod = clampedRoughness * max(u_environmentMapTextureMipCount - 1.0, 0.0);
2291
- vec3 sampledSpecular = a3dTexturedPbrBoundHdrSpecularRadiance(a3dTexturedPbrDecodeEnvironmentSample(a3dTexturedPbrEnvironmentSampleRaw(reflectionDirection, environmentLod)));
2292
- vec2 brdfLut = texture(u_environmentBrdfLutTexture, vec2(nDotV, clampedRoughness)).rg;
2293
- sampledSpecular = a3dTexturedPbrClampSampledSpecularEdgeEnergy(sampledSpecular, nDotV, clampedRoughness);
2294
- sampledSpecular *= u_environmentMapTextureSpecularIntensity * sampledEnvironmentWeight * mix(0.84, 0.58, clampedRoughness);
2295
- float proceduralSpecularScale = materialFiniteSpecularScale * mix(0.34, 1.0, faceOnSpecularGate);
2296
- float sampledSpecularScale = materialEnvironmentSpecularScale * mix(0.18, 1.0, faceOnSpecularGate);
2297
- return proceduralSpecular * proceduralSpecularScale + sampledSpecular * sampledSpecularScale;
2298
- }
2299
- vec3 a3dTexturedPbrIridescenceColor(float minimumThickness, float maximumThickness, float iridescenceIor) {
2300
- float thickness = clamp((minimumThickness + maximumThickness) * 0.5, 0.0, 1200.0);
2301
- float phase = clamp((thickness - 100.0) / 1100.0, 0.0, 1.0) * 6.2831853;
2302
- float iorShift = clamp((iridescenceIor - 1.0) / 2.0, 0.0, 1.0) * 0.65;
2303
- return clamp(0.5 + 0.5 * cos(phase + iorShift + vec3(0.0, 2.0943951, 4.1887902)), vec3(0.0), vec3(1.0));
2304
- }
2305
- vec3 a3dTexturedPbrExtensionDirectLight(
2306
- vec3 normal,
2307
- vec3 viewDirection,
2308
- vec3 lightDirection,
2309
- vec3 lightColor,
2310
- float lightIntensity,
2311
- float clearcoat,
2312
- float clearcoatRoughness,
2313
- vec3 sheenColor,
2314
- float sheenRoughness,
2315
- float anisotropy,
2316
- float anisotropyRotation,
2317
- float iridescence,
2318
- float iridescenceIor,
2319
- float iridescenceThicknessMinimum,
2320
- float iridescenceThicknessMaximum
2321
- ) {
2322
- vec3 N = normalize(normal);
2323
- vec3 V = normalize(viewDirection);
2324
- vec3 L = normalize(lightDirection);
2325
- vec3 H = normalize(V + L);
2326
- float nDotL = a3dSaturate(dot(N, L));
2327
- float nDotV = max(a3dSaturate(dot(N, V)), A3D_EPSILON);
2328
- float nDotH = a3dSaturate(dot(N, H));
2329
- float vDotH = a3dSaturate(dot(V, H));
2330
- float clearcoatRough = clamp(clearcoatRoughness, 0.18, 1.0);
2331
- vec3 clearcoatF = a3dFresnelSchlick(vec3(0.04), vDotH);
2332
- float clearcoatD = a3dDistributionGGX(nDotH, clearcoatRough);
2333
- float clearcoatG = a3dGeometrySmithGGXCorrelated(nDotV, nDotL, clearcoatRough);
2334
- vec3 clearcoatLobe = clearcoatF * clearcoatD * clearcoatG * clamp(clearcoat, 0.0, 1.0) * 0.12;
2335
- float sheenStrength = (1.0 - clamp(sheenRoughness, 0.0, 1.0)) * pow(a3dSaturate(1.0 - vDotH), 5.0);
2336
- vec3 sheenLobe = clamp(sheenColor, vec3(0.0), vec3(1.0)) * sheenStrength * 0.18;
2337
- vec3 anisotropyAxis = normalize(vec3(cos(anisotropyRotation), 0.0, sin(anisotropyRotation)));
2338
- float anisotropyBand = pow(abs(dot(H, anisotropyAxis)), mix(28.0, 6.0, clearcoatRough));
2339
- vec3 anisotropyLobe = vec3(clamp(anisotropy, 0.0, 1.0) * anisotropyBand * 0.055);
2340
- vec3 iridescenceColor = a3dTexturedPbrIridescenceColor(iridescenceThicknessMinimum, iridescenceThicknessMaximum, iridescenceIor);
2341
- vec3 iridescenceLobe = iridescenceColor * clamp(iridescence, 0.0, 1.0) * clearcoatF * pow(a3dSaturate(1.0 - nDotV), 2.0) * 0.12;
2342
- return (clearcoatLobe + sheenLobe + anisotropyLobe + iridescenceLobe) * lightColor * lightIntensity * nDotL;
2343
- }
2344
- vec3 a3dTexturedPbrExtensionEnvironmentLight(
2345
- vec3 normal,
2346
- vec3 viewDirection,
2347
- vec3 specularRadiance,
2348
- float clearcoat,
2349
- float clearcoatRoughness,
2350
- vec3 sheenColor,
2351
- float sheenRoughness,
2352
- float anisotropy,
2353
- float anisotropyRotation,
2354
- float iridescence,
2355
- float iridescenceIor,
2356
- float iridescenceThicknessMinimum,
2357
- float iridescenceThicknessMaximum
2358
- ) {
2359
- float nDotV = max(a3dSaturate(dot(normalize(normal), normalize(viewDirection))), A3D_EPSILON);
2360
- vec3 clearcoatF = a3dFresnelSchlickRoughness(vec3(0.04), nDotV, clamp(clearcoatRoughness, 0.18, 1.0));
2361
- float faceOn = smoothstep(0.18, 0.68, nDotV);
2362
- vec3 boundedSpecularRadiance = min(specularRadiance * mix(0.1, 0.82, faceOn), vec3(mix(0.08, 0.95, faceOn)));
2363
- vec3 clearcoatLobe = boundedSpecularRadiance * clearcoatF * clamp(clearcoat, 0.0, 1.0) * 0.045;
2364
- vec3 sheenLobe = clamp(sheenColor, vec3(0.0), vec3(1.0)) * (1.0 - clamp(sheenRoughness, 0.0, 1.0)) * pow(a3dSaturate(1.0 - nDotV), 5.0) * 0.12;
2365
- float anisotropyBand = 0.5 + 0.5 * cos(anisotropyRotation * 2.0);
2366
- vec3 anisotropyLobe = boundedSpecularRadiance * clamp(anisotropy, 0.0, 1.0) * mix(0.025, 0.07, anisotropyBand);
2367
- vec3 iridescenceColor = a3dTexturedPbrIridescenceColor(iridescenceThicknessMinimum, iridescenceThicknessMaximum, iridescenceIor);
2368
- vec3 iridescenceLobe = boundedSpecularRadiance * iridescenceColor * clamp(iridescence, 0.0, 1.0) * pow(a3dSaturate(1.0 - nDotV), 2.0) * 0.09;
2369
- return clearcoatLobe + sheenLobe + anisotropyLobe + iridescenceLobe;
2370
- }
2371
- void main() {
2372
- vec2 baseColorUv = a3dTexturedPbrUv(u_baseColorTextureTexCoord, u_baseColorTextureOffset, u_baseColorTextureScale, u_baseColorTextureRotation);
2373
- vec2 normalUv = a3dTexturedPbrUv(u_normalTextureTexCoord, u_normalTextureOffset, u_normalTextureScale, u_normalTextureRotation);
2374
- vec2 emissiveUv = a3dTexturedPbrUv(u_emissiveTextureTexCoord, u_emissiveTextureOffset, u_emissiveTextureScale, u_emissiveTextureRotation);
2375
- vec2 metallicRoughnessUv = a3dTexturedPbrUv(u_metallicRoughnessTextureTexCoord, u_metallicRoughnessTextureOffset, u_metallicRoughnessTextureScale, u_metallicRoughnessTextureRotation);
2376
- vec2 occlusionUv = a3dTexturedPbrUv(u_occlusionTextureTexCoord, u_occlusionTextureOffset, u_occlusionTextureScale, u_occlusionTextureRotation);
2377
- #ifdef A3D_PBR_CLEARCOAT_TEXTURES
2378
- vec2 clearcoatUv = a3dTexturedPbrUv(u_clearcoatTextureTexCoord, u_clearcoatTextureOffset, u_clearcoatTextureScale, u_clearcoatTextureRotation);
2379
- vec2 clearcoatRoughnessUv = a3dTexturedPbrUv(u_clearcoatRoughnessTextureTexCoord, u_clearcoatRoughnessTextureOffset, u_clearcoatRoughnessTextureScale, u_clearcoatRoughnessTextureRotation);
2380
- vec2 clearcoatNormalUv = a3dTexturedPbrUv(u_clearcoatNormalTextureTexCoord, u_clearcoatNormalTextureOffset, u_clearcoatNormalTextureScale, u_clearcoatNormalTextureRotation);
2381
- #endif
2382
- #ifdef A3D_PBR_TRANSMISSION_VOLUME_TEXTURES
2383
- vec2 transmissionUv = a3dTexturedPbrUv(u_transmissionTextureTexCoord, u_transmissionTextureOffset, u_transmissionTextureScale, u_transmissionTextureRotation);
2384
- vec2 diffuseTransmissionUv = a3dTexturedPbrUv(u_diffuseTransmissionTextureTexCoord, u_diffuseTransmissionTextureOffset, u_diffuseTransmissionTextureScale, u_diffuseTransmissionTextureRotation);
2385
- vec2 diffuseTransmissionColorUv = a3dTexturedPbrUv(u_diffuseTransmissionColorTextureTexCoord, u_diffuseTransmissionColorTextureOffset, u_diffuseTransmissionColorTextureScale, u_diffuseTransmissionColorTextureRotation);
2386
- vec2 volumeThicknessUv = a3dTexturedPbrUv(u_volumeThicknessTextureTexCoord, u_volumeThicknessTextureOffset, u_volumeThicknessTextureScale, u_volumeThicknessTextureRotation);
2387
- #endif
2388
- #ifdef A3D_PBR_SPECULAR_TEXTURES
2389
- vec2 specularUv = a3dTexturedPbrUv(u_specularTextureTexCoord, u_specularTextureOffset, u_specularTextureScale, u_specularTextureRotation);
2390
- vec2 specularColorUv = a3dTexturedPbrUv(u_specularColorTextureTexCoord, u_specularColorTextureOffset, u_specularColorTextureScale, u_specularColorTextureRotation);
2391
- #endif
2392
- #ifdef A3D_PBR_SPECULAR_SHEEN_ANISOTROPY_TEXTURES
2393
- vec2 sheenColorUv = a3dTexturedPbrUv(u_sheenColorTextureTexCoord, u_sheenColorTextureOffset, u_sheenColorTextureScale, u_sheenColorTextureRotation);
2394
- vec2 sheenRoughnessUv = a3dTexturedPbrUv(u_sheenRoughnessTextureTexCoord, u_sheenRoughnessTextureOffset, u_sheenRoughnessTextureScale, u_sheenRoughnessTextureRotation);
2395
- vec2 anisotropyUv = a3dTexturedPbrUv(u_anisotropyTextureTexCoord, u_anisotropyTextureOffset, u_anisotropyTextureScale, u_anisotropyTextureRotation);
2396
- #endif
2397
- #ifdef A3D_PBR_IRIDESCENCE_TEXTURES
2398
- vec2 iridescenceUv = a3dTexturedPbrUv(u_iridescenceTextureTexCoord, u_iridescenceTextureOffset, u_iridescenceTextureScale, u_iridescenceTextureRotation);
2399
- vec2 iridescenceThicknessUv = a3dTexturedPbrUv(u_iridescenceThicknessTextureTexCoord, u_iridescenceThicknessTextureOffset, u_iridescenceThicknessTextureScale, u_iridescenceThicknessTextureRotation);
2400
- #endif
2401
- vec4 baseColorSample = texture(u_baseColorTexture, a3dTexturedPbrWrapUv(baseColorUv, u_baseColorTextureWrap));
2402
- vec4 decodedBaseColor = vec4(a3dTexturedPbrDecodeSrgb(baseColorSample.rgb), baseColorSample.a);
2403
- float baseColorTextureWeight = clamp(u_baseColorTextureEnabled, 0.0, 1.0);
2404
- float sourcePaintMaterialGate = smoothstep(0.015, 0.09, u_materialEnvironmentSpecularScale)
2405
- * smoothstep(0.18, 0.62, u_baseColor.r)
2406
- * (1.0 - smoothstep(0.045, 0.16, max(u_baseColor.g, u_baseColor.b)));
2407
- float sourcePaintDetailGate = baseColorTextureWeight * sourcePaintMaterialGate;
2408
- float sourcePaintDetailLuma = dot(decodedBaseColor.rgb, vec3(0.2126, 0.7152, 0.0722));
2409
- float sourcePaintDepthSignal = smoothstep(0.14, 0.86, sourcePaintDetailLuma);
2410
- vec3 sourcePaintTextureResponse = min(
2411
- u_baseColor.rgb * vec3(
2412
- mix(0.72, 1.22, sourcePaintDepthSignal),
2413
- mix(0.76, 1.03, sourcePaintDepthSignal),
2414
- mix(0.8, 1.0, sourcePaintDepthSignal)
2415
- ),
2416
- vec3(0.98, 0.12, 0.075)
2417
- );
2418
- float sourcePaintDetailContrast = clamp((sourcePaintDepthSignal - 0.5) * 1.9, -0.68, 0.68);
2419
- vec3 sourcePaintDetailScale = vec3(
2420
- 1.0 + sourcePaintDetailContrast * 0.48,
2421
- 1.0 + sourcePaintDetailContrast * 0.06,
2422
- 1.0 + sourcePaintDetailContrast * 0.035
2423
- );
2424
- vec3 sourceNormalDetailSample = texture(u_normalTexture, a3dTexturedPbrWrapUv(normalUv, u_normalTextureWrap)).xyz * 2.0 - 1.0;
2425
- float sourcePaintNormalDetailWeight = sourcePaintDetailGate
2426
- * step(0.5, u_normalTextureEnabled)
2427
- * smoothstep(0.08, 0.42, clamp(u_normalScale, 0.0, 1.0));
2428
- float sourcePaintNormalColorDetail = clamp(
2429
- (sourceNormalDetailSample.x * 0.65 + sourceNormalDetailSample.y * 0.35)
2430
- * sourcePaintNormalDetailWeight
2431
- * 1.15,
2432
- -0.16,
2433
- 0.16
2434
- );
2435
- sourcePaintDetailScale *= vec3(
2436
- 1.0 + sourcePaintNormalColorDetail * 0.24,
2437
- 1.0 + sourcePaintNormalColorDetail * 0.03,
2438
- 1.0 + sourcePaintNormalColorDetail * 0.02
2439
- );
2440
- float sourcePaintNormalEdge = clamp(
2441
- length(sourceNormalDetailSample.xy) * sourcePaintNormalDetailWeight * 0.7,
2442
- 0.0,
2443
- 0.14
2444
- );
2445
- sourcePaintDetailScale *= vec3(
2446
- 1.0 + sourcePaintNormalEdge * 0.16,
2447
- 1.0 + sourcePaintNormalEdge * 0.02,
2448
- 1.0 + sourcePaintNormalEdge * 0.014
2449
- );
2450
- vec3 sourceTexturedBaseColor = mix(u_baseColor.rgb * decodedBaseColor.rgb, sourcePaintTextureResponse, sourcePaintMaterialGate);
2451
- vec4 texturedBase = vec4(mix(u_baseColor.rgb, sourceTexturedBaseColor, baseColorTextureWeight), mix(u_baseColor.a, u_baseColor.a * decodedBaseColor.a, baseColorTextureWeight)) * v_vertexColor;
2452
- vec3 sourcePaintDetailedBase = texturedBase.rgb * mix(vec3(1.0), sourcePaintDetailScale, sourcePaintDetailGate);
2453
- vec3 sourcePaintColorCap = mix(vec3(1.0), vec3(0.98, 0.12, 0.075), sourcePaintDetailGate);
2454
- texturedBase.rgb = clamp(min(sourcePaintDetailedBase, sourcePaintColorCap), vec3(0.0), vec3(1.0));
2455
- vec4 metallicRoughnessSample = texture(u_metallicRoughnessTexture, a3dTexturedPbrWrapUv(metallicRoughnessUv, u_metallicRoughnessTextureWrap));
2456
- float roughness = mix(u_roughness, clamp(u_roughness * metallicRoughnessSample.g, 0.0, 1.0), step(0.5, u_metallicRoughnessTextureEnabled));
2457
- float metallic = mix(u_metallic, clamp(u_metallic * metallicRoughnessSample.b, 0.0, 1.0), step(0.5, u_metallicRoughnessTextureEnabled));
2458
- float occlusion = mix(1.0, mix(1.0, texture(u_occlusionTexture, a3dTexturedPbrWrapUv(occlusionUv, u_occlusionTextureWrap)).r, clamp(u_occlusionStrength, 0.0, 1.0)), step(0.5, u_occlusionTextureEnabled));
2459
- vec3 geometryNormal = normalize(v_normal);
2460
- vec3 mappedNormal = mix(geometryNormal, a3dTexturedPbrApplyNormalSample(geometryNormal, v_tangent, sourceNormalDetailSample, u_normalScale), step(0.5, u_normalTextureEnabled));
2461
- if (!gl_FrontFacing) mappedNormal = -mappedNormal;
2462
- vec3 clearcoatNormalDirection = mappedNormal;
2463
- float clearcoatNormalBoost = max(0.25, 1.0 - clamp(u_clearcoatNormalScale, 0.0, 1.0) * 0.12);
2464
- #ifdef A3D_PBR_CLEARCOAT_TEXTURES
2465
- float clearcoatNormalTextureWeight = step(0.5, u_clearcoatNormalTextureEnabled);
2466
- vec3 clearcoatNormalSample = texture(u_clearcoatNormalTexture, a3dTexturedPbrWrapUv(clearcoatNormalUv, u_clearcoatNormalTextureWrap)).xyz * 2.0 - 1.0;
2467
- vec3 sampledClearcoatNormal = a3dTexturedPbrApplyNormalSample(mappedNormal, v_tangent, clearcoatNormalSample, u_clearcoatNormalScale);
2468
- clearcoatNormalDirection = normalize(mix(mappedNormal, sampledClearcoatNormal, 0.26 * clearcoatNormalTextureWeight));
2469
- clearcoatNormalBoost *= mix(1.0, max(0.25, clearcoatNormalSample.z), clearcoatNormalTextureWeight);
2470
- #endif
2471
- float clearcoat = clamp(u_clearcoatFactor * clearcoatNormalBoost, 0.0, 1.0);
2472
- float clearcoatRoughness = max(clamp(u_clearcoatRoughnessFactor, 0.0, 1.0), 0.18);
2473
- float transmission = clamp(u_transmissionFactor, 0.0, 1.0);
2474
- float diffuseTransmission = clamp(u_diffuseTransmissionFactor, 0.0, 1.0);
2475
- vec3 diffuseTransmissionColor = clamp(u_diffuseTransmissionColorFactor, vec3(0.0), vec3(1.0));
2476
- float volumeThickness = max(u_volumeThicknessFactor, 0.0);
2477
- float specular = clamp(u_specularFactor, 0.0, 1.0);
2478
- vec3 specularColor = max(u_specularColorFactor, vec3(0.0));
2479
- vec3 sheenColor = clamp(u_sheenColorFactor, vec3(0.0), vec3(1.0));
2480
- float sheenRoughness = clamp(u_sheenRoughnessFactor, 0.0, 1.0);
2481
- float anisotropy = clamp(u_anisotropyStrength, 0.0, 1.0);
2482
- float anisotropyRotation = u_anisotropyRotation;
2483
- float iridescence = clamp(u_iridescenceFactor, 0.0, 1.0);
2484
- float iridescenceThickness = mix(u_iridescenceThicknessMinimum, u_iridescenceThicknessMaximum, 0.5);
2485
- #ifdef A3D_PBR_CLEARCOAT_TEXTURES
2486
- clearcoat = clamp(clearcoat * mix(1.0, texture(u_clearcoatTexture, a3dTexturedPbrWrapUv(clearcoatUv, u_clearcoatTextureWrap)).r, step(0.5, u_clearcoatTextureEnabled)), 0.0, 1.0);
2487
- clearcoatRoughness = clamp(clearcoatRoughness * mix(1.0, texture(u_clearcoatRoughnessTexture, a3dTexturedPbrWrapUv(clearcoatRoughnessUv, u_clearcoatRoughnessTextureWrap)).g, step(0.5, u_clearcoatRoughnessTextureEnabled)), 0.0, 1.0);
2488
- clearcoatRoughness = max(clearcoatRoughness, 0.19 + (1.0 - clearcoatNormalBoost) * 0.12);
2489
- #endif
2490
- #ifdef A3D_PBR_TRANSMISSION_VOLUME_TEXTURES
2491
- transmission = clamp(transmission * mix(1.0, texture(u_transmissionTexture, a3dTexturedPbrWrapUv(transmissionUv, u_transmissionTextureWrap)).r, step(0.5, u_transmissionTextureEnabled)), 0.0, 1.0);
2492
- diffuseTransmission = clamp(diffuseTransmission * mix(1.0, texture(u_diffuseTransmissionTexture, a3dTexturedPbrWrapUv(diffuseTransmissionUv, u_diffuseTransmissionTextureWrap)).r, step(0.5, u_diffuseTransmissionTextureEnabled)), 0.0, 1.0);
2493
- diffuseTransmissionColor *= mix(vec3(1.0), a3dTexturedPbrDecodeSrgb(texture(u_diffuseTransmissionColorTexture, a3dTexturedPbrWrapUv(diffuseTransmissionColorUv, u_diffuseTransmissionColorTextureWrap)).rgb), step(0.5, u_diffuseTransmissionColorTextureEnabled));
2494
- volumeThickness *= mix(1.0, texture(u_volumeThicknessTexture, a3dTexturedPbrWrapUv(volumeThicknessUv, u_volumeThicknessTextureWrap)).g, step(0.5, u_volumeThicknessTextureEnabled));
2495
- #endif
2496
- #ifdef A3D_PBR_SPECULAR_TEXTURES
2497
- specular = clamp(specular * mix(1.0, texture(u_specularTexture, a3dTexturedPbrWrapUv(specularUv, u_specularTextureWrap)).a, step(0.5, u_specularTextureEnabled)), 0.0, 1.0);
2498
- specularColor *= mix(vec3(1.0), a3dTexturedPbrDecodeSrgb(texture(u_specularColorTexture, a3dTexturedPbrWrapUv(specularColorUv, u_specularColorTextureWrap)).rgb), step(0.5, u_specularColorTextureEnabled));
2499
- #endif
2500
- #ifdef A3D_PBR_SPECULAR_SHEEN_ANISOTROPY_TEXTURES
2501
- sheenColor *= mix(vec3(1.0), a3dTexturedPbrDecodeSrgb(texture(u_sheenColorTexture, a3dTexturedPbrWrapUv(sheenColorUv, u_sheenColorTextureWrap)).rgb), step(0.5, u_sheenColorTextureEnabled));
2502
- sheenRoughness = clamp(sheenRoughness * mix(1.0, texture(u_sheenRoughnessTexture, a3dTexturedPbrWrapUv(sheenRoughnessUv, u_sheenRoughnessTextureWrap)).a, step(0.5, u_sheenRoughnessTextureEnabled)), 0.0, 1.0);
2503
- anisotropy = clamp(anisotropy * mix(1.0, texture(u_anisotropyTexture, a3dTexturedPbrWrapUv(anisotropyUv, u_anisotropyTextureWrap)).b, step(0.5, u_anisotropyTextureEnabled)), 0.0, 1.0);
2504
- #endif
2505
- #ifdef A3D_PBR_IRIDESCENCE_TEXTURES
2506
- iridescence = clamp(iridescence * mix(1.0, texture(u_iridescenceTexture, a3dTexturedPbrWrapUv(iridescenceUv, u_iridescenceTextureWrap)).r, step(0.5, u_iridescenceTextureEnabled)), 0.0, 1.0);
2507
- iridescenceThickness = mix(iridescenceThickness, mix(u_iridescenceThicknessMinimum, u_iridescenceThicknessMaximum, texture(u_iridescenceThicknessTexture, a3dTexturedPbrWrapUv(iridescenceThicknessUv, u_iridescenceThicknessTextureWrap)).g), step(0.5, u_iridescenceThicknessTextureEnabled));
2508
- #endif
2509
- vec3 base = a3dApplyAdvancedPbrLobes(
2510
- texturedBase.rgb,
2511
- clearcoat,
2512
- clearcoatRoughness,
2513
- transmission,
2514
- diffuseTransmission,
2515
- diffuseTransmissionColor,
2516
- u_transmissionFallbackEnergy,
2517
- volumeThickness,
2518
- u_volumeAttenuationDistance,
2519
- u_volumeAttenuationColor,
2520
- u_ior,
2521
- specular,
2522
- specularColor,
2523
- sheenColor,
2524
- sheenRoughness,
2525
- anisotropy,
2526
- anisotropyRotation,
2527
- iridescence,
2528
- u_iridescenceIor,
2529
- iridescenceThickness,
2530
- iridescenceThickness,
2531
- u_dispersion
2532
- );
2533
- vec3 viewDirection = normalize(u_cameraPosition - v_worldPosition);
2534
- float materialRedPaintGate = smoothstep(0.28, 0.72, texturedBase.r)
2535
- * (1.0 - smoothstep(0.05, 0.18, max(texturedBase.g, texturedBase.b)))
2536
- * smoothstep(0.015, 0.07, u_materialEnvironmentSpecularScale);
2537
- float directTextureOcclusion = mix(1.0, occlusion, clamp(baseColorTextureWeight * mix(0.2, 0.56, materialRedPaintGate), 0.0, 0.56));
2538
- float sourcePaintDirectDetail = mix(0.82, 1.22, sourcePaintDepthSignal);
2539
- float environmentNdotV = clamp(dot(mappedNormal, viewDirection), 0.0, 1.0);
2540
- vec2 environmentBrdf = mix(vec2(1.0, 0.0), texture(u_environmentBrdfLutTexture, vec2(environmentNdotV, roughness)).rg, step(0.0001, u_environmentBrdfLutEnabled));
2541
- vec3 extensionEnvironmentSpecular = a3dTexturedPbrEnvironmentSpecularInput(clearcoatNormalDirection, viewDirection, clamp(clearcoatRoughness, 0.18, 1.0));
2542
- vec3 emissiveSample = a3dTexturedPbrDecodeSrgb(texture(u_emissiveTexture, a3dTexturedPbrWrapUv(emissiveUv, u_emissiveTextureWrap)).rgb);
2543
- vec3 shaded = u_emissiveColor * u_emissiveStrength * mix(vec3(1.0), emissiveSample, step(0.5, u_emissiveTextureEnabled)) + a3dPbrEnvironmentLightSplitSum(
2544
- mappedNormal,
2545
- viewDirection,
2546
- a3dTexturedPbrEnvironmentDiffuseInput(mappedNormal) * occlusion,
2547
- a3dTexturedPbrEnvironmentSpecularInput(mappedNormal, viewDirection, roughness),
2548
- environmentBrdf,
2549
- base,
2550
- metallic,
2551
- roughness,
2552
- specular,
2553
- specularColor
2554
- ) + a3dTexturedPbrExtensionEnvironmentLight(
2555
- clearcoatNormalDirection,
2556
- viewDirection,
2557
- extensionEnvironmentSpecular,
2558
- clearcoat,
2559
- clearcoatRoughness,
2560
- sheenColor,
2561
- sheenRoughness,
2562
- anisotropy,
2563
- anisotropyRotation,
2564
- iridescence,
2565
- u_iridescenceIor,
2566
- iridescenceThickness,
2567
- iridescenceThickness
2568
- );
2569
- float texturedTransmissionAmount = clamp(max(transmission, diffuseTransmission), 0.0, 1.0);
2570
- float texturedSampledEnvironmentWeight = step(0.0001, u_environmentMapTextureEnabled * u_environmentMapTextureIntensity);
2571
- #ifndef A3D_PBR_DISABLE_TRANSMISSION_BACKDROP
2572
- float texturedBackdropWeight = clamp(u_transmissionBackdropEnabled * u_transmissionBackdropStrength, 0.0, 1.0);
2573
- #else
2574
- float texturedBackdropWeight = 0.0;
2575
- #endif
2576
- if (texturedTransmissionAmount > 0.0001 && (texturedSampledEnvironmentWeight > 0.0001 || texturedBackdropWeight > 0.0001)) {
2577
- vec3 texturedReflectionDirection = reflect(-viewDirection, mappedNormal);
2578
- vec3 texturedRefractionDirection = refract(-viewDirection, mappedNormal, 1.0 / max(u_ior, 1.0));
2579
- texturedRefractionDirection = length(texturedRefractionDirection) > 0.0001 ? normalize(texturedRefractionDirection) : -texturedReflectionDirection;
2580
- float texturedParallaxStrength = clamp(u_transmissionParallaxStrength, 0.0, 1.0);
2581
- if (texturedParallaxStrength > 0.0001) {
2582
- vec3 texturedParallaxDirection = a3dTexturedPbrBoxProjectedDirection(v_worldPosition, texturedRefractionDirection, u_transmissionParallaxBoxMin, u_transmissionParallaxBoxMax);
2583
- texturedRefractionDirection = normalize(mix(texturedRefractionDirection, texturedParallaxDirection, texturedParallaxStrength));
2584
- }
2585
- float texturedBounceCount = clamp(u_transmissionBounceCount, 0.0, 4.0);
2586
- float texturedRefractionLod = clamp(roughness + volumeThickness * 0.12 + texturedBounceCount * 0.04 * texturedParallaxStrength, 0.0, 1.0) * max(u_environmentMapTextureMipCount - 1.0, 0.0);
2587
- vec3 texturedRefractedEnvironment = a3dTexturedPbrBoundHdrTransmissionRadiance(a3dTexturedPbrDecodeEnvironmentSample(a3dTexturedPbrEnvironmentSampleRaw(texturedRefractionDirection, texturedRefractionLod)));
2588
- float texturedVolumeTravel = clamp((max(volumeThickness, 0.0) * (1.0 + texturedBounceCount * 0.18 * texturedParallaxStrength)) / max(u_volumeAttenuationDistance, 0.0001), 0.0, 16.0);
2589
- vec3 texturedVolumeTint = pow(clamp(u_volumeAttenuationColor, vec3(0.0001), vec3(1.0)), vec3(texturedVolumeTravel));
2590
- float texturedCausticEnergy = u_transmissionCausticStrength * texturedParallaxStrength * texturedTransmissionAmount * pow(1.0 - roughness, 2.0) / (1.0 + texturedBounceCount * 0.35);
2591
- float texturedRoughVolumeIorLift = mix(1.0, 1.45, clamp((u_ior - 1.0) / 1.5, 0.0, 1.0) * smoothstep(0.35, 0.95, roughness + volumeThickness * 0.2));
2592
- float texturedFallbackEnvironmentTransmissionEnergy = mix(1.0, clamp(u_transmissionFallbackEnergy, 0.0, 1.0), texturedTransmissionAmount);
2593
- vec3 texturedRefractionRadiance = (texturedRefractedEnvironment + vec3(texturedCausticEnergy)) * texturedVolumeTint * u_environmentMapTextureIntensity * texturedTransmissionAmount * texturedFallbackEnvironmentTransmissionEnergy * mix(0.9, 0.55, roughness) * texturedRoughVolumeIorLift * texturedSampledEnvironmentWeight * clamp(u_materialEnvironmentSpecularScale, 0.0, 1.0);
2594
- #ifndef A3D_PBR_DISABLE_TRANSMISSION_BACKDROP
2595
- if (texturedBackdropWeight > 0.0001) {
2596
- vec2 backdropUv = gl_FragCoord.xy / max(u_transmissionBackdropResolution, vec2(1.0));
2597
- vec2 backdropOffset = normalize(texturedRefractionDirection.xy + vec2(0.0001)) * u_transmissionBackdropRefractionScale * mix(1.25, 0.55, roughness) * clamp(u_ior - 1.0, 0.0, 2.5);
2598
- float backdropLod = clamp((roughness + volumeThickness * 0.08) * max(u_transmissionBackdropMipCount - 1.0, 0.0), 0.0, max(u_transmissionBackdropMipCount - 1.0, 0.0));
2599
- vec3 backdropRadiance = a3dTexturedPbrDecodeSrgb(textureLod(u_transmissionBackdropTexture, clamp(backdropUv + backdropOffset, vec2(0.001), vec2(0.999)), backdropLod).rgb);
2600
- texturedRefractionRadiance = mix(texturedRefractionRadiance, backdropRadiance * texturedTransmissionAmount * mix(1.0, 1.22, texturedBackdropWeight), texturedBackdropWeight);
2601
- }
2602
- #endif
2603
- shaded = mix(shaded, shaded * 0.72 + texturedRefractionRadiance, texturedTransmissionAmount * mix(0.08, 0.58, texturedFallbackEnvironmentTransmissionEnergy));
2604
- }
2605
- int count = min(int(u_lightCount), 16);
2606
- for (int i = 0; i < count; ++i) {
2607
- int baseIndex = i * 4;
2608
- vec4 colorIntensity = u_lightData[baseIndex];
2609
- vec4 positionRange = u_lightData[baseIndex + 1];
2610
- vec4 directionKind = u_lightData[baseIndex + 2];
2611
- vec4 spotShadowLayer = u_lightData[baseIndex + 3];
2612
- float kind = directionKind.w;
2613
- vec3 lightDirection = -directionKind.xyz;
2614
- float attenuation = 1.0;
2615
- if (kind > 0.5) {
2616
- vec3 toLight = positionRange.xyz - v_worldPosition;
2617
- float distanceToLight = length(toLight);
2618
- lightDirection = distanceToLight > 0.0001 ? toLight / distanceToLight : -directionKind.xyz;
2619
- float range = max(positionRange.w, 0.0001);
2620
- float rangeFalloff = clamp(1.0 - pow(distanceToLight / range, 4.0), 0.0, 1.0);
2621
- rangeFalloff *= rangeFalloff;
2622
- attenuation = rangeFalloff / max(distanceToLight * distanceToLight, 1.0);
2623
- }
2624
- if (kind > 1.5) {
2625
- vec3 lightToFragment = normalize(v_worldPosition - positionRange.xyz);
2626
- float cone = dot(normalize(directionKind.xyz), lightToFragment);
2627
- float outer = cos(spotShadowLayer.x);
2628
- float inner = cos(spotShadowLayer.x * max(1.0 - spotShadowLayer.y, 0.001));
2629
- attenuation *= smoothstep(outer, inner, cone);
2630
- }
2631
- float shadowFactor = mix(1.0, kind > 0.5 && kind < 1.5 ? a3dTexturedPbrPointShadowFactor(v_worldPosition, mappedNormal, lightDirection) : a3dTexturedPbrShadowFactor(v_worldPosition, mappedNormal, lightDirection), step(0.5, spotShadowLayer.z));
2632
- float directLightIntensity = colorIntensity.a
2633
- * attenuation
2634
- * shadowFactor
2635
- * directTextureOcclusion
2636
- * mix(1.0, sourcePaintDirectDetail, sourcePaintDetailGate * 0.48);
2637
- float directExtensionNdotV = clamp(dot(clearcoatNormalDirection, viewDirection), 0.0, 1.0);
2638
- float directExtensionGrazingGate = mix(0.18, 1.0, smoothstep(0.12, 0.58, directExtensionNdotV));
2639
- float directExtensionSpecularScale = sqrt(clamp(u_materialEnvironmentSpecularScale, 0.0, 1.0)) * directExtensionGrazingGate;
2640
- shaded += a3dPbrDirectLight(mappedNormal, viewDirection, lightDirection, colorIntensity.rgb, directLightIntensity, base, metallic, roughness, specular, specularColor);
2641
- shaded += a3dTexturedPbrExtensionDirectLight(
2642
- clearcoatNormalDirection,
2643
- viewDirection,
2644
- lightDirection,
2645
- colorIntensity.rgb,
2646
- directLightIntensity,
2647
- clearcoat,
2648
- clearcoatRoughness,
2649
- sheenColor,
2650
- sheenRoughness,
2651
- anisotropy,
2652
- anisotropyRotation,
2653
- iridescence,
2654
- u_iridescenceIor,
2655
- iridescenceThickness,
2656
- iridescenceThickness
2657
- ) * directExtensionSpecularScale;
2658
- }
2659
- float alpha = texturedBase.a;
2660
- if (alpha < u_alphaCutoff) discard;
2661
- vec3 fogged = a3dApplyEnvironmentFog(max(shaded, vec3(0.0)), v_worldPosition);
2662
- outColor = vec4(a3dTexturedPbrEncodeOutput(fogged), alpha);
2663
- }
2664
- `
2665
- });
2666
- library.register({
2667
- name: DEFAULT_ENVIRONMENT_BACKGROUND_SHADER_NAME,
2668
- marker: DEFAULT_ENVIRONMENT_BACKGROUND_SHADER_MARKER,
2669
- vertex: `#version 300 es
2670
- // ${DEFAULT_ENVIRONMENT_BACKGROUND_SHADER_MARKER}
2671
- precision highp float;
2672
- layout(location = 0) in vec3 a_position;
2673
- out vec2 v_backgroundNdc;
2674
- void main() {
2675
- v_backgroundNdc = a_position.xy;
2676
- gl_Position = vec4(a_position.xy, 1.0, 1.0);
2677
- }
2678
- `,
2679
- fragment: `#version 300 es
2680
- // ${DEFAULT_ENVIRONMENT_BACKGROUND_SHADER_MARKER}
2681
- precision highp float;
2682
- uniform sampler2D u_environmentBackgroundTexture;
2683
- uniform samplerCube u_environmentBackgroundCubeTexture;
2684
- uniform float u_environmentBackgroundProjection;
2685
- uniform float u_environmentBackgroundRotation;
2686
- uniform float u_environmentBackgroundIntensity;
2687
- uniform float u_environmentBackgroundEncoding;
2688
- uniform float u_outputColorSpace;
2689
- uniform mat4 u_environmentBackgroundInverseViewProjection;
2690
- in vec2 v_backgroundNdc;
2691
- out vec4 outColor;
2692
- vec2 a3dBackgroundEquirectUv(vec3 direction) {
2693
- vec3 d = normalize(direction);
2694
- float u = atan(d.z, d.x) / 6.28318530718 + 0.5;
2695
- float v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
2696
- return vec2(fract(u), clamp(v, 0.0, 1.0));
2697
- }
2698
- vec3 a3dRotateBackgroundDirection(vec3 direction, float rotation) {
2699
- float angle = rotation * 6.28318530718;
2700
- float c = cos(angle);
2701
- float s = sin(angle);
2702
- vec3 d = normalize(direction);
2703
- return normalize(vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z));
2704
- }
2705
- vec3 a3dBackgroundDirectionFromNdc(vec2 ndc) {
2706
- vec4 nearPoint = u_environmentBackgroundInverseViewProjection * vec4(ndc, -1.0, 1.0);
2707
- vec4 farPoint = u_environmentBackgroundInverseViewProjection * vec4(ndc, 1.0, 1.0);
2708
- vec3 nearWorld = nearPoint.xyz / max(nearPoint.w, 0.00001);
2709
- vec3 farWorld = farPoint.xyz / max(farPoint.w, 0.00001);
2710
- return normalize(farWorld - nearWorld);
2711
- }
2712
- vec3 a3dBackgroundDecodeRgbe(vec4 encodedSample) {
2713
- float exponent = encodedSample.a * 255.0;
2714
- float scale = exponent <= 0.0 ? 0.0 : exp2(exponent - 128.0) * (255.0 / 256.0);
2715
- return max(encodedSample.rgb * scale, vec3(0.0));
2716
- }
2717
- vec3 a3dBackgroundDecode(vec4 encodedSample) {
2718
- if (u_environmentBackgroundEncoding > 1.5) return a3dBackgroundDecodeRgbe(encodedSample);
2719
- return max(encodedSample.rgb, vec3(0.0));
2720
- }
2721
- vec3 a3dBackgroundEncodeOutput(vec3 linearColor) {
2722
- vec3 color = max(linearColor, vec3(0.0));
2723
- vec3 filmic = clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), vec3(0.0), vec3(1.0));
2724
- vec3 srgb = pow(filmic, vec3(1.0 / 2.2));
2725
- return mix(color, srgb, step(0.5, u_outputColorSpace));
2726
- }
2727
- void main() {
2728
- vec3 direction = a3dRotateBackgroundDirection(a3dBackgroundDirectionFromNdc(v_backgroundNdc), u_environmentBackgroundRotation);
2729
- vec4 encodedSample = vec4(0.0, 0.0, 0.0, 1.0);
2730
- if (u_environmentBackgroundProjection > 1.5) {
2731
- encodedSample = texture(u_environmentBackgroundCubeTexture, direction);
2732
- } else {
2733
- encodedSample = texture(u_environmentBackgroundTexture, a3dBackgroundEquirectUv(direction));
2734
- }
2735
- vec3 color = a3dBackgroundDecode(encodedSample) * max(u_environmentBackgroundIntensity, 0.0);
2736
- outColor = vec4(a3dBackgroundEncodeOutput(color), 1.0);
2737
- }
2738
- `
2739
- });
2740
- library.register({
2741
- name: DEFAULT_DEPTH_SHADER_NAME,
2742
- marker: DEFAULT_DEPTH_SHADER_MARKER,
2743
- vertex: `#version 300 es
2744
- // ${DEFAULT_DEPTH_SHADER_MARKER}
2745
- precision highp float;
2746
- layout(location = 0) in vec3 a_position;
2747
- uniform mat4 u_modelViewProjection;
2748
- void main() {
2749
- gl_Position = u_modelViewProjection * vec4(a_position, 1.0);
2750
- }
2751
- `,
2752
- fragment: `#version 300 es
2753
- // ${DEFAULT_DEPTH_SHADER_MARKER}
2754
- precision highp float;
2755
- out vec4 outColor;
2756
- void main() {
2757
- outColor = vec4(vec3(gl_FragCoord.z), 1.0);
2758
- }
2759
- `
2760
- });
2761
- // Register the animation/cel toon program (banded N·L ramp + Fresnel rim).
2762
- // This is a real GLSL program compiled through the same library path as the
2763
- // built-in shaders; AnimationToonMaterial targets it via ANIMATION_TOON_SHADER_NAME.
2764
- registerAnimationToonShader(library);
2765
- return library;
2766
- }