1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import { NodeMaterialBlock } from '../../nodeMaterialBlock';
- import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
- import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
- import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
- import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
- import { _TypeStore } from '../../../../Misc/typeStore';
- import { Effect } from '../../../effect';
- import { NodeMaterial } from '../../nodeMaterial';
- import { Mesh } from '../../../../Meshes/mesh';
- import { Scene } from '../../../../scene';
- /**
- * Block used to get the screen sizes
- */
- export class ScreenSizeBlock extends NodeMaterialBlock {
- private _varName: string;
- private _scene: Scene;
- /**
- * Creates a new ScreenSizeBlock
- * @param name defines the block name
- */
- public constructor(name: string) {
- super(name, NodeMaterialBlockTargets.Fragment);
- this.registerOutput("xy", NodeMaterialBlockConnectionPointTypes.Vector2, NodeMaterialBlockTargets.Fragment);
- this.registerOutput("x", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
- this.registerOutput("y", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
- }
- /**
- * Gets the current class name
- * @returns the class name
- */
- public getClassName() {
- return "ScreenSizeBlock";
- }
- /**
- * Gets the xy component
- */
- public get xy(): NodeMaterialConnectionPoint {
- return this._outputs[0];
- }
- /**
- * Gets the x component
- */
- public get x(): NodeMaterialConnectionPoint {
- return this._outputs[1];
- }
- /**
- * Gets the y component
- */
- public get y(): NodeMaterialConnectionPoint {
- return this._outputs[2];
- }
- public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {
- const engine = this._scene.getEngine();
- effect.setFloat2(this._varName, engine.getRenderWidth(), engine.getRenderWidth());
- }
- protected writeOutputs(state: NodeMaterialBuildState, varName: string): string {
- let code = "";
- for (var output of this._outputs) {
- if (output.hasEndpoints) {
- code += `${this._declareOutput(output, state)} = ${varName}.${output.name};\r\n`;
- }
- }
- return code;
- }
- protected _buildBlock(state: NodeMaterialBuildState) {
- super._buildBlock(state);
- this._scene = state.sharedData.scene;
- if (state.target === NodeMaterialBlockTargets.Vertex) {
- throw "ScreenSizeBlock must only be used in a fragment shader";
- }
- state.sharedData.bindableBlocks.push(this);
- this._varName = state._getFreeVariableName("screenSize");
- state._emitUniformFromString(this._varName, "vec2");
- state.compilationString += this.writeOutputs(state, this._varName);
- return this;
- }
- }
- _TypeStore.RegisteredTypes["BABYLON.ScreenSizeBlock"] = ScreenSizeBlock;
|