alphaTestBlock.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  4. import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
  5. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  6. /**
  7. * Block used to add an alpha test in the fragment shader
  8. */
  9. export class AlphaTestBlock extends NodeMaterialBlock {
  10. /**
  11. * Gets or sets the alpha value where alpha testing happens
  12. */
  13. public alphaCutOff = 0.4;
  14. /**
  15. * Create a new AlphaTestBlock
  16. * @param name defines the block name
  17. */
  18. public constructor(name: string) {
  19. super(name, NodeMaterialBlockTargets.Fragment);
  20. this.registerInput("color", NodeMaterialBlockConnectionPointTypes.Color4);
  21. }
  22. /**
  23. * Gets the current class name
  24. * @returns the class name
  25. */
  26. public getClassName() {
  27. return "AlphaTestBlock";
  28. }
  29. /**
  30. * Gets the color input component
  31. */
  32. public get color(): NodeMaterialConnectionPoint {
  33. return this._inputs[0];
  34. }
  35. protected _buildBlock(state: NodeMaterialBuildState) {
  36. super._buildBlock(state);
  37. state.sharedData.hints.needAlphaTesting = true;
  38. state.compilationString += `if (${this.color.associatedVariableName}.a < ${this.alphaCutOff}) discard;\r\n`;
  39. return this;
  40. }
  41. }