currentScreenBlock.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 = true;
  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 = true;
  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. // TextureBlock has a special optimizations for uvs that come from the vertex shaders as they can be packed into a single varyings.
  112. // But we need to detect uvs coming from fragment then
  113. if (!this.uv.isConnected) {
  114. return NodeMaterialBlockTargets.VertexAndFragment;
  115. }
  116. if (this.uv.sourceBlock!.isInput) {
  117. return NodeMaterialBlockTargets.VertexAndFragment;
  118. }
  119. let parent = this.uv.connectedPoint;
  120. while (parent) {
  121. if (parent.target === NodeMaterialBlockTargets.Fragment) {
  122. return NodeMaterialBlockTargets.Fragment;
  123. }
  124. if (parent.target === NodeMaterialBlockTargets.Vertex) {
  125. return NodeMaterialBlockTargets.VertexAndFragment;
  126. }
  127. if (parent.target === NodeMaterialBlockTargets.Neutral || parent.target === NodeMaterialBlockTargets.VertexAndFragment) {
  128. let parentBlock = parent.ownerBlock;
  129. parent = null;
  130. for (var input of parentBlock.inputs) {
  131. if (input.connectedPoint) {
  132. parent = input.connectedPoint;
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. return NodeMaterialBlockTargets.VertexAndFragment;
  139. }
  140. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  141. defines.setValue(this._linearDefineName, this.convertToGammaSpace, true);
  142. defines.setValue(this._gammaDefineName, this.convertToLinearSpace, true);
  143. }
  144. public isReady() {
  145. if (this.texture && !this.texture.isReadyOrNotBlocking()) {
  146. return false;
  147. }
  148. return true;
  149. }
  150. private _injectVertexCode(state: NodeMaterialBuildState) {
  151. let uvInput = this.uv;
  152. if (uvInput.connectedPoint!.ownerBlock.isInput) {
  153. let uvInputOwnerBlock = uvInput.connectedPoint!.ownerBlock as InputBlock;
  154. if (!uvInputOwnerBlock.isAttribute) {
  155. state._emitUniformFromString(uvInput.associatedVariableName, "vec2");
  156. }
  157. }
  158. this._mainUVName = "vMain" + uvInput.associatedVariableName;
  159. state._emitVaryingFromString(this._mainUVName, "vec2");
  160. state.compilationString += `${this._mainUVName} = ${uvInput.associatedVariableName}.xy;\r\n`;
  161. if (!this._outputs.some((o) => o.isConnectedInVertexShader)) {
  162. return;
  163. }
  164. this._writeTextureRead(state, true);
  165. for (var output of this._outputs) {
  166. if (output.hasEndpoints) {
  167. this._writeOutput(state, output, output.name, true);
  168. }
  169. }
  170. }
  171. private _writeTextureRead(state: NodeMaterialBuildState, vertexMode = false) {
  172. let uvInput = this.uv;
  173. if (vertexMode) {
  174. if (state.target === NodeMaterialBlockTargets.Fragment) {
  175. return;
  176. }
  177. state.compilationString += `vec4 ${this._tempTextureRead} = texture2D(${this._samplerName}, ${uvInput.associatedVariableName});\r\n`;
  178. return;
  179. }
  180. if (this.uv.ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  181. state.compilationString += `vec4 ${this._tempTextureRead} = texture2D(${this._samplerName}, ${uvInput.associatedVariableName});\r\n`;
  182. return;
  183. }
  184. state.compilationString += `vec4 ${this._tempTextureRead} = texture2D(${this._samplerName}, ${this._mainUVName});\r\n`;
  185. }
  186. private _writeOutput(state: NodeMaterialBuildState, output: NodeMaterialConnectionPoint, swizzle: string, vertexMode = false) {
  187. if (vertexMode) {
  188. if (state.target === NodeMaterialBlockTargets.Fragment) {
  189. return;
  190. }
  191. state.compilationString += `${this._declareOutput(output, state)} = ${this._tempTextureRead}.${swizzle};\r\n`;
  192. return;
  193. }
  194. if (this.uv.ownerBlock.target === NodeMaterialBlockTargets.Fragment) {
  195. state.compilationString += `${this._declareOutput(output, state)} = ${this._tempTextureRead}.${swizzle};\r\n`;
  196. return;
  197. }
  198. state.compilationString += `${this._declareOutput(output, state)} = ${this._tempTextureRead}.${swizzle};\r\n`;
  199. state.compilationString += `#ifdef ${this._linearDefineName}\r\n`;
  200. state.compilationString += `${output.associatedVariableName} = toGammaSpace(${output.associatedVariableName});\r\n`;
  201. state.compilationString += `#endif\r\n`;
  202. state.compilationString += `#ifdef ${this._gammaDefineName}\r\n`;
  203. state.compilationString += `${output.associatedVariableName} = toLinearSpace(${output.associatedVariableName});\r\n`;
  204. state.compilationString += `#endif\r\n`;
  205. }
  206. protected _buildBlock(state: NodeMaterialBuildState) {
  207. super._buildBlock(state);
  208. if (state.target === NodeMaterialBlockTargets.Vertex) {
  209. this._tempTextureRead = state._getFreeVariableName("tempTextureRead");
  210. state._emit2DSampler(this._samplerName);
  211. state.sharedData.blockingBlocks.push(this);
  212. state.sharedData.textureBlocks.push(this);
  213. state.sharedData.blocksWithDefines.push(this);
  214. }
  215. if (state.target !== NodeMaterialBlockTargets.Fragment) {
  216. // Vertex
  217. this._injectVertexCode(state);
  218. return;
  219. }
  220. // Fragment
  221. if (!this._outputs.some((o) => o.isConnectedInFragmentShader)) {
  222. return;
  223. }
  224. state._emit2DSampler(this._samplerName);
  225. this._linearDefineName = state._getFreeDefineName("ISLINEAR");
  226. this._gammaDefineName = state._getFreeDefineName("ISGAMMA");
  227. let comments = `//${this.name}`;
  228. state._emitFunctionFromInclude("helperFunctions", comments);
  229. this._writeTextureRead(state);
  230. for (var output of this._outputs) {
  231. if (output.hasEndpoints) {
  232. this._writeOutput(state, output, output.name);
  233. }
  234. }
  235. return this;
  236. }
  237. public serialize(): any {
  238. let serializationObject = super.serialize();
  239. serializationObject.convertToGammaSpace = this.convertToGammaSpace;
  240. serializationObject.convertToLinearSpace = this.convertToLinearSpace;
  241. if (this.texture) {
  242. serializationObject.texture = this.texture.serialize();
  243. }
  244. return serializationObject;
  245. }
  246. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  247. super._deserialize(serializationObject, scene, rootUrl);
  248. this.convertToGammaSpace = serializationObject.convertToGammaSpace;
  249. this.convertToLinearSpace = !!serializationObject.convertToLinearSpace;
  250. if (serializationObject.texture) {
  251. rootUrl = serializationObject.texture.url.indexOf("data:") === 0 ? "" : rootUrl;
  252. this.texture = Texture.Parse(serializationObject.texture, scene, rootUrl) as Texture;
  253. }
  254. }
  255. }
  256. _TypeStore.RegisteredTypes["BABYLON.CurrentScreenBlock"] = CurrentScreenBlock;