@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
121 lines (117 loc) • 3.67 kB
JavaScript
import { f as createMappedBuffer, B as BU, F as F32, j as createUniformBuffer, U as U16, k as SCENE_UBO_WGSL } from './index-DMbDahsc.esm.js';
import { c as createCubemapSkyboxMaterial } from './cubemap-skybox-material-Bh9lLQtL.esm.js';
import { s as skyboxVertSrc } from './skybox.vertex-Ban7q6IM-Ban7q6IM.esm.js';
const skyboxHdrFragSrc = "struct g{world:mat4x4<f32>,primaryColor:vec3<f32>,_pad:f32,skyOutputColor:vec3<f32>,_pad2:f32,exposureLinear:f32,contrast:f32,_pad3:f32,_pad4:f32}@group(1) @binding(0) var<uniform> mesh:g;@group(1) @binding(1) var b:texture_cube<f32>;@group(1) @binding(2) var c:sampler;struct h{@location(0) positionUVW:vec3<f32>,@location(1) positionW:vec3<f32>}@fragment fn main(d:h)->@location(0) vec4<f32>{var e=normalize(d.positionUVW);var a=textureSampleLevel(b,c,e,0.0).rgb;a*=mesh.exposureLinear;a=pow(a,vec3<f32>(1.0/2.2));a=clamp(a,vec3<f32>(0.0),vec3<f32>(1.0));let f=a*a*(3.0-2.0*a);if (mesh.contrast<1.0){a=mix(vec3<f32>(0.5),a,mesh.contrast);} else{a=mix(a,f,mesh.contrast-1.0);}a=max(a,vec3<f32>(0.0));return vec4<f32>(a,1.0);}";
const SKY_HDR_UNIFORM_SIZE = 112;
function createSkyboxBuffers(engine, S) {
const positions = new F32([
-S,
-S,
-S,
S,
-S,
-S,
-S,
S,
-S,
S,
S,
-S,
-S,
-S,
S,
S,
-S,
S,
-S,
S,
S,
S,
S,
S
]);
const indices = new U16([
6,
4,
5,
7,
6,
5,
0,
2,
3,
1,
0,
3,
5,
1,
3,
7,
5,
3,
0,
4,
6,
2,
0,
6,
3,
2,
6,
7,
3,
6,
0,
1,
5,
4,
0,
5
]);
return [createMappedBuffer(engine, positions, BU.VERTEX), createMappedBuffer(engine, indices, BU.INDEX)];
}
async function buildHdrSkyboxRenderable(scene, envTextures, skyHalfSize, rootPosition, primaryColor) {
const engine = scene.surface.engine;
const [positionBuffer, indexBuffer] = createSkyboxBuffers(engine, skyHalfSize);
const skyboxFragment = await scene._environmentSkyboxShaderComposer?.(skyboxHdrFragSrc, "hdr") ?? skyboxHdrFragSrc;
const mat = createCubemapSkyboxMaterial("skybox-hdr", SCENE_UBO_WGSL + skyboxVertSrc, skyboxFragment);
const ubo = createSkyHdrMeshUBO(engine, rootPosition, primaryColor, scene.clearColor, scene.imageProcessing.exposure, scene.imageProcessing.contrast);
const bindGroup = mat.createBindGroup(engine, ubo, envTextures.specularCubeView, envTextures.cubeSampler);
const r = {
order: 0,
isTransparent: false,
bind(eng, sig) {
return {
renderable: r,
pipeline: mat.getPipeline(eng, sig),
draw(pass) {
pass.setBindGroup(1, bindGroup);
pass.setVertexBuffer(0, positionBuffer);
pass.setIndexBuffer(indexBuffer, "uint16");
pass.drawIndexed(36);
return 1;
}
};
}
};
r._rebuild = () => buildHdrSkyboxRenderable(scene, envTextures, skyHalfSize, rootPosition, primaryColor);
return r;
}
function createSkyHdrMeshUBO(engine, rootPosition, primaryColor, skyOutputColor, exposure, contrast) {
const data = new F32(SKY_HDR_UNIFORM_SIZE / 4);
data[0] = data[5] = data[10] = data[15] = 1;
data[12] = rootPosition[0];
data[13] = rootPosition[1];
data[14] = rootPosition[2];
data[16] = primaryColor[0];
data[17] = primaryColor[1];
data[18] = primaryColor[2];
data[20] = skyOutputColor.r;
data[21] = skyOutputColor.g;
data[22] = skyOutputColor.b;
data[24] = exposure;
data[25] = contrast;
return createUniformBuffer(engine, data);
}
export { buildHdrSkyboxRenderable };
//# sourceMappingURL=background-hdr-skybox-FpJ-ApA1.esm.js.map