inputBlock.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBlockConnectionPointMode } from '../../NodeMaterialBlockConnectionPointMode';
  4. import { NodeMaterialWellKnownValues } from '../../nodeMaterialWellKnownValues';
  5. import { Nullable } from '../../../../types';
  6. import { Effect } from '../../../../Materials/effect';
  7. import { Matrix, Vector2, Vector3, Vector4 } from '../../../../Maths/math.vector';
  8. import { Scene } from '../../../../scene';
  9. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  10. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  11. import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
  12. import { _TypeStore } from '../../../../Misc/typeStore';
  13. import { Color3, Color4 } from '../../../../Maths/math';
  14. /**
  15. * Block used to expose an input value
  16. */
  17. export class InputBlock extends NodeMaterialBlock {
  18. private _mode = NodeMaterialBlockConnectionPointMode.Undefined;
  19. private _associatedVariableName: string;
  20. private _storedValue: any;
  21. private _valueCallback: () => any;
  22. private _type: NodeMaterialBlockConnectionPointTypes;
  23. /** @hidden */
  24. public _wellKnownValue: Nullable<NodeMaterialWellKnownValues> = null;
  25. /**
  26. * Gets or sets the connection point type (default is float)
  27. */
  28. public get type(): NodeMaterialBlockConnectionPointTypes {
  29. if (this._type === NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  30. if (this.isUniform && this.value != null) {
  31. if (!isNaN(this.value)) {
  32. this._type = NodeMaterialBlockConnectionPointTypes.Float;
  33. return this._type;
  34. }
  35. switch (this.value.getClassName()) {
  36. case "Vector2":
  37. this._type = NodeMaterialBlockConnectionPointTypes.Vector2;
  38. return this._type;
  39. case "Vector3":
  40. this._type = NodeMaterialBlockConnectionPointTypes.Vector3;
  41. return this._type;
  42. case "Vector4":
  43. this._type = NodeMaterialBlockConnectionPointTypes.Vector4;
  44. return this._type;
  45. case "Color3":
  46. this._type = NodeMaterialBlockConnectionPointTypes.Color3;
  47. return this._type;
  48. case "Color4":
  49. this._type = NodeMaterialBlockConnectionPointTypes.Color4;
  50. return this._type;
  51. }
  52. }
  53. if (this.isAttribute) {
  54. switch (this.name) {
  55. case "position":
  56. case "normal":
  57. case "tangent":
  58. this._type = NodeMaterialBlockConnectionPointTypes.Vector3;
  59. return this._type;
  60. case "uv":
  61. case "uv2":
  62. this._type = NodeMaterialBlockConnectionPointTypes.Vector2;
  63. return this._type;
  64. }
  65. }
  66. if (this.isWellKnownValue) {
  67. switch (this._wellKnownValue) {
  68. case NodeMaterialWellKnownValues.World:
  69. case NodeMaterialWellKnownValues.WorldView:
  70. case NodeMaterialWellKnownValues.WorldViewProjection:
  71. case NodeMaterialWellKnownValues.View:
  72. case NodeMaterialWellKnownValues.ViewProjection:
  73. case NodeMaterialWellKnownValues.Projection:
  74. this._type = NodeMaterialBlockConnectionPointTypes.Matrix;
  75. return this._type;
  76. case NodeMaterialWellKnownValues.CameraPosition:
  77. this._type = NodeMaterialBlockConnectionPointTypes.Vector3;
  78. return this._type;
  79. }
  80. }
  81. }
  82. return this._type;
  83. }
  84. /**
  85. * Creates a new InputBlock
  86. * @param name defines the block name
  87. * @param target defines the target of that block (Vertex by default)
  88. * @param type defines the type of the input (can be set to NodeMaterialBlockConnectionPointTypes.AutoDetect)
  89. */
  90. public constructor(name: string, target = NodeMaterialBlockTargets.Vertex, type: NodeMaterialBlockConnectionPointTypes = NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  91. super(name, target, false, true);
  92. this._type = type;
  93. this.setDefaultValue();
  94. this.registerOutput("output", type);
  95. }
  96. /**
  97. * Gets the output component
  98. */
  99. public get output(): NodeMaterialConnectionPoint {
  100. return this._outputs[0];
  101. }
  102. /**
  103. * Set the source of this connection point to a vertex attribute
  104. * @param attributeName defines the attribute name (position, uv, normal, etc...). If not specified it will take the connection point name
  105. * @returns the current connection point
  106. */
  107. public setAsAttribute(attributeName?: string): InputBlock {
  108. if (attributeName) {
  109. this.name = attributeName;
  110. }
  111. this._mode = NodeMaterialBlockConnectionPointMode.Attribute;
  112. return this;
  113. }
  114. /**
  115. * Set the source of this connection point to a well known value
  116. * @param value define the well known value to use (world, view, etc...) or null to switch to manual value
  117. * @returns the current connection point
  118. */
  119. public setAsWellKnownValue(value: Nullable<NodeMaterialWellKnownValues>): InputBlock {
  120. this.wellKnownValue = value;
  121. return this;
  122. }
  123. /**
  124. * Gets or sets the value of that point.
  125. * Please note that this value will be ignored if valueCallback is defined
  126. */
  127. public get value(): any {
  128. return this._storedValue;
  129. }
  130. public set value(value: any) {
  131. this._storedValue = value;
  132. this._mode = NodeMaterialBlockConnectionPointMode.Uniform;
  133. }
  134. /**
  135. * Gets or sets a callback used to get the value of that point.
  136. * Please note that setting this value will force the connection point to ignore the value property
  137. */
  138. public get valueCallback(): () => any {
  139. return this._valueCallback;
  140. }
  141. public set valueCallback(value: () => any) {
  142. this._valueCallback = value;
  143. this._mode = NodeMaterialBlockConnectionPointMode.Uniform;
  144. }
  145. /**
  146. * Gets or sets the associated variable name in the shader
  147. */
  148. public get associatedVariableName(): string {
  149. return this._associatedVariableName;
  150. }
  151. public set associatedVariableName(value: string) {
  152. this._associatedVariableName = value;
  153. }
  154. /**
  155. * Gets a boolean indicating that this connection point not defined yet
  156. */
  157. public get isUndefined(): boolean {
  158. return this._mode === NodeMaterialBlockConnectionPointMode.Undefined;
  159. }
  160. /**
  161. * Gets or sets a boolean indicating that this connection point is coming from an uniform.
  162. * In this case the connection point name must be the name of the uniform to use.
  163. * Can only be set on inputs
  164. */
  165. public get isUniform(): boolean {
  166. return this._mode === NodeMaterialBlockConnectionPointMode.Uniform;
  167. }
  168. public set isUniform(value: boolean) {
  169. this._mode = value ? NodeMaterialBlockConnectionPointMode.Uniform : NodeMaterialBlockConnectionPointMode.Undefined;
  170. this.associatedVariableName = "";
  171. }
  172. /**
  173. * Gets or sets a boolean indicating that this connection point is coming from an attribute.
  174. * In this case the connection point name must be the name of the attribute to use
  175. * Can only be set on inputs
  176. */
  177. public get isAttribute(): boolean {
  178. return this._mode === NodeMaterialBlockConnectionPointMode.Attribute;
  179. }
  180. public set isAttribute(value: boolean) {
  181. this._mode = value ? NodeMaterialBlockConnectionPointMode.Attribute : NodeMaterialBlockConnectionPointMode.Undefined;
  182. this.associatedVariableName = "";
  183. }
  184. /**
  185. * Gets or sets a boolean indicating that this connection point is generating a varying variable.
  186. * Can only be set on exit points
  187. */
  188. public get isVarying(): boolean {
  189. return this._mode === NodeMaterialBlockConnectionPointMode.Varying;
  190. }
  191. public set isVarying(value: boolean) {
  192. this._mode = value ? NodeMaterialBlockConnectionPointMode.Varying : NodeMaterialBlockConnectionPointMode.Undefined;
  193. this.associatedVariableName = "";
  194. }
  195. /**
  196. * Gets a boolean indicating that the current connection point is a well known value
  197. */
  198. public get isWellKnownValue(): boolean {
  199. return this._wellKnownValue != null;
  200. }
  201. /**
  202. * Gets or sets the current well known value or null if not defined as well know value
  203. */
  204. public get wellKnownValue(): Nullable<NodeMaterialWellKnownValues> {
  205. return this._wellKnownValue;
  206. }
  207. public set wellKnownValue(value: Nullable<NodeMaterialWellKnownValues>) {
  208. this._mode = NodeMaterialBlockConnectionPointMode.Uniform;
  209. this.associatedVariableName = "";
  210. this._wellKnownValue = value;
  211. }
  212. /**
  213. * Gets the current class name
  214. * @returns the class name
  215. */
  216. public getClassName() {
  217. return "InputBlock";
  218. }
  219. private _emitDefine(define: string): string {
  220. if (define[0] === "!") {
  221. return `#ifndef ${define.substring(1)}\r\n`;
  222. }
  223. return `#ifdef ${define}\r\n`;
  224. }
  225. /**
  226. * Set the input block to its default value (based on its type)
  227. */
  228. public setDefaultValue() {
  229. switch (this.type) {
  230. case NodeMaterialBlockConnectionPointTypes.Float:
  231. this.value = 0;
  232. break;
  233. case NodeMaterialBlockConnectionPointTypes.Vector2:
  234. this.value = Vector2.Zero();
  235. break;
  236. case NodeMaterialBlockConnectionPointTypes.Vector3:
  237. this.value = Vector3.Zero();
  238. break;
  239. case NodeMaterialBlockConnectionPointTypes.Vector4:
  240. this.value = Vector4.Zero();
  241. break;
  242. case NodeMaterialBlockConnectionPointTypes.Color3:
  243. this.value = Color3.White();
  244. break;
  245. case NodeMaterialBlockConnectionPointTypes.Color4:
  246. this.value = new Color4(1, 1, 1, 1);
  247. break;
  248. case NodeMaterialBlockConnectionPointTypes.Matrix:
  249. this.value = Matrix.Identity();
  250. break;
  251. }
  252. }
  253. private _emit(state: NodeMaterialBuildState, define?: string) {
  254. // Uniforms
  255. if (this.isUniform) {
  256. if (!this.associatedVariableName) {
  257. this.associatedVariableName = state._getFreeVariableName("u_" + this.name);
  258. }
  259. if (state.uniforms.indexOf(this.associatedVariableName) !== -1) {
  260. return;
  261. }
  262. state.uniforms.push(this.associatedVariableName);
  263. if (define) {
  264. state._uniformDeclaration += this._emitDefine(define);
  265. }
  266. state._uniformDeclaration += `uniform ${state._getGLType(this.type)} ${this.associatedVariableName};\r\n`;
  267. if (define) {
  268. state._uniformDeclaration += `#endif\r\n`;
  269. }
  270. // well known
  271. let hints = state.sharedData.hints;
  272. if (this._wellKnownValue !== null) {
  273. switch (this._wellKnownValue) {
  274. case NodeMaterialWellKnownValues.WorldView:
  275. hints.needWorldViewMatrix = true;
  276. break;
  277. case NodeMaterialWellKnownValues.WorldViewProjection:
  278. hints.needWorldViewProjectionMatrix = true;
  279. break;
  280. }
  281. }
  282. return;
  283. }
  284. // Attribute
  285. if (this.isAttribute) {
  286. this.associatedVariableName = this.name;
  287. if (this.target === NodeMaterialBlockTargets.Vertex && state._vertexState) { // Attribute for fragment need to be carried over by varyings
  288. this._emit(state._vertexState, define);
  289. return;
  290. }
  291. if (state.attributes.indexOf(this.associatedVariableName) !== -1) {
  292. return;
  293. }
  294. state.attributes.push(this.associatedVariableName);
  295. if (define) {
  296. state._attributeDeclaration += this._emitDefine(define);
  297. }
  298. state._attributeDeclaration += `attribute ${state._getGLType(this.type)} ${this.associatedVariableName};\r\n`;
  299. if (define) {
  300. state._attributeDeclaration += `#endif\r\n`;
  301. }
  302. }
  303. }
  304. /** @hidden */
  305. public _transmitWorld(effect: Effect, world: Matrix, worldView: Matrix, worldViewProjection: Matrix) {
  306. if (!this._wellKnownValue) {
  307. return;
  308. }
  309. let variableName = this.associatedVariableName;
  310. switch (this._wellKnownValue) {
  311. case NodeMaterialWellKnownValues.World:
  312. effect.setMatrix(variableName, world);
  313. break;
  314. case NodeMaterialWellKnownValues.WorldView:
  315. effect.setMatrix(variableName, worldView);
  316. break;
  317. case NodeMaterialWellKnownValues.WorldViewProjection:
  318. effect.setMatrix(variableName, worldViewProjection);
  319. break;
  320. }
  321. }
  322. /** @hidden */
  323. public _transmit(effect: Effect, scene: Scene) {
  324. if (this.isAttribute) {
  325. return;
  326. }
  327. let variableName = this.associatedVariableName;
  328. if (this._wellKnownValue) {
  329. switch (this._wellKnownValue) {
  330. case NodeMaterialWellKnownValues.World:
  331. case NodeMaterialWellKnownValues.WorldView:
  332. case NodeMaterialWellKnownValues.WorldViewProjection:
  333. return;
  334. case NodeMaterialWellKnownValues.View:
  335. effect.setMatrix(variableName, scene.getViewMatrix());
  336. break;
  337. case NodeMaterialWellKnownValues.Projection:
  338. effect.setMatrix(variableName, scene.getProjectionMatrix());
  339. break;
  340. case NodeMaterialWellKnownValues.ViewProjection:
  341. effect.setMatrix(variableName, scene.getTransformMatrix());
  342. break;
  343. case NodeMaterialWellKnownValues.CameraPosition:
  344. effect.setVector3(variableName, scene.activeCamera!.globalPosition);
  345. break;
  346. case NodeMaterialWellKnownValues.FogColor:
  347. effect.setColor3(variableName, scene.fogColor);
  348. break;
  349. }
  350. return;
  351. }
  352. let value = this._valueCallback ? this._valueCallback() : this._storedValue;
  353. if (value === null) {
  354. return;
  355. }
  356. switch (this.type) {
  357. case NodeMaterialBlockConnectionPointTypes.Float:
  358. effect.setFloat(variableName, value);
  359. break;
  360. case NodeMaterialBlockConnectionPointTypes.Int:
  361. effect.setInt(variableName, value);
  362. break;
  363. case NodeMaterialBlockConnectionPointTypes.Color3:
  364. effect.setColor3(variableName, value);
  365. break;
  366. case NodeMaterialBlockConnectionPointTypes.Color4:
  367. effect.setDirectColor4(variableName, value);
  368. break;
  369. case NodeMaterialBlockConnectionPointTypes.Vector2:
  370. effect.setVector2(variableName, value);
  371. break;
  372. case NodeMaterialBlockConnectionPointTypes.Vector3:
  373. effect.setVector3(variableName, value);
  374. break;
  375. case NodeMaterialBlockConnectionPointTypes.Color3OrColor4:
  376. effect.setFloat4(variableName, value.r, value.g, value.b, value.a || 1.0);
  377. break;
  378. case NodeMaterialBlockConnectionPointTypes.Vector4OrColor4:
  379. case NodeMaterialBlockConnectionPointTypes.Vector4:
  380. effect.setVector4(variableName, value);
  381. break;
  382. case NodeMaterialBlockConnectionPointTypes.Matrix:
  383. effect.setMatrix(variableName, value);
  384. break;
  385. }
  386. }
  387. protected _buildBlock(state: NodeMaterialBuildState) {
  388. super._buildBlock(state);
  389. if (this.isUniform || this.isWellKnownValue) {
  390. state.sharedData.inputBlocks.push(this);
  391. }
  392. this._emit(state);
  393. }
  394. public serialize(): any {
  395. let serializationObject = super.serialize();
  396. serializationObject.type = this.type;
  397. serializationObject.mode = this._mode;
  398. serializationObject.wellKnownValue = this._wellKnownValue;
  399. if (this._storedValue != null && this._mode === NodeMaterialBlockConnectionPointMode.Uniform) {
  400. if (this._storedValue.asArray) {
  401. serializationObject.valueType = "BABYLON." + this._storedValue.getClassName();
  402. serializationObject.value = this._storedValue.asArray();
  403. } else {
  404. serializationObject.valueType = "number";
  405. serializationObject.value = this._storedValue;
  406. }
  407. }
  408. return serializationObject;
  409. }
  410. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  411. super._deserialize(serializationObject, scene, rootUrl);
  412. this._type = serializationObject.type;
  413. this._mode = serializationObject.mode;
  414. this._wellKnownValue = serializationObject.wellKnownValue;
  415. if (!serializationObject.valueType) {
  416. return;
  417. }
  418. if (serializationObject.valueType === "number") {
  419. this._storedValue = serializationObject.value;
  420. } else {
  421. let valueType = _TypeStore.GetClass(serializationObject.valueType);
  422. if (valueType) {
  423. this._storedValue = valueType.FromArray(serializationObject.value);
  424. }
  425. }
  426. }
  427. }
  428. _TypeStore.RegisteredTypes["BABYLON.InputBlock"] = InputBlock;