UNPKG

@babylonjs/core

Version:

Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.

176 lines (175 loc) 7.65 kB
import { __decorate } from "../tslib.es6.js"; import { Vector3 } from "../Maths/math.vector.js"; import { Node } from "../node.js"; import { Light } from "./light.js"; import { RegisterClass } from "../Misc/typeStore.js"; import { serialize } from "../Misc/decorators.js"; import { AreaLight } from "./areaLight.js"; Node.AddNodeConstructor("Light_Type_4", (name, scene) => { return () => new RectAreaLight(name, Vector3.Zero(), 1, 1, scene); }); /** * A rectangular area light defined by an unique point in world space, a width and a height. * The light is emitted from the rectangular area in the -Z direction. */ export class RectAreaLight extends AreaLight { /** * Gets Rect Area Light emission texture. (Note: This texture needs pre-processing! Use AreaLightTextureTools to pre-process the texture). */ get emissionTexture() { return this._emissionTextureTexture; } /** * Sets Rect Area Light emission texture. (Note: This texture needs pre-processing! Use AreaLightTextureTools to pre-process the texture). */ set emissionTexture(value) { if (this._emissionTextureTexture === value) { return; } this._emissionTextureTexture = value; if (this._emissionTextureTexture) { this._emissionTextureTexture.wrapU = 0; this._emissionTextureTexture.wrapV = 0; } if (this._emissionTextureTexture && RectAreaLight._IsTexture(this._emissionTextureTexture)) { this._emissionTextureTexture.onLoadObservable.addOnce(() => { this._markMeshesAsLightDirty(); }); } } /** * Rect Area Light width. */ get width() { return this._width.x; } /** * Rect Area Light width. */ set width(value) { this._width.x = value; } /** * Rect Area Light height. */ get height() { return this._height.y; } /** * Rect Area Light height. */ set height(value) { this._height.y = value; } /** * Creates a rectangular area light object. * Documentation : https://doc.babylonjs.com/features/featuresDeepDive/lights/lights_introduction * @param name The friendly name of the light * @param position The position of the area light. * @param width The width of the area light. * @param height The height of the area light. * @param scene The scene the light belongs to * @param dontAddToScene True to not add the light to the scene */ constructor(name, position, width, height, scene, dontAddToScene) { super(name, position, scene, dontAddToScene); this._emissionTextureTexture = null; this._width = new Vector3(width, 0, 0); this._height = new Vector3(0, height, 0); this._pointTransformedPosition = Vector3.Zero(); this._pointTransformedWidth = Vector3.Zero(); this._pointTransformedHeight = Vector3.Zero(); } /** * Returns the string "RectAreaLight" * @returns the class name */ getClassName() { return "RectAreaLight"; } /** * Returns the integer 4. * @returns The light Type id as a constant defines in Light.LIGHTTYPEID_x */ // eslint-disable-next-line @typescript-eslint/naming-convention getTypeID() { return Light.LIGHTTYPEID_RECT_AREALIGHT; } _buildUniformLayout() { this._uniformBuffer.addUniform("vLightData", 4); this._uniformBuffer.addUniform("vLightDiffuse", 4); this._uniformBuffer.addUniform("vLightSpecular", 4); this._uniformBuffer.addUniform("vLightWidth", 4); this._uniformBuffer.addUniform("vLightHeight", 4); this._uniformBuffer.addUniform("shadowsInfo", 3); this._uniformBuffer.addUniform("depthValues", 2); this._uniformBuffer.create(); } _computeTransformedInformation() { if (this.parent && this.parent.getWorldMatrix) { Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this._pointTransformedPosition); Vector3.TransformNormalToRef(this._width, this.parent.getWorldMatrix(), this._pointTransformedWidth); Vector3.TransformNormalToRef(this._height, this.parent.getWorldMatrix(), this._pointTransformedHeight); return true; } return false; } static _IsTexture(texture) { return texture.onLoadObservable !== undefined; } /** * Sets the passed Effect "effect" with the PointLight transformed position (or position, if none) and passed name (string). * @param effect The effect to update * @param lightIndex The index of the light in the effect to update * @returns The point light */ transferToEffect(effect, lightIndex) { const offset = this._scene.floatingOriginOffset; if (this._computeTransformedInformation()) { this._uniformBuffer.updateFloat4("vLightData", this._pointTransformedPosition.x - offset.x, this._pointTransformedPosition.y - offset.y, this._pointTransformedPosition.z - offset.z, 0, lightIndex); this._uniformBuffer.updateFloat4("vLightWidth", this._pointTransformedWidth.x / 2, this._pointTransformedWidth.y / 2, this._pointTransformedWidth.z / 2, 0, lightIndex); this._uniformBuffer.updateFloat4("vLightHeight", this._pointTransformedHeight.x / 2, this._pointTransformedHeight.y / 2, this._pointTransformedHeight.z / 2, 0, lightIndex); } else { this._uniformBuffer.updateFloat4("vLightData", this.position.x - offset.x, this.position.y - offset.y, this.position.z - offset.z, 0, lightIndex); this._uniformBuffer.updateFloat4("vLightWidth", this._width.x / 2, this._width.y / 2, this._width.z / 2, 0.0, lightIndex); this._uniformBuffer.updateFloat4("vLightHeight", this._height.x / 2, this._height.y / 2, this._height.z / 2, 0.0, lightIndex); } return this; } transferTexturesToEffect(effect, lightIndex) { super.transferTexturesToEffect(effect, lightIndex); if (this._emissionTextureTexture && this._emissionTextureTexture.isReady()) { effect.setTexture("rectAreaLightEmissionTexture" + lightIndex, this._emissionTextureTexture); } return this; } transferToNodeMaterialEffect(effect, lightDataUniformName) { const offset = this._scene.floatingOriginOffset; if (this._computeTransformedInformation()) { effect.setFloat3(lightDataUniformName, this._pointTransformedPosition.x - offset.x, this._pointTransformedPosition.y - offset.y, this._pointTransformedPosition.z - offset.z); } else { effect.setFloat3(lightDataUniformName, this.position.x - offset.x, this.position.y - offset.y, this.position.z - offset.z); } return this; } /** * Prepares the list of defines specific to the light type. * @param defines the list of defines * @param lightIndex defines the index of the light for the effect */ prepareLightSpecificDefines(defines, lightIndex) { super.prepareLightSpecificDefines(defines, lightIndex); defines["RECTAREALIGHTEMISSIONTEXTURE" + lightIndex] = this._emissionTextureTexture && this._emissionTextureTexture.isReady() ? true : false; } } __decorate([ serialize() ], RectAreaLight.prototype, "width", null); __decorate([ serialize() ], RectAreaLight.prototype, "height", null); // Register Class Name RegisterClass("BABYLON.RectAreaLight", RectAreaLight); //# sourceMappingURL=rectAreaLight.js.map