123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- import { NodeMaterialBlock } from '../../nodeMaterialBlock';
- import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
- import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
- import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
- import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
- import { AbstractMesh } from '../../../../Meshes/abstractMesh';
- import { NodeMaterial, NodeMaterialDefines } from '../../nodeMaterial';
- import { InputBlock } from '../Input/inputBlock';
- import { Effect } from '../../../effect';
- import { Mesh } from '../../../../Meshes/mesh';
- import { Nullable } from '../../../../types';
- import { _TypeStore } from '../../../../Misc/typeStore';
- import { Texture } from '../../../Textures/texture';
- import { Scene } from '../../../../scene';
- import "../../../../Shaders/ShadersInclude/helperFunctions";
- /**
- * Block used to read a texture from a sampler
- */
- export class TextureBlock extends NodeMaterialBlock {
- private _defineName: string;
- private _linearDefineName: string;
- private _samplerName: string;
- private _transformedUVName: string;
- private _textureTransformName: string;
- private _textureInfoName: string;
- private _mainUVName: string;
- private _mainUVDefineName: string;
- /**
- * Gets or sets the texture associated with the node
- */
- public texture: Nullable<Texture>;
- /**
- * Create a new TextureBlock
- * @param name defines the block name
- */
- public constructor(name: string) {
- super(name, NodeMaterialBlockTargets.VertexAndFragment);
- this.registerInput("uv", NodeMaterialBlockConnectionPointTypes.Vector2, false, NodeMaterialBlockTargets.VertexAndFragment);
- this.registerOutput("rgba", NodeMaterialBlockConnectionPointTypes.Color4, NodeMaterialBlockTargets.Neutral);
- this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Neutral);
- this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
- this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
- this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
- this.registerOutput("a", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
- this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector3);
- this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector4);
- this._inputs[0]._prioritizeVertex = true;
- }
- /**
- * Gets the current class name
- * @returns the class name
- */
- public getClassName() {
- return "TextureBlock";
- }
- /**
- * Gets the uv input component
- */
- public get uv(): NodeMaterialConnectionPoint {
- return this._inputs[0];
- }
- /**
- * Gets the rgba output component
- */
- public get rgba(): NodeMaterialConnectionPoint {
- return this._outputs[0];
- }
- /**
- * Gets the rgb output component
- */
- public get rgb(): NodeMaterialConnectionPoint {
- return this._outputs[1];
- }
- /**
- * Gets the r output component
- */
- public get r(): NodeMaterialConnectionPoint {
- return this._outputs[2];
- }
- /**
- * Gets the g output component
- */
- public get g(): NodeMaterialConnectionPoint {
- return this._outputs[3];
- }
- /**
- * Gets the b output component
- */
- public get b(): NodeMaterialConnectionPoint {
- return this._outputs[4];
- }
- /**
- * Gets the a output component
- */
- public get a(): NodeMaterialConnectionPoint {
- return this._outputs[5];
- }
- public get target() {
- // TextureBlock has a special optimizations for uvs that come from the vertex shaders as they can be packed into a single varyings.
- // But we need to detect uvs coming from fragment then
- if (!this.uv.isConnected) {
- return NodeMaterialBlockTargets.VertexAndFragment;
- }
- if (this.uv.sourceBlock!.isInput) {
- return NodeMaterialBlockTargets.VertexAndFragment;
- }
- let parent = this.uv.connectedPoint;
- while (parent) {
- if (parent.target === NodeMaterialBlockTargets.Fragment) {
- return NodeMaterialBlockTargets.Fragment;
- }
- if (parent.target === NodeMaterialBlockTargets.Vertex) {
- return NodeMaterialBlockTargets.VertexAndFragment;
- }
- if (parent.target === NodeMaterialBlockTargets.Neutral || parent.target === NodeMaterialBlockTargets.VertexAndFragment) {
- let parentBlock = parent.ownerBlock;
- parent = null;
- for (var input of parentBlock.inputs) {
- if (input.connectedPoint) {
- parent = input.connectedPoint;
- break;
- }
- }
- }
- }
- return NodeMaterialBlockTargets.VertexAndFragment;
- }
- public autoConfigure(material: NodeMaterial) {
- if (!this.uv.isConnected) {
- let uvInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "uv");
- if (!uvInput) {
- uvInput = new InputBlock("uv");
- uvInput.setAsAttribute();
- }
- uvInput.output.connectTo(this.uv);
- }
- }
- public initializeDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, useInstances: boolean = false) {
- if (!defines._areTexturesDirty) {
- return;
- }
- defines.setValue(this._mainUVDefineName, false);
- }
- public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
- if (!defines._areTexturesDirty) {
- return;
- }
- if (!this.texture || !this.texture.getTextureMatrix) {
- defines.setValue(this._defineName, false);
- defines.setValue(this._mainUVDefineName, true);
- return;
- }
- defines.setValue(this._linearDefineName, !this.texture.gammaSpace);
- if (this._isMixed) {
- if (!this.texture.getTextureMatrix().isIdentityAs3x2()) {
- defines.setValue(this._defineName, true);
- } else {
- defines.setValue(this._defineName, false);
- defines.setValue(this._mainUVDefineName, true);
- }
- }
- }
- public isReady() {
- if (this.texture && !this.texture.isReadyOrNotBlocking()) {
- return false;
- }
- return true;
- }
- public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {
- if (!mesh || !this.texture) {
- return;
- }
- if (this._isMixed) {
- effect.setFloat(this._textureInfoName, this.texture.level);
- effect.setMatrix(this._textureTransformName, this.texture.getTextureMatrix());
- }
- effect.setTexture(this._samplerName, this.texture);
- }
- private get _isMixed() {
- return this.target !== NodeMaterialBlockTargets.Fragment;
- }
- private _injectVertexCode(state: NodeMaterialBuildState) {
- let uvInput = this.uv;
- // Inject code in vertex
- this._defineName = state._getFreeDefineName("UVTRANSFORM");
- this._mainUVDefineName = "VMAIN" + uvInput.associatedVariableName.toUpperCase();
- if (uvInput.connectedPoint!.ownerBlock.isInput) {
- let uvInputOwnerBlock = uvInput.connectedPoint!.ownerBlock as InputBlock;
- if (!uvInputOwnerBlock.isAttribute) {
- state._emitUniformFromString(uvInput.associatedVariableName, "vec2");
- }
- }
- this._mainUVName = "vMain" + uvInput.associatedVariableName;
- this._transformedUVName = state._getFreeVariableName("transformedUV");
- this._textureTransformName = state._getFreeVariableName("textureTransform");
- this._textureInfoName = state._getFreeVariableName("textureInfoName");
- state._emitVaryingFromString(this._transformedUVName, "vec2", this._defineName);
- state._emitVaryingFromString(this._mainUVName, "vec2", this._mainUVDefineName);
- state._emitUniformFromString(this._textureTransformName, "mat4", this._defineName);
- state.compilationString += `#ifdef ${this._defineName}\r\n`;
- state.compilationString += `${this._transformedUVName} = vec2(${this._textureTransformName} * vec4(${uvInput.associatedVariableName}.xy, 1.0, 0.0));\r\n`;
- state.compilationString += `#endif\r\n`;
- state.compilationString += `#ifdef ${this._mainUVDefineName}\r\n`;
- state.compilationString += `${this._mainUVName} = ${uvInput.associatedVariableName}.xy;\r\n`;
- state.compilationString += `#endif\r\n`;
- if (!this._outputs.some((o) => o.isConnectedInVertexShader)) {
- return;
- }
- for (var output of this._outputs) {
- if (output.hasEndpoints) {
- this._writeOutput(state, output, output.name, true);
- }
- }
- }
- private _writeOutput(state: NodeMaterialBuildState, output: NodeMaterialConnectionPoint, swizzle: string, vertexMode = false) {
- let uvInput = this.uv;
- if (vertexMode) {
- if (state.target === NodeMaterialBlockTargets.Fragment) {
- return;
- }
- state.compilationString += `${this._declareOutput(output, state)} = texture2D(${this._samplerName}, ${uvInput.associatedVariableName}).${swizzle};\r\n`;
- return;
- }
- if (this.uv.ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
- state.compilationString += `${this._declareOutput(output, state)} = texture2D(${this._samplerName}, ${uvInput.associatedVariableName}).${swizzle};\r\n`;
- return;
- }
- const complement = ` * ${this._textureInfoName}`;
- state.compilationString += `#ifdef ${this._defineName}\r\n`;
- state.compilationString += `${this._declareOutput(output, state)} = texture2D(${this._samplerName}, ${this._transformedUVName}).${swizzle}${complement};\r\n`;
- state.compilationString += `#endif\r\n`;
- state.compilationString += `#ifdef ${this._mainUVDefineName}\r\n`;
- state.compilationString += `${this._declareOutput(output, state)} = texture2D(${this._samplerName}, ${this._mainUVName}).${swizzle}${complement};\r\n`;
- state.compilationString += `#endif\r\n`;
- state.compilationString += `#ifdef ${this._linearDefineName}\r\n`;
- state.compilationString += `${output.associatedVariableName} = toGammaSpace(${output.associatedVariableName});\r\n`;
- state.compilationString += `#endif\r\n`;
- }
- protected _buildBlock(state: NodeMaterialBuildState) {
- super._buildBlock(state);
- if (!this._isMixed && state.target === NodeMaterialBlockTargets.Fragment || this._isMixed && state.target === NodeMaterialBlockTargets.Vertex) {
- this._samplerName = state._getFreeVariableName(this.name + "Sampler");
- state._emit2DSampler(this._samplerName);
- // Declarations
- state.sharedData.blockingBlocks.push(this);
- state.sharedData.textureBlocks.push(this);
- state.sharedData.blocksWithDefines.push(this);
- state.sharedData.bindableBlocks.push(this);
- }
- if (state.target !== NodeMaterialBlockTargets.Fragment) {
- // Vertex
- this._injectVertexCode(state);
- return;
- }
- // Fragment
- if (!this._outputs.some((o) => o.isConnectedInFragmentShader)) {
- return;
- }
- if (this._isMixed) {
- // Reexport the sampler
- state._emit2DSampler(this._samplerName);
- }
- this._linearDefineName = state._getFreeDefineName("ISLINEAR");
- let comments = `//${this.name}`;
- state._emitFunctionFromInclude("helperFunctions", comments);
- if (this._isMixed) {
- state._emitUniformFromString(this._textureInfoName, "float");
- }
- for (var output of this._outputs) {
- if (output.hasEndpoints) {
- this._writeOutput(state, output, output.name);
- }
- }
- return this;
- }
- protected _dumpPropertiesCode() {
- if (!this.texture) {
- return "";
- }
- var codeString = `${this._codeVariableName}.texture = new BABYLON.Texture("${this.texture.name}");\r\n`;
- codeString += `${this._codeVariableName}.texture.wrapU = ${this.texture.wrapU};\r\n`;
- codeString += `${this._codeVariableName}.texture.wrapV = ${this.texture.wrapV};\r\n`;
- codeString += `${this._codeVariableName}.texture.uAng = ${this.texture.uAng};\r\n`;
- codeString += `${this._codeVariableName}.texture.vAng = ${this.texture.vAng};\r\n`;
- codeString += `${this._codeVariableName}.texture.wAng = ${this.texture.wAng};\r\n`;
- codeString += `${this._codeVariableName}.texture.uOffset = ${this.texture.uOffset};\r\n`;
- codeString += `${this._codeVariableName}.texture.vOffset = ${this.texture.vOffset};\r\n`;
- codeString += `${this._codeVariableName}.texture.uScale = ${this.texture.uScale};\r\n`;
- codeString += `${this._codeVariableName}.texture.vScale = ${this.texture.vScale};\r\n`;
- codeString += `${this._codeVariableName}.texture.gammaSpace = ${this.texture.gammaSpace};\r\n`;
- return codeString;
- }
- public serialize(): any {
- let serializationObject = super.serialize();
- if (this.texture) {
- serializationObject.texture = this.texture.serialize();
- }
- return serializationObject;
- }
- public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
- super._deserialize(serializationObject, scene, rootUrl);
- if (serializationObject.texture) {
- rootUrl = serializationObject.texture.url.indexOf("data:") === 0 ? "" : rootUrl;
- this.texture = Texture.Parse(serializationObject.texture, scene, rootUrl) as Texture;
- }
- }
- }
- _TypeStore.RegisteredTypes["BABYLON.TextureBlock"] = TextureBlock;
|