playcanvas
Version:
PlayCanvas WebGL game engine
135 lines (132 loc) • 4.18 kB
JavaScript
import { Vec3 } from '../../core/math/vec3.js';
import { SKYTYPE_INFINITE } from '../constants.js';
import { GraphNode } from '../graph-node.js';
import { SkyMesh } from './sky-mesh.js';
/**
* @import { Scene } from '../scene.js'
*/ /**
* Implementation of the sky.
*
* @category Graphics
*/ class Sky {
applySettings(render) {
var _render_skyType;
this.type = (_render_skyType = render.skyType) != null ? _render_skyType : SKYTYPE_INFINITE;
var _render_skyMeshPosition;
this.node.setLocalPosition(new Vec3((_render_skyMeshPosition = render.skyMeshPosition) != null ? _render_skyMeshPosition : [
0,
0,
0
]));
var _render_skyMeshRotation;
this.node.setLocalEulerAngles(new Vec3((_render_skyMeshRotation = render.skyMeshRotation) != null ? _render_skyMeshRotation : [
0,
0,
0
]));
var _render_skyMeshScale;
this.node.setLocalScale(new Vec3((_render_skyMeshScale = render.skyMeshScale) != null ? _render_skyMeshScale : [
1,
1,
1
]));
if (render.skyCenter) {
this._center = new Vec3(render.skyCenter);
}
}
/**
* The type of the sky. One of the SKYMESH_* constants. Defaults to {@link SKYTYPE_INFINITE}.
* Can be:
*
* - {@link SKYTYPE_INFINITE}
* - {@link SKYTYPE_BOX}
* - {@link SKYTYPE_DOME}
*
* @type {string}
*/ set type(value) {
if (this._type !== value) {
this._type = value;
this.scene.updateShaders = true;
this.updateSkyMesh();
}
}
get type() {
return this._type;
}
/**
* The center of the sky. Ignored for {@link SKYTYPE_INFINITE}. Typically only the y-coordinate
* is used, representing the tripod height. Defaults to (0, 1, 0).
*
* @type {Vec3}
*/ set center(value) {
this._center.copy(value);
}
get center() {
return this._center;
}
updateSkyMesh() {
var texture = this.scene._getSkyboxTex();
if (texture) {
this.resetSkyMesh();
this.skyMesh = new SkyMesh(this.device, this.scene, this.node, texture, this.type);
this.scene.fire('set:skybox', texture);
}
}
resetSkyMesh() {
var _this_skyMesh;
(_this_skyMesh = this.skyMesh) == null ? undefined : _this_skyMesh.destroy();
this.skyMesh = null;
}
update() {
// uniforms
if (this.type !== SKYTYPE_INFINITE) {
var { center, centerArray } = this;
// tripod position is relative to the node, transform it to the world space
var temp = new Vec3();
this.node.getWorldTransform().transformPoint(center, temp);
centerArray[0] = temp.x;
centerArray[1] = temp.y;
centerArray[2] = temp.z;
this.projectedSkydomeCenterId.setValue(centerArray);
}
}
/**
* Constructs a new sky.
*
* @param {Scene} scene - The scene owning the sky.
* @ignore
*/ constructor(scene){
/**
* The type of the sky. One of the SKYMESH_* constants.
*
* @type {string}
* @private
*/ this._type = SKYTYPE_INFINITE;
/**
* The center of the sky.
*
* @type {Vec3}
* @private
*/ this._center = new Vec3(0, 1, 0);
/**
* The sky mesh of the scene.
*
* @type {SkyMesh|null}
* @ignore
*/ this.skyMesh = null;
/**
* A graph node with a transform used to render the sky mesh. Adjust the position, rotation and
* scale of this node to orient the sky mesh. Ignored for {@link SKYTYPE_INFINITE}.
*
* @type {GraphNode}
* @readonly
*/ this.node = new GraphNode('SkyMeshNode');
this.device = scene.device;
this.scene = scene;
// defaults
this.center = new Vec3(0, 1, 0);
this.centerArray = new Float32Array(3);
this.projectedSkydomeCenterId = this.device.scope.resolve('projectedSkydomeCenter');
}
}
export { Sky };