lightBlock.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
  3. import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
  4. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  5. import { NodeMaterialConnectionPoint } from '../../nodeMaterialBlockConnectionPoint';
  6. import { MaterialHelper } from '../../../materialHelper';
  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 { NodeMaterialSystemValues } from '../../Enums/nodeMaterialSystemValues';
  12. import { InputBlock } from '../Input/inputBlock';
  13. import { Light } from '../../../../Lights/light';
  14. import { Nullable } from '../../../../types';
  15. import { _TypeStore } from '../../../../Misc/typeStore';
  16. import { Scene } from '../../../../scene';
  17. import "../../../../Shaders/ShadersInclude/lightFragmentDeclaration";
  18. import "../../../../Shaders/ShadersInclude/lightUboDeclaration";
  19. import "../../../../Shaders/ShadersInclude/lightFragment";
  20. import "../../../../Shaders/ShadersInclude/helperFunctions";
  21. import "../../../../Shaders/ShadersInclude/lightsFragmentFunctions";
  22. import "../../../../Shaders/ShadersInclude/shadowsFragmentFunctions";
  23. import "../../../../Shaders/ShadersInclude/shadowsVertex";
  24. /**
  25. * Block used to add light in the fragment shader
  26. */
  27. export class LightBlock extends NodeMaterialBlock {
  28. private _lightId: number;
  29. /**
  30. * Gets or sets the light associated with this block
  31. */
  32. public light: Nullable<Light>;
  33. /**
  34. * Create a new LightBlock
  35. * @param name defines the block name
  36. */
  37. public constructor(name: string) {
  38. super(name, NodeMaterialBlockTargets.VertexAndFragment);
  39. this.registerInput("worldPosition", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Vertex);
  40. this.registerInput("worldNormal", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Fragment);
  41. this.registerInput("cameraPosition", NodeMaterialBlockConnectionPointTypes.Vector3, false, NodeMaterialBlockTargets.Fragment);
  42. this.registerInput("glossiness", NodeMaterialBlockConnectionPointTypes.Float, true, NodeMaterialBlockTargets.Fragment);
  43. this.registerInput("glossPower", NodeMaterialBlockConnectionPointTypes.Float, true, NodeMaterialBlockTargets.Fragment);
  44. this.registerInput("diffuseColor", NodeMaterialBlockConnectionPointTypes.Color3, true, NodeMaterialBlockTargets.Fragment);
  45. this.registerInput("specularColor", NodeMaterialBlockConnectionPointTypes.Color3, true, NodeMaterialBlockTargets.Fragment);
  46. this.registerOutput("diffuseOutput", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  47. this.registerOutput("specularOutput", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  48. }
  49. /**
  50. * Gets the current class name
  51. * @returns the class name
  52. */
  53. public getClassName() {
  54. return "LightBlock";
  55. }
  56. /**
  57. * Gets the world position input component
  58. */
  59. public get worldPosition(): NodeMaterialConnectionPoint {
  60. return this._inputs[0];
  61. }
  62. /**
  63. * Gets the world normal input component
  64. */
  65. public get worldNormal(): NodeMaterialConnectionPoint {
  66. return this._inputs[1];
  67. }
  68. /**
  69. * Gets the camera (or eye) position component
  70. */
  71. public get cameraPosition(): NodeMaterialConnectionPoint {
  72. return this._inputs[2];
  73. }
  74. /**
  75. * Gets the glossiness component
  76. */
  77. public get glossiness(): NodeMaterialConnectionPoint {
  78. return this._inputs[3];
  79. }
  80. /**
  81. * Gets the glossinness power component
  82. */
  83. public get glossPower(): NodeMaterialConnectionPoint {
  84. return this._inputs[4];
  85. }
  86. /**
  87. * Gets the diffuse color component
  88. */
  89. public get diffuseColor(): NodeMaterialConnectionPoint {
  90. return this._inputs[5];
  91. }
  92. /**
  93. * Gets the specular color component
  94. */
  95. public get specularColor(): NodeMaterialConnectionPoint {
  96. return this._inputs[6];
  97. }
  98. /**
  99. * Gets the diffuse output component
  100. */
  101. public get diffuseOutput(): NodeMaterialConnectionPoint {
  102. return this._outputs[0];
  103. }
  104. /**
  105. * Gets the specular output component
  106. */
  107. public get specularOutput(): NodeMaterialConnectionPoint {
  108. return this._outputs[1];
  109. }
  110. public autoConfigure(material: NodeMaterial) {
  111. if (!this.cameraPosition.isConnected) {
  112. let cameraPositionInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.CameraPosition);
  113. if (!cameraPositionInput) {
  114. cameraPositionInput = new InputBlock("cameraPosition");
  115. cameraPositionInput.setAsSystemValue(NodeMaterialSystemValues.CameraPosition);
  116. }
  117. cameraPositionInput.output.connectTo(this.cameraPosition);
  118. }
  119. }
  120. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  121. if (!defines._areLightsDirty) {
  122. return;
  123. }
  124. const scene = mesh.getScene();
  125. if (!this.light) {
  126. MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, true, nodeMaterial.maxSimultaneousLights);
  127. } else {
  128. let state = {
  129. needNormals: false,
  130. needRebuild: false,
  131. lightmapMode: false,
  132. shadowEnabled: false,
  133. specularEnabled: false
  134. };
  135. MaterialHelper.PrepareDefinesForLight(scene, mesh, this.light, this._lightId, defines, true, state);
  136. if (state.needRebuild) {
  137. defines.rebuild();
  138. }
  139. }
  140. }
  141. public updateUniformsAndSamples(state: NodeMaterialBuildState, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, uniformBuffers: string[]) {
  142. for (var lightIndex = 0; lightIndex < nodeMaterial.maxSimultaneousLights; lightIndex++) {
  143. if (!defines["LIGHT" + lightIndex]) {
  144. break;
  145. }
  146. MaterialHelper.PrepareUniformsAndSamplersForLight(lightIndex, state.uniforms, state.samplers, false, uniformBuffers);
  147. }
  148. }
  149. public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {
  150. if (!mesh) {
  151. return;
  152. }
  153. const scene = mesh.getScene();
  154. if (!this.light) {
  155. MaterialHelper.BindLights(scene, mesh, effect, true, nodeMaterial.maxSimultaneousLights, false);
  156. } else {
  157. MaterialHelper.BindLight(this.light, this._lightId, scene, effect, true, false);
  158. }
  159. }
  160. private _injectVertexCode(state: NodeMaterialBuildState) {
  161. let worldPos = this.worldPosition;
  162. let comments = `//${this.name}`;
  163. // Declaration
  164. if (!this.light) { // Emit for all lights
  165. state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
  166. repeatKey: "maxSimultaneousLights"
  167. });
  168. this._lightId = 0;
  169. state.sharedData.dynamicUniformBlocks.push(this);
  170. } else {
  171. this._lightId = (state.counters["lightCounter"] !== undefined ? state.counters["lightCounter"] : -1) + 1;
  172. state.counters["lightCounter"] = this._lightId;
  173. state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
  174. replaceStrings: [{ search: /{X}/g, replace: this._lightId.toString() }]
  175. }, this._lightId.toString());
  176. }
  177. // Inject code in vertex
  178. let worldPosVaryingName = "v_" + worldPos.associatedVariableName;
  179. if (state._emitVaryingFromString(worldPosVaryingName, "vec4")) {
  180. state.compilationString += `${worldPosVaryingName} = ${worldPos.associatedVariableName};\r\n`;
  181. }
  182. if (this.light) {
  183. state.compilationString += state._emitCodeFromInclude("shadowsVertex", comments, {
  184. replaceStrings: [
  185. { search: /{X}/g, replace: this._lightId.toString() },
  186. { search: /worldPos/g, replace: worldPos.associatedVariableName }
  187. ]
  188. });
  189. } else {
  190. state.compilationString += `vec4 worldPos = ${worldPos.associatedVariableName};\r\n`;
  191. state.compilationString += state._emitCodeFromInclude("shadowsVertex", comments, {
  192. repeatKey: "maxSimultaneousLights"
  193. });
  194. }
  195. }
  196. protected _buildBlock(state: NodeMaterialBuildState) {
  197. super._buildBlock(state);
  198. if (state.target !== NodeMaterialBlockTargets.Fragment) {
  199. // Vertex
  200. this._injectVertexCode(state);
  201. return;
  202. }
  203. // Fragment
  204. state.sharedData.bindableBlocks.push(this);
  205. state.sharedData.blocksWithDefines.push(this);
  206. let comments = `//${this.name}`;
  207. let worldPos = this.worldPosition;
  208. state._emitFunctionFromInclude("helperFunctions", comments);
  209. state._emitFunctionFromInclude("lightsFragmentFunctions", comments, {
  210. replaceStrings: [
  211. { search: /vPositionW/g, replace: "v_" + worldPos.associatedVariableName + ".xyz" }
  212. ]
  213. });
  214. state._emitFunctionFromInclude("shadowsFragmentFunctions", comments, {
  215. replaceStrings: [
  216. { search: /vPositionW/g, replace: "v_" + worldPos.associatedVariableName + ".xyz" }
  217. ]
  218. });
  219. if (!this.light) { // Emit for all lights
  220. state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
  221. repeatKey: "maxSimultaneousLights"
  222. });
  223. } else {
  224. state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
  225. replaceStrings: [{ search: /{X}/g, replace: this._lightId.toString() }]
  226. }, this._lightId.toString());
  227. }
  228. // Code
  229. if (this._lightId === 0) {
  230. if (state._registerTempVariable("viewDirectionW")) {
  231. state.compilationString += `vec3 viewDirectionW = normalize(${this.cameraPosition.associatedVariableName} - ${"v_" + worldPos.associatedVariableName}.xyz);\r\n`;
  232. }
  233. state.compilationString += `lightingInfo info;\r\n`;
  234. state.compilationString += `float shadow = 1.;\r\n`;
  235. state.compilationString += `float glossiness = ${this.glossiness.isConnected ? this.glossiness.associatedVariableName : "1.0"} * ${this.glossPower.isConnected ? this.glossPower.associatedVariableName : "1024.0"};\r\n`;
  236. state.compilationString += `vec3 diffuseBase = vec3(0., 0., 0.);\r\n`;
  237. state.compilationString += `vec3 specularBase = vec3(0., 0., 0.);\r\n`;
  238. state.compilationString += `vec3 normalW = ${this.worldNormal.associatedVariableName}.xyz;\r\n`;
  239. }
  240. if (this.light) {
  241. state.compilationString += state._emitCodeFromInclude("lightFragment", comments, {
  242. replaceStrings: [
  243. { search: /{X}/g, replace: this._lightId.toString() }
  244. ]
  245. });
  246. } else {
  247. state.compilationString += state._emitCodeFromInclude("lightFragment", comments, {
  248. repeatKey: "maxSimultaneousLights"
  249. });
  250. }
  251. let diffuseOutput = this.diffuseOutput;
  252. let specularOutput = this.specularOutput;
  253. state.compilationString += this._declareOutput(diffuseOutput, state) + ` = diffuseBase${this.diffuseColor.isConnected ? " * " + this.diffuseColor.associatedVariableName : ""};\r\n`;
  254. if (specularOutput.hasEndpoints) {
  255. state.compilationString += this._declareOutput(specularOutput, state) + ` = specularBase${this.specularColor.isConnected ? " * " + this.specularColor.associatedVariableName : ""};\r\n`;
  256. }
  257. return this;
  258. }
  259. public serialize(): any {
  260. let serializationObject = super.serialize();
  261. if (this.light) {
  262. serializationObject.lightId = this.light.id;
  263. }
  264. return serializationObject;
  265. }
  266. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  267. super._deserialize(serializationObject, scene, rootUrl);
  268. if (serializationObject.lightId) {
  269. this.light = scene.getLightByID(serializationObject.lightId);
  270. }
  271. }
  272. }
  273. _TypeStore.RegisteredTypes["BABYLON.LightBlock"] = LightBlock;