playcanvas
Version:
PlayCanvas WebGL game engine
91 lines (88 loc) • 3.82 kB
JavaScript
import { CULLFACE_FRONT } from '../../platform/graphics/constants.js';
import { ShaderProcessorOptions } from '../../platform/graphics/shader-processor-options.js';
import { LAYERID_SKYBOX } from '../constants.js';
import { ShaderMaterial } from '../materials/shader-material.js';
import { MeshInstance } from '../mesh-instance.js';
import { getProgramLibrary } from '../shader-lib/get-program-library.js';
import { skybox } from '../shader-lib/programs/skybox.js';
import { getMaterialShaderDefines } from '../shader-lib/utils.js';
import { SkyGeometry } from './sky-geometry.js';
/**
* @import { GraphNode } from '../graph-node.js'
* @import { GraphicsDevice } from '../../platform/graphics/graphics-device.js'
* @import { Scene } from '../scene.js'
* @import { Texture } from '../../platform/graphics/texture.js'
*/ /**
* A visual representation of the sky.
*/ class SkyMesh {
destroy() {
if (this.meshInstance) {
if (this.skyLayer) {
this.skyLayer.removeMeshInstances([
this.meshInstance
]);
}
this.meshInstance.destroy();
this.meshInstance = null;
}
}
/**
* @param {GraphicsDevice} device - The graphics device.
* @param {Scene} scene - The scene owning the sky.
* @param {GraphNode} node - The graph node of the sky mesh instance.
* @param {Texture} texture - The texture of the sky.
* @param {string} type - The type of the sky. One of the SKYMESH_* constants.
*/ constructor(device, scene, node, texture, type){
/**
* Mesh instance representing the visuals of the sky.
*
* @type {MeshInstance|null}
*/ this.meshInstance = null;
var material = new ShaderMaterial();
material.name = 'SkyMaterial';
material.getShaderVariant = function(params) {
var { scene: _$scene, cameraShaderParams } = params;
var options = {
defines: getMaterialShaderDefines(this, cameraShaderParams),
pass: params.pass,
encoding: texture.encoding,
gamma: cameraShaderParams.shaderOutputGamma,
toneMapping: cameraShaderParams.toneMapping,
skymesh: type
};
if (texture.cubemap) {
options.type = 'cubemap';
options.mip = _$scene.skyboxMip;
} else {
options.type = 'envAtlas';
}
var processingOptions = new ShaderProcessorOptions(params.viewUniformFormat, params.viewBindGroupFormat);
var library = getProgramLibrary(device);
library.register('skybox', skybox);
return library.getProgram('skybox', options, processingOptions);
};
material.setParameter('skyboxHighlightMultiplier', scene.skyboxHighlightMultiplier);
if (texture.cubemap) {
material.setParameter('texture_cubeMap', texture);
} else {
material.setParameter('texture_envAtlas', texture);
material.setParameter('mipLevel', scene.skyboxMip);
}
material.cull = CULLFACE_FRONT;
material.depthWrite = false;
var skyLayer = scene.layers.getLayerById(LAYERID_SKYBOX);
if (skyLayer) {
var mesh = SkyGeometry.create(device, type);
var meshInstance = new MeshInstance(mesh, material, node);
this.meshInstance = meshInstance;
meshInstance.cull = false;
// disable picker, the material has custom update shader and does not handle picker variant
meshInstance.pick = false;
skyLayer.addMeshInstances([
meshInstance
]);
this.skyLayer = skyLayer;
}
}
}
export { SkyMesh };