reflectionTextureBlock.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 { BaseTexture } from '../../../Textures/baseTexture';
  7. import { AbstractMesh } from '../../../../Meshes/abstractMesh';
  8. import { NodeMaterial, NodeMaterialDefines } from '../../nodeMaterial';
  9. import { Effect } from '../../../effect';
  10. import { Mesh } from '../../../../Meshes/mesh';
  11. import { Nullable } from '../../../../types';
  12. import { _TypeStore } from '../../../../Misc/typeStore';
  13. import { Scene } from '../../../../scene';
  14. import { InputBlock } from '../Input/inputBlock';
  15. import { NodeMaterialSystemValues } from '../../Enums/nodeMaterialSystemValues';
  16. import { Constants } from '../../../../Engines/constants';
  17. import "../../../../Shaders/ShadersInclude/reflectionFunction";
  18. import { CubeTexture } from '../../../Textures/cubeTexture';
  19. import { Texture } from '../../../Textures/texture';
  20. /**
  21. * Block used to read a reflection texture from a sampler
  22. */
  23. export class ReflectionTextureBlock extends NodeMaterialBlock {
  24. private _define3DName: string;
  25. private _defineCubicName: string;
  26. private _defineExplicitName: string;
  27. private _defineProjectionName: string;
  28. private _defineLocalCubicName: string;
  29. private _defineSphericalName: string;
  30. private _definePlanarName: string;
  31. private _defineEquirectangularName: string;
  32. private _defineMirroredEquirectangularFixedName: string;
  33. private _defineEquirectangularFixedName: string;
  34. private _defineSkyboxName: string;
  35. private _cubeSamplerName: string;
  36. private _2DSamplerName: string;
  37. private _positionUVWName: string;
  38. private _directionWName: string;
  39. private _reflectionCoordsName: string;
  40. private _reflection2DCoordsName: string;
  41. private _reflectionColorName: string;
  42. private _reflectionMatrixName: string;
  43. /**
  44. * Gets or sets the texture associated with the node
  45. */
  46. public texture: Nullable<BaseTexture>;
  47. /**
  48. * Create a new TextureBlock
  49. * @param name defines the block name
  50. */
  51. public constructor(name: string) {
  52. super(name, NodeMaterialBlockTargets.VertexAndFragment);
  53. this.registerInput("position", NodeMaterialBlockConnectionPointTypes.Vector3, false, NodeMaterialBlockTargets.Vertex);
  54. this.registerInput("worldPosition", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Vertex);
  55. this.registerInput("worldNormal", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Fragment); // Flagging as fragment as the normal can be changed by fragment code
  56. this.registerInput("world", NodeMaterialBlockConnectionPointTypes.Matrix, false, NodeMaterialBlockTargets.Vertex);
  57. this.registerInput("cameraPosition", NodeMaterialBlockConnectionPointTypes.Vector3, false, NodeMaterialBlockTargets.Fragment);
  58. this.registerInput("view", NodeMaterialBlockConnectionPointTypes.Matrix, false, NodeMaterialBlockTargets.Fragment);
  59. this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  60. this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  61. this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  62. this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  63. }
  64. /**
  65. * Gets the current class name
  66. * @returns the class name
  67. */
  68. public getClassName() {
  69. return "ReflectionTextureBlock";
  70. }
  71. /**
  72. * Gets the world position input component
  73. */
  74. public get position(): NodeMaterialConnectionPoint {
  75. return this._inputs[0];
  76. }
  77. /**
  78. * Gets the world position input component
  79. */
  80. public get worldPosition(): NodeMaterialConnectionPoint {
  81. return this._inputs[1];
  82. }
  83. /**
  84. * Gets the world normal input component
  85. */
  86. public get worldNormal(): NodeMaterialConnectionPoint {
  87. return this._inputs[2];
  88. }
  89. /**
  90. * Gets the world input component
  91. */
  92. public get world(): NodeMaterialConnectionPoint {
  93. return this._inputs[3];
  94. }
  95. /**
  96. * Gets the camera (or eye) position component
  97. */
  98. public get cameraPosition(): NodeMaterialConnectionPoint {
  99. return this._inputs[4];
  100. }
  101. /**
  102. * Gets the view input component
  103. */
  104. public get view(): NodeMaterialConnectionPoint {
  105. return this._inputs[5];
  106. }
  107. /**
  108. * Gets the rgb output component
  109. */
  110. public get rgb(): NodeMaterialConnectionPoint {
  111. return this._outputs[0];
  112. }
  113. /**
  114. * Gets the r output component
  115. */
  116. public get r(): NodeMaterialConnectionPoint {
  117. return this._outputs[1];
  118. }
  119. /**
  120. * Gets the g output component
  121. */
  122. public get g(): NodeMaterialConnectionPoint {
  123. return this._outputs[2];
  124. }
  125. /**
  126. * Gets the b output component
  127. */
  128. public get b(): NodeMaterialConnectionPoint {
  129. return this._outputs[3];
  130. }
  131. public autoConfigure(material: NodeMaterial) {
  132. if (!this.position.isConnected) {
  133. let positionInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "position");
  134. if (!positionInput) {
  135. positionInput = new InputBlock("position");
  136. positionInput.setAsAttribute();
  137. }
  138. positionInput.output.connectTo(this.position);
  139. }
  140. if (!this.world.isConnected) {
  141. let worldInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.World);
  142. if (!worldInput) {
  143. worldInput = new InputBlock("world");
  144. worldInput.setAsSystemValue(NodeMaterialSystemValues.World);
  145. }
  146. worldInput.output.connectTo(this.world);
  147. }
  148. if (!this.cameraPosition.isConnected) {
  149. let cameraPositionInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.CameraPosition);
  150. if (!cameraPositionInput) {
  151. cameraPositionInput = new InputBlock("cameraPosition");
  152. cameraPositionInput.setAsSystemValue(NodeMaterialSystemValues.CameraPosition);
  153. }
  154. cameraPositionInput.output.connectTo(this.cameraPosition);
  155. }
  156. if (!this.view.isConnected) {
  157. let viewInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.View);
  158. if (!viewInput) {
  159. viewInput = new InputBlock("view");
  160. viewInput.setAsSystemValue(NodeMaterialSystemValues.View);
  161. }
  162. viewInput.output.connectTo(this.view);
  163. }
  164. }
  165. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  166. if (!defines._areTexturesDirty) {
  167. return;
  168. }
  169. if (!this.texture || !this.texture.getTextureMatrix) {
  170. return;
  171. }
  172. defines.setValue(this._define3DName, this.texture.isCube);
  173. defines.setValue(this._defineLocalCubicName, (<any>this.texture).boundingBoxSize ? true : false);
  174. defines.setValue(this._defineExplicitName, this.texture.coordinatesMode === Constants.TEXTURE_EXPLICIT_MODE);
  175. defines.setValue(this._defineSkyboxName, this.texture.coordinatesMode === Constants.TEXTURE_SKYBOX_MODE);
  176. defines.setValue(this._defineCubicName, this.texture.coordinatesMode === Constants.TEXTURE_CUBIC_MODE);
  177. defines.setValue(this._defineSphericalName, this.texture.coordinatesMode === Constants.TEXTURE_SPHERICAL_MODE);
  178. defines.setValue(this._definePlanarName, this.texture.coordinatesMode === Constants.TEXTURE_PLANAR_MODE);
  179. defines.setValue(this._defineProjectionName, this.texture.coordinatesMode === Constants.TEXTURE_PROJECTION_MODE);
  180. defines.setValue(this._defineEquirectangularName, this.texture.coordinatesMode === Constants.TEXTURE_EQUIRECTANGULAR_MODE);
  181. defines.setValue(this._defineEquirectangularFixedName, this.texture.coordinatesMode === Constants.TEXTURE_FIXED_EQUIRECTANGULAR_MODE);
  182. defines.setValue(this._defineMirroredEquirectangularFixedName, this.texture.coordinatesMode === Constants.TEXTURE_FIXED_EQUIRECTANGULAR_MIRRORED_MODE);
  183. }
  184. public isReady() {
  185. if (this.texture && !this.texture.isReadyOrNotBlocking()) {
  186. return false;
  187. }
  188. return true;
  189. }
  190. public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {
  191. if (!mesh || !this.texture) {
  192. return;
  193. }
  194. effect.setMatrix(this._reflectionMatrixName, this.texture.getReflectionTextureMatrix());
  195. if (this.texture.isCube) {
  196. effect.setTexture(this._cubeSamplerName, this.texture);
  197. } else {
  198. effect.setTexture(this._2DSamplerName, this.texture);
  199. }
  200. }
  201. private _injectVertexCode(state: NodeMaterialBuildState) {
  202. let worldPosVaryingName = "v_" + this.worldPosition.associatedVariableName;
  203. if (state._emitVaryingFromString(worldPosVaryingName, "vec4")) {
  204. state.compilationString += `${worldPosVaryingName} = ${this.worldPosition.associatedVariableName};\r\n`;
  205. }
  206. this._positionUVWName = state._getFreeVariableName("positionUVW");
  207. this._directionWName = state._getFreeVariableName("directionW");
  208. if (state._emitVaryingFromString(this._positionUVWName, "vec3", this._defineSkyboxName)) {
  209. state.compilationString += `#ifdef ${this._defineSkyboxName}\r\n`;
  210. state.compilationString += `${this._positionUVWName} = ${this.position.associatedVariableName};\r\n`;
  211. state.compilationString += `#endif\r\n`;
  212. }
  213. if (state._emitVaryingFromString(this._directionWName, "vec3", `defined(${this._defineEquirectangularFixedName}) || defined(${this._defineMirroredEquirectangularFixedName})`)) {
  214. state.compilationString += `#if defined(${this._defineEquirectangularFixedName}) || defined(${this._defineMirroredEquirectangularFixedName})\r\n`;
  215. state.compilationString += `${this._directionWName} = normalize(vec3(${this.world.associatedVariableName} * vec4(${this.position.associatedVariableName}, 0.0)));\r\n`;
  216. state.compilationString += `#endif\r\n`;
  217. }
  218. }
  219. private _writeOutput(state: NodeMaterialBuildState, output: NodeMaterialConnectionPoint, swizzle: string) {
  220. state.compilationString += `${this._declareOutput(output, state)} = ${this._reflectionColorName}.${swizzle};\r\n`;
  221. }
  222. protected _buildBlock(state: NodeMaterialBuildState) {
  223. super._buildBlock(state);
  224. if (!this.texture) {
  225. if (state.target === NodeMaterialBlockTargets.Fragment) {
  226. for (var output of this._outputs) {
  227. if (output.hasEndpoints) {
  228. state.compilationString += `${this._declareOutput(output, state)} = vec3(0.).${output.name};\r\n`;
  229. }
  230. }
  231. }
  232. return;
  233. }
  234. if (state.target !== NodeMaterialBlockTargets.Fragment) {
  235. this._define3DName = state._getFreeDefineName("REFLECTIONMAP_3D");
  236. this._defineCubicName = state._getFreeDefineName("REFLECTIONMAP_CUBIC");
  237. this._defineSphericalName = state._getFreeDefineName("REFLECTIONMAP_SPHERICAL");
  238. this._definePlanarName = state._getFreeDefineName("REFLECTIONMAP_PLANAR");
  239. this._defineProjectionName = state._getFreeDefineName("REFLECTIONMAP_PROJECTION");
  240. this._defineExplicitName = state._getFreeDefineName("REFLECTIONMAP_EXPLICIT");
  241. this._defineEquirectangularName = state._getFreeDefineName("REFLECTIONMAP_EQUIRECTANGULAR");
  242. this._defineLocalCubicName = state._getFreeDefineName("USE_LOCAL_REFLECTIONMAP_CUBIC");
  243. this._defineMirroredEquirectangularFixedName = state._getFreeDefineName("REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED");
  244. this._defineEquirectangularFixedName = state._getFreeDefineName("REFLECTIONMAP_EQUIRECTANGULAR_FIXED");
  245. this._defineSkyboxName = state._getFreeDefineName("REFLECTIONMAP_SKYBOX");
  246. // Vertex
  247. this._injectVertexCode(state);
  248. return;
  249. }
  250. state.sharedData.blockingBlocks.push(this);
  251. state.sharedData.textureBlocks.push(this);
  252. // Samplers
  253. this._cubeSamplerName = state._getFreeVariableName(this.name + "CubeSampler");
  254. state.samplers.push(this._cubeSamplerName);
  255. this._2DSamplerName = state._getFreeVariableName(this.name + "2DSampler");
  256. state.samplers.push(this._2DSamplerName);
  257. state._samplerDeclaration += `#ifdef ${this._define3DName}\r\n`;
  258. state._samplerDeclaration += `uniform samplerCube ${this._cubeSamplerName};\r\n`;
  259. state._samplerDeclaration += `#else\r\n`;
  260. state._samplerDeclaration += `uniform sampler2D ${this._2DSamplerName};\r\n`;
  261. state._samplerDeclaration += `#endif\r\n`;
  262. // Fragment
  263. state.sharedData.blocksWithDefines.push(this);
  264. state.sharedData.bindableBlocks.push(this);
  265. let comments = `//${this.name}`;
  266. state._emitFunction("ReciprocalPI", "#define RECIPROCAL_PI2 0.15915494", "");
  267. state._emitFunctionFromInclude("reflectionFunction", comments);
  268. this._reflectionColorName = state._getFreeVariableName("reflectionColor");
  269. this._reflectionCoordsName = state._getFreeVariableName("reflectionUVW");
  270. this._reflection2DCoordsName = state._getFreeVariableName("reflectionUV");
  271. this._reflectionMatrixName = state._getFreeVariableName("reflectionMatrix");
  272. state._emitUniformFromString(this._reflectionMatrixName, "mat4");
  273. // Code
  274. let worldPos = `v_${this.worldPosition.associatedVariableName}`;
  275. let worldNormal = this.worldNormal.associatedVariableName + ".xyz";
  276. let reflectionMatrix = this._reflectionMatrixName;
  277. let direction = `normalize(${this._directionWName})`;
  278. let positionUVW = `${this._positionUVWName}`;
  279. let vEyePosition = `${this.cameraPosition.associatedVariableName}`;
  280. let view = `${this.view.associatedVariableName}`;
  281. state.compilationString += `vec3 ${this._reflectionColorName};\r\n`;
  282. state.compilationString += `#ifdef ${this._defineMirroredEquirectangularFixedName}\r\n`;
  283. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computeMirroredFixedEquirectangularCoords(${worldPos}, ${worldNormal}, ${direction});\r\n`;
  284. state.compilationString += `#endif\r\n`;
  285. state.compilationString += `#ifdef ${this._defineEquirectangularFixedName}\r\n`;
  286. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computeFixedEquirectangularCoords(${worldPos}, ${worldNormal}, ${direction});\r\n`;
  287. state.compilationString += `#endif\r\n`;
  288. state.compilationString += `#ifdef ${this._defineEquirectangularName}\r\n`;
  289. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computeEquirectangularCoords(${worldPos}, ${worldNormal}, ${vEyePosition}.xyz, ${reflectionMatrix});\r\n`;
  290. state.compilationString += ` #endif\r\n`;
  291. state.compilationString += `#ifdef ${this._defineSphericalName}\r\n`;
  292. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computeSphericalCoords(${worldPos}, ${worldNormal}, ${view}, ${reflectionMatrix});\r\n`;
  293. state.compilationString += `#endif\r\n`;
  294. state.compilationString += `#ifdef ${this._definePlanarName}\r\n`;
  295. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computePlanarCoords(${worldPos}, ${worldNormal}, ${vEyePosition}.xyz, ${reflectionMatrix});\r\n`;
  296. state.compilationString += `#endif\r\n`;
  297. state.compilationString += `#ifdef ${this._defineCubicName}\r\n`;
  298. state.compilationString += ` #ifdef ${this._defineLocalCubicName}\r\n`;
  299. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computeCubicLocalCoords(${worldPos}, ${worldNormal}, ${vEyePosition}.xyz, ${reflectionMatrix}, vReflectionSize, vReflectionPosition);\r\n`;
  300. state.compilationString += ` #else\r\n`;
  301. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computeCubicCoords(${worldPos}, ${worldNormal}, ${vEyePosition}.xyz, ${reflectionMatrix});\r\n`;
  302. state.compilationString += ` #endif\r\n`;
  303. state.compilationString += `#endif\r\n`;
  304. state.compilationString += `#ifdef ${this._defineProjectionName}\r\n`;
  305. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computeProjectionCoords(${worldPos}, ${view}, ${reflectionMatrix});\r\n`;
  306. state.compilationString += `#endif\r\n`;
  307. state.compilationString += `#ifdef ${this._defineSkyboxName}\r\n`;
  308. state.compilationString += ` vec3 ${this._reflectionCoordsName} = computeSkyBoxCoords(${positionUVW}, ${reflectionMatrix});\r\n`;
  309. state.compilationString += `#endif\r\n`;
  310. state.compilationString += `#ifdef ${this._defineExplicitName}\r\n`;
  311. state.compilationString += ` vec3 ${this._reflectionCoordsName} = vec3(0, 0, 0);\r\n`;
  312. state.compilationString += `#endif\r\n`;
  313. state.compilationString += `#ifdef ${this._define3DName}\r\n`;
  314. state.compilationString += `${this._reflectionColorName} = textureCube(${this._cubeSamplerName}, ${this._reflectionCoordsName}).rgb;\r\n`;
  315. state.compilationString += `#else\r\n`;
  316. state.compilationString += `vec2 ${this._reflection2DCoordsName} = ${this._reflectionCoordsName}.xy;\r\n`;
  317. state.compilationString += `#ifdef ${this._defineProjectionName}\r\n`;
  318. state.compilationString += `${this._reflection2DCoordsName} /= ${this._reflectionCoordsName}.z;\r\n`;
  319. state.compilationString += `#endif\r\n`;
  320. state.compilationString += `${this._reflection2DCoordsName}.y = 1.0 - ${this._reflection2DCoordsName}.y;\r\n`;
  321. state.compilationString += `${this._reflectionColorName} = texture2D(${this._2DSamplerName}, ${this._reflection2DCoordsName}).rgb;\r\n`;
  322. state.compilationString += `#endif\r\n`;
  323. for (var output of this._outputs) {
  324. if (output.hasEndpoints) {
  325. this._writeOutput(state, output, output.name);
  326. }
  327. }
  328. return this;
  329. }
  330. protected _dumpPropertiesCode() {
  331. if (!this.texture) {
  332. return "";
  333. }
  334. let codeString: string;
  335. if (this.texture.isCube) {
  336. codeString = `${this._codeVariableName}.texture = new BABYLON.CubeTexture("${this.texture.name}");\r\n`;
  337. } else {
  338. codeString = `${this._codeVariableName}.texture = new BABYLON.Texture("${this.texture.name}");\r\n`;
  339. }
  340. codeString += `${this._codeVariableName}.texture.coordinatesMode = ${this.texture.coordinatesMode};\r\n`;
  341. return codeString;
  342. }
  343. public serialize(): any {
  344. let serializationObject = super.serialize();
  345. if (this.texture) {
  346. serializationObject.texture = this.texture.serialize();
  347. }
  348. return serializationObject;
  349. }
  350. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  351. super._deserialize(serializationObject, scene, rootUrl);
  352. if (serializationObject.texture) {
  353. rootUrl = serializationObject.texture.url.indexOf("data:") === 0 ? "" : rootUrl;
  354. if (serializationObject.texture.isCube) {
  355. this.texture = CubeTexture.Parse(serializationObject.texture, scene, rootUrl);
  356. } else {
  357. this.texture = Texture.Parse(serializationObject.texture, scene, rootUrl);
  358. }
  359. }
  360. }
  361. }
  362. _TypeStore.RegisteredTypes["BABYLON.ReflectionTextureBlock"] = ReflectionTextureBlock;