inputBlock.ts 22 KB

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