replaceColorBlock.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { NodeMaterialBlock } from '../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
  4. import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
  5. import { NodeMaterialBlockTargets } from '../Enums/nodeMaterialBlockTargets';
  6. import { _TypeStore } from '../../../Misc/typeStore';
  7. /**
  8. * Block used to replace a color by another one
  9. */
  10. export class ReplaceColorBlock extends NodeMaterialBlock {
  11. /**
  12. * Creates a new ReplaceColorBlock
  13. * @param name defines the block name
  14. */
  15. public constructor(name: string) {
  16. super(name, NodeMaterialBlockTargets.Neutral);
  17. this.registerInput("value", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  18. this.registerInput("reference", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  19. this.registerInput("distance", NodeMaterialBlockConnectionPointTypes.Float);
  20. this.registerInput("replacement", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  21. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
  22. this._outputs[0]._typeConnectionSource = this._inputs[0];
  23. this._linkConnectionTypes(0, 1);
  24. this._linkConnectionTypes(0, 3);
  25. this._inputs[0].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Float);
  26. this._inputs[0].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Matrix);
  27. this._inputs[1].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Float);
  28. this._inputs[1].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Matrix);
  29. this._inputs[3].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Float);
  30. this._inputs[3].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Matrix);
  31. }
  32. /**
  33. * Gets the current class name
  34. * @returns the class name
  35. */
  36. public getClassName() {
  37. return "ReplaceColorBlock";
  38. }
  39. /**
  40. * Gets the value input component
  41. */
  42. public get value(): NodeMaterialConnectionPoint {
  43. return this._inputs[0];
  44. }
  45. /**
  46. * Gets the reference input component
  47. */
  48. public get reference(): NodeMaterialConnectionPoint {
  49. return this._inputs[1];
  50. }
  51. /**
  52. * Gets the distance input component
  53. */
  54. public get distance(): NodeMaterialConnectionPoint {
  55. return this._inputs[2];
  56. }
  57. /**
  58. * Gets the replacement input component
  59. */
  60. public get replacement(): NodeMaterialConnectionPoint {
  61. return this._inputs[3];
  62. }
  63. /**
  64. * Gets the output component
  65. */
  66. public get output(): NodeMaterialConnectionPoint {
  67. return this._outputs[0];
  68. }
  69. protected _buildBlock(state: NodeMaterialBuildState) {
  70. super._buildBlock(state);
  71. let output = this._outputs[0];
  72. state.compilationString += this._declareOutput(output, state) + `;\r\n`;
  73. state.compilationString += `if (length(${this.value.associatedVariableName} - ${this.reference.associatedVariableName}) < ${this.distance.associatedVariableName}) {\r\n`;
  74. state.compilationString += `${output.associatedVariableName} = ${this.replacement.associatedVariableName};\r\n`;
  75. state.compilationString += `} else {\r\n`;
  76. state.compilationString += `${output.associatedVariableName} = ${this.value.associatedVariableName};\r\n`;
  77. state.compilationString += `}\r\n`;
  78. return this;
  79. }
  80. }
  81. _TypeStore.RegisteredTypes["BABYLON.ReplaceColorBlock"] = ReplaceColorBlock;