reflectivityBlock.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { NodeMaterial, NodeMaterialDefines } from '../../../nodeMaterial';
  2. import { NodeMaterialBuildState } from '../../../nodeMaterialBuildState';
  3. import { NodeMaterialBlock } from '../../../nodeMaterialBlock';
  4. import { _TypeStore } from '../../../../../Misc/typeStore';
  5. import { editableInPropertyPage, PropertyTypeForEdition } from "../../../nodeMaterialDecorator";
  6. import { AbstractMesh } from '../../../../../Meshes/abstractMesh';
  7. import { NodeMaterialBlockConnectionPointTypes } from '../../../Enums/nodeMaterialBlockConnectionPointTypes';
  8. import { NodeMaterialBlockTargets } from '../../../Enums/nodeMaterialBlockTargets';
  9. import { NodeMaterialConnectionPointCustomObject } from "../../../nodeMaterialConnectionPointCustomObject";
  10. import { NodeMaterialConnectionPoint, NodeMaterialConnectionPointDirection } from '../../../nodeMaterialBlockConnectionPoint';
  11. export class ReflectivityBlock extends NodeMaterialBlock {
  12. @editableInPropertyPage("AO from red channel", PropertyTypeForEdition.Boolean, "METALLIC WORKFLOW")
  13. public useAmbientOcclusionFromMetallicTextureRed: boolean = false;
  14. @editableInPropertyPage("Metallness from blue channel", PropertyTypeForEdition.Boolean, "METALLIC WORKFLOW")
  15. public useMetallnessFromMetallicTextureBlue: boolean = true;
  16. @editableInPropertyPage("Roughness from alpha channel", PropertyTypeForEdition.Boolean, "METALLIC WORKFLOW")
  17. public useRoughnessFromMetallicTextureAlpha: boolean = false;
  18. @editableInPropertyPage("Roughness from green channel", PropertyTypeForEdition.Boolean, "METALLIC WORKFLOW")
  19. public useRoughnessFromMetallicTextureGreen: boolean = true;
  20. @editableInPropertyPage("Metallic F0 from alpha channel", PropertyTypeForEdition.Boolean, "METALLIC WORKFLOW")
  21. public useMetallicF0FactorFromMetallicTexture: boolean = false;
  22. /**
  23. * Create a new ReflectivityBlock
  24. * @param name defines the block name
  25. */
  26. public constructor(name: string) {
  27. super(name, NodeMaterialBlockTargets.Fragment);
  28. this.registerInput("metallic", NodeMaterialBlockConnectionPointTypes.Float, false, NodeMaterialBlockTargets.Fragment);
  29. this.registerInput("roughness", NodeMaterialBlockConnectionPointTypes.Float, false, NodeMaterialBlockTargets.Fragment);
  30. this.registerInput("texture", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
  31. this.registerOutput("reflectivity", NodeMaterialBlockConnectionPointTypes.Object, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("reflectivity", this, NodeMaterialConnectionPointDirection.Output, ReflectivityBlock, "ReflectivityBlock"));
  32. }
  33. /**
  34. * Initialize the block and prepare the context for build
  35. * @param state defines the state that will be used for the build
  36. */
  37. public initialize(state: NodeMaterialBuildState) {
  38. state._excludeVariableName("baseColor");
  39. state._excludeVariableName("reflectivityOut");
  40. state._excludeVariableName("microSurface");
  41. state._excludeVariableName("roughness");
  42. }
  43. /**
  44. * Gets the current class name
  45. * @returns the class name
  46. */
  47. public getClassName() {
  48. return "ReflectivityBlock";
  49. }
  50. public get metallic(): NodeMaterialConnectionPoint {
  51. return this._inputs[0];
  52. }
  53. public get roughness(): NodeMaterialConnectionPoint {
  54. return this._inputs[1];
  55. }
  56. public get texture(): NodeMaterialConnectionPoint {
  57. return this._inputs[2];
  58. }
  59. /**
  60. * Gets the reflectivity output component
  61. */
  62. public get reflectivity(): NodeMaterialConnectionPoint {
  63. return this._outputs[0];
  64. }
  65. public getCode(aoIntensityVarName: string): string {
  66. const metalRoughTexture = this.texture.isConnected ? this.texture.connectedPoint?.associatedVariableName : null;
  67. let code = `vec3 baseColor = surfaceAlbedo;\r\nreflectivityOutParams reflectivityOut;\r\n`;
  68. code += `reflectivityBlock(
  69. vec4(${this.metallic.associatedVariableName}, ${this.roughness.associatedVariableName}, 0., 0.04),
  70. #ifdef METALLICWORKFLOW
  71. surfaceAlbedo,
  72. #endif
  73. #ifdef REFLECTIVITY
  74. vec3(0., 0., ${aoIntensityVarName}),
  75. ${metalRoughTexture},
  76. #endif
  77. #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
  78. aoOut.ambientOcclusionColor,
  79. #endif
  80. #ifdef MICROSURFACEMAP
  81. microSurfaceTexel, <== not handled!
  82. #endif
  83. reflectivityOut
  84. );
  85. float microSurface = reflectivityOut.microSurface;
  86. float roughness = reflectivityOut.roughness;
  87. #ifdef METALLICWORKFLOW
  88. surfaceAlbedo = reflectivityOut.surfaceAlbedo;
  89. #endif
  90. #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
  91. aoOut.ambientOcclusionColor = reflectivityOut.ambientOcclusionColor;
  92. #endif\r\n`;
  93. return code;
  94. }
  95. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  96. defines.setValue("REFLECTIVITY", this.texture.isConnected);
  97. defines.setValue("AOSTOREINMETALMAPRED", this.useAmbientOcclusionFromMetallicTextureRed);
  98. defines.setValue("METALLNESSSTOREINMETALMAPBLUE", this.useMetallnessFromMetallicTextureBlue);
  99. defines.setValue("ROUGHNESSSTOREINMETALMAPALPHA", this.useRoughnessFromMetallicTextureAlpha);
  100. defines.setValue("ROUGHNESSSTOREINMETALMAPGREEN", !this.useRoughnessFromMetallicTextureAlpha && this.useRoughnessFromMetallicTextureGreen);
  101. defines.setValue("METALLICF0FACTORFROMMETALLICMAP", this.useMetallicF0FactorFromMetallicTexture);
  102. }
  103. protected _buildBlock(state: NodeMaterialBuildState) {
  104. super._buildBlock(state);
  105. return this;
  106. }
  107. }
  108. _TypeStore.RegisteredTypes["BABYLON.ReflectivityBlock"] = ReflectivityBlock;