reflectionTextureBlock.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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("rgba", NodeMaterialBlockConnectionPointTypes.Color4, NodeMaterialBlockTargets.Fragment);
  28. this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  29. this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  30. this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  31. this.registerOutput("a", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  32. this._inputs[0].acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector4);
  33. }
  34. /**
  35. * Gets the current class name
  36. * @returns the class name
  37. */
  38. public getClassName() {
  39. return "ReflectionTextureBlock";
  40. }
  41. /**
  42. * Gets the world position input component
  43. */
  44. public get position(): NodeMaterialConnectionPoint {
  45. return this._inputs[0];
  46. }
  47. /**
  48. * Gets the world position input component
  49. */
  50. public get worldPosition(): NodeMaterialConnectionPoint {
  51. return this._inputs[1];
  52. }
  53. /**
  54. * Gets the world normal input component
  55. */
  56. public get worldNormal(): NodeMaterialConnectionPoint {
  57. return this._inputs[2];
  58. }
  59. /**
  60. * Gets the world input component
  61. */
  62. public get world(): NodeMaterialConnectionPoint {
  63. return this._inputs[3];
  64. }
  65. /**
  66. * Gets the camera (or eye) position component
  67. */
  68. public get cameraPosition(): NodeMaterialConnectionPoint {
  69. return this._inputs[4];
  70. }
  71. /**
  72. * Gets the view input component
  73. */
  74. public get view(): NodeMaterialConnectionPoint {
  75. return this._inputs[5];
  76. }
  77. /**
  78. * Gets the rgb output component
  79. */
  80. public get rgb(): NodeMaterialConnectionPoint {
  81. return this._outputs[0];
  82. }
  83. /**
  84. * Gets the rgba output component
  85. */
  86. public get rgba(): NodeMaterialConnectionPoint {
  87. return this._outputs[1];
  88. }
  89. /**
  90. * Gets the r output component
  91. */
  92. public get r(): NodeMaterialConnectionPoint {
  93. return this._outputs[2];
  94. }
  95. /**
  96. * Gets the g output component
  97. */
  98. public get g(): NodeMaterialConnectionPoint {
  99. return this._outputs[3];
  100. }
  101. /**
  102. * Gets the b output component
  103. */
  104. public get b(): NodeMaterialConnectionPoint {
  105. return this._outputs[4];
  106. }
  107. /**
  108. * Gets the a output component
  109. */
  110. public get a(): NodeMaterialConnectionPoint {
  111. return this._outputs[5];
  112. }
  113. public autoConfigure(material: NodeMaterial) {
  114. super.autoConfigure(material);
  115. if (!this.cameraPosition.isConnected) {
  116. let cameraPositionInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.CameraPosition);
  117. if (!cameraPositionInput) {
  118. cameraPositionInput = new InputBlock("cameraPosition");
  119. cameraPositionInput.setAsSystemValue(NodeMaterialSystemValues.CameraPosition);
  120. }
  121. cameraPositionInput.output.connectTo(this.cameraPosition);
  122. }
  123. }
  124. protected _buildBlock(state: NodeMaterialBuildState) {
  125. super._buildBlock(state);
  126. if (!this.texture) {
  127. state.compilationString += this.writeOutputs(state, "vec3(0.)");
  128. return this;
  129. }
  130. if (state.target !== NodeMaterialBlockTargets.Fragment) {
  131. state.compilationString += this.handleVertexSide(state);
  132. return this;
  133. }
  134. this.handleFragmentSideInits(state);
  135. const normalWUnit = state._getFreeVariableName("normalWUnit");
  136. state.compilationString += `vec4 ${normalWUnit} = normalize(${this.worldNormal.associatedVariableName});\r\n`;
  137. state.compilationString += this.handleFragmentSideCodeReflectionCoords(normalWUnit);
  138. state.compilationString += this.handleFragmentSideCodeReflectionColor(undefined, "");
  139. state.compilationString += this.writeOutputs(state, this._reflectionColorName);
  140. return this;
  141. }
  142. }
  143. _TypeStore.RegisteredTypes["BABYLON.ReflectionTextureBlock"] = ReflectionTextureBlock;