nodeMaterialCompilationState.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { NodeMaterialConnectionPoint } from './nodeMaterialBlockConnectionPoint';
  2. import { NodeMaterialBlockConnectionPointTypes } from './nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialWellKnownValues } from './nodeMaterialWellKnownValues';
  4. import { NodeMaterialBlockTargets } from './nodeMaterialBlockTargets';
  5. import { NodeMaterialCompilationStateSharedData } from './nodeMaterialCompilationStateSharedData';
  6. import { Effect } from '../../Materials/effect';
  7. /**
  8. * Class used to store node based material compilation state
  9. */
  10. export class NodeMaterialCompilationState {
  11. /**
  12. * Gets the list of emitted attributes
  13. */
  14. public attributes = new Array<string>();
  15. /**
  16. * Gets the list of emitted uniforms
  17. */
  18. public uniforms = new Array<string>();
  19. /**
  20. * Gets the list of emitted samplers
  21. */
  22. public samplers = new Array<string>();
  23. /**
  24. * Gets the list of emitted functions
  25. */
  26. public functions: { [key: string]: string } = {};
  27. /**
  28. * Gets the target of the compilation state
  29. */
  30. public target: NodeMaterialBlockTargets;
  31. /**
  32. * Shared data between multiple NodeMaterialCompilationState instances
  33. */
  34. public sharedData: NodeMaterialCompilationStateSharedData;
  35. /** @hidden */
  36. public _uniformConnectionPoints = new Array<NodeMaterialConnectionPoint>();
  37. /** @hidden */
  38. public _vertexState: NodeMaterialCompilationState;
  39. private _attributeDeclaration = "";
  40. private _uniformDeclaration = "";
  41. private _samplerDeclaration = "";
  42. private _varyingTransfer = "";
  43. /**
  44. * Gets the emitted compilation strings
  45. */
  46. public compilationString = "";
  47. /**
  48. * Finalize the compilation strings
  49. * @param state defines the current compilation state
  50. */
  51. public finalize(state: NodeMaterialCompilationState) {
  52. let emitComments = state.sharedData.emitComments;
  53. let isFragmentMode = (this.target === NodeMaterialBlockTargets.Fragment);
  54. this.compilationString = `\r\n${emitComments ? "//Entry point\r\n" : ""}void main(void) {\r\n${this.compilationString}`;
  55. for (var functionName in this.functions) {
  56. let functionCode = this.functions[functionName];
  57. this.compilationString = `\r\n${functionCode}\r\n${this.compilationString}`;
  58. }
  59. if (!isFragmentMode && this._varyingTransfer) {
  60. this.compilationString = `${this.compilationString}\r\n${this._varyingTransfer}`;
  61. }
  62. this.compilationString = `${this.compilationString}\r\n}`;
  63. if (this.sharedData.varyingDeclaration) {
  64. this.compilationString = `\r\n${emitComments ? "//Varyings\r\n" : ""}${this.sharedData.varyingDeclaration}\r\n${this.compilationString}`;
  65. }
  66. if (this._samplerDeclaration) {
  67. this.compilationString = `\r\n${emitComments ? "//Samplers\r\n" : ""}${this._samplerDeclaration}\r\n${this.compilationString}`;
  68. }
  69. if (this._uniformDeclaration) {
  70. this.compilationString = `\r\n${emitComments ? "//Uniforms\r\n" : ""}${this._uniformDeclaration}\r\n${this.compilationString}`;
  71. }
  72. if (this._attributeDeclaration && !isFragmentMode) {
  73. this.compilationString = `\r\n${emitComments ? "//Attributes\r\n" : ""}${this._attributeDeclaration}\r\n${this.compilationString}`;
  74. }
  75. }
  76. /** @hidden */
  77. public _getFreeVariableName(prefix: string): string {
  78. if (this.sharedData.variableNames[prefix] === undefined) {
  79. this.sharedData.variableNames[prefix] = 0;
  80. } else {
  81. this.sharedData.variableNames[prefix]++;
  82. }
  83. return prefix + this.sharedData.variableNames[prefix];
  84. }
  85. /** @hidden */
  86. public _getGLType(type: NodeMaterialBlockConnectionPointTypes): string {
  87. switch (type) {
  88. case NodeMaterialBlockConnectionPointTypes.Float:
  89. return "float";
  90. case NodeMaterialBlockConnectionPointTypes.Int:
  91. return "int";
  92. case NodeMaterialBlockConnectionPointTypes.Vector2:
  93. return "vec2";
  94. case NodeMaterialBlockConnectionPointTypes.Color3:
  95. case NodeMaterialBlockConnectionPointTypes.Vector3:
  96. case NodeMaterialBlockConnectionPointTypes.Vector3OrColor3:
  97. return "vec3";
  98. case NodeMaterialBlockConnectionPointTypes.Color4:
  99. case NodeMaterialBlockConnectionPointTypes.Vector4:
  100. case NodeMaterialBlockConnectionPointTypes.Vector4OrColor4:
  101. return "vec4";
  102. case NodeMaterialBlockConnectionPointTypes.Matrix:
  103. return "mat4";
  104. case NodeMaterialBlockConnectionPointTypes.Texture:
  105. return "sampler2D";
  106. }
  107. return "";
  108. }
  109. /** @hidden */
  110. public _emitFunction(name: string, code: string) {
  111. if (this.functions[name]) {
  112. return;
  113. }
  114. this.functions[name] = code;
  115. }
  116. /** @hidden */
  117. public _emitFunctionFromInclude(name: string, includeName: string, options?: {
  118. removeUniforms?: boolean,
  119. removeVaryings?: boolean,
  120. removeifDef?: boolean,
  121. replaceString?: string[],
  122. }) {
  123. if (this.functions[name]) {
  124. return;
  125. }
  126. this.functions[name] = Effect.IncludesShadersStore[includeName];
  127. if (!options) {
  128. return;
  129. }
  130. if (options.removeifDef) {
  131. this.functions[name] = this.functions[name].replace(/^\s*?#.+$/gm, "");
  132. }
  133. if (options.removeUniforms) {
  134. this.functions[name] = this.functions[name].replace(/^\s*?uniform.+$/gm, "");
  135. }
  136. if (options.removeVaryings) {
  137. this.functions[name] = this.functions[name].replace(/^\s*?varying.+$/gm, "");
  138. }
  139. if (options.replaceString) {
  140. for (var index = 0; index < options.replaceString.length; index += 2) {
  141. this.functions[name] = this.functions[name].replace(options.replaceString[index], options.replaceString[index + 1]);
  142. }
  143. }
  144. }
  145. /** @hidden */
  146. public _emitVaryings(point: NodeMaterialConnectionPoint, force = false, fromFragment = false) {
  147. if (point.isVarying || force) {
  148. if (this.sharedData.varyings.indexOf(point.associatedVariableName) !== -1) {
  149. return;
  150. }
  151. this.sharedData.varyings.push(point.associatedVariableName);
  152. this.sharedData.varyingDeclaration += `varying ${this._getGLType(point.type)} ${point.associatedVariableName};\r\n`;
  153. if (this.target === NodeMaterialBlockTargets.Vertex && fromFragment) {
  154. this._varyingTransfer += `${point.associatedVariableName} = ${point.name};\r\n`;
  155. }
  156. }
  157. }
  158. /** @hidden */
  159. public _emitUniformOrAttributes(point: NodeMaterialConnectionPoint) {
  160. // Samplers
  161. if (point.type === NodeMaterialBlockConnectionPointTypes.Texture) {
  162. point.name = this._getFreeVariableName(point.name);
  163. point.associatedVariableName = point.name;
  164. if (this.samplers.indexOf(point.name) !== -1) {
  165. return;
  166. }
  167. this.samplers.push(point.name);
  168. this._samplerDeclaration += `uniform ${this._getGLType(point.type)} ${point.name};\r\n`;
  169. this._uniformConnectionPoints.push(point);
  170. return;
  171. }
  172. if (!point.isUniform && !point.isAttribute) {
  173. return;
  174. }
  175. point.associatedVariableName = point.name;
  176. // Uniforms
  177. if (point.isUniform) {
  178. if (this.uniforms.indexOf(point.name) !== -1) {
  179. return;
  180. }
  181. this.uniforms.push(point.name);
  182. this._uniformDeclaration += `uniform ${this._getGLType(point.type)} ${point.name};\r\n`;
  183. // well known
  184. let hints = this.sharedData.hints;
  185. if (point._wellKnownValue !== null) {
  186. switch (point._wellKnownValue) {
  187. case NodeMaterialWellKnownValues.World:
  188. hints.needWorldMatrix = true;
  189. break;
  190. case NodeMaterialWellKnownValues.View:
  191. hints.needViewMatrix = true;
  192. break;
  193. case NodeMaterialWellKnownValues.Projection:
  194. hints.needProjectionMatrix = true;
  195. break;
  196. case NodeMaterialWellKnownValues.ViewProjection:
  197. hints.needViewProjectionMatrix = true;
  198. break;
  199. case NodeMaterialWellKnownValues.WorldView:
  200. hints.needWorldViewMatrix = true;
  201. break;
  202. case NodeMaterialWellKnownValues.WorldViewProjection:
  203. hints.needWorldViewProjectionMatrix = true;
  204. break;
  205. case NodeMaterialWellKnownValues.FogColor:
  206. hints.needFogColor = true;
  207. break;
  208. case NodeMaterialWellKnownValues.FogParameters:
  209. hints.needFogParameters = true;
  210. break;
  211. }
  212. } else {
  213. this._uniformConnectionPoints.push(point);
  214. }
  215. return;
  216. }
  217. if (point.isAttribute) {
  218. if (this.target === NodeMaterialBlockTargets.Fragment) { // Attribute for fragment need to be carried over by varyings
  219. this._vertexState._emitUniformOrAttributes(point);
  220. point.associatedVariableName = this._getFreeVariableName(point.name);
  221. this._emitVaryings(point, true);
  222. this._vertexState._emitVaryings(point, true, true);
  223. return;
  224. }
  225. if (this.attributes.indexOf(point.name) !== -1) {
  226. return;
  227. }
  228. this.attributes.push(point.name);
  229. this._attributeDeclaration += `attribute ${this._getGLType(point.type)} ${point.name};\r\n`;
  230. }
  231. }
  232. }