imageProcessingBlock.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 { AbstractMesh } from '../../../../Meshes/abstractMesh';
  7. import { NodeMaterial, NodeMaterialDefines } from '../../nodeMaterial';
  8. import { Effect } from '../../../effect';
  9. import { Mesh } from '../../../../Meshes/mesh';
  10. import { _TypeStore } from '../../../../Misc/typeStore';
  11. import "../../../../Shaders/ShadersInclude/helperFunctions";
  12. import "../../../../Shaders/ShadersInclude/imageProcessingDeclaration";
  13. import "../../../../Shaders/ShadersInclude/imageProcessingFunctions";
  14. /**
  15. * Block used to add image processing support to fragment shader
  16. */
  17. export class ImageProcessingBlock extends NodeMaterialBlock {
  18. /**
  19. * Create a new ImageProcessingBlock
  20. * @param name defines the block name
  21. */
  22. public constructor(name: string) {
  23. super(name, NodeMaterialBlockTargets.Fragment);
  24. this.registerInput("color", NodeMaterialBlockConnectionPointTypes.Color4);
  25. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Color4);
  26. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color3);
  27. }
  28. /**
  29. * Gets the current class name
  30. * @returns the class name
  31. */
  32. public getClassName() {
  33. return "ImageProcessingBlock";
  34. }
  35. /**
  36. * Gets the color input component
  37. */
  38. public get color(): NodeMaterialConnectionPoint {
  39. return this._inputs[0];
  40. }
  41. /**
  42. * Gets the output component
  43. */
  44. public get output(): NodeMaterialConnectionPoint {
  45. return this._outputs[0];
  46. }
  47. /**
  48. * Initialize the block and prepare the context for build
  49. * @param state defines the state that will be used for the build
  50. */
  51. public initialize(state: NodeMaterialBuildState) {
  52. state._excludeVariableName("exposureLinear");
  53. state._excludeVariableName("contrast");
  54. state._excludeVariableName("vInverseScreenSize");
  55. state._excludeVariableName("vignetteSettings1");
  56. state._excludeVariableName("vignetteSettings2");
  57. state._excludeVariableName("vCameraColorCurveNegative");
  58. state._excludeVariableName("vCameraColorCurveNeutral");
  59. state._excludeVariableName("vCameraColorCurvePositive");
  60. state._excludeVariableName("txColorTransform");
  61. state._excludeVariableName("colorTransformSettings");
  62. }
  63. public isReady(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  64. if (defines._areImageProcessingDirty && nodeMaterial.imageProcessingConfiguration) {
  65. if (!nodeMaterial.imageProcessingConfiguration.isReady()) {
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  72. if (defines._areImageProcessingDirty && nodeMaterial.imageProcessingConfiguration) {
  73. nodeMaterial.imageProcessingConfiguration.prepareDefines(defines);
  74. }
  75. }
  76. public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {
  77. if (!mesh) {
  78. return;
  79. }
  80. if (!nodeMaterial.imageProcessingConfiguration) {
  81. return;
  82. }
  83. nodeMaterial.imageProcessingConfiguration.bind(effect);
  84. }
  85. protected _buildBlock(state: NodeMaterialBuildState) {
  86. super._buildBlock(state);
  87. // Register for defines
  88. state.sharedData.blocksWithDefines.push(this);
  89. // Register for blocking
  90. state.sharedData.blockingBlocks.push(this);
  91. // Register for binding
  92. state.sharedData.bindableBlocks.push(this);
  93. // Uniforms
  94. state.uniforms.push("exposureLinear");
  95. state.uniforms.push("contrast");
  96. state.uniforms.push("vInverseScreenSize");
  97. state.uniforms.push("vignetteSettings1");
  98. state.uniforms.push("vignetteSettings2");
  99. state.uniforms.push("vCameraColorCurveNegative");
  100. state.uniforms.push("vCameraColorCurveNeutral");
  101. state.uniforms.push("vCameraColorCurvePositive");
  102. state.uniforms.push("txColorTransform");
  103. state.uniforms.push("colorTransformSettings");
  104. // Emit code
  105. let color = this.color;
  106. let output = this._outputs[0];
  107. let comments = `//${this.name}`;
  108. state._emitFunctionFromInclude("helperFunctions", comments);
  109. state._emitFunctionFromInclude("imageProcessingDeclaration", comments);
  110. state._emitFunctionFromInclude("imageProcessingFunctions", comments);
  111. if (color.connectedPoint!.type === NodeMaterialBlockConnectionPointTypes.Color4) {
  112. state.compilationString += `${this._declareOutput(output, state)} = ${color.associatedVariableName};\r\n`;
  113. } else {
  114. state.compilationString += `${this._declareOutput(output, state)} = vec4(${color.associatedVariableName}, 1.0);\r\n`;
  115. }
  116. state.compilationString += `#ifdef IMAGEPROCESSINGPOSTPROCESS\r\n`;
  117. state.compilationString += `${output.associatedVariableName}.rgb = toLinearSpace(${color.associatedVariableName}.rgb);\r\n`;
  118. state.compilationString += `#else\r\n`;
  119. state.compilationString += `#ifdef IMAGEPROCESSING\r\n`;
  120. state.compilationString += `${output.associatedVariableName}.rgb = toLinearSpace(${color.associatedVariableName}.rgb);\r\n`;
  121. state.compilationString += `${output.associatedVariableName} = applyImageProcessing(${output.associatedVariableName});\r\n`;
  122. state.compilationString += `#endif\r\n`;
  123. state.compilationString += `#endif\r\n`;
  124. return this;
  125. }
  126. }
  127. _TypeStore.RegisteredTypes["BABYLON.ImageProcessingBlock"] = ImageProcessingBlock;