reflectionTextureBlock.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
  2. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  3. import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
  4. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  5. import { NodeMaterial } from '../../nodeMaterial';
  6. import { _TypeStore } from '../../../../Misc/typeStore';
  7. import { InputBlock } from '../Input/inputBlock';
  8. import { NodeMaterialSystemValues } from '../../Enums/nodeMaterialSystemValues';
  9. import { ReflectionTextureBaseBlock } from './reflectionTextureBaseBlock';
  10. /**
  11. * Block used to read a reflection texture from a sampler
  12. */
  13. export class ReflectionTextureBlock extends ReflectionTextureBaseBlock {
  14. /**
  15. * Create a new ReflectionTextureBlock
  16. * @param name defines the block name
  17. */
  18. public constructor(name: string) {
  19. super(name);
  20. this.registerInput("position", NodeMaterialBlockConnectionPointTypes.Vector3, false, NodeMaterialBlockTargets.Vertex);
  21. this.registerInput("worldPosition", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Vertex);
  22. this.registerInput("worldNormal", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Fragment); // Flagging as fragment as the normal can be changed by fragment code
  23. this.registerInput("world", NodeMaterialBlockConnectionPointTypes.Matrix, false, NodeMaterialBlockTargets.Vertex);
  24. this.registerInput("cameraPosition", NodeMaterialBlockConnectionPointTypes.Vector3, false, NodeMaterialBlockTargets.Fragment);
  25. this.registerInput("view", NodeMaterialBlockConnectionPointTypes.Matrix, false, NodeMaterialBlockTargets.Fragment);
  26. this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  27. this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  28. this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  29. this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  30. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector4);
  31. }
  32. /**
  33. * Gets the current class name
  34. * @returns the class name
  35. */
  36. public getClassName() {
  37. return "ReflectionTextureBlock";
  38. }
  39. /**
  40. * Gets the world position input component
  41. */
  42. public get position(): NodeMaterialConnectionPoint {
  43. return this._inputs[0];
  44. }
  45. /**
  46. * Gets the world position input component
  47. */
  48. public get worldPosition(): NodeMaterialConnectionPoint {
  49. return this._inputs[1];
  50. }
  51. /**
  52. * Gets the world normal input component
  53. */
  54. public get worldNormal(): NodeMaterialConnectionPoint {
  55. return this._inputs[2];
  56. }
  57. /**
  58. * Gets the world input component
  59. */
  60. public get world(): NodeMaterialConnectionPoint {
  61. return this._inputs[3];
  62. }
  63. /**
  64. * Gets the camera (or eye) position component
  65. */
  66. public get cameraPosition(): NodeMaterialConnectionPoint {
  67. return this._inputs[4];
  68. }
  69. /**
  70. * Gets the view input component
  71. */
  72. public get view(): NodeMaterialConnectionPoint {
  73. return this._inputs[5];
  74. }
  75. /**
  76. * Gets the rgb output component
  77. */
  78. public get rgb(): NodeMaterialConnectionPoint {
  79. return this._outputs[0];
  80. }
  81. /**
  82. * Gets the r output component
  83. */
  84. public get r(): NodeMaterialConnectionPoint {
  85. return this._outputs[1];
  86. }
  87. /**
  88. * Gets the g output component
  89. */
  90. public get g(): NodeMaterialConnectionPoint {
  91. return this._outputs[2];
  92. }
  93. /**
  94. * Gets the b output component
  95. */
  96. public get b(): NodeMaterialConnectionPoint {
  97. return this._outputs[3];
  98. }
  99. public autoConfigure(material: NodeMaterial) {
  100. super.autoConfigure(material);
  101. if (!this.cameraPosition.isConnected) {
  102. let cameraPositionInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.CameraPosition);
  103. if (!cameraPositionInput) {
  104. cameraPositionInput = new InputBlock("cameraPosition");
  105. cameraPositionInput.setAsSystemValue(NodeMaterialSystemValues.CameraPosition);
  106. }
  107. cameraPositionInput.output.connectTo(this.cameraPosition);
  108. }
  109. }
  110. protected _buildBlock(state: NodeMaterialBuildState) {
  111. super._buildBlock(state);
  112. if (!this.texture) {
  113. state.compilationString += this.writeOutputs(state, "vec3(0.)");
  114. return this;
  115. }
  116. if (state.target !== NodeMaterialBlockTargets.Fragment) {
  117. state.compilationString += this.handleVertexSide(state);
  118. return this;
  119. }
  120. this.handleFragmentSideInits(state);
  121. const normalWUnit = state._getFreeVariableName("normalWUnit");
  122. state.compilationString += `vec4 ${normalWUnit} = normalize(${this.worldNormal.associatedVariableName});\r\n`;
  123. state.compilationString += this.handleFragmentSideCodeReflectionCoords(normalWUnit);
  124. state.compilationString += this.handleFragmentSideCodeReflectionColor();
  125. state.compilationString += this.writeOutputs(state, this._reflectionColorName);
  126. return this;
  127. }
  128. }
  129. _TypeStore.RegisteredTypes["BABYLON.ReflectionTextureBlock"] = ReflectionTextureBlock;