fragmentOutputBlock.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  4. import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
  5. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  6. import { _TypeStore } from '../../../../Misc/typeStore';
  7. /**
  8. * Block used to output the final color
  9. */
  10. export class FragmentOutputBlock extends NodeMaterialBlock {
  11. /**
  12. * Create a new FragmentOutputBlock
  13. * @param name defines the block name
  14. */
  15. public constructor(name: string) {
  16. super(name, NodeMaterialBlockTargets.Fragment, true);
  17. this.registerInput("rgba", NodeMaterialBlockConnectionPointTypes.Color4, true);
  18. this.registerInput("rgb", NodeMaterialBlockConnectionPointTypes.Color3, true);
  19. this.registerInput("a", NodeMaterialBlockConnectionPointTypes.Float, true);
  20. this.rgb.acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Float);
  21. }
  22. /**
  23. * Gets the current class name
  24. * @returns the class name
  25. */
  26. public getClassName() {
  27. return "FragmentOutputBlock";
  28. }
  29. /**
  30. * Gets the rgba input component
  31. */
  32. public get rgba(): NodeMaterialConnectionPoint {
  33. return this._inputs[0];
  34. }
  35. /**
  36. * Gets the rgb input component
  37. */
  38. public get rgb(): NodeMaterialConnectionPoint {
  39. return this._inputs[1];
  40. }
  41. /**
  42. * Gets the a input component
  43. */
  44. public get a(): NodeMaterialConnectionPoint {
  45. return this._inputs[2];
  46. }
  47. protected _buildBlock(state: NodeMaterialBuildState) {
  48. super._buildBlock(state);
  49. let rgba = this.rgba;
  50. let rgb = this.rgb;
  51. let a = this.a;
  52. state.sharedData.hints.needAlphaBlending = rgba.isConnected || a.isConnected;
  53. if (rgba.connectedPoint) {
  54. state.compilationString += `gl_FragColor = ${rgba.associatedVariableName};\r\n`;
  55. } else if (rgb.connectedPoint) {
  56. if (a.connectedPoint) {
  57. state.compilationString += `gl_FragColor = vec4(${rgb.associatedVariableName}, ${a.associatedVariableName});\r\n`;
  58. } else {
  59. if (rgb.connectedPoint.type === NodeMaterialBlockConnectionPointTypes.Float) {
  60. state.compilationString += `gl_FragColor = vec4(${rgb.associatedVariableName}, ${rgb.associatedVariableName}, ${rgb.associatedVariableName}, 1.0);\r\n`;
  61. } else {
  62. state.compilationString += `gl_FragColor = vec4(${rgb.associatedVariableName}, 1.0);\r\n`;
  63. }
  64. }
  65. } else {
  66. state.sharedData.checks.notConnectedNonOptionalInputs.push(rgba);
  67. }
  68. return this;
  69. }
  70. }
  71. _TypeStore.RegisteredTypes["BABYLON.FragmentOutputBlock"] = FragmentOutputBlock;