nodeMaterialBuildState.ts 10 KB

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