lightBlock.ts 13 KB

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