posterizeBlock.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 posterize a value
  9. * @see https://en.wikipedia.org/wiki/Posterization
  10. */
  11. export class PosterizeBlock extends NodeMaterialBlock {
  12. /**
  13. * Creates a new PosterizeBlock
  14. * @param name defines the block name
  15. */
  16. public constructor(name: string) {
  17. super(name, NodeMaterialBlockTargets.Neutral);
  18. this.registerInput("value", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  19. this.registerInput("steps", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  20. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
  21. this._outputs[0]._typeConnectionSource = this._inputs[0];
  22. this._linkConnectionTypes(0, 1);
  23. this._inputs[0].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Matrix);
  24. this._inputs[1].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Matrix);
  25. }
  26. /**
  27. * Gets the current class name
  28. * @returns the class name
  29. */
  30. public getClassName() {
  31. return "PosterizeBlock";
  32. }
  33. /**
  34. * Gets the value input component
  35. */
  36. public get value(): NodeMaterialConnectionPoint {
  37. return this._inputs[0];
  38. }
  39. /**
  40. * Gets the steps input component
  41. */
  42. public get steps(): NodeMaterialConnectionPoint {
  43. return this._inputs[1];
  44. }
  45. /**
  46. * Gets the output component
  47. */
  48. public get output(): NodeMaterialConnectionPoint {
  49. return this._outputs[0];
  50. }
  51. protected _buildBlock(state: NodeMaterialBuildState) {
  52. super._buildBlock(state);
  53. let output = this._outputs[0];
  54. state.compilationString += this._declareOutput(output, state) + ` = floor(${this.value.associatedVariableName} / (1.0 / ${this.steps.associatedVariableName})) * (1.0 / ${this.steps.associatedVariableName});\r\n`;
  55. return this;
  56. }
  57. }
  58. _TypeStore.RegisteredTypes["BABYLON.PosterizeBlock"] = PosterizeBlock;