inputBlock.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBlockConnectionPointMode } from '../../Enums/nodeMaterialBlockConnectionPointMode';
  4. import { NodeMaterialSystemValues } from '../../Enums/nodeMaterialSystemValues';
  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 '../../Enums/nodeMaterialBlockTargets';
  12. import { _TypeStore } from '../../../../Misc/typeStore';
  13. import { Color3, Color4 } from '../../../../Maths/math';
  14. import { AnimatedInputBlockTypes } from './animatedInputBlockTypes';
  15. import { Observable } from '../../../../Misc/observable';
  16. import { MaterialHelper } from '../../../../Materials/materialHelper';
  17. const remapAttributeName: { [name: string]: string } = {
  18. "position2d": "position",
  19. "particle_uv": "vUV",
  20. "particle_color": "vColor",
  21. "particle_texturemask": "textureMask",
  22. };
  23. const attributeInFragmentOnly: { [name: string]: boolean } = {
  24. "particle_uv": true,
  25. "particle_color": true,
  26. "particle_texturemask": true,
  27. };
  28. const attributeAsUniform: { [name: string]: boolean } = {
  29. "particle_texturemask": true,
  30. };
  31. /**
  32. * Block used to expose an input value
  33. */
  34. export class InputBlock extends NodeMaterialBlock {
  35. private _mode = NodeMaterialBlockConnectionPointMode.Undefined;
  36. private _associatedVariableName: string;
  37. private _storedValue: any;
  38. private _valueCallback: () => any;
  39. private _type: NodeMaterialBlockConnectionPointTypes;
  40. private _animationType = AnimatedInputBlockTypes.None;
  41. /** Gets or set a value used to limit the range of float values */
  42. public min: number = 0;
  43. /** Gets or set a value used to limit the range of float values */
  44. public max: number = 0;
  45. /** Gets or set a value indicating that this input can only get 0 and 1 values */
  46. public isBoolean: boolean = false;
  47. /** Gets or sets a value used by the Node Material editor to determine how to configure the current value if it is a matrix */
  48. public matrixMode: number = 0;
  49. /** @hidden */
  50. public _systemValue: Nullable<NodeMaterialSystemValues> = null;
  51. /** Gets or sets a boolean indicating that this input can be edited in the Inspector (false by default) */
  52. public visibleInInspector = false;
  53. /** Gets or sets a boolean indicating that the value of this input will not change after a build */
  54. public isConstant = false;
  55. /** Gets or sets the group to use to display this block in the Inspector */
  56. public groupInInspector = "";
  57. /** Gets an observable raised when the value is changed */
  58. public onValueChangedObservable = new Observable<InputBlock>();
  59. /**
  60. * Gets or sets the connection point type (default is float)
  61. */
  62. public get type(): NodeMaterialBlockConnectionPointTypes {
  63. if (this._type === NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  64. if (this.isUniform && this.value != null) {
  65. if (!isNaN(this.value)) {
  66. this._type = NodeMaterialBlockConnectionPointTypes.Float;
  67. return this._type;
  68. }
  69. switch (this.value.getClassName()) {
  70. case "Vector2":
  71. this._type = NodeMaterialBlockConnectionPointTypes.Vector2;
  72. return this._type;
  73. case "Vector3":
  74. this._type = NodeMaterialBlockConnectionPointTypes.Vector3;
  75. return this._type;
  76. case "Vector4":
  77. this._type = NodeMaterialBlockConnectionPointTypes.Vector4;
  78. return this._type;
  79. case "Color3":
  80. this._type = NodeMaterialBlockConnectionPointTypes.Color3;
  81. return this._type;
  82. case "Color4":
  83. this._type = NodeMaterialBlockConnectionPointTypes.Color4;
  84. return this._type;
  85. case "Matrix":
  86. this._type = NodeMaterialBlockConnectionPointTypes.Matrix;
  87. return this._type;
  88. }
  89. }
  90. if (this.isAttribute) {
  91. switch (this.name) {
  92. case "position":
  93. case "normal":
  94. case "tangent":
  95. this._type = NodeMaterialBlockConnectionPointTypes.Vector3;
  96. return this._type;
  97. case "uv":
  98. case "uv2":
  99. case "position2d":
  100. case "particle_uv":
  101. this._type = NodeMaterialBlockConnectionPointTypes.Vector2;
  102. return this._type;
  103. case "matricesIndices":
  104. case "matricesWeights":
  105. case "world0":
  106. case "world1":
  107. case "world2":
  108. case "world3":
  109. this._type = NodeMaterialBlockConnectionPointTypes.Vector4;
  110. return this._type;
  111. case "color":
  112. case "particle_color":
  113. case "particle_texturemask":
  114. this._type = NodeMaterialBlockConnectionPointTypes.Color4;
  115. return this._type;
  116. }
  117. }
  118. if (this.isSystemValue) {
  119. switch (this._systemValue) {
  120. case NodeMaterialSystemValues.World:
  121. case NodeMaterialSystemValues.WorldView:
  122. case NodeMaterialSystemValues.WorldViewProjection:
  123. case NodeMaterialSystemValues.View:
  124. case NodeMaterialSystemValues.ViewProjection:
  125. case NodeMaterialSystemValues.Projection:
  126. this._type = NodeMaterialBlockConnectionPointTypes.Matrix;
  127. return this._type;
  128. case NodeMaterialSystemValues.CameraPosition:
  129. this._type = NodeMaterialBlockConnectionPointTypes.Vector3;
  130. return this._type;
  131. case NodeMaterialSystemValues.FogColor:
  132. this._type = NodeMaterialBlockConnectionPointTypes.Color3;
  133. return this._type;
  134. case NodeMaterialSystemValues.DeltaTime:
  135. this._type = NodeMaterialBlockConnectionPointTypes.Float;
  136. return this._type;
  137. }
  138. }
  139. }
  140. return this._type;
  141. }
  142. /**
  143. * Creates a new InputBlock
  144. * @param name defines the block name
  145. * @param target defines the target of that block (Vertex by default)
  146. * @param type defines the type of the input (can be set to NodeMaterialBlockConnectionPointTypes.AutoDetect)
  147. */
  148. public constructor(name: string, target = NodeMaterialBlockTargets.Vertex, type: NodeMaterialBlockConnectionPointTypes = NodeMaterialBlockConnectionPointTypes.AutoDetect) {
  149. super(name, target, false, true);
  150. this._type = type;
  151. this.setDefaultValue();
  152. this.registerOutput("output", type);
  153. }
  154. /**
  155. * Gets the output component
  156. */
  157. public get output(): NodeMaterialConnectionPoint {
  158. return this._outputs[0];
  159. }
  160. /**
  161. * Set the source of this connection point to a vertex attribute
  162. * @param attributeName defines the attribute name (position, uv, normal, etc...). If not specified it will take the connection point name
  163. * @returns the current connection point
  164. */
  165. public setAsAttribute(attributeName?: string): InputBlock {
  166. if (attributeName) {
  167. this.name = attributeName;
  168. }
  169. this._mode = NodeMaterialBlockConnectionPointMode.Attribute;
  170. return this;
  171. }
  172. /**
  173. * Set the source of this connection point to a system value
  174. * @param value define the system value to use (world, view, etc...) or null to switch to manual value
  175. * @returns the current connection point
  176. */
  177. public setAsSystemValue(value: Nullable<NodeMaterialSystemValues>): InputBlock {
  178. this.systemValue = value;
  179. return this;
  180. }
  181. /**
  182. * Gets or sets the value of that point.
  183. * Please note that this value will be ignored if valueCallback is defined
  184. */
  185. public get value(): any {
  186. return this._storedValue;
  187. }
  188. public set value(value: any) {
  189. if (this.type === NodeMaterialBlockConnectionPointTypes.Float) {
  190. if (this.isBoolean) {
  191. value = value ? 1 : 0;
  192. }
  193. else if (this.min !== this.max) {
  194. value = Math.max(this.min, value);
  195. value = Math.min(this.max, value);
  196. }
  197. }
  198. this._storedValue = value;
  199. this._mode = NodeMaterialBlockConnectionPointMode.Uniform;
  200. this.onValueChangedObservable.notifyObservers(this);
  201. }
  202. /**
  203. * Gets or sets a callback used to get the value of that point.
  204. * Please note that setting this value will force the connection point to ignore the value property
  205. */
  206. public get valueCallback(): () => any {
  207. return this._valueCallback;
  208. }
  209. public set valueCallback(value: () => any) {
  210. this._valueCallback = value;
  211. this._mode = NodeMaterialBlockConnectionPointMode.Uniform;
  212. }
  213. /**
  214. * Gets or sets the associated variable name in the shader
  215. */
  216. public get associatedVariableName(): string {
  217. return this._associatedVariableName;
  218. }
  219. public set associatedVariableName(value: string) {
  220. this._associatedVariableName = value;
  221. }
  222. /** Gets or sets the type of animation applied to the input */
  223. public get animationType() {
  224. return this._animationType;
  225. }
  226. public set animationType(value: AnimatedInputBlockTypes) {
  227. this._animationType = value;
  228. }
  229. /**
  230. * Gets a boolean indicating that this connection point not defined yet
  231. */
  232. public get isUndefined(): boolean {
  233. return this._mode === NodeMaterialBlockConnectionPointMode.Undefined;
  234. }
  235. /**
  236. * Gets or sets a boolean indicating that this connection point is coming from an uniform.
  237. * In this case the connection point name must be the name of the uniform to use.
  238. * Can only be set on inputs
  239. */
  240. public get isUniform(): boolean {
  241. return this._mode === NodeMaterialBlockConnectionPointMode.Uniform;
  242. }
  243. public set isUniform(value: boolean) {
  244. this._mode = value ? NodeMaterialBlockConnectionPointMode.Uniform : NodeMaterialBlockConnectionPointMode.Undefined;
  245. this.associatedVariableName = "";
  246. }
  247. /**
  248. * Gets or sets a boolean indicating that this connection point is coming from an attribute.
  249. * In this case the connection point name must be the name of the attribute to use
  250. * Can only be set on inputs
  251. */
  252. public get isAttribute(): boolean {
  253. return this._mode === NodeMaterialBlockConnectionPointMode.Attribute;
  254. }
  255. public set isAttribute(value: boolean) {
  256. this._mode = value ? NodeMaterialBlockConnectionPointMode.Attribute : NodeMaterialBlockConnectionPointMode.Undefined;
  257. this.associatedVariableName = "";
  258. }
  259. /**
  260. * Gets or sets a boolean indicating that this connection point is generating a varying variable.
  261. * Can only be set on exit points
  262. */
  263. public get isVarying(): boolean {
  264. return this._mode === NodeMaterialBlockConnectionPointMode.Varying;
  265. }
  266. public set isVarying(value: boolean) {
  267. this._mode = value ? NodeMaterialBlockConnectionPointMode.Varying : NodeMaterialBlockConnectionPointMode.Undefined;
  268. this.associatedVariableName = "";
  269. }
  270. /**
  271. * Gets a boolean indicating that the current connection point is a system value
  272. */
  273. public get isSystemValue(): boolean {
  274. return this._systemValue != null;
  275. }
  276. /**
  277. * Gets or sets the current well known value or null if not defined as a system value
  278. */
  279. public get systemValue(): Nullable<NodeMaterialSystemValues> {
  280. return this._systemValue;
  281. }
  282. public set systemValue(value: Nullable<NodeMaterialSystemValues>) {
  283. this._mode = NodeMaterialBlockConnectionPointMode.Uniform;
  284. this.associatedVariableName = "";
  285. this._systemValue = value;
  286. }
  287. /**
  288. * Gets the current class name
  289. * @returns the class name
  290. */
  291. public getClassName() {
  292. return "InputBlock";
  293. }
  294. /**
  295. * Animate the input if animationType !== None
  296. * @param scene defines the rendering scene
  297. */
  298. public animate(scene: Scene) {
  299. switch (this._animationType) {
  300. case AnimatedInputBlockTypes.Time: {
  301. if (this.type === NodeMaterialBlockConnectionPointTypes.Float) {
  302. this.value += scene.getAnimationRatio() * 0.01;
  303. }
  304. break;
  305. }
  306. }
  307. }
  308. private _emitDefine(define: string): string {
  309. if (define[0] === "!") {
  310. return `#ifndef ${define.substring(1)}\r\n`;
  311. }
  312. return `#ifdef ${define}\r\n`;
  313. }
  314. public initialize(state: NodeMaterialBuildState) {
  315. this.associatedVariableName = "";
  316. }
  317. /**
  318. * Set the input block to its default value (based on its type)
  319. */
  320. public setDefaultValue() {
  321. switch (this.type) {
  322. case NodeMaterialBlockConnectionPointTypes.Float:
  323. this.value = 0;
  324. break;
  325. case NodeMaterialBlockConnectionPointTypes.Vector2:
  326. this.value = Vector2.Zero();
  327. break;
  328. case NodeMaterialBlockConnectionPointTypes.Vector3:
  329. this.value = Vector3.Zero();
  330. break;
  331. case NodeMaterialBlockConnectionPointTypes.Vector4:
  332. this.value = Vector4.Zero();
  333. break;
  334. case NodeMaterialBlockConnectionPointTypes.Color3:
  335. this.value = Color3.White();
  336. break;
  337. case NodeMaterialBlockConnectionPointTypes.Color4:
  338. this.value = new Color4(1, 1, 1, 1);
  339. break;
  340. case NodeMaterialBlockConnectionPointTypes.Matrix:
  341. this.value = Matrix.Identity();
  342. break;
  343. }
  344. }
  345. private _emitConstant(state: NodeMaterialBuildState) {
  346. switch (this.type) {
  347. case NodeMaterialBlockConnectionPointTypes.Float:
  348. return `${state._emitFloat(this.value)}`;
  349. case NodeMaterialBlockConnectionPointTypes.Vector2:
  350. return `vec2(${this.value.x}, ${this.value.y})`;
  351. case NodeMaterialBlockConnectionPointTypes.Vector3:
  352. return `vec3(${this.value.x}, ${this.value.y}, ${this.value.z})`;
  353. case NodeMaterialBlockConnectionPointTypes.Vector4:
  354. return `vec4(${this.value.x}, ${this.value.y}, ${this.value.z}, ${this.value.w})`;
  355. case NodeMaterialBlockConnectionPointTypes.Color3:
  356. return `vec3(${this.value.r}, ${this.value.g}, ${this.value.b})`;
  357. case NodeMaterialBlockConnectionPointTypes.Color4:
  358. return `vec4(${this.value.r}, ${this.value.g}, ${this.value.b}, ${this.value.a})`;
  359. }
  360. return "";
  361. }
  362. /** @hidden */
  363. public get _noContextSwitch(): boolean {
  364. return attributeInFragmentOnly[this.name];
  365. }
  366. private _emit(state: NodeMaterialBuildState, define?: string) {
  367. // Uniforms
  368. if (this.isUniform) {
  369. if (!this.associatedVariableName) {
  370. this.associatedVariableName = state._getFreeVariableName("u_" + this.name);
  371. }
  372. if (this.isConstant) {
  373. if (state.constants.indexOf(this.associatedVariableName) !== -1) {
  374. return;
  375. }
  376. state.constants.push(this.associatedVariableName);
  377. state._constantDeclaration += this._declareOutput(this.output, state) + ` = ${this._emitConstant(state)};\r\n`;
  378. return;
  379. }
  380. if (state.uniforms.indexOf(this.associatedVariableName) !== -1) {
  381. return;
  382. }
  383. state.uniforms.push(this.associatedVariableName);
  384. if (define) {
  385. state._uniformDeclaration += this._emitDefine(define);
  386. }
  387. state._uniformDeclaration += `uniform ${state._getGLType(this.type)} ${this.associatedVariableName};\r\n`;
  388. if (define) {
  389. state._uniformDeclaration += `#endif\r\n`;
  390. }
  391. // well known
  392. let hints = state.sharedData.hints;
  393. if (this._systemValue !== null && this._systemValue !== undefined) {
  394. switch (this._systemValue) {
  395. case NodeMaterialSystemValues.WorldView:
  396. hints.needWorldViewMatrix = true;
  397. break;
  398. case NodeMaterialSystemValues.WorldViewProjection:
  399. hints.needWorldViewProjectionMatrix = true;
  400. break;
  401. }
  402. } else {
  403. if (this._animationType !== AnimatedInputBlockTypes.None) {
  404. state.sharedData.animatedInputs.push(this);
  405. }
  406. }
  407. return;
  408. }
  409. // Attribute
  410. if (this.isAttribute) {
  411. this.associatedVariableName = remapAttributeName[this.name] ?? this.name;
  412. if (this.target === NodeMaterialBlockTargets.Vertex && state._vertexState) { // Attribute for fragment need to be carried over by varyings
  413. if (attributeInFragmentOnly[this.name]) {
  414. if (attributeAsUniform[this.name]) {
  415. state._emitUniformFromString(this.associatedVariableName, state._getGLType(this.type), define);
  416. } else {
  417. state._emitVaryingFromString(this.associatedVariableName, state._getGLType(this.type), define);
  418. }
  419. } else {
  420. this._emit(state._vertexState, define);
  421. }
  422. return;
  423. }
  424. if (state.attributes.indexOf(this.associatedVariableName) !== -1) {
  425. return;
  426. }
  427. state.attributes.push(this.associatedVariableName);
  428. if (attributeInFragmentOnly[this.name]) {
  429. if (attributeAsUniform[this.name]) {
  430. state._emitUniformFromString(this.associatedVariableName, state._getGLType(this.type), define);
  431. } else {
  432. state._emitVaryingFromString(this.associatedVariableName, state._getGLType(this.type), define);
  433. }
  434. } else {
  435. if (define) {
  436. state._attributeDeclaration += this._emitDefine(define);
  437. }
  438. state._attributeDeclaration += `attribute ${state._getGLType(this.type)} ${this.associatedVariableName};\r\n`;
  439. if (define) {
  440. state._attributeDeclaration += `#endif\r\n`;
  441. }
  442. }
  443. }
  444. }
  445. /** @hidden */
  446. public _transmitWorld(effect: Effect, world: Matrix, worldView: Matrix, worldViewProjection: Matrix) {
  447. if (!this._systemValue) {
  448. return;
  449. }
  450. let variableName = this.associatedVariableName;
  451. switch (this._systemValue) {
  452. case NodeMaterialSystemValues.World:
  453. effect.setMatrix(variableName, world);
  454. break;
  455. case NodeMaterialSystemValues.WorldView:
  456. effect.setMatrix(variableName, worldView);
  457. break;
  458. case NodeMaterialSystemValues.WorldViewProjection:
  459. effect.setMatrix(variableName, worldViewProjection);
  460. break;
  461. }
  462. }
  463. /** @hidden */
  464. public _transmit(effect: Effect, scene: Scene) {
  465. if (this.isAttribute) {
  466. return;
  467. }
  468. let variableName = this.associatedVariableName;
  469. if (this._systemValue) {
  470. switch (this._systemValue) {
  471. case NodeMaterialSystemValues.World:
  472. case NodeMaterialSystemValues.WorldView:
  473. case NodeMaterialSystemValues.WorldViewProjection:
  474. return;
  475. case NodeMaterialSystemValues.View:
  476. effect.setMatrix(variableName, scene.getViewMatrix());
  477. break;
  478. case NodeMaterialSystemValues.Projection:
  479. effect.setMatrix(variableName, scene.getProjectionMatrix());
  480. break;
  481. case NodeMaterialSystemValues.ViewProjection:
  482. effect.setMatrix(variableName, scene.getTransformMatrix());
  483. break;
  484. case NodeMaterialSystemValues.CameraPosition:
  485. MaterialHelper.BindEyePosition(effect, scene, variableName);
  486. break;
  487. case NodeMaterialSystemValues.FogColor:
  488. effect.setColor3(variableName, scene.fogColor);
  489. break;
  490. case NodeMaterialSystemValues.DeltaTime:
  491. effect.setFloat(variableName, scene.deltaTime / 1000.0);
  492. }
  493. return;
  494. }
  495. let value = this._valueCallback ? this._valueCallback() : this._storedValue;
  496. if (value === null) {
  497. return;
  498. }
  499. switch (this.type) {
  500. case NodeMaterialBlockConnectionPointTypes.Float:
  501. effect.setFloat(variableName, value);
  502. break;
  503. case NodeMaterialBlockConnectionPointTypes.Int:
  504. effect.setInt(variableName, value);
  505. break;
  506. case NodeMaterialBlockConnectionPointTypes.Color3:
  507. effect.setColor3(variableName, value);
  508. break;
  509. case NodeMaterialBlockConnectionPointTypes.Color4:
  510. effect.setDirectColor4(variableName, value);
  511. break;
  512. case NodeMaterialBlockConnectionPointTypes.Vector2:
  513. effect.setVector2(variableName, value);
  514. break;
  515. case NodeMaterialBlockConnectionPointTypes.Vector3:
  516. effect.setVector3(variableName, value);
  517. break;
  518. case NodeMaterialBlockConnectionPointTypes.Vector4:
  519. effect.setVector4(variableName, value);
  520. break;
  521. case NodeMaterialBlockConnectionPointTypes.Matrix:
  522. effect.setMatrix(variableName, value);
  523. break;
  524. }
  525. }
  526. protected _buildBlock(state: NodeMaterialBuildState) {
  527. super._buildBlock(state);
  528. if (this.isUniform || this.isSystemValue) {
  529. state.sharedData.inputBlocks.push(this);
  530. }
  531. this._emit(state);
  532. }
  533. protected _dumpPropertiesCode() {
  534. let variableName = this._codeVariableName;
  535. if (this.isAttribute) {
  536. return `${variableName}.setAsAttribute("${this.name}");\r\n`;
  537. }
  538. if (this.isSystemValue) {
  539. return `${variableName}.setAsSystemValue(BABYLON.NodeMaterialSystemValues.${NodeMaterialSystemValues[this._systemValue!]});\r\n`;
  540. }
  541. if (this.isUniform) {
  542. const codes: string[] = [];
  543. let valueString = "";
  544. switch (this.type) {
  545. case NodeMaterialBlockConnectionPointTypes.Float:
  546. valueString = `${this.value}`;
  547. break;
  548. case NodeMaterialBlockConnectionPointTypes.Vector2:
  549. valueString = `new BABYLON.Vector2(${this.value.x}, ${this.value.y})`;
  550. break;
  551. case NodeMaterialBlockConnectionPointTypes.Vector3:
  552. valueString = `new BABYLON.Vector3(${this.value.x}, ${this.value.y}, ${this.value.z})`;
  553. break;
  554. case NodeMaterialBlockConnectionPointTypes.Vector4:
  555. valueString = `new BABYLON.Vector4(${this.value.x}, ${this.value.y}, ${this.value.z}, ${this.value.w})`;
  556. break;
  557. case NodeMaterialBlockConnectionPointTypes.Color3:
  558. valueString = `new BABYLON.Color3(${this.value.r}, ${this.value.g}, ${this.value.b})`;
  559. break;
  560. case NodeMaterialBlockConnectionPointTypes.Color4:
  561. valueString = `new BABYLON.Color4(${this.value.r}, ${this.value.g}, ${this.value.b}, ${this.value.a})`;
  562. break;
  563. case NodeMaterialBlockConnectionPointTypes.Matrix:
  564. valueString = `BABYLON.Matrix.FromArray([${(this.value as Matrix).m}])`;
  565. break;
  566. }
  567. // Common Property "Value"
  568. codes.push(`${variableName}.value = ${valueString}`);
  569. // Float-Value-Specific Properties
  570. if (this.type === NodeMaterialBlockConnectionPointTypes.Float) {
  571. codes.push(
  572. `${variableName}.min = ${this.min}`,
  573. `${variableName}.max = ${this.max}`,
  574. `${variableName}.isBoolean = ${this.isBoolean}`,
  575. `${variableName}.matrixMode = ${this.matrixMode}`,
  576. `${variableName}.animationType = BABYLON.AnimatedInputBlockTypes.${AnimatedInputBlockTypes[this.animationType]}`
  577. );
  578. }
  579. // Common Property "Type"
  580. codes.push(
  581. `${variableName}.isConstant = ${this.isConstant}`,
  582. `${variableName}.visibleInInspector = ${this.visibleInInspector}`
  583. );
  584. codes.push('');
  585. return codes.join(';\r\n');
  586. }
  587. return "";
  588. }
  589. public dispose() {
  590. this.onValueChangedObservable.clear();
  591. super.dispose();
  592. }
  593. public serialize(): any {
  594. let serializationObject = super.serialize();
  595. serializationObject.type = this.type;
  596. serializationObject.mode = this._mode;
  597. serializationObject.systemValue = this._systemValue;
  598. serializationObject.animationType = this._animationType;
  599. serializationObject.visibleInInspector = this.visibleInInspector;
  600. serializationObject.min = this.min;
  601. serializationObject.max = this.max;
  602. serializationObject.isBoolean = this.isBoolean;
  603. serializationObject.matrixMode = this.matrixMode;
  604. serializationObject.isConstant = this.isConstant;
  605. serializationObject.groupInInspector = this.groupInInspector;
  606. if (this._storedValue != null && this._mode === NodeMaterialBlockConnectionPointMode.Uniform) {
  607. if (this._storedValue.asArray) {
  608. serializationObject.valueType = "BABYLON." + this._storedValue.getClassName();
  609. serializationObject.value = this._storedValue.asArray();
  610. } else {
  611. serializationObject.valueType = "number";
  612. serializationObject.value = this._storedValue;
  613. }
  614. }
  615. return serializationObject;
  616. }
  617. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  618. super._deserialize(serializationObject, scene, rootUrl);
  619. this._type = serializationObject.type;
  620. this._mode = serializationObject.mode;
  621. this._systemValue = serializationObject.systemValue || serializationObject.wellKnownValue;
  622. this._animationType = serializationObject.animationType;
  623. this.visibleInInspector = serializationObject.visibleInInspector;
  624. this.min = serializationObject.min || 0;
  625. this.max = serializationObject.max || 0;
  626. this.isBoolean = !!serializationObject.isBoolean;
  627. this.matrixMode = serializationObject.matrixMode || 0;
  628. this.isConstant = !!serializationObject.isConstant;
  629. this.groupInInspector = serializationObject.groupInInspector || "";
  630. if (!serializationObject.valueType) {
  631. return;
  632. }
  633. if (serializationObject.valueType === "number") {
  634. this._storedValue = serializationObject.value;
  635. } else {
  636. let valueType = _TypeStore.GetClass(serializationObject.valueType);
  637. if (valueType) {
  638. this._storedValue = valueType.FromArray(serializationObject.value);
  639. }
  640. }
  641. }
  642. }
  643. _TypeStore.RegisteredTypes["BABYLON.InputBlock"] = InputBlock;