fogBlock.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  4. import { NodeMaterialSystemValues } from '../../nodeMaterialSystemValues';
  5. import { NodeMaterialBlockTargets } from '../../nodeMaterialBlockTargets';
  6. import { Mesh } from '../../../../Meshes/mesh';
  7. import { Effect } from '../../../effect';
  8. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  9. import { AbstractMesh } from '../../../../Meshes/abstractMesh';
  10. import { MaterialHelper } from '../../../materialHelper';
  11. import { NodeMaterial, NodeMaterialDefines } from '../../nodeMaterial';
  12. import { InputBlock } from '../Input/inputBlock';
  13. import { _TypeStore } from '../../../../Misc/typeStore';
  14. /**
  15. * Block used to add support for scene fog
  16. */
  17. export class FogBlock extends NodeMaterialBlock {
  18. private _fogDistanceName: string;
  19. private _fogParameters: string;
  20. /**
  21. * Create a new FogBlock
  22. * @param name defines the block name
  23. */
  24. public constructor(name: string) {
  25. super(name, NodeMaterialBlockTargets.VertexAndFragment, true);
  26. // Vertex
  27. this.registerInput("worldPosition", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Vertex);
  28. this.registerInput("view", NodeMaterialBlockConnectionPointTypes.Matrix, false, NodeMaterialBlockTargets.Vertex);
  29. // Fragment
  30. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.Color3, false, NodeMaterialBlockTargets.Fragment);
  31. this.registerInput("fogColor", NodeMaterialBlockConnectionPointTypes.Color3, false, NodeMaterialBlockTargets.Fragment);
  32. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  33. this.input.acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color4);
  34. this.fogColor.acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Color4);
  35. }
  36. /**
  37. * Gets the current class name
  38. * @returns the class name
  39. */
  40. public getClassName() {
  41. return "FogBlock";
  42. }
  43. /**
  44. * Gets the world position input component
  45. */
  46. public get worldPosition(): NodeMaterialConnectionPoint {
  47. return this._inputs[0];
  48. }
  49. /**
  50. * Gets the view input component
  51. */
  52. public get view(): NodeMaterialConnectionPoint {
  53. return this._inputs[1];
  54. }
  55. /**
  56. * Gets the color input component
  57. */
  58. public get input(): NodeMaterialConnectionPoint {
  59. return this._inputs[2];
  60. }
  61. /**
  62. * Gets the fog color input component
  63. */
  64. public get fogColor(): NodeMaterialConnectionPoint {
  65. return this._inputs[3];
  66. }
  67. /**
  68. * Gets the output component
  69. */
  70. public get output(): NodeMaterialConnectionPoint {
  71. return this._outputs[0];
  72. }
  73. public autoConfigure(material: NodeMaterial) {
  74. if (!this.view.isConnected) {
  75. let viewInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.View);
  76. if (!viewInput) {
  77. viewInput = new InputBlock("view");
  78. viewInput.setAsSystemValue(NodeMaterialSystemValues.View);
  79. }
  80. viewInput.output.connectTo(this.view);
  81. }
  82. if (!this.fogColor.isConnected) {
  83. let fogColorInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.FogColor);
  84. if (!fogColorInput) {
  85. fogColorInput = new InputBlock("fogColor", undefined, NodeMaterialBlockConnectionPointTypes.Color3);
  86. fogColorInput.setAsSystemValue(NodeMaterialSystemValues.FogColor);
  87. }
  88. fogColorInput.output.connectTo(this.fogColor);
  89. }
  90. }
  91. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  92. let scene = mesh.getScene();
  93. defines.setValue("FOG", nodeMaterial.fogEnabled && MaterialHelper.GetFogState(mesh, scene));
  94. }
  95. public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {
  96. if (!mesh) {
  97. return;
  98. }
  99. const scene = mesh.getScene();
  100. effect.setFloat4(this._fogParameters, scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  101. }
  102. protected _buildBlock(state: NodeMaterialBuildState) {
  103. super._buildBlock(state);
  104. if (state.target === NodeMaterialBlockTargets.Fragment) {
  105. state.sharedData.blocksWithDefines.push(this);
  106. state.sharedData.bindableBlocks.push(this);
  107. state._emitFunctionFromInclude("fogFragmentDeclaration", `//${this.name}`, {
  108. removeUniforms: true,
  109. removeVaryings: true,
  110. removeIfDef: false,
  111. replaceStrings: [{ search: /float CalcFogFactor\(\)/, replace: "float CalcFogFactor(vec3 vFogDistance, vec4 vFogInfos)" }]
  112. });
  113. let tempFogVariablename = state._getFreeVariableName("fog");
  114. let color = this.input;
  115. let fogColor = this.fogColor;
  116. this._fogParameters = state._getFreeVariableName("fogParameters");
  117. let output = this._outputs[0];
  118. state._emitUniformFromString(this._fogParameters, "vec4");
  119. state.compilationString += `#ifdef FOG\r\n`;
  120. state.compilationString += `float ${tempFogVariablename} = CalcFogFactor(${this._fogDistanceName}, ${this._fogParameters});\r\n`;
  121. state.compilationString += this._declareOutput(output, state) + ` = ${tempFogVariablename} * ${color.associatedVariableName}.rgb + (1.0 - ${tempFogVariablename}) * ${fogColor.associatedVariableName}.rgb;\r\n`;
  122. state.compilationString += `#else\r\n${this._declareOutput(output, state)} = ${color.associatedVariableName}.rgb;\r\n`;
  123. state.compilationString += `#endif\r\n`;
  124. } else {
  125. let worldPos = this.worldPosition;
  126. let view = this.view;
  127. this._fogDistanceName = state._getFreeVariableName("vFogDistance");
  128. state._emitVaryingFromString(this._fogDistanceName, "vec3");
  129. state.compilationString += `${this._fogDistanceName} = (${view.associatedVariableName} * ${worldPos.associatedVariableName}).xyz;\r\n`;
  130. }
  131. return this;
  132. }
  133. }
  134. _TypeStore.RegisteredTypes["BABYLON.FogBlock"] = FogBlock;