Reference Source

src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPBRRenderer.js

  1. import {LinearEncoding, sRGBEncoding} from "../../../../../constants/constants.js";
  2. import {TrianglesInstancingRenderer} from "./TrianglesInstancingRenderer.js";
  3.  
  4. const TEXTURE_DECODE_FUNCS = {};
  5. TEXTURE_DECODE_FUNCS[LinearEncoding] = "linearToLinear";
  6. TEXTURE_DECODE_FUNCS[sRGBEncoding] = "sRGBToLinear";
  7.  
  8. /**
  9. * @private
  10. */
  11. export class TrianglesPBRRenderer extends TrianglesInstancingRenderer {
  12. _getHash() {
  13. const scene = this._scene;
  14. return [scene.gammaOutput, scene._lightsState.getHash(), scene._sectionPlanesState.getHash(), (this._withSAO ? "sao" : "nosao")].join(";");
  15. }
  16.  
  17. drawLayer(frameCtx, layer, renderPass) {
  18. super.drawLayer(frameCtx, layer, renderPass, { incrementDrawState: true });
  19. }
  20.  
  21. _buildVertexShader() {
  22.  
  23. const scene = this._scene;
  24. const sectionPlanesState = scene._sectionPlanesState;
  25. const lightsState = scene._lightsState;
  26. const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
  27. const clippingCaps = sectionPlanesState.clippingCaps;
  28. const src = [];
  29. src.push("#version 300 es");
  30. src.push("// Instancing geometry quality drawing vertex shader");
  31.  
  32.  
  33. src.push("uniform int renderPass;");
  34.  
  35. src.push("in vec3 position;");
  36. src.push("in vec3 normal;");
  37. src.push("in vec4 color;");
  38. src.push("in vec2 uv;");
  39. src.push("in vec2 metallicRoughness;");
  40. src.push("in float flags;");
  41.  
  42. if (scene.entityOffsetsEnabled) {
  43. src.push("in vec3 offset;");
  44. }
  45.  
  46. src.push("in vec4 modelMatrixCol0;"); // Modeling matrix
  47. src.push("in vec4 modelMatrixCol1;");
  48. src.push("in vec4 modelMatrixCol2;");
  49.  
  50. src.push("in vec4 modelNormalMatrixCol0;");
  51. src.push("in vec4 modelNormalMatrixCol1;");
  52. src.push("in vec4 modelNormalMatrixCol2;");
  53.  
  54. this._addMatricesUniformBlockLines(src, true);
  55.  
  56. src.push("uniform mat3 uvDecodeMatrix;")
  57.  
  58. if (scene.logarithmicDepthBufferEnabled) {
  59. src.push("uniform float logDepthBufFC;");
  60. src.push("out float vFragDepth;");
  61. src.push("bool isPerspectiveMatrix(mat4 m) {");
  62. src.push(" return (m[2][3] == - 1.0);");
  63. src.push("}");
  64. src.push("out float isPerspective;");
  65. }
  66.  
  67. src.push("vec3 octDecode(vec2 oct) {");
  68. src.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));");
  69. src.push(" if (v.z < 0.0) {");
  70. src.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);");
  71. src.push(" }");
  72. src.push(" return normalize(v);");
  73. src.push("}");
  74.  
  75. src.push("out vec4 vViewPosition;");
  76. src.push("out vec3 vViewNormal;");
  77. src.push("out vec4 vColor;");
  78. src.push("out vec2 vUV;");
  79. src.push("out vec2 vMetallicRoughness;");
  80.  
  81. if (lightsState.lightMaps.length > 0) {
  82. src.push("out vec3 vWorldNormal;");
  83. }
  84.  
  85. if (clipping) {
  86. src.push("out vec4 vWorldPosition;");
  87. src.push("out float vFlags;");
  88. if (clippingCaps) {
  89. src.push("out vec4 vClipPosition;");
  90. }
  91. }
  92.  
  93. src.push("void main(void) {");
  94.  
  95. // colorFlag = NOT_RENDERED | COLOR_OPAQUE | COLOR_TRANSPARENT
  96. // renderPass = COLOR_OPAQUE | COLOR_TRANSPARENT
  97.  
  98. src.push(`int colorFlag = int(flags) & 0xF;`);
  99. src.push(`if (colorFlag != renderPass) {`);
  100. src.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"); // Cull vertex
  101.  
  102. src.push("} else {");
  103.  
  104. src.push("vec4 worldPosition = positionsDecodeMatrix * vec4(position, 1.0); ");
  105. src.push("worldPosition = worldMatrix * vec4(dot(worldPosition, modelMatrixCol0), dot(worldPosition, modelMatrixCol1), dot(worldPosition, modelMatrixCol2), 1.0);");
  106. if (scene.entityOffsetsEnabled) {
  107. src.push(" worldPosition.xyz = worldPosition.xyz + offset;");
  108. }
  109.  
  110. src.push("vec4 viewPosition = viewMatrix * worldPosition; ");
  111.  
  112. src.push("vec4 modelNormal = vec4(octDecode(normal.xy), 0.0); ");
  113. src.push("vec4 worldNormal = worldNormalMatrix * vec4(dot(modelNormal, modelNormalMatrixCol0), dot(modelNormal, modelNormalMatrixCol1), dot(modelNormal, modelNormalMatrixCol2), 1.0);");
  114. src.push("vec3 viewNormal = vec4(viewNormalMatrix * worldNormal).xyz;");
  115.  
  116. src.push("vec4 clipPos = projMatrix * viewPosition;");
  117. if (scene.logarithmicDepthBufferEnabled) {
  118. src.push("vFragDepth = 1.0 + clipPos.w;");
  119. src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");
  120. }
  121.  
  122. if (clipping) {
  123. src.push("vWorldPosition = worldPosition;");
  124. src.push("vFlags = flags;");
  125. if (clippingCaps) {
  126. src.push("vClipPosition = clipPos;");
  127. }
  128. }
  129.  
  130. src.push("vViewPosition = viewPosition;");
  131. src.push("vViewNormal = viewNormal;");
  132. src.push("vColor = color;");
  133. src.push("vUV = (uvDecodeMatrix * vec3(uv, 1.0)).xy;");
  134. src.push("vMetallicRoughness = metallicRoughness;");
  135.  
  136. if (lightsState.lightMaps.length > 0) {
  137. src.push("vWorldNormal = worldNormal.xyz;");
  138. }
  139.  
  140. src.push("gl_Position = clipPos;");
  141. src.push("}");
  142. src.push("}");
  143. return src;
  144. }
  145.  
  146. _buildFragmentShader() {
  147.  
  148. const scene = this._scene;
  149. const gammaOutput = scene.gammaOutput; // If set, then it expects that all textures and colors need to be outputted in premultiplied gamma. Default is false.
  150. const sectionPlanesState = scene._sectionPlanesState;
  151. const lightsState = scene._lightsState;
  152. const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
  153. const clippingCaps = sectionPlanesState.clippingCaps;
  154. const src = [];
  155. src.push("#version 300 es");
  156. src.push("// Instancing geometry quality drawing fragment shader");
  157.  
  158.  
  159. src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
  160. src.push("precision highp float;");
  161. src.push("precision highp int;");
  162. src.push("#else");
  163. src.push("precision mediump float;");
  164. src.push("precision mediump int;");
  165. src.push("#endif");
  166.  
  167. if (scene.logarithmicDepthBufferEnabled) {
  168. src.push("in float isPerspective;");
  169. src.push("uniform float logDepthBufFC;");
  170. src.push("in float vFragDepth;");
  171. }
  172.  
  173. src.push("uniform sampler2D uColorMap;");
  174. src.push("uniform sampler2D uMetallicRoughMap;");
  175. src.push("uniform sampler2D uEmissiveMap;");
  176. src.push("uniform sampler2D uNormalMap;");
  177.  
  178. if (this._withSAO) {
  179. src.push("uniform sampler2D uOcclusionTexture;");
  180. src.push("uniform vec4 uSAOParams;");
  181.  
  182. src.push("const float packUpscale = 256. / 255.;");
  183. src.push("const float unpackDownScale = 255. / 256.;");
  184. src.push("const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );");
  185. src.push("const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. );");
  186.  
  187. src.push("float unpackRGBToFloat( const in vec4 v ) {");
  188. src.push(" return dot( v, unPackFactors );");
  189. src.push("}");
  190. }
  191.  
  192. if (lightsState.reflectionMaps.length > 0) {
  193. src.push("uniform samplerCube reflectionMap;");
  194. }
  195.  
  196. if (lightsState.lightMaps.length > 0) {
  197. src.push("uniform samplerCube lightMap;");
  198. }
  199.  
  200. src.push("uniform vec4 lightAmbient;");
  201.  
  202. for (let i = 0, len = lightsState.lights.length; i < len; i++) {
  203. const light = lightsState.lights[i];
  204. if (light.type === "ambient") {
  205. continue;
  206. }
  207. src.push("uniform vec4 lightColor" + i + ";");
  208. if (light.type === "dir") {
  209. src.push("uniform vec3 lightDir" + i + ";");
  210. }
  211. if (light.type === "point") {
  212. src.push("uniform vec3 lightPos" + i + ";");
  213. }
  214. if (light.type === "spot") {
  215. src.push("uniform vec3 lightPos" + i + ";");
  216. src.push("uniform vec3 lightDir" + i + ";");
  217. }
  218. }
  219.  
  220. src.push("uniform float gammaFactor;");
  221. src.push("vec4 linearToLinear( in vec4 value ) {");
  222. src.push(" return value;");
  223. src.push("}");
  224. src.push("vec4 sRGBToLinear( in vec4 value ) {");
  225. src.push(" return vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.w );");
  226. src.push("}");
  227. src.push("vec4 gammaToLinear( in vec4 value) {");
  228. src.push(" return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );");
  229. src.push("}");
  230. if (gammaOutput) {
  231. src.push("vec4 linearToGamma( in vec4 value, in float gammaFactor ) {");
  232. src.push(" return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );");
  233. src.push("}");
  234. }
  235.  
  236. if (clipping) {
  237. src.push("in vec4 vWorldPosition;");
  238. src.push("in float vFlags;");
  239. if (clippingCaps) {
  240. src.push("in vec4 vClipPosition;");
  241. }
  242. for (let i = 0, len = sectionPlanesState.getNumAllocatedSectionPlanes(); i < len; i++) {
  243. src.push("uniform bool sectionPlaneActive" + i + ";");
  244. src.push("uniform vec3 sectionPlanePos" + i + ";");
  245. src.push("uniform vec3 sectionPlaneDir" + i + ";");
  246. }
  247. }
  248.  
  249. src.push("in vec4 vViewPosition;");
  250. src.push("in vec3 vViewNormal;");
  251. src.push("in vec4 vColor;");
  252. src.push("in vec2 vUV;");
  253. src.push("in vec2 vMetallicRoughness;");
  254.  
  255. if (lightsState.lightMaps.length > 0) {
  256. src.push("in vec3 vWorldNormal;");
  257. }
  258.  
  259. this._addMatricesUniformBlockLines(src, true);
  260.  
  261. // CONSTANT DEFINITIONS
  262.  
  263. src.push("#define PI 3.14159265359");
  264. src.push("#define RECIPROCAL_PI 0.31830988618");
  265. src.push("#define RECIPROCAL_PI2 0.15915494");
  266. src.push("#define EPSILON 1e-6");
  267.  
  268. src.push("#define saturate(a) clamp( a, 0.0, 1.0 )");
  269.  
  270. // UTILITY DEFINITIONS
  271.  
  272. src.push("vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec2 uv ) {");
  273. src.push(" vec3 texel = texture( uNormalMap, uv ).xyz;");
  274. src.push(" if (texel.r == 0.0 && texel.g == 0.0 && texel.b == 0.0) {");
  275. src.push(" return normalize(surf_norm );");
  276. src.push(" }");
  277. src.push(" vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );");
  278. src.push(" vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );");
  279. src.push(" vec2 st0 = dFdx( uv.st );");
  280. src.push(" vec2 st1 = dFdy( uv.st );");
  281. src.push(" vec3 S = normalize( q0 * st1.t - q1 * st0.t );");
  282. src.push(" vec3 T = normalize( -q0 * st1.s + q1 * st0.s );");
  283. src.push(" vec3 N = normalize( surf_norm );");
  284. src.push(" vec3 mapN = texel.xyz * 2.0 - 1.0;");
  285. src.push(" mat3 tsn = mat3( S, T, N );");
  286. // src.push(" mapN *= 3.0;");
  287. src.push(" return normalize( tsn * mapN );");
  288. src.push("}");
  289.  
  290. src.push("vec3 inverseTransformDirection(in vec3 dir, in mat4 matrix) {");
  291. src.push(" return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );");
  292. src.push("}");
  293.  
  294. // STRUCTURES
  295.  
  296. src.push("struct IncidentLight {");
  297. src.push(" vec3 color;");
  298. src.push(" vec3 direction;");
  299. src.push("};");
  300.  
  301. src.push("struct ReflectedLight {");
  302. src.push(" vec3 diffuse;");
  303. src.push(" vec3 specular;");
  304. src.push("};");
  305.  
  306. src.push("struct Geometry {");
  307. src.push(" vec3 position;");
  308. src.push(" vec3 viewNormal;");
  309. src.push(" vec3 worldNormal;");
  310. src.push(" vec3 viewEyeDir;");
  311. src.push("};");
  312.  
  313. src.push("struct Material {");
  314. src.push(" vec3 diffuseColor;");
  315. src.push(" float specularRoughness;");
  316. src.push(" vec3 specularColor;");
  317. src.push(" float shine;"); // Only used for Phong
  318. src.push("};");
  319.  
  320. // IRRADIANCE EVALUATION
  321.  
  322. src.push("float GGXRoughnessToBlinnExponent(const in float ggxRoughness) {");
  323. src.push(" float r = ggxRoughness + 0.0001;");
  324. src.push(" return (2.0 / (r * r) - 2.0);");
  325. src.push("}");
  326.  
  327. src.push("float getSpecularMIPLevel(const in float blinnShininessExponent, const in int maxMIPLevel) {");
  328. src.push(" float maxMIPLevelScalar = float( maxMIPLevel );");
  329. src.push(" float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( ( blinnShininessExponent * blinnShininessExponent ) + 1.0 );");
  330. src.push(" return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );");
  331. src.push("}");
  332.  
  333. if (lightsState.reflectionMaps.length > 0) {
  334. src.push("vec3 getLightProbeIndirectRadiance(const in vec3 reflectVec, const in float blinnShininessExponent, const in int maxMIPLevel) {");
  335. src.push(" float mipLevel = 0.5 * getSpecularMIPLevel(blinnShininessExponent, maxMIPLevel);"); //TODO: a random factor - fix this
  336. src.push(" vec3 envMapColor = " + TEXTURE_DECODE_FUNCS[lightsState.reflectionMaps[0].encoding] + "(texture(reflectionMap, reflectVec, mipLevel)).rgb;");
  337. src.push(" return envMapColor;");
  338. src.push("}");
  339. }
  340.  
  341. // SPECULAR BRDF EVALUATION
  342.  
  343. src.push("vec3 F_Schlick(const in vec3 specularColor, const in float dotLH) {");
  344. src.push(" float fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );");
  345. src.push(" return ( 1.0 - specularColor ) * fresnel + specularColor;");
  346. src.push("}");
  347.  
  348. src.push("float G_GGX_Smith(const in float alpha, const in float dotNL, const in float dotNV) {");
  349. src.push(" float a2 = ( alpha * alpha );");
  350. src.push(" float gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * ( dotNL * dotNL ) );");
  351. src.push(" float gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * ( dotNV * dotNV ) );");
  352. src.push(" return 1.0 / ( gl * gv );");
  353. src.push("}");
  354.  
  355. src.push("float G_GGX_SmithCorrelated(const in float alpha, const in float dotNL, const in float dotNV) {");
  356. src.push(" float a2 = ( alpha * alpha );");
  357. src.push(" float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * ( dotNV * dotNV ) );");
  358. src.push(" float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * ( dotNL * dotNL ) );");
  359. src.push(" return 0.5 / max( gv + gl, EPSILON );");
  360. src.push("}");
  361.  
  362. src.push("float D_GGX(const in float alpha, const in float dotNH) {");
  363. src.push(" float a2 = ( alpha * alpha );");
  364. src.push(" float denom = ( dotNH * dotNH) * ( a2 - 1.0 ) + 1.0;");
  365. src.push(" return RECIPROCAL_PI * a2 / ( denom * denom);");
  366. src.push("}");
  367.  
  368. src.push("vec3 BRDF_Specular_GGX(const in IncidentLight incidentLight, const in Geometry geometry, const in vec3 specularColor, const in float roughness) {");
  369. src.push(" float alpha = ( roughness * roughness );");
  370. src.push(" vec3 halfDir = normalize( incidentLight.direction + geometry.viewEyeDir );");
  371. src.push(" float dotNL = saturate( dot( geometry.viewNormal, incidentLight.direction ) );");
  372. src.push(" float dotNV = saturate( dot( geometry.viewNormal, geometry.viewEyeDir ) );");
  373. src.push(" float dotNH = saturate( dot( geometry.viewNormal, halfDir ) );");
  374. src.push(" float dotLH = saturate( dot( incidentLight.direction, halfDir ) );");
  375. src.push(" vec3 F = F_Schlick( specularColor, dotLH );");
  376. src.push(" float G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );");
  377. src.push(" float D = D_GGX( alpha, dotNH );");
  378. src.push(" return F * (G * D);");
  379. src.push("}");
  380.  
  381. src.push("vec3 BRDF_Specular_GGX_Environment(const in Geometry geometry, const in vec3 specularColor, const in float roughness) {");
  382. src.push(" float dotNV = saturate(dot(geometry.viewNormal, geometry.viewEyeDir));");
  383. src.push(" const vec4 c0 = vec4( -1, -0.0275, -0.572, 0.022);");
  384. src.push(" const vec4 c1 = vec4( 1, 0.0425, 1.04, -0.04);");
  385. src.push(" vec4 r = roughness * c0 + c1;");
  386. src.push(" float a004 = min(r.x * r.x, exp2(-9.28 * dotNV)) * r.x + r.y;");
  387. src.push(" vec2 AB = vec2(-1.04, 1.04) * a004 + r.zw;");
  388. src.push(" return specularColor * AB.x + AB.y;");
  389. src.push("}");
  390.  
  391. if (lightsState.lightMaps.length > 0 || lightsState.reflectionMaps.length > 0) {
  392.  
  393. src.push("void computePBRLightMapping(const in Geometry geometry, const in Material material, inout ReflectedLight reflectedLight) {");
  394.  
  395. if (lightsState.lightMaps.length > 0) {
  396. src.push(" vec3 irradiance = " + TEXTURE_DECODE_FUNCS[lightsState.lightMaps[0].encoding] + "(texture(lightMap, geometry.worldNormal)).rgb;");
  397. src.push(" irradiance *= PI;");
  398. src.push(" vec3 diffuseBRDFContrib = (RECIPROCAL_PI * material.diffuseColor);");
  399. src.push(" reflectedLight.diffuse += irradiance * diffuseBRDFContrib;");
  400. }
  401.  
  402. if (lightsState.reflectionMaps.length > 0) {
  403. src.push(" vec3 reflectVec = reflect(geometry.viewEyeDir, geometry.viewNormal);");
  404. src.push(" reflectVec = inverseTransformDirection(reflectVec, viewMatrix);");
  405. src.push(" float blinnExpFromRoughness = GGXRoughnessToBlinnExponent(material.specularRoughness);");
  406. src.push(" vec3 radiance = getLightProbeIndirectRadiance(reflectVec, blinnExpFromRoughness, 8);");
  407. src.push(" vec3 specularBRDFContrib = BRDF_Specular_GGX_Environment(geometry, material.specularColor, material.specularRoughness);");
  408. src.push(" reflectedLight.specular += radiance * specularBRDFContrib;");
  409. }
  410.  
  411. src.push("}");
  412. }
  413.  
  414. // MAIN LIGHTING COMPUTATION FUNCTION
  415.  
  416. src.push("void computePBRLighting(const in IncidentLight incidentLight, const in Geometry geometry, const in Material material, inout ReflectedLight reflectedLight) {");
  417. src.push(" float dotNL = saturate(dot(geometry.viewNormal, incidentLight.direction));");
  418. src.push(" vec3 irradiance = dotNL * incidentLight.color * PI;");
  419. src.push(" reflectedLight.diffuse += irradiance * (RECIPROCAL_PI * material.diffuseColor);");
  420. src.push(" reflectedLight.specular += irradiance * BRDF_Specular_GGX(incidentLight, geometry, material.specularColor, material.specularRoughness);");
  421. src.push("}");
  422.  
  423. src.push("out vec4 outColor;");
  424.  
  425. src.push("void main(void) {");
  426.  
  427. if (clipping) {
  428. src.push(" bool clippable = (int(vFlags) >> 16 & 0xF) == 1;");
  429. src.push(" if (clippable) {");
  430. src.push(" float dist = 0.0;");
  431. for (let i = 0, len = sectionPlanesState.getNumAllocatedSectionPlanes(); i < len; i++) {
  432. src.push("if (sectionPlaneActive" + i + ") {");
  433. src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
  434. src.push("}");
  435. }
  436. if (clippingCaps) {
  437. src.push(" if (dist > (0.002 * vClipPosition.w)) {");
  438. src.push(" discard;");
  439. src.push(" }");
  440. src.push(" if (dist > 0.0) { ");
  441. src.push(" outColor=vec4(1.0, 0.0, 0.0, 1.0);");
  442. if (scene.logarithmicDepthBufferEnabled) {
  443. src.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;");
  444. }
  445. src.push(" return;");
  446. src.push("}");
  447. } else {
  448. src.push(" if (dist > 0.0) { ");
  449. src.push(" discard;")
  450. src.push(" }");
  451. }
  452. src.push("}");
  453. }
  454.  
  455. src.push("IncidentLight light;");
  456. src.push("Material material;");
  457. src.push("Geometry geometry;");
  458. src.push("ReflectedLight reflectedLight = ReflectedLight(vec3(0.0,0.0,0.0), vec3(0.0,0.0,0.0));");
  459.  
  460. src.push("vec3 rgb = (vec3(float(vColor.r) / 255.0, float(vColor.g) / 255.0, float(vColor.b) / 255.0));");
  461. src.push("float opacity = float(vColor.a) / 255.0;");
  462.  
  463. src.push("vec3 baseColor = rgb;");
  464. src.push("float specularF0 = 1.0;");
  465. src.push("float metallic = float(vMetallicRoughness.r) / 255.0;");
  466. src.push("float roughness = float(vMetallicRoughness.g) / 255.0;");
  467. src.push("float dielectricSpecular = 0.16 * specularF0 * specularF0;");
  468.  
  469. src.push("vec4 colorTexel = sRGBToLinear(texture(uColorMap, vUV));");
  470. src.push("baseColor *= colorTexel.rgb;");
  471. // src.push("opacity = colorTexel.a;");
  472.  
  473. src.push("vec3 metalRoughTexel = texture(uMetallicRoughMap, vUV).rgb;");
  474. src.push("metallic *= metalRoughTexel.b;");
  475. src.push("roughness *= metalRoughTexel.g;");
  476.  
  477. src.push("vec3 viewNormal = perturbNormal2Arb( vViewPosition.xyz, normalize(vViewNormal), vUV );");
  478.  
  479. src.push("material.diffuseColor = baseColor * (1.0 - dielectricSpecular) * (1.0 - metallic);");
  480. src.push("material.specularRoughness = clamp(roughness, 0.04, 1.0);");
  481. src.push("material.specularColor = mix(vec3(dielectricSpecular), baseColor, metallic);");
  482.  
  483. src.push("geometry.position = vViewPosition.xyz;");
  484. src.push("geometry.viewNormal = -normalize(viewNormal);");
  485. src.push("geometry.viewEyeDir = normalize(vViewPosition.xyz);");
  486.  
  487. if (lightsState.lightMaps.length > 0) {
  488. src.push("geometry.worldNormal = normalize(vWorldNormal);");
  489. }
  490.  
  491. if (lightsState.lightMaps.length > 0 || lightsState.reflectionMaps.length > 0) {
  492. src.push("computePBRLightMapping(geometry, material, reflectedLight);");
  493. }
  494.  
  495. for (let i = 0, len = lightsState.lights.length; i < len; i++) {
  496.  
  497. const light = lightsState.lights[i];
  498.  
  499. if (light.type === "ambient") {
  500. continue;
  501. }
  502. if (light.type === "dir") {
  503. if (light.space === "view") {
  504. src.push("light.direction = normalize(lightDir" + i + ");");
  505. } else {
  506. src.push("light.direction = normalize((viewMatrix * vec4(lightDir" + i + ", 0.0)).xyz);");
  507. }
  508. } else if (light.type === "point") {
  509. if (light.space === "view") {
  510. src.push("light.direction = normalize(lightPos" + i + " - vViewPosition.xyz);");
  511. } else {
  512. src.push("light.direction = normalize((viewMatrix * vec4(lightPos" + i + ", 0.0)).xyz);");
  513. }
  514. } else if (light.type === "spot") {
  515. if (light.space === "view") {
  516. src.push("light.direction = normalize(lightDir" + i + ");");
  517. } else {
  518. src.push("light.direction = normalize((viewMatrix * vec4(lightDir" + i + ", 0.0)).xyz);");
  519. }
  520. } else {
  521. continue;
  522. }
  523.  
  524. src.push("light.color = lightColor" + i + ".rgb * lightColor" + i + ".a;"); // a is intensity
  525.  
  526. src.push("computePBRLighting(light, geometry, material, reflectedLight);");
  527. }
  528.  
  529. src.push("vec3 emissiveColor = sRGBToLinear(texture(uEmissiveMap, vUV)).rgb;"); // TODO: correct gamma function
  530.  
  531. src.push("vec3 outgoingLight = (lightAmbient.rgb * lightAmbient.a * baseColor * opacity * rgb) + (reflectedLight.diffuse) + (reflectedLight.specular) + emissiveColor;");
  532. src.push("vec4 fragColor;");
  533.  
  534. if (this._withSAO) {
  535. // Doing SAO blend in the main solid fill draw shader just so that edge lines can be drawn over the top
  536. // Would be more efficient to defer this, then render lines later, using same depth buffer for Z-reject
  537. src.push(" float viewportWidth = uSAOParams[0];");
  538. src.push(" float viewportHeight = uSAOParams[1];");
  539. src.push(" float blendCutoff = uSAOParams[2];");
  540. src.push(" float blendFactor = uSAOParams[3];");
  541. src.push(" vec2 uv = vec2(gl_FragCoord.x / viewportWidth, gl_FragCoord.y / viewportHeight);");
  542. src.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture(uOcclusionTexture, uv))) * blendFactor;");
  543. src.push(" fragColor = vec4(outgoingLight.rgb * ambient, opacity);");
  544. } else {
  545. src.push(" fragColor = vec4(outgoingLight.rgb, opacity);");
  546. }
  547.  
  548. if (gammaOutput) {
  549. src.push("fragColor = linearToGamma(fragColor, gammaFactor);");
  550. }
  551.  
  552. src.push("outColor = fragColor;");
  553.  
  554. if (scene.logarithmicDepthBufferEnabled) {
  555. src.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
  556. }
  557.  
  558. src.push("}");
  559. return src;
  560. }
  561. }