pbrMetallicRoughnessBlock.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. import { NodeMaterialBlock } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../../nodeMaterialBuildState';
  4. import { NodeMaterialConnectionPoint, NodeMaterialConnectionPointDirection } from '../../nodeMaterialBlockConnectionPoint';
  5. import { MaterialHelper } from '../../../materialHelper';
  6. import { NodeMaterialBlockTargets } from '../../Enums/nodeMaterialBlockTargets';
  7. import { NodeMaterial, NodeMaterialDefines } from '../../nodeMaterial';
  8. import { NodeMaterialSystemValues } from '../../Enums/nodeMaterialSystemValues';
  9. import { InputBlock } from '../Input/inputBlock';
  10. import { Light } from '../../../../Lights/light';
  11. import { Nullable } from '../../../../types';
  12. import { _TypeStore } from '../../../../Misc/typeStore';
  13. import { AbstractMesh } from '../../../../Meshes/abstractMesh';
  14. import { Effect, IEffectCreationOptions } from '../../../effect';
  15. import { Mesh } from '../../../../Meshes/mesh';
  16. import { PBRBaseMaterial } from '../../../PBR/pbrBaseMaterial';
  17. import { Scene } from '../../../../scene';
  18. import { editableInPropertyPage, PropertyTypeForEdition } from "../../nodeMaterialDecorator";
  19. import { NodeMaterialConnectionPointCustomObject } from "../../nodeMaterialConnectionPointCustomObject";
  20. import { AmbientOcclusionBlock } from './ambientOcclusionBlock';
  21. import { SheenBlock } from './sheenBlock';
  22. export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
  23. private _lightId: number;
  24. /**
  25. * Gets or sets the light associated with this block
  26. */
  27. public light: Nullable<Light>;
  28. public constructor(name: string) {
  29. super(name, NodeMaterialBlockTargets.VertexAndFragment);
  30. this._isUnique = true;
  31. this.registerInput("worldPosition", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Vertex);
  32. this.registerInput("worldNormal", NodeMaterialBlockConnectionPointTypes.Vector4, false, NodeMaterialBlockTargets.Fragment);
  33. this.registerInput("perturbedNormal", NodeMaterialBlockConnectionPointTypes.Vector4, true, NodeMaterialBlockTargets.Fragment);
  34. this.registerInput("cameraPosition", NodeMaterialBlockConnectionPointTypes.Vector3, false, NodeMaterialBlockTargets.Fragment);
  35. this.registerInput("baseColor", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
  36. this.registerInput("baseTexture", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
  37. this.registerInput("metallic", NodeMaterialBlockConnectionPointTypes.Float, true, NodeMaterialBlockTargets.Fragment);
  38. this.registerInput("roughness", NodeMaterialBlockConnectionPointTypes.Float, true, NodeMaterialBlockTargets.Fragment);
  39. this.registerInput("metalRoughText", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
  40. this.registerInput("opacityTexture", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
  41. this.registerInput("ambientColor", NodeMaterialBlockConnectionPointTypes.Color3, true, NodeMaterialBlockTargets.Fragment);
  42. this.registerInput("ambientOcclusion", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("ambientOcclusion", this, NodeMaterialConnectionPointDirection.Input, AmbientOcclusionBlock, "AOBlock"));
  43. this.registerInput("reflection", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment);
  44. this.registerInput("sheen", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("sheen", this, NodeMaterialConnectionPointDirection.Input, SheenBlock, "SheenBlock"));
  45. this.registerInput("clearCoat", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment);
  46. this.registerInput("subSurface", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment);
  47. this.registerInput("anisotropy", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment);
  48. this.registerOutput("ambient", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  49. this.registerOutput("diffuse", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  50. this.registerOutput("specular", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  51. this.registerOutput("sheen", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  52. this.registerOutput("clearcoat", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  53. this.registerOutput("diffuseInd", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  54. this.registerOutput("specularInd", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  55. this.registerOutput("sheenInd", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  56. this.registerOutput("clearcoatInd", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  57. this.registerOutput("refraction", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
  58. this.registerOutput("lighting", NodeMaterialBlockConnectionPointTypes.Color4, NodeMaterialBlockTargets.Fragment);
  59. this.registerOutput("shadow", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
  60. }
  61. @editableInPropertyPage("Alpha from albedo", PropertyTypeForEdition.Boolean, "TRANSPARENCY")
  62. public useAlphaFromAlbedoTexture: boolean = false;
  63. @editableInPropertyPage("Alpha Testing", PropertyTypeForEdition.Boolean, "TRANSPARENCY")
  64. public useAlphaTest: boolean = false;
  65. @editableInPropertyPage("Alpha CutOff", PropertyTypeForEdition.Float, "TRANSPARENCY", { min: 0, max: 1})
  66. public alphaTestCutoff: number = 0.4;
  67. @editableInPropertyPage("Alpha blending", PropertyTypeForEdition.Boolean, "TRANSPARENCY")
  68. public useAlphaBlending: boolean = false;
  69. @editableInPropertyPage("Get alpha from opacity texture RGB", PropertyTypeForEdition.Boolean, "TRANSPARENCY")
  70. public opacityRGB: boolean = false;
  71. /**
  72. * Initialize the block and prepare the context for build
  73. * @param state defines the state that will be used for the build
  74. */
  75. public initialize(state: NodeMaterialBuildState) {
  76. state._excludeVariableName("surfaceAlbedo");
  77. state._excludeVariableName("alpha");
  78. }
  79. /**
  80. * Gets the current class name
  81. * @returns the class name
  82. */
  83. public getClassName() {
  84. return "PBRMetallicRoughnessBlock";
  85. }
  86. public get worldPosition(): NodeMaterialConnectionPoint {
  87. return this._inputs[0];
  88. }
  89. public get worldNormal(): NodeMaterialConnectionPoint {
  90. return this._inputs[1];
  91. }
  92. public get perturbedNormal(): NodeMaterialConnectionPoint {
  93. return this._inputs[2];
  94. }
  95. public get cameraPosition(): NodeMaterialConnectionPoint {
  96. return this._inputs[3];
  97. }
  98. public get baseColor(): NodeMaterialConnectionPoint {
  99. return this._inputs[4];
  100. }
  101. public get baseTexture(): NodeMaterialConnectionPoint {
  102. return this._inputs[5];
  103. }
  104. public get metallic(): NodeMaterialConnectionPoint {
  105. return this._inputs[6];
  106. }
  107. public get roughness(): NodeMaterialConnectionPoint {
  108. return this._inputs[7];
  109. }
  110. public get metalRoughTexture(): NodeMaterialConnectionPoint {
  111. return this._inputs[8];
  112. }
  113. public get opacityTexture(): NodeMaterialConnectionPoint {
  114. return this._inputs[9];
  115. }
  116. public get ambientColor(): NodeMaterialConnectionPoint {
  117. return this._inputs[10];
  118. }
  119. public get ambientOcclusionParams(): NodeMaterialConnectionPoint {
  120. return this._inputs[11];
  121. }
  122. public get reflectionParams(): NodeMaterialConnectionPoint {
  123. return this._inputs[12];
  124. }
  125. public get sheenParams(): NodeMaterialConnectionPoint {
  126. return this._inputs[13];
  127. }
  128. public get clearcoatParams(): NodeMaterialConnectionPoint {
  129. return this._inputs[14];
  130. }
  131. public get subSurfaceParams(): NodeMaterialConnectionPoint {
  132. return this._inputs[15];
  133. }
  134. public get anisotropyParams(): NodeMaterialConnectionPoint {
  135. return this._inputs[16];
  136. }
  137. public get ambient(): NodeMaterialConnectionPoint {
  138. return this._outputs[0];
  139. }
  140. public get diffuse(): NodeMaterialConnectionPoint {
  141. return this._outputs[1];
  142. }
  143. public get specular(): NodeMaterialConnectionPoint {
  144. return this._outputs[2];
  145. }
  146. public get sheen(): NodeMaterialConnectionPoint {
  147. return this._outputs[3];
  148. }
  149. public get clearcoat(): NodeMaterialConnectionPoint {
  150. return this._outputs[4];
  151. }
  152. public get diffuseIndirect(): NodeMaterialConnectionPoint {
  153. return this._outputs[5];
  154. }
  155. public get specularIndirect(): NodeMaterialConnectionPoint {
  156. return this._outputs[6];
  157. }
  158. public get sheenIndirect(): NodeMaterialConnectionPoint {
  159. return this._outputs[7];
  160. }
  161. public get clearcoatIndirect(): NodeMaterialConnectionPoint {
  162. return this._outputs[8];
  163. }
  164. public get refraction(): NodeMaterialConnectionPoint {
  165. return this._outputs[9];
  166. }
  167. public get lighting(): NodeMaterialConnectionPoint {
  168. return this._outputs[10];
  169. }
  170. public get shadow(): NodeMaterialConnectionPoint {
  171. return this._outputs[11];
  172. }
  173. public autoConfigure(material: NodeMaterial) {
  174. if (!this.cameraPosition.isConnected) {
  175. let cameraPositionInput = material.getInputBlockByPredicate((b) => b.systemValue === NodeMaterialSystemValues.CameraPosition);
  176. if (!cameraPositionInput) {
  177. cameraPositionInput = new InputBlock("cameraPosition");
  178. cameraPositionInput.setAsSystemValue(NodeMaterialSystemValues.CameraPosition);
  179. }
  180. cameraPositionInput.output.connectTo(this.cameraPosition);
  181. }
  182. }
  183. public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
  184. defines.setValue("PBR", true);
  185. // Albedo & Opacity
  186. if (this.baseTexture.isConnected) {
  187. defines.setValue("ALBEDO", true);
  188. }
  189. if (this.opacityTexture.isConnected) {
  190. defines.setValue("OPACITY", true);
  191. }
  192. defines.setValue("ALPHABLEND", this.useAlphaBlending);
  193. defines.setValue("ALPHAFROMALBEDO", this.useAlphaFromAlbedoTexture);
  194. defines.setValue("ALPHATEST", this.useAlphaTest);
  195. defines.setValue("ALPHATESTVALUE", this.alphaTestCutoff);
  196. defines.setValue("OPACITYRGB", this.opacityRGB);
  197. // Ambient occlusion
  198. const aoBlock = this.ambientOcclusionParams.connectedPoint?.ownerBlock as Nullable<AmbientOcclusionBlock>;
  199. defines.setValue("AMBIENT", aoBlock?.texture.isConnected ?? false);
  200. if (!defines._areLightsDirty) {
  201. return;
  202. }
  203. const scene = mesh.getScene();
  204. if (!this.light) {
  205. // Lights
  206. MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, true, nodeMaterial.maxSimultaneousLights);
  207. defines._needNormals = true;
  208. defines.setValue("SPECULARTERM", false); //!! DBG
  209. // Multiview
  210. //MaterialHelper.PrepareDefinesForMultiview(scene, defines);
  211. } else {
  212. let state = {
  213. needNormals: false,
  214. needRebuild: false,
  215. lightmapMode: false,
  216. shadowEnabled: false,
  217. specularEnabled: false
  218. };
  219. MaterialHelper.PrepareDefinesForLight(scene, mesh, this.light, this._lightId, defines, true, state);
  220. if (state.needRebuild) {
  221. defines.rebuild();
  222. }
  223. }
  224. }
  225. public updateUniformsAndSamples(state: NodeMaterialBuildState, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, uniformBuffers: string[]) {
  226. MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
  227. uniformsNames: state.uniforms,
  228. uniformBuffersNames: uniformBuffers,
  229. samplers: state.samplers,
  230. defines: defines,
  231. maxSimultaneousLights: nodeMaterial.maxSimultaneousLights
  232. });
  233. }
  234. public isReady() {
  235. /*if (this.texture && !this.texture.isReadyOrNotBlocking()) {
  236. return false;
  237. }*/
  238. return true;
  239. }
  240. public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {
  241. if (!mesh) {
  242. return;
  243. }
  244. const scene = mesh.getScene();
  245. if (!this.light) {
  246. MaterialHelper.BindLights(scene, mesh, effect, true, nodeMaterial.maxSimultaneousLights);
  247. } else {
  248. MaterialHelper.BindLight(this.light, this._lightId, scene, effect, true);
  249. }
  250. /*if (!mesh || !this.texture) {
  251. return;
  252. }
  253. effect.setMatrix(this._reflectionMatrixName, this.texture.getReflectionTextureMatrix());
  254. if (this.texture.isCube) {
  255. effect.setTexture(this._cubeSamplerName, this.texture);
  256. } else {
  257. effect.setTexture(this._2DSamplerName, this.texture);
  258. }*/
  259. }
  260. private _injectVertexCode(state: NodeMaterialBuildState) {
  261. let worldPos = this.worldPosition;
  262. let comments = `//${this.name}`;
  263. // Declaration
  264. if (!this.light) { // Emit for all lights
  265. state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
  266. repeatKey: "maxSimultaneousLights"
  267. });
  268. this._lightId = 0;
  269. state.sharedData.dynamicUniformBlocks.push(this);
  270. } else {
  271. this._lightId = (state.counters["lightCounter"] !== undefined ? state.counters["lightCounter"] : -1) + 1;
  272. state.counters["lightCounter"] = this._lightId;
  273. state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
  274. replaceStrings: [{ search: /{X}/g, replace: this._lightId.toString() }]
  275. }, this._lightId.toString());
  276. }
  277. // Inject code in vertex
  278. let worldPosVaryingName = "v_" + worldPos.associatedVariableName;
  279. if (state._emitVaryingFromString(worldPosVaryingName, "vec4")) {
  280. state.compilationString += `${worldPosVaryingName} = ${worldPos.associatedVariableName};\r\n`;
  281. }
  282. if (this.light) {
  283. state.compilationString += state._emitCodeFromInclude("shadowsVertex", comments, {
  284. replaceStrings: [
  285. { search: /{X}/g, replace: this._lightId.toString() },
  286. { search: /worldPos/g, replace: worldPos.associatedVariableName }
  287. ]
  288. });
  289. } else {
  290. state.compilationString += `vec4 worldPos = ${worldPos.associatedVariableName};\r\n`;
  291. state.compilationString += state._emitCodeFromInclude("shadowsVertex", comments, {
  292. repeatKey: "maxSimultaneousLights"
  293. });
  294. }
  295. }
  296. protected _buildBlock(state: NodeMaterialBuildState) {
  297. super._buildBlock(state);
  298. if (state.target !== NodeMaterialBlockTargets.Fragment) {
  299. // Vertex
  300. this._injectVertexCode(state);
  301. return;
  302. }
  303. // Fragment
  304. state.sharedData.bindableBlocks.push(this);
  305. state.sharedData.blocksWithDefines.push(this);
  306. let comments = `//${this.name}`;
  307. let worldPos = this.worldPosition;
  308. let normalShading = this.perturbedNormal;
  309. //
  310. // Includes
  311. //
  312. if (!this.light) { // Emit for all lights
  313. state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
  314. repeatKey: "maxSimultaneousLights"
  315. });
  316. } else {
  317. state._emitFunctionFromInclude(state.supportUniformBuffers ? "lightUboDeclaration" : "lightFragmentDeclaration", comments, {
  318. replaceStrings: [{ search: /{X}/g, replace: this._lightId.toString() }]
  319. }, this._lightId.toString());
  320. }
  321. state._emitFunctionFromInclude("helperFunctions", comments);
  322. state._emitFunctionFromInclude("pbrHelperFunctions", comments);
  323. state._emitFunctionFromInclude("shadowsFragmentFunctions", comments, {
  324. replaceStrings: [
  325. { search: /vPositionW/g, replace: "v_" + worldPos.associatedVariableName + ".xyz" }
  326. ]
  327. });
  328. state._emitFunctionFromInclude("harmonicsFunctions", comments);
  329. state._emitFunctionFromInclude("pbrDirectLightingSetupFunctions", comments, {
  330. replaceStrings: [
  331. { search: /vPositionW/g, replace: "v_" + worldPos.associatedVariableName + ".xyz" }
  332. ]
  333. });
  334. state._emitFunctionFromInclude("pbrDirectLightingFalloffFunctions", comments);
  335. state._emitFunctionFromInclude("pbrBRDFFunctions", comments);
  336. state._emitFunctionFromInclude("pbrDirectLightingFunctions", comments, {
  337. replaceStrings: [
  338. { search: /vPositionW/g, replace: "v_" + worldPos.associatedVariableName + ".xyz" }
  339. ]
  340. });
  341. state._emitFunctionFromInclude("pbrIBLFunctions", comments);
  342. state._emitFunctionFromInclude("pbrBlockAlbedoOpacity", comments);
  343. state._emitFunctionFromInclude("pbrBlockReflectivity", comments);
  344. state._emitFunctionFromInclude("pbrBlockAmbientOcclusion", comments);
  345. state._emitFunctionFromInclude("pbrBlockAlphaFresnel", comments);
  346. state._emitFunctionFromInclude("pbrBlockAnisotropic", comments);
  347. state._emitFunctionFromInclude("pbrBlockReflection", comments);
  348. state._emitFunctionFromInclude("pbrBlockSheen", comments);
  349. state._emitFunctionFromInclude("pbrBlockClearcoat", comments);
  350. state._emitFunctionFromInclude("pbrBlockSubSurface", comments);
  351. //
  352. // code
  353. //
  354. const vLightingIntensity = "vec4(1.)";
  355. const aoBlock = this.ambientOcclusionParams.connectedPoint?.ownerBlock as Nullable<AmbientOcclusionBlock>;
  356. const aoColor = this.ambientColor.isConnected ? this.ambientColor.associatedVariableName : "vec3(0., 0., 0.)";
  357. let aoDirectLightIntensity = aoBlock?.directLightIntensity.isConnected ? aoBlock.directLightIntensity.associatedVariableName : PBRBaseMaterial.DEFAULT_AO_ON_ANALYTICAL_LIGHTS.toString();
  358. if (!aoBlock?.directLightIntensity.isConnected && aoDirectLightIntensity.charAt(aoDirectLightIntensity.length - 1) !== '.') {
  359. aoDirectLightIntensity += ".";
  360. }
  361. if (this._lightId === 0) {
  362. // _____________________________ Geometry Information ____________________________
  363. if (state._registerTempVariable("viewDirectionW")) {
  364. state.compilationString += `vec3 viewDirectionW = normalize(${this.cameraPosition.associatedVariableName} - ${"v_" + worldPos.associatedVariableName}.xyz);\r\n`;
  365. }
  366. state.compilationString += `vec3 geometricNormalW = ${this.worldNormal.associatedVariableName}.xyz;\r\n`;
  367. state.compilationString += `vec3 normalW = ${normalShading.isConnected ? normalShading.associatedVariableName + ".xyz" : "geometricNormalW"};\r\n`;
  368. state.compilationString += state._emitCodeFromInclude("pbrBlockNormalFinal", comments, {
  369. replaceStrings: [
  370. { search: /vPositionW/g, replace: worldPos.associatedVariableName },
  371. { search: /vEyePosition.w/g, replace: "1." },
  372. ]
  373. });
  374. // _____________________________ Albedo & Opacity ______________________________
  375. state.compilationString += `albedoOpacityOutParams albedoOpacityOut;\r\n`;
  376. const albedoColor = this.baseColor.isConnected ? this.baseColor.associatedVariableName : "vec4(1., 1., 1., 1.)";
  377. const albedoTexture = this.baseTexture.isConnected ? this.baseTexture.associatedVariableName : "";
  378. const opacityTexture = this.opacityTexture.isConnected ? this.opacityTexture.associatedVariableName : "";
  379. state.compilationString += `albedoOpacityBlock(
  380. ${albedoColor},
  381. #ifdef ALBEDO
  382. ${albedoTexture},
  383. vec2(1., 1.),
  384. #endif
  385. #ifdef OPACITY
  386. ${opacityTexture},
  387. vec2(1., 1.),
  388. #endif
  389. albedoOpacityOut
  390. );
  391. vec3 surfaceAlbedo = albedoOpacityOut.surfaceAlbedo;
  392. float alpha = albedoOpacityOut.alpha;\r\n`;
  393. // _____________________________ AO _______________________________
  394. state.compilationString += `ambientOcclusionOutParams aoOut;\r\n`;
  395. const aoTexture = aoBlock?.texture.isConnected ? aoBlock.texture.associatedVariableName : "vec2(0., 0.)";
  396. const aoLevel = "1.";
  397. const aoIntensity = aoBlock?.intensity.isConnected ? aoBlock.intensity.associatedVariableName : "1.";
  398. state.compilationString += `ambientOcclusionBlock(
  399. #ifdef AMBIENT
  400. ${aoTexture},
  401. vec4(0., ${aoLevel}, ${aoIntensity}, 0.),
  402. #endif
  403. aoOut
  404. );\r\n`;
  405. // _____________________________ Direct Lighting Info __________________________________
  406. state.compilationString += state._emitCodeFromInclude("pbrBlockDirectLighting", comments);
  407. }
  408. /*if (this.light) {
  409. state.compilationString += state._emitCodeFromInclude("lightFragment", comments, {
  410. replaceStrings: [
  411. { search: /{X}/g, replace: this._lightId.toString() }
  412. ]
  413. });
  414. } else {
  415. state.compilationString += state._emitCodeFromInclude("lightFragment", comments, {
  416. repeatKey: "maxSimultaneousLights"
  417. });
  418. }*/
  419. // _____________________________ Compute Final Lit and Unlit Components ________________________
  420. //state.compilationString += state._emitCodeFromInclude("pbrBlockFinalLitComponents", comments);
  421. state.compilationString += state._emitCodeFromInclude("pbrBlockFinalUnlitComponents", comments, {
  422. replaceStrings: [
  423. { search: /vec3 finalEmissive[\s\S]*?finalEmissive\*=vLightingIntensity\.y;/g, replace: "" },
  424. { search: /vAmbientColor/g, replace: aoColor },
  425. { search: /vLightingIntensity/g, replace: vLightingIntensity },
  426. { search: /vAmbientInfos\.w/g, replace: aoDirectLightIntensity },
  427. ]
  428. });
  429. state.compilationString += state._emitCodeFromInclude("pbrBlockFinalColorComposition", comments, {
  430. replaceStrings: [
  431. { search: /finalEmissive/g, replace: "vec3(0.)" },
  432. ]
  433. });
  434. if (this.lighting.hasEndpoints) {
  435. state.compilationString += this._declareOutput(this.lighting, state) + ` = finalColor;\r\n`;
  436. }
  437. if (this.shadow.hasEndpoints) {
  438. state.compilationString += this._declareOutput(this.shadow, state) + ` = shadow;\r\n`;
  439. }
  440. (window as any).sheenParams = this.sheenParams.connectedPoint?.ownerBlock;
  441. return this;
  442. }
  443. public serialize(): any {
  444. let serializationObject = super.serialize();
  445. if (this.light) {
  446. serializationObject.lightId = this.light.id;
  447. }
  448. /*if (this.texture) {
  449. serializationObject.texture = this.texture.serialize();
  450. }*/
  451. return serializationObject;
  452. }
  453. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  454. super._deserialize(serializationObject, scene, rootUrl);
  455. if (serializationObject.lightId) {
  456. this.light = scene.getLightByID(serializationObject.lightId);
  457. }
  458. /*if (serializationObject.texture) {
  459. rootUrl = serializationObject.texture.url.indexOf("data:") === 0 ? "" : rootUrl;
  460. if (serializationObject.texture.isCube) {
  461. this.texture = CubeTexture.Parse(serializationObject.texture, scene, rootUrl);
  462. } else {
  463. this.texture = Texture.Parse(serializationObject.texture, scene, rootUrl);
  464. }
  465. }*/
  466. }
  467. }
  468. _TypeStore.RegisteredTypes["BABYLON.PBRMetallicRoughnessBlock"] = PBRMetallicRoughnessBlock;