fragmentOutputBlock.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. import { Scene } from '../../../../scene';
  8. import { AbstractMesh } from '../../../../Meshes/abstractMesh';
  9. import { NodeMaterialDefines } from '../../nodeMaterial';
  10. import { editableInPropertyPage, PropertyTypeForEdition } from "../../nodeMaterialDecorator";
  11. declare type NodeMaterial = import("../../nodeMaterial").NodeMaterial;
  12. /**
  13. * Block used to output the final color
  14. */
  15. export class FragmentOutputBlock extends NodeMaterialBlock {
  16. private _linearDefineName: string;
  17. private _gammaDefineName: string;
  18. /**
  19. * Create a new FragmentOutputBlock
  20. * @param name defines the block name
  21. */
  22. public constructor(name: string) {
  23. super(name, NodeMaterialBlockTargets.Fragment, true);
  24. this.registerInput("rgba", NodeMaterialBlockConnectionPointTypes.Color4, true);
  25. this.registerInput("rgb", NodeMaterialBlockConnectionPointTypes.Color3, true);
  26. this.registerInput("a", NodeMaterialBlockConnectionPointTypes.Float, true);
  27. this.rgb.acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Float);
  28. }
  29. /** Gets or sets a boolean indicating if content needs to be converted to gamma space */
  30. @editableInPropertyPage("Convert to gamma space", PropertyTypeForEdition.Boolean, "PROPERTIES", { "notifiers": { "update": true }})
  31. public convertToGammaSpace = false;
  32. /** Gets or sets a boolean indicating if content needs to be converted to linear space */
  33. @editableInPropertyPage("Convert to linear space", PropertyTypeForEdition.Boolean, "PROPERTIES", { "notifiers": { "update": true }})
  34. public convertToLinearSpace = false;
  35. /**
  36. * Gets the current class name
  37. * @returns the class name
  38. */
  39. public getClassName() {
  40. return "FragmentOutputBlock";
  41. }
  42. /**
  43. * Gets the rgba input component
  44. */
  45. public get rgba(): NodeMaterialConnectionPoint {
  46. return this._inputs[0];
  47. }
  48. /**
  49. * Gets the rgb input component
  50. */
  51. public get rgb(): NodeMaterialConnectionPoint {
  52. return this._inputs[1];
  53. }
  54. /**
  55. * Gets the a input component
  56. */
  57. public get a(): NodeMaterialConnectionPoint {
  58. return this._inputs[2];
  59. }
  60. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  61. defines.setValue(this._linearDefineName, this.convertToLinearSpace, true);
  62. defines.setValue(this._gammaDefineName, this.convertToGammaSpace, true);
  63. }
  64. protected _buildBlock(state: NodeMaterialBuildState) {
  65. super._buildBlock(state);
  66. let rgba = this.rgba;
  67. let rgb = this.rgb;
  68. let a = this.a;
  69. state.sharedData.hints.needAlphaBlending = rgba.isConnected || a.isConnected;
  70. state.sharedData.blocksWithDefines.push(this);
  71. this._linearDefineName = state._getFreeDefineName("CONVERTTOLINEAR");
  72. this._gammaDefineName = state._getFreeDefineName("CONVERTTOGAMMA");
  73. let comments = `//${this.name}`;
  74. state._emitFunctionFromInclude("helperFunctions", comments);
  75. if (rgba.connectedPoint) {
  76. if (a.isConnected) {
  77. state.compilationString += `gl_FragColor = vec4(${rgba.associatedVariableName}.rgb, ${a.associatedVariableName});\r\n`;
  78. } else {
  79. state.compilationString += `gl_FragColor = ${rgba.associatedVariableName};\r\n`;
  80. }
  81. } else if (rgb.connectedPoint) {
  82. let aValue = "1.0";
  83. if (a.connectedPoint) {
  84. aValue = a.associatedVariableName;
  85. }
  86. if (rgb.connectedPoint.type === NodeMaterialBlockConnectionPointTypes.Float) {
  87. state.compilationString += `gl_FragColor = vec4(${rgb.associatedVariableName}, ${rgb.associatedVariableName}, ${rgb.associatedVariableName}, ${aValue});\r\n`;
  88. } else {
  89. state.compilationString += `gl_FragColor = vec4(${rgb.associatedVariableName}, ${aValue});\r\n`;
  90. }
  91. } else {
  92. state.sharedData.checks.notConnectedNonOptionalInputs.push(rgba);
  93. }
  94. state.compilationString += `#ifdef ${this._linearDefineName}\r\n`;
  95. state.compilationString += `gl_FragColor = toLinearSpace(gl_FragColor);\r\n`;
  96. state.compilationString += `#endif\r\n`;
  97. state.compilationString += `#ifdef ${this._gammaDefineName}\r\n`;
  98. state.compilationString += `gl_FragColor = toGammaSpace(gl_FragColor);\r\n`;
  99. state.compilationString += `#endif\r\n`;
  100. return this;
  101. }
  102. protected _dumpPropertiesCode() {
  103. var codeString = "";
  104. codeString += `${this._codeVariableName}.convertToGammaSpace = ${this.convertToGammaSpace};\r\n`;
  105. codeString += `${this._codeVariableName}.convertToLinearSpace = ${this.convertToLinearSpace};\r\n`;
  106. return codeString;
  107. }
  108. public serialize(): any {
  109. let serializationObject = super.serialize();
  110. serializationObject.convertToGammaSpace = this.convertToGammaSpace;
  111. serializationObject.convertToLinearSpace = this.convertToLinearSpace;
  112. return serializationObject;
  113. }
  114. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  115. super._deserialize(serializationObject, scene, rootUrl);
  116. this.convertToGammaSpace = serializationObject.convertToGammaSpace;
  117. this.convertToLinearSpace = serializationObject.convertToLinearSpace;
  118. }
  119. }
  120. _TypeStore.RegisteredTypes["BABYLON.FragmentOutputBlock"] = FragmentOutputBlock;