diff --git a/build/app.js b/build/app.js index 4cc2e51..386eb68 100644 --- a/build/app.js +++ b/build/app.js @@ -2783,12 +2783,12 @@ var Utils = Utils || {}; gl.texImage2D( gl.TEXTURE_2D, // assumed 0, // Level of details - // gl.RGB, // Format - // gl.RGB, - gl.RGBA, // Format - gl.RGBA, - gl.UNSIGNED_BYTE, // Size of each channel - // gl.FLOAT, + gl.RG16F, // Format + gl.RG, + // gl.RGBA, // Format + // gl.RGBA, + // gl.UNSIGNED_BYTE, // Size of each channel + gl.FLOAT, img ); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); @@ -2825,6 +2825,20 @@ var Utils = Utils || {}; '../textures/environment/diffuse/bakedDiffuse_05.jpg', '../textures/environment/diffuse/bakedDiffuse_06.jpg', + // '../textures/papermill/environment_right_0.jpg', + // '../textures/papermill/environment_left_0.jpg', + // '../textures/papermill/environment_top_0.jpg', + // '../textures/papermill/environment_bottom_0.jpg', + // '../textures/papermill/environment_front_0.jpg', + // '../textures/papermill/environment_back_0.jpg', + + // '../textures/papermill/diffuse/diffuse_right_0.jpg', + // '../textures/papermill/diffuse/diffuse_left_0.jpg', + // '../textures/papermill/diffuse/diffuse_top_0.jpg', + // '../textures/papermill/diffuse/diffuse_bottom_0.jpg', + // '../textures/papermill/diffuse/diffuse_front_0.jpg', + // '../textures/papermill/diffuse/diffuse_back_0.jpg', + // @tmp, ugly, load brdfLUT here '../textures/brdfLUT.png' ], @@ -3110,6 +3124,7 @@ var Utils = Utils || {}; us.MVP = gl.getUniformLocation(program, 'u_MVP'); us.MVNormal = gl.getUniformLocation(program, 'u_MVNormal'); + us.MV = gl.getUniformLocation(program, 'u_MV'); us.baseColorFactor = gl.getUniformLocation(program, 'u_baseColorFactor'); us.metallicFactor = gl.getUniformLocation(program, 'u_metallicFactor'); us.roughnessFactor = gl.getUniformLocation(program, 'u_roughnessFactor'); @@ -3608,8 +3623,10 @@ var Utils = Utils || {}; gl.uniform4fv(program.uniformLocations.baseColorFactor, baseColor); + gl.uniformMatrix4fv(program.uniformLocations.MV, false, localMV); gl.uniformMatrix4fv(program.uniformLocations.MVP, false, localMVP); gl.uniformMatrix4fv(program.uniformLocations.MVNormal, false, localMVNormal); + gl.bindVertexArray(primitive.vertexArray); @@ -9833,13 +9850,13 @@ module.exports = "#version 300 es\r\nprecision highp float;\r\nprecision highp i /* 21 */ /***/ (function(module, exports) { -module.exports = "#define POSITION_LOCATION 0\r\n#define NORMAL_LOCATION 1\r\n#define TEXCOORD_0_LOCATION 2\r\n#define JOINTS_0_LOCATION 3\r\n#define JOINTS_1_LOCATION 5\r\n#define WEIGHTS_0_LOCATION 4\r\n#define WEIGHTS_1_LOCATION 6\r\n\r\nprecision highp float;\r\nprecision highp int;\r\n\r\nuniform mat4 u_MVP;\r\nuniform mat4 u_MVNormal;\r\n\r\n#ifdef HAS_SKIN\r\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\r\n#endif\r\n\r\nlayout(location = POSITION_LOCATION) in vec3 position;\r\nlayout(location = NORMAL_LOCATION) in vec3 normal;\r\nlayout(location = TEXCOORD_0_LOCATION) in vec2 uv;\r\n\r\n#ifdef HAS_SKIN\r\nlayout(location = JOINTS_0_LOCATION) in vec4 joint0;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight0;\r\n#ifdef SKIN_VEC8\r\nlayout(location = JOINTS_1_LOCATION) in vec4 joint1;\r\nlayout(location = WEIGHTS_1_LOCATION) in vec4 weight1;\r\n#endif\r\n#endif\r\n// TODO: tangents\r\n\r\nout vec3 v_normal;\r\nout vec2 v_uv;\r\n\r\nvoid main()\r\n{\r\n\r\n#ifdef HAS_SKIN\r\n mat4 skinMatrix = \r\n weight0.x * u_jointMatrix.matrix[int(joint0.x)] +\r\n weight0.y * u_jointMatrix.matrix[int(joint0.y)] +\r\n weight0.z * u_jointMatrix.matrix[int(joint0.z)] +\r\n weight0.w * u_jointMatrix.matrix[int(joint0.w)];\r\n#ifdef SKIN_VEC8\r\n skinMatrix +=\r\n weight1.x * u_jointMatrix.matrix[int(joint1.x)] +\r\n weight1.y * u_jointMatrix.matrix[int(joint1.y)] +\r\n weight1.z * u_jointMatrix.matrix[int(joint1.z)] +\r\n weight1.w * u_jointMatrix.matrix[int(joint1.w)];\r\n#endif\r\n#endif\r\n\r\n v_uv = uv;\r\n\r\n#ifdef HAS_SKIN\r\n v_normal = normalize(( u_MVNormal * transpose(inverse(skinMatrix)) * vec4(normal, 0)).xyz);\r\n gl_Position = u_MVP * skinMatrix * vec4(position, 1.0);\r\n#else\r\n v_normal = normalize((u_MVNormal * vec4(normal, 0)).xyz);\r\n gl_Position = u_MVP * vec4(position, 1.0);\r\n#endif\r\n \r\n}" +module.exports = "#define POSITION_LOCATION 0\r\n#define NORMAL_LOCATION 1\r\n#define TEXCOORD_0_LOCATION 2\r\n#define JOINTS_0_LOCATION 3\r\n#define JOINTS_1_LOCATION 5\r\n#define WEIGHTS_0_LOCATION 4\r\n#define WEIGHTS_1_LOCATION 6\r\n#define TANGENT_LOCATION 7\r\n\r\nprecision highp float;\r\nprecision highp int;\r\n\r\nuniform mat4 u_MVP;\r\nuniform mat4 u_MV;\r\nuniform mat4 u_MVNormal;\r\n\r\n#ifdef HAS_SKIN\r\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\r\n#endif\r\n\r\nlayout(location = POSITION_LOCATION) in vec3 position;\r\nlayout(location = NORMAL_LOCATION) in vec3 normal;\r\nlayout(location = TEXCOORD_0_LOCATION) in vec2 uv;\r\n\r\n#ifdef HAS_SKIN\r\nlayout(location = JOINTS_0_LOCATION) in vec4 joint0;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight0;\r\n#ifdef SKIN_VEC8\r\nlayout(location = JOINTS_1_LOCATION) in vec4 joint1;\r\nlayout(location = WEIGHTS_1_LOCATION) in vec4 weight1;\r\n#endif\r\n#endif\r\n\r\n\r\n// #ifdef HAS_TANGENTS\r\n// layout(location = TANGENT_LOCATION) in vec4 tangent;\r\n\r\n// out vec3 v_tangentW;\r\n// out vec3 v_bitangentW;\r\n// #endif\r\n\r\n\r\nout vec3 v_position;\r\nout vec3 v_normal;\r\nout vec2 v_uv;\r\n\r\nvoid main()\r\n{\r\n\r\n#ifdef HAS_SKIN\r\n mat4 skinMatrix = \r\n weight0.x * u_jointMatrix.matrix[int(joint0.x)] +\r\n weight0.y * u_jointMatrix.matrix[int(joint0.y)] +\r\n weight0.z * u_jointMatrix.matrix[int(joint0.z)] +\r\n weight0.w * u_jointMatrix.matrix[int(joint0.w)];\r\n#ifdef SKIN_VEC8\r\n skinMatrix +=\r\n weight1.x * u_jointMatrix.matrix[int(joint1.x)] +\r\n weight1.y * u_jointMatrix.matrix[int(joint1.y)] +\r\n weight1.z * u_jointMatrix.matrix[int(joint1.z)] +\r\n weight1.w * u_jointMatrix.matrix[int(joint1.w)];\r\n#endif\r\n#endif\r\n\r\n v_uv = uv;\r\n\r\n#ifdef HAS_SKIN\r\n v_normal = normalize(( u_MVNormal * transpose(inverse(skinMatrix)) * vec4(normal, 0)).xyz);\r\n vec4 pos = u_MV * skinMatrix * vec4(position, 1.0);\r\n gl_Position = u_MVP * skinMatrix * vec4(position, 1.0);\r\n#else\r\n v_normal = normalize((u_MVNormal * vec4(normal, 0)).xyz);\r\n vec4 pos = u_MV * vec4(position, 1.0);\r\n gl_Position = u_MVP * vec4(position, 1.0);\r\n#endif\r\n\r\n v_position = vec3(pos.xyz) / pos.w;\r\n \r\n \r\n}" /***/ }), /* 22 */ /***/ (function(module, exports) { -module.exports = "#define FRAG_COLOR_LOCATION 0\r\n\r\n// reference: https://github.com/KhronosGroup/glTF-WebGL-PBR/blob/master/shaders/pbr-frag.glsl\r\n\r\nprecision highp float;\r\nprecision highp int;\r\n\r\n// IBL\r\nuniform samplerCube u_DiffuseEnvSampler;\r\nuniform samplerCube u_SpecularEnvSampler;\r\nuniform sampler2D u_brdfLUT;\r\n\r\n// Metallic-roughness material\r\n\r\n// TODO: use #define string for a full and dynamic support renderer\r\n\r\n// base color\r\nuniform vec4 u_baseColorFactor;\r\n#ifdef HAS_BASECOLORMAP\r\nuniform sampler2D u_baseColorTexture;\r\n#endif\r\n\r\n// normal map\r\n#ifdef HAS_NORMALMAP\r\nuniform sampler2D u_normalTexture;\r\nuniform float u_normalScale;\r\n#endif\r\n\r\n// emmisve map\r\n#ifdef HAS_EMISSIVEMAP\r\nuniform sampler2D u_emissiveTexture;\r\nuniform vec3 u_emissiveFactor;\r\n#endif\r\n\r\n// metal roughness\r\n#ifdef HAS_METALROUGHNESSMAP\r\nuniform sampler2D u_metallicRoughnessTexture;\r\n#endif\r\nuniform float u_metallicFactor;\r\nuniform float u_roughnessFactor;\r\n\r\n// occlusion texture\r\n#ifdef HAS_OCCLUSIONMAP\r\nuniform sampler2D u_occlusionTexture;\r\nuniform float u_occlusionStrength;\r\n#endif\r\n\r\nin vec3 v_normal;\r\nin vec2 v_uv;\r\n\r\nlayout(location = FRAG_COLOR_LOCATION) out vec4 frag_color;\r\n\r\nstruct PBRInfo\r\n{\r\n float NdotL; // cos angle between normal and light direction\r\n float NdotV; // cos angle between normal and view direction\r\n float NdotH; // cos angle between normal and half vector\r\n float LdotH; // cos angle between light direction and half vector\r\n float VdotH; // cos angle between view direction and half vector\r\n float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)\r\n float metalness; // metallic value at the surface\r\n vec3 reflectance0; // full reflectance color (normal incidence angle)\r\n vec3 reflectance90; // reflectance color at grazing angle\r\n float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])\r\n vec3 diffuseColor; // color contribution from diffuse lighting\r\n vec3 specularColor; // color contribution from specular lighting\r\n};\r\n\r\n\r\nvec3 applyNormalMap(vec3 geomnor, vec3 normap) {\r\n normap = normap * 2.0 - 1.0;\r\n vec3 up = normalize(vec3(0.001, 1, 0.001));\r\n vec3 surftan = normalize(cross(geomnor, up));\r\n vec3 surfbinor = cross(geomnor, surftan);\r\n return normap.y * surftan + normap.x * surfbinor + normap.z * geomnor;\r\n}\r\n\r\nconst float M_PI = 3.141592653589793;\r\nconst float c_MinRoughness = 0.04;\r\n\r\n\r\nvec3 getIBLContribution(PBRInfo pbrInputs, vec3 n, vec3 reflection)\r\n{\r\n // float mipCount = 9.0; // resolution of 512x512\r\n float mipCount = 10.0; // resolution of 1024x1024\r\n float lod = (pbrInputs.perceptualRoughness * mipCount);\r\n // retrieve a scale and bias to F0. See [1], Figure 3\r\n vec3 brdf = texture(u_brdfLUT, vec2(pbrInputs.NdotV, 1.0 - pbrInputs.perceptualRoughness)).rgb;\r\n vec3 diffuseLight = texture(u_DiffuseEnvSampler, n).rgb;\r\n\r\n// #ifdef USE_TEX_LOD\r\n vec3 specularLight = texture(u_SpecularEnvSampler, reflection, lod).rgb;\r\n// #else\r\n // vec3 specularLight = texture(u_SpecularEnvSampler, reflection).rgb;\r\n// #endif\r\n\r\n vec3 diffuse = diffuseLight * pbrInputs.diffuseColor;\r\n vec3 specular = specularLight * (pbrInputs.specularColor * brdf.x + brdf.y);\r\n\r\n // // For presentation, this allows us to disable IBL terms\r\n // diffuse *= u_ScaleIBLAmbient.x;\r\n // specular *= u_ScaleIBLAmbient.y;\r\n\r\n return diffuse + specular;\r\n}\r\n\r\n// Basic Lambertian diffuse\r\n// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog\r\n// See also [1], Equation 1\r\nvec3 diffuse(PBRInfo pbrInputs)\r\n{\r\n return pbrInputs.diffuseColor / M_PI;\r\n}\r\n\r\n\r\n// The following equation models the Fresnel reflectance term of the spec equation (aka F())\r\n// Implementation of fresnel from [4], Equation 15\r\nvec3 specularReflection(PBRInfo pbrInputs)\r\n{\r\n return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0);\r\n}\r\n\r\n\r\n// This calculates the specular geometric attenuation (aka G()),\r\n// where rougher material will reflect less light back to the viewer.\r\n// This implementation is based on [1] Equation 4, and we adopt their modifications to\r\n// alphaRoughness as input as originally proposed in [2].\r\nfloat geometricOcclusion(PBRInfo pbrInputs)\r\n{\r\n float NdotL = pbrInputs.NdotL;\r\n float NdotV = pbrInputs.NdotV;\r\n float r = pbrInputs.alphaRoughness;\r\n\r\n float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));\r\n float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));\r\n return attenuationL * attenuationV;\r\n}\r\n\r\n\r\n// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())\r\n// Implementation from \"Average Irregularity Representation of a Roughened Surface for Ray Reflection\" by T. S. Trowbridge, and K. P. Reitz\r\n// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.\r\nfloat microfacetDistribution(PBRInfo pbrInputs)\r\n{\r\n float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;\r\n float f = (pbrInputs.NdotH * roughnessSq - pbrInputs.NdotH) * pbrInputs.NdotH + 1.0;\r\n return roughnessSq / (M_PI * f * f);\r\n}\r\n\r\n\r\n\r\n\r\n\r\n\r\nvoid main()\r\n{\r\n float perceptualRoughness = u_roughnessFactor;\r\n float metallic = u_metallicFactor;\r\n\r\n#ifdef HAS_METALROUGHNESSMAP\r\n // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.\r\n // This layout intentionally reserves the 'r' channel for (optional) occlusion map data\r\n vec4 mrSample = texture(u_metallicRoughnessTexture, v_uv);\r\n perceptualRoughness = mrSample.g * perceptualRoughness;\r\n metallic = mrSample.b * metallic;\r\n#endif\r\n perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);\r\n metallic = clamp(metallic, 0.0, 1.0);\r\n // Roughness is authored as perceptual roughness; as is convention,\r\n // convert to material roughness by squaring the perceptual roughness [2].\r\n float alphaRoughness = perceptualRoughness * perceptualRoughness;\r\n\r\n\r\n // The albedo may be defined from a base texture or a flat color\r\n#ifdef HAS_BASECOLORMAP\r\n vec4 baseColor = texture(u_baseColorTexture, v_uv) * u_baseColorFactor;\r\n#else\r\n vec4 baseColor = u_baseColorFactor;\r\n#endif\r\n\r\n\r\n\r\n vec3 f0 = vec3(0.04);\r\n vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);\r\n diffuseColor *= 1.0 - metallic;\r\n vec3 specularColor = mix(f0, baseColor.rgb, metallic);\r\n\r\n // Compute reflectance.\r\n float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\r\n\r\n\r\n // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.\r\n // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.\r\n float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);\r\n vec3 specularEnvironmentR0 = specularColor.rgb;\r\n vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;\r\n\r\n\r\n // vec3 n = getNormal(); // normal at surface point\r\n#ifdef HAS_NORMALMAP\r\n vec3 n = applyNormalMap( v_normal, texture(u_normalTexture, v_uv).rgb );\r\n#else\r\n vec3 n = v_normal;\r\n#endif\r\n vec3 v = vec3( 0.0, 0.0, 1.0 ); // Vector from surface point to camera\r\n // vec3 l = normalize(u_LightDirection); // Vector from surface point to light\r\n vec3 l = vec3( 0.6, 0.8, 0.0 ); // Vector from surface point to light\r\n vec3 h = normalize(l+v); // Half vector between both l and v\r\n vec3 reflection = -normalize(reflect(v, n));\r\n\r\n float NdotL = clamp(dot(n, l), 0.001, 1.0);\r\n float NdotV = abs(dot(n, v)) + 0.001;\r\n float NdotH = clamp(dot(n, h), 0.0, 1.0);\r\n float LdotH = clamp(dot(l, h), 0.0, 1.0);\r\n float VdotH = clamp(dot(v, h), 0.0, 1.0);\r\n\r\n PBRInfo pbrInputs = PBRInfo(\r\n NdotL,\r\n NdotV,\r\n NdotH,\r\n LdotH,\r\n VdotH,\r\n perceptualRoughness,\r\n metallic,\r\n specularEnvironmentR0,\r\n specularEnvironmentR90,\r\n alphaRoughness,\r\n diffuseColor,\r\n specularColor\r\n );\r\n\r\n // Calculate the shading terms for the microfacet specular shading model\r\n vec3 F = specularReflection(pbrInputs);\r\n float G = geometricOcclusion(pbrInputs);\r\n float D = microfacetDistribution(pbrInputs);\r\n\r\n // Calculation of analytical lighting contribution\r\n vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);\r\n vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);\r\n // vec3 color = NdotL * u_LightColor * (diffuseContrib + specContrib);\r\n vec3 color = NdotL * (diffuseContrib + specContrib); // assume light color vec3(1, 1, 1)\r\n\r\n // Calculate lighting contribution from image based lighting source (IBL)\r\n// #ifdef USE_IBL\r\n color += getIBLContribution(pbrInputs, n, reflection);\r\n// #endif\r\n\r\n\r\n // Apply optional PBR terms for additional (optional) shading\r\n#ifdef HAS_OCCLUSIONMAP\r\n float ao = texture(u_occlusionTexture, v_uv).r;\r\n color = mix(color, color * ao, u_occlusionStrength);\r\n#endif\r\n\r\n#ifdef HAS_EMISSIVEMAP\r\n vec3 emissive = texture(u_emissiveTexture, v_uv).rgb * u_emissiveFactor;\r\n color += emissive;\r\n#endif\r\n\r\n // // This section uses mix to override final color for reference app visualization\r\n // // of various parameters in the lighting equation.\r\n // color = mix(color, F, u_ScaleFGDSpec.x);\r\n // color = mix(color, vec3(G), u_ScaleFGDSpec.y);\r\n // color = mix(color, vec3(D), u_ScaleFGDSpec.z);\r\n // color = mix(color, specContrib, u_ScaleFGDSpec.w);\r\n\r\n // color = mix(color, diffuseContrib, u_ScaleDiffBaseMR.x);\r\n // color = mix(color, baseColor.rgb, u_ScaleDiffBaseMR.y);\r\n // color = mix(color, vec3(metallic), u_ScaleDiffBaseMR.z);\r\n // color = mix(color, vec3(perceptualRoughness), u_ScaleDiffBaseMR.w);\r\n\r\n frag_color = vec4(color, baseColor.a);\r\n}" +module.exports = "#define FRAG_COLOR_LOCATION 0\r\n\r\n// reference: https://github.com/KhronosGroup/glTF-WebGL-PBR/blob/master/shaders/pbr-frag.glsl\r\n\r\nprecision highp float;\r\nprecision highp int;\r\n\r\n// IBL\r\nuniform samplerCube u_DiffuseEnvSampler;\r\nuniform samplerCube u_SpecularEnvSampler;\r\nuniform sampler2D u_brdfLUT;\r\n\r\n// Metallic-roughness material\r\n\r\n// base color\r\nuniform vec4 u_baseColorFactor;\r\n#ifdef HAS_BASECOLORMAP\r\nuniform sampler2D u_baseColorTexture;\r\n#endif\r\n\r\n// normal map\r\n#ifdef HAS_NORMALMAP\r\nuniform sampler2D u_normalTexture;\r\nuniform float u_normalTextureScale;\r\n#endif\r\n\r\n// emmisve map\r\n#ifdef HAS_EMISSIVEMAP\r\nuniform sampler2D u_emissiveTexture;\r\nuniform vec3 u_emissiveFactor;\r\n#endif\r\n\r\n// metal roughness\r\n#ifdef HAS_METALROUGHNESSMAP\r\nuniform sampler2D u_metallicRoughnessTexture;\r\n#endif\r\nuniform float u_metallicFactor;\r\nuniform float u_roughnessFactor;\r\n\r\n// occlusion texture\r\n#ifdef HAS_OCCLUSIONMAP\r\nuniform sampler2D u_occlusionTexture;\r\nuniform float u_occlusionStrength;\r\n#endif\r\n\r\nin vec3 v_position;\r\nin vec3 v_normal;\r\nin vec2 v_uv;\r\n\r\nlayout(location = FRAG_COLOR_LOCATION) out vec4 frag_color;\r\n\r\nstruct PBRInfo\r\n{\r\n float NdotL; // cos angle between normal and light direction\r\n float NdotV; // cos angle between normal and view direction\r\n float NdotH; // cos angle between normal and half vector\r\n float LdotH; // cos angle between light direction and half vector\r\n float VdotH; // cos angle between view direction and half vector\r\n float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)\r\n float metalness; // metallic value at the surface\r\n vec3 reflectance0; // full reflectance color (normal incidence angle)\r\n vec3 reflectance90; // reflectance color at grazing angle\r\n float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])\r\n vec3 diffuseColor; // color contribution from diffuse lighting\r\n vec3 specularColor; // color contribution from specular lighting\r\n};\r\n\r\n\r\n// vec3 applyNormalMap(vec3 geomnor, vec3 normap) {\r\n// normap = normap * 2.0 - 1.0;\r\n// vec3 up = normalize(vec3(0.01, 1, 0.01));\r\n// vec3 surftan = normalize(cross(geomnor, up));\r\n// vec3 surfbinor = cross(geomnor, surftan);\r\n// return normap.y * surftan * u_normalTextureScale + normap.x * surfbinor * u_normalTextureScale + normap.z * geomnor;\r\n// }\r\n\r\nconst float M_PI = 3.141592653589793;\r\nconst float c_MinRoughness = 0.04;\r\n\r\n\r\n// vec3 getNormal()\r\n// {\r\n\r\n// #ifdef HAS_NORMALMAP\r\n// #ifdef HAS_TANGENTS\r\n// vec3 n = texture(u_normalTexture, v_uv).rgb;\r\n// n = normalize(v_TBN * (2.0 * n - 1.0) - vec3(u_normalTextureScale, u_normalTextureScale, 1.0));\r\n// #else\r\n// vec3 n = applyNormalMap( v_normal, texture(u_normalTexture, v_uv).rgb );\r\n// #endif\r\n// #else\r\n// vec3 n = v_normal;\r\n// #endif\r\n// return n;\r\n\r\n// #endif\r\n// }\r\n\r\n// Find the normal for this fragment, pulling either from a predefined normal map\r\n// or from the interpolated mesh normal and tangent attributes.\r\nvec3 getNormal()\r\n{\r\n\r\n// #ifdef HAS_NORMALMAP\r\n// vec3 n = applyNormalMap( v_normal, texture(u_normalTexture, v_uv).rgb );\r\n// #else\r\n// vec3 n = v_normal;\r\n// #endif\r\n// return n;\r\n\r\n\r\n // Retrieve the tangent space matrix\r\n// #ifndef HAS_TANGENTS\r\n vec3 pos_dx = dFdx(v_position);\r\n vec3 pos_dy = dFdy(v_position);\r\n vec3 tex_dx = dFdx(vec3(v_uv, 0.0));\r\n vec3 tex_dy = dFdy(vec3(v_uv, 0.0));\r\n vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);\r\n\r\n vec3 ng = v_normal;\r\n// #ifdef HAS_NORMALS\r\n// vec3 ng = normalize(v_normal);\r\n// #else\r\n// vec3 ng = cross(pos_dx, pos_dy);\r\n// #endif\r\n\r\n t = normalize(t - ng * dot(ng, t));\r\n vec3 b = normalize(cross(ng, t));\r\n mat3 tbn = mat3(t, b, ng);\r\n// #else // HAS_TANGENTS\r\n // mat3 tbn = v_TBN;\r\n// #endif\r\n\r\n// TODO: TANGENTS\r\n\r\n#ifdef HAS_NORMALMAP\r\n vec3 n = texture(u_normalTexture, v_uv).rgb;\r\n n = normalize(tbn * ((2.0 * n - 1.0) * vec3(u_normalTextureScale, u_normalTextureScale, 1.0)));\r\n#else\r\n vec3 n = tbn[2].xyz;\r\n#endif\r\n\r\n return n;\r\n}\r\n\r\nvec3 getIBLContribution(PBRInfo pbrInputs, vec3 n, vec3 reflection)\r\n{\r\n // float mipCount = 9.0; // resolution of 512x512\r\n // float mipCount = 10.0; // resolution of 1024x1024\r\n float mipCount = 10.0; // resolution of 256x256\r\n float lod = (pbrInputs.perceptualRoughness * mipCount);\r\n // retrieve a scale and bias to F0. See [1], Figure 3\r\n vec3 brdf = texture(u_brdfLUT, vec2(pbrInputs.NdotV, 1.0 - pbrInputs.perceptualRoughness)).rgb;\r\n vec3 diffuseLight = texture(u_DiffuseEnvSampler, n).rgb;\r\n\r\n// #ifdef USE_TEX_LOD\r\n vec3 specularLight = texture(u_SpecularEnvSampler, reflection, lod).rgb;\r\n// #else\r\n // vec3 specularLight = texture(u_SpecularEnvSampler, reflection).rgb;\r\n// #endif\r\n\r\n vec3 diffuse = diffuseLight * pbrInputs.diffuseColor;\r\n vec3 specular = specularLight * (pbrInputs.specularColor * brdf.x + brdf.y);\r\n\r\n // // For presentation, this allows us to disable IBL terms\r\n // diffuse *= u_ScaleIBLAmbient.x;\r\n // specular *= u_ScaleIBLAmbient.y;\r\n\r\n return diffuse + specular;\r\n}\r\n\r\n// Basic Lambertian diffuse\r\n// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog\r\n// See also [1], Equation 1\r\nvec3 diffuse(PBRInfo pbrInputs)\r\n{\r\n return pbrInputs.diffuseColor / M_PI;\r\n}\r\n\r\n\r\n// The following equation models the Fresnel reflectance term of the spec equation (aka F())\r\n// Implementation of fresnel from [4], Equation 15\r\nvec3 specularReflection(PBRInfo pbrInputs)\r\n{\r\n return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0);\r\n}\r\n\r\n\r\n// This calculates the specular geometric attenuation (aka G()),\r\n// where rougher material will reflect less light back to the viewer.\r\n// This implementation is based on [1] Equation 4, and we adopt their modifications to\r\n// alphaRoughness as input as originally proposed in [2].\r\nfloat geometricOcclusion(PBRInfo pbrInputs)\r\n{\r\n float NdotL = pbrInputs.NdotL;\r\n float NdotV = pbrInputs.NdotV;\r\n float r = pbrInputs.alphaRoughness;\r\n\r\n float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));\r\n float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));\r\n return attenuationL * attenuationV;\r\n}\r\n\r\n\r\n// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())\r\n// Implementation from \"Average Irregularity Representation of a Roughened Surface for Ray Reflection\" by T. S. Trowbridge, and K. P. Reitz\r\n// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.\r\nfloat microfacetDistribution(PBRInfo pbrInputs)\r\n{\r\n float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;\r\n float f = (pbrInputs.NdotH * roughnessSq - pbrInputs.NdotH) * pbrInputs.NdotH + 1.0;\r\n return roughnessSq / (M_PI * f * f);\r\n}\r\n\r\n\r\n\r\n\r\n\r\n\r\nvoid main()\r\n{\r\n float perceptualRoughness = u_roughnessFactor;\r\n float metallic = u_metallicFactor;\r\n\r\n#ifdef HAS_METALROUGHNESSMAP\r\n // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.\r\n // This layout intentionally reserves the 'r' channel for (optional) occlusion map data\r\n vec4 mrSample = texture(u_metallicRoughnessTexture, v_uv);\r\n perceptualRoughness = mrSample.g * perceptualRoughness;\r\n metallic = mrSample.b * metallic;\r\n#endif\r\n perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);\r\n metallic = clamp(metallic, 0.0, 1.0);\r\n // Roughness is authored as perceptual roughness; as is convention,\r\n // convert to material roughness by squaring the perceptual roughness [2].\r\n float alphaRoughness = perceptualRoughness * perceptualRoughness;\r\n\r\n\r\n // The albedo may be defined from a base texture or a flat color\r\n#ifdef HAS_BASECOLORMAP\r\n vec4 baseColor = texture(u_baseColorTexture, v_uv) * u_baseColorFactor;\r\n#else\r\n vec4 baseColor = u_baseColorFactor;\r\n#endif\r\n\r\n\r\n\r\n vec3 f0 = vec3(0.04);\r\n vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);\r\n diffuseColor *= 1.0 - metallic;\r\n vec3 specularColor = mix(f0, baseColor.rgb, metallic);\r\n\r\n // Compute reflectance.\r\n float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\r\n\r\n\r\n // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.\r\n // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.\r\n float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);\r\n vec3 specularEnvironmentR0 = specularColor.rgb;\r\n vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;\r\n\r\n\r\n vec3 n = getNormal(); // normal at surface point\r\n // vec3 v = vec3( 0.0, 0.0, 1.0 ); // Vector from surface point to camera\r\n vec3 v = normalize(-v_position); // Vector from surface point to camera\r\n // vec3 l = normalize(u_LightDirection); // Vector from surface point to light\r\n vec3 l = normalize(vec3( 1.0, 1.0, 1.0 )); // Vector from surface point to light\r\n // vec3 l = vec3( 0.0, 0.0, 1.0 ); // Vector from surface point to light\r\n vec3 h = normalize(l+v); // Half vector between both l and v\r\n vec3 reflection = -normalize(reflect(v, n));\r\n\r\n float NdotL = clamp(dot(n, l), 0.001, 1.0);\r\n float NdotV = abs(dot(n, v)) + 0.001;\r\n float NdotH = clamp(dot(n, h), 0.0, 1.0);\r\n float LdotH = clamp(dot(l, h), 0.0, 1.0);\r\n float VdotH = clamp(dot(v, h), 0.0, 1.0);\r\n\r\n PBRInfo pbrInputs = PBRInfo(\r\n NdotL,\r\n NdotV,\r\n NdotH,\r\n LdotH,\r\n VdotH,\r\n perceptualRoughness,\r\n metallic,\r\n specularEnvironmentR0,\r\n specularEnvironmentR90,\r\n alphaRoughness,\r\n diffuseColor,\r\n specularColor\r\n );\r\n\r\n // Calculate the shading terms for the microfacet specular shading model\r\n vec3 F = specularReflection(pbrInputs);\r\n float G = geometricOcclusion(pbrInputs);\r\n float D = microfacetDistribution(pbrInputs);\r\n\r\n // Calculation of analytical lighting contribution\r\n vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);\r\n vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);\r\n // vec3 color = NdotL * u_LightColor * (diffuseContrib + specContrib);\r\n vec3 color = NdotL * (diffuseContrib + specContrib); // assume light color vec3(1, 1, 1)\r\n\r\n // Calculate lighting contribution from image based lighting source (IBL)\r\n// #ifdef USE_IBL\r\n color += getIBLContribution(pbrInputs, n, reflection);\r\n// #endif\r\n\r\n\r\n // Apply optional PBR terms for additional (optional) shading\r\n#ifdef HAS_OCCLUSIONMAP\r\n float ao = texture(u_occlusionTexture, v_uv).r;\r\n color = mix(color, color * ao, u_occlusionStrength);\r\n#endif\r\n\r\n#ifdef HAS_EMISSIVEMAP\r\n vec3 emissive = texture(u_emissiveTexture, v_uv).rgb * u_emissiveFactor;\r\n color += emissive;\r\n#endif\r\n\r\n // // This section uses mix to override final color for reference app visualization\r\n // // of various parameters in the lighting equation.\r\n // color = mix(color, F, u_ScaleFGDSpec.x);\r\n // color = mix(color, vec3(G), u_ScaleFGDSpec.y);\r\n // color = mix(color, vec3(D), u_ScaleFGDSpec.z);\r\n // color = mix(color, specContrib, u_ScaleFGDSpec.w);\r\n\r\n // color = mix(color, diffuseContrib, u_ScaleDiffBaseMR.x);\r\n // color = mix(color, baseColor.rgb, u_ScaleDiffBaseMR.y);\r\n // color = mix(color, vec3(metallic), u_ScaleDiffBaseMR.z);\r\n // color = mix(color, vec3(perceptualRoughness), u_ScaleDiffBaseMR.w);\r\n\r\n frag_color = vec4(color, baseColor.a);\r\n}" /***/ }) /******/ ]); \ No newline at end of file diff --git a/examples/webgl2-renderer.html b/examples/webgl2-renderer.html index c085ff1..5bdab1b 100644 --- a/examples/webgl2-renderer.html +++ b/examples/webgl2-renderer.html @@ -46,6 +46,7 @@ +
Draw Bounding Box:
Bounding Box type: