currentScreenBlock.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  4. import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
  5. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  6. import { AbstractMesh } from '../../../../Meshes/abstractMesh';
  7. import { NodeMaterialDefines } from '../../nodeMaterial';
  8. import { BaseTexture } from '../../../Textures/baseTexture';
  9. import { Nullable } from '../../../../types';
  10. import { _TypeStore } from '../../../../Misc/typeStore';
  11. import { Texture } from '../../../Textures/texture';
  12. import { Scene } from '../../../../scene';
  13. import { InputBlock } from '../Input/inputBlock';
  14. declare type NodeMaterial = import("../../nodeMaterial").NodeMaterial;
  15. /**
  16. * Base block used as input for post process
  17. */
  18. export class CurrentScreenBlock extends NodeMaterialBlock {
  19. private _samplerName = "textureSampler";
  20. private _linearDefineName: string;
  21. private _gammaDefineName: string;
  22. private _mainUVName: string;
  23. private _tempTextureRead: string;
  24. /**
  25. * Gets or sets the texture associated with the node
  26. */
  27. public texture: Nullable<BaseTexture>;
  28. /**
  29. * Gets or sets a boolean indicating if content needs to be converted to gamma space
  30. */
  31. public convertToGammaSpace = false;
  32. /**
  33. * Gets or sets a boolean indicating if content needs to be converted to linear space
  34. */
  35. public convertToLinearSpace = false;
  36. /**
  37. * Create a new CurrentScreenBlock
  38. * @param name defines the block name
  39. */
  40. public constructor(name: string) {
  41. super(name, NodeMaterialBlockTargets.VertexAndFragment);
  42. this._isUnique = false;
  43. this.registerInput("uv", NodeMaterialBlockConnectionPointTypes.Vector2, false, NodeMaterialBlockTargets.VertexAndFragment);
  44. this.registerOutput("rgba", NodeMaterialBlockConnectionPointTypes.Color4, NodeMaterialBlockTargets.Neutral);
  45. this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Neutral);
  46. this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
  47. this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
  48. this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
  49. this.registerOutput("a", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
  50. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector3);
  51. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector4);
  52. this._inputs[0]._prioritizeVertex = false;
  53. }
  54. /**
  55. * Gets the current class name
  56. * @returns the class name
  57. */
  58. public getClassName() {
  59. return "CurrentScreenBlock";
  60. }
  61. /**
  62. * Gets the uv input component
  63. */
  64. public get uv(): NodeMaterialConnectionPoint {
  65. return this._inputs[0];
  66. }
  67. /**
  68. * Gets the rgba output component
  69. */
  70. public get rgba(): NodeMaterialConnectionPoint {
  71. return this._outputs[0];
  72. }
  73. /**
  74. * Gets the rgb output component
  75. */
  76. public get rgb(): NodeMaterialConnectionPoint {
  77. return this._outputs[1];
  78. }
  79. /**
  80. * Gets the r output component
  81. */
  82. public get r(): NodeMaterialConnectionPoint {
  83. return this._outputs[2];
  84. }
  85. /**
  86. * Gets the g output component
  87. */
  88. public get g(): NodeMaterialConnectionPoint {
  89. return this._outputs[3];
  90. }
  91. /**
  92. * Gets the b output component
  93. */
  94. public get b(): NodeMaterialConnectionPoint {
  95. return this._outputs[4];
  96. }
  97. /**
  98. * Gets the a output component
  99. */
  100. public get a(): NodeMaterialConnectionPoint {
  101. return this._outputs[5];
  102. }
  103. /**
  104. * Initialize the block and prepare the context for build
  105. * @param state defines the state that will be used for the build
  106. */
  107. public initialize(state: NodeMaterialBuildState) {
  108. state._excludeVariableName("textureSampler");
  109. }
  110. public get target() {
  111. if (!this.uv.isConnected) {
  112. return NodeMaterialBlockTargets.VertexAndFragment;
  113. }
  114. if (this.uv.sourceBlock!.isInput) {
  115. return NodeMaterialBlockTargets.VertexAndFragment;
  116. }
  117. return NodeMaterialBlockTargets.Fragment;
  118. }
  119. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  120. defines.setValue(this._linearDefineName, this.convertToGammaSpace, true);
  121. defines.setValue(this._gammaDefineName, this.convertToLinearSpace, true);
  122. }
  123. public isReady() {
  124. if (this.texture && !this.texture.isReadyOrNotBlocking()) {
  125. return false;
  126. }
  127. return true;
  128. }
  129. private _injectVertexCode(state: NodeMaterialBuildState) {
  130. let uvInput = this.uv;
  131. if (uvInput.connectedPoint!.ownerBlock.isInput) {
  132. let uvInputOwnerBlock = uvInput.connectedPoint!.ownerBlock as InputBlock;
  133. if (!uvInputOwnerBlock.isAttribute) {
  134. state._emitUniformFromString(uvInput.associatedVariableName, "vec2");
  135. }
  136. }
  137. this._mainUVName = "vMain" + uvInput.associatedVariableName;
  138. state._emitVaryingFromString(this._mainUVName, "vec2");
  139. state.compilationString += `${this._mainUVName} = ${uvInput.associatedVariableName}.xy;\r\n`;
  140. if (!this._outputs.some((o) => o.isConnectedInVertexShader)) {
  141. return;
  142. }
  143. this._writeTextureRead(state, true);
  144. for (var output of this._outputs) {
  145. if (output.hasEndpoints) {
  146. this._writeOutput(state, output, output.name, true);
  147. }
  148. }
  149. }
  150. private _writeTextureRead(state: NodeMaterialBuildState, vertexMode = false) {
  151. let uvInput = this.uv;
  152. if (vertexMode) {
  153. if (state.target === NodeMaterialBlockTargets.Fragment) {
  154. return;
  155. }
  156. state.compilationString += `vec4 ${this._tempTextureRead} = texture2D(${this._samplerName}, ${uvInput.associatedVariableName});\r\n`;
  157. return;
  158. }
  159. if (this.uv.ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  160. state.compilationString += `vec4 ${this._tempTextureRead} = texture2D(${this._samplerName}, ${uvInput.associatedVariableName});\r\n`;
  161. return;
  162. }
  163. state.compilationString += `vec4 ${this._tempTextureRead} = texture2D(${this._samplerName}, ${this._mainUVName});\r\n`;
  164. }
  165. private _writeOutput(state: NodeMaterialBuildState, output: NodeMaterialConnectionPoint, swizzle: string, vertexMode = false) {
  166. if (vertexMode) {
  167. if (state.target === NodeMaterialBlockTargets.Fragment) {
  168. return;
  169. }
  170. state.compilationString += `${this._declareOutput(output, state)} = ${this._tempTextureRead}.${swizzle};\r\n`;
  171. return;
  172. }
  173. if (this.uv.ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  174. state.compilationString += `${this._declareOutput(output, state)} = ${this._tempTextureRead}.${swizzle};\r\n`;
  175. return;
  176. }
  177. state.compilationString += `${this._declareOutput(output, state)} = ${this._tempTextureRead}.${swizzle};\r\n`;
  178. state.compilationString += `#ifdef ${this._linearDefineName}\r\n`;
  179. state.compilationString += `${output.associatedVariableName} = toGammaSpace(${output.associatedVariableName});\r\n`;
  180. state.compilationString += `#endif\r\n`;
  181. state.compilationString += `#ifdef ${this._gammaDefineName}\r\n`;
  182. state.compilationString += `${output.associatedVariableName} = toLinearSpace(${output.associatedVariableName});\r\n`;
  183. state.compilationString += `#endif\r\n`;
  184. }
  185. protected _buildBlock(state: NodeMaterialBuildState) {
  186. super._buildBlock(state);
  187. this._tempTextureRead = state._getFreeVariableName("tempTextureRead");
  188. if (state.sharedData.blockingBlocks.indexOf(this) < 0) {
  189. state.sharedData.blockingBlocks.push(this);
  190. }
  191. if (state.sharedData.textureBlocks.indexOf(this) < 0) {
  192. state.sharedData.textureBlocks.push(this);
  193. }
  194. if (state.sharedData.blocksWithDefines.indexOf(this) < 0) {
  195. state.sharedData.blocksWithDefines.push(this);
  196. }
  197. if (state.target !== NodeMaterialBlockTargets.Fragment) {
  198. // Vertex
  199. state._emit2DSampler(this._samplerName);
  200. this._injectVertexCode(state);
  201. return;
  202. }
  203. // Fragment
  204. if (!this._outputs.some((o) => o.isConnectedInFragmentShader)) {
  205. return;
  206. }
  207. state._emit2DSampler(this._samplerName);
  208. this._linearDefineName = state._getFreeDefineName("ISLINEAR");
  209. this._gammaDefineName = state._getFreeDefineName("ISGAMMA");
  210. let comments = `//${this.name}`;
  211. state._emitFunctionFromInclude("helperFunctions", comments);
  212. this._writeTextureRead(state);
  213. for (var output of this._outputs) {
  214. if (output.hasEndpoints) {
  215. this._writeOutput(state, output, output.name);
  216. }
  217. }
  218. return this;
  219. }
  220. public serialize(): any {
  221. let serializationObject = super.serialize();
  222. serializationObject.convertToGammaSpace = this.convertToGammaSpace;
  223. serializationObject.convertToLinearSpace = this.convertToLinearSpace;
  224. if (this.texture && !this.texture.isRenderTarget) {
  225. serializationObject.texture = this.texture.serialize();
  226. }
  227. return serializationObject;
  228. }
  229. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  230. super._deserialize(serializationObject, scene, rootUrl);
  231. this.convertToGammaSpace = serializationObject.convertToGammaSpace;
  232. this.convertToLinearSpace = !!serializationObject.convertToLinearSpace;
  233. if (serializationObject.texture) {
  234. rootUrl = serializationObject.texture.url.indexOf("data:") === 0 ? "" : rootUrl;
  235. this.texture = Texture.Parse(serializationObject.texture, scene, rootUrl) as Texture;
  236. }
  237. }
  238. }
  239. _TypeStore.RegisteredTypes["BABYLON.CurrentScreenBlock"] = CurrentScreenBlock;