lightBlock.ts 14 KB

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