diff --git a/build/app.js b/build/app.js index 56aec50..70656cc 100644 --- a/build/app.js +++ b/build/app.js @@ -2766,6 +2766,27 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); gl.bindVertexArray(null); + var BRDF_LUT = { + texture: null, + + createTexture: function (img) { + this.texture = gl.createTexture(); + gl.bindTexture(gl.TEXTURE_2D, this.texture); + 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 + img + ); + gl.bindTexture(gl.TEXTURE_2D, null); + } + } + + // Environment maps var CUBE_MAP = { @@ -2777,6 +2798,9 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); '../textures/environment/ny.jpg', '../textures/environment/pz.jpg', '../textures/environment/nz.jpg', + + // @tmp, ugly, load brdfLUT here + '../textures/brdfLUT.png' ], images: null, @@ -2810,6 +2834,9 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); gl.bindTexture(gl.TEXTURE_CUBE_MAP, null); + // @tmp + BRDF_LUT.createTexture(this.images[6]); + if (this.finishLoadingCallback) { this.finishLoadingCallback(); } @@ -2869,7 +2896,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); vertexBuffer: gl.createBuffer(), // program: createProgram(gl, require('./shaders/vs-bbox'), require('./shaders/fs-bbox')), - program: createProgram(gl, __webpack_require__(28), __webpack_require__(29)), + program: createProgram(gl, __webpack_require__(22), __webpack_require__(23)), positionLocation: 0, uniformMvpLocation: 0, uniformEnvironmentLocation: 0, @@ -2916,7 +2943,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); // // -- Initialize program - var program = createProgram(gl, __webpack_require__(22), __webpack_require__(1)); + var program = createProgram(gl, __webpack_require__(24), __webpack_require__(1)); var programBaseColor = { program: program, uniformMvpLocation: gl.getUniformLocation(program, "u_MVP"), @@ -2934,7 +2961,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); }; // @temp test - program = createProgram(gl, __webpack_require__(7), __webpack_require__(23)); + program = createProgram(gl, __webpack_require__(7), __webpack_require__(25)); var programBaseTextureNormalMap = { program: program, uniformMvpLocation: gl.getUniformLocation(program, "u_MVP"), @@ -2945,7 +2972,33 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); uniformNormalTextureLocation: gl.getUniformLocation(program, "u_normalTexture") }; - program = createProgram(gl, __webpack_require__(24), __webpack_require__(1)); + // @temp PBR + program = createProgram(gl, __webpack_require__(30), __webpack_require__(31)); + var programPBR = { + program: program, + + uniformMvpLocation: gl.getUniformLocation(program, "u_MVP"), + uniformMvNormalLocation: gl.getUniformLocation(program, "u_MVNormal"), + uniformBaseColorFactorLocation: gl.getUniformLocation(program, "u_baseColorFactor"), + uniformBaseColorTextureLocation: gl.getUniformLocation(program, "u_baseColorTexture"), + + uniformNormalTextureScaleLocation: gl.getUniformLocation(program, "u_normalTextureScale"), + uniformNormalTextureLocation: gl.getUniformLocation(program, "u_normalTexture"), + + uniformDiffuseEnvSamplerLocation: gl.getUniformLocation(program, "u_DiffuseEnvSampler"), + uniformSpecularEnvSamplerLocation: gl.getUniformLocation(program, "u_SpecularEnvSampler"), + uniformBrdfLUTLocation: gl.getUniformLocation(program, "u_brdfLUT"), + + + uniformMetallicRoughnessTextureLocation: gl.getUniformLocation(program, "u_metallicRoughnessTexture"), + uniformMetallicFactorLocation: gl.getUniformLocation(program, "u_metallicFactor"), + uniformRoughnessFactorLocation: gl.getUniformLocation(program, "u_roughnessFactor"), + + uniformOcclusionTextureLocation: gl.getUniformLocation(program, "u_occlusionTexture"), + uniformOcclusionStrengthLocation: gl.getUniformLocation(program, "u_occlusionStrength") + }; + + program = createProgram(gl, __webpack_require__(26), __webpack_require__(1)); var programSkinBaseColor = { program: program, uniformMvpLocation: gl.getUniformLocation(program, "u_MVP"), @@ -2954,7 +3007,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); uniformBlockIndexJointMatrix: gl.getUniformBlockIndex(program, "JointMatrix") }; - program = createProgram(gl, __webpack_require__(25), __webpack_require__(1)); + program = createProgram(gl, __webpack_require__(27), __webpack_require__(1)); var programSkinBaseColorVec8 = { program: program, uniformMvpLocation: gl.getUniformLocation(program, "u_MVP"), @@ -2964,7 +3017,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); }; // temp - program = createProgram(gl, __webpack_require__(26), __webpack_require__(2)); + program = createProgram(gl, __webpack_require__(28), __webpack_require__(2)); var programSkinBaseTexture = { program: program, uniformMvpLocation: gl.getUniformLocation(program, "u_MVP"), @@ -2974,7 +3027,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); uniformBlockIndexJointMatrix: gl.getUniformBlockIndex(program, "JointMatrix") }; - program = createProgram(gl, __webpack_require__(27), __webpack_require__(2)); + program = createProgram(gl, __webpack_require__(29), __webpack_require__(2)); var programSkinBaseTextureVec8 = { program: program, uniformMvpLocation: gl.getUniformLocation(program, "u_MVP"), @@ -3061,7 +3114,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); // var gltfUrl = '../glTFs/glTF_version_2/2CylinderEngine/glTF/2CylinderEngine.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/GearboxAssy/glTF/GearboxAssy.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/Buggy/glTF/Buggy.gltf'; - // var gltfUrl = '../glTFs/glTF_version_2/DamagedHelmet/glTF/DamagedHelmet.gltf'; + var gltfUrl = '../glTFs/glTF_version_2/DamagedHelmet/glTF/DamagedHelmet.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/Avocado/glTF/Avocado.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/BoomBox/glTF/BoomBox.gltf'; @@ -3070,7 +3123,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); // var gltfUrl = '../glTFs/glTF_version_2/RiggedSimple/glTF/RiggedSimple.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/RiggedFigure/glTF/RiggedFigure.gltf'; - var gltfUrl = '../glTFs/glTF_version_2/BrainStem/glTF/BrainStem.gltf'; + // var gltfUrl = '../glTFs/glTF_version_2/BrainStem/glTF/BrainStem.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/CesiumMan/glTF/CesiumMan.gltf'; // var gltfUrl = 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Monster/glTF/Monster.gltf'; @@ -9765,49 +9818,61 @@ module.exports = "#version 300 es\r\n#define FRAG_COLOR_LOCATION 0\r\n\r\nprecis /* 22 */ /***/ (function(module, exports) { -module.exports = "#version 300 es\r\n#define POSITION_LOCATION 0\r\n#define NORMAL_LOCATION 1\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\nlayout(location = POSITION_LOCATION) in vec3 position;\r\nlayout(location = NORMAL_LOCATION) in vec3 normal;\r\n\r\nout vec3 v_normal;\r\n\r\nvoid main()\r\n{\r\n v_normal = normalize((u_MVNormal * vec4(normal, 0)).xyz);\r\n gl_Position = u_MVP * vec4(position, 1.0) ;\r\n}" +module.exports = "#version 300 es\r\nprecision highp float;\r\nprecision highp int;\r\n\r\nuniform mat4 u_MVP;\r\n\r\nlayout(location = 0) in vec3 position;\r\n\r\nout vec3 texcoord;\r\n\r\nvoid main()\r\n{\r\n vec4 pos = u_MVP * vec4(position, 1.0);\r\n gl_Position = pos.xyww;\r\n texcoord = position;\r\n}" /***/ }), /* 23 */ /***/ (function(module, exports) { -module.exports = "#version 300 es\r\n#define FRAG_COLOR_LOCATION 0\r\n\r\nprecision highp float;\r\nprecision highp int;\r\n\r\nuniform vec4 u_baseColorFactor;\r\nuniform sampler2D u_baseColorTexture;\r\nuniform sampler2D u_normalTexture;\r\n\r\nin vec3 v_normal;\r\nin vec2 v_uv;\r\n\r\nlayout(location = FRAG_COLOR_LOCATION) out vec4 color;\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\nvoid main()\r\n{\r\n vec3 normal = applyNormalMap( v_normal, texture(u_normalTexture, v_uv).rgb );\r\n normal = gl_FrontFacing ? normal : -normal;\r\n\r\n float intensity = dot(normal, vec3(0.0, 0.0, 1.0));\r\n color = u_baseColorFactor * texture(u_baseColorTexture, v_uv) * intensity;\r\n color.a = 1.0;\r\n}" +module.exports = "#version 300 es\r\nprecision highp float;\r\nprecision highp int;\r\n\r\nuniform samplerCube u_environment;\r\n\r\nin vec3 texcoord;\r\n\r\nout vec4 color;\r\n\r\nvoid main()\r\n{\r\n color = texture(u_environment, texcoord);\r\n}" /***/ }), /* 24 */ /***/ (function(module, exports) { -module.exports = "#version 300 es\r\n#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 WEIGHTS_0_LOCATION 4\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\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\r\n\r\nlayout(location = POSITION_LOCATION) in vec3 position;\r\nlayout(location = NORMAL_LOCATION) in vec3 normal;\r\nlayout(location = JOINTS_0_LOCATION) in vec4 joint;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight;\r\n\r\nout vec3 v_normal;\r\n\r\nvoid main()\r\n{\r\n mat4 skinMatrix = \r\n weight.x * u_jointMatrix.matrix[int(joint.x)] +\r\n weight.y * u_jointMatrix.matrix[int(joint.y)] +\r\n weight.z * u_jointMatrix.matrix[int(joint.z)] +\r\n weight.w * u_jointMatrix.matrix[int(joint.w)];\r\n\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}" +module.exports = "#version 300 es\r\n#define POSITION_LOCATION 0\r\n#define NORMAL_LOCATION 1\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\nlayout(location = POSITION_LOCATION) in vec3 position;\r\nlayout(location = NORMAL_LOCATION) in vec3 normal;\r\n\r\nout vec3 v_normal;\r\n\r\nvoid main()\r\n{\r\n v_normal = normalize((u_MVNormal * vec4(normal, 0)).xyz);\r\n gl_Position = u_MVP * vec4(position, 1.0) ;\r\n}" /***/ }), /* 25 */ /***/ (function(module, exports) { -module.exports = "#version 300 es\r\n#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\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\r\n\r\nlayout(location = POSITION_LOCATION) in vec3 position;\r\nlayout(location = NORMAL_LOCATION) in vec3 normal;\r\nlayout(location = JOINTS_0_LOCATION) in vec4 joint0;\r\nlayout(location = JOINTS_1_LOCATION) in vec4 joint1;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight0;\r\nlayout(location = WEIGHTS_1_LOCATION) in vec4 weight1;\r\n\r\nout vec3 v_normal;\r\n\r\nvoid main()\r\n{\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 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\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}" +module.exports = "#version 300 es\r\n#define FRAG_COLOR_LOCATION 0\r\n\r\nprecision highp float;\r\nprecision highp int;\r\n\r\nuniform vec4 u_baseColorFactor;\r\nuniform sampler2D u_baseColorTexture;\r\nuniform sampler2D u_normalTexture;\r\n\r\nin vec3 v_normal;\r\nin vec2 v_uv;\r\n\r\nlayout(location = FRAG_COLOR_LOCATION) out vec4 color;\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\nvoid main()\r\n{\r\n vec3 normal = applyNormalMap( v_normal, texture(u_normalTexture, v_uv).rgb );\r\n normal = gl_FrontFacing ? normal : -normal;\r\n\r\n float intensity = dot(normal, vec3(0.0, 0.0, 1.0));\r\n color = u_baseColorFactor * texture(u_baseColorTexture, v_uv) * intensity;\r\n color.a = 1.0;\r\n}" /***/ }), /* 26 */ /***/ (function(module, exports) { -module.exports = "#version 300 es\r\n#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 WEIGHTS_0_LOCATION 4\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\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\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\nlayout(location = JOINTS_0_LOCATION) in vec4 joint;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight;\r\n\r\nout vec3 v_normal;\r\nout vec2 v_uv;\r\n\r\nvoid main()\r\n{\r\n mat4 skinMatrix = \r\n weight.x * u_jointMatrix.matrix[int(joint.x)] +\r\n weight.y * u_jointMatrix.matrix[int(joint.y)] +\r\n weight.z * u_jointMatrix.matrix[int(joint.z)] +\r\n weight.w * u_jointMatrix.matrix[int(joint.w)];\r\n\r\n v_normal = normalize(( u_MVNormal * transpose(inverse(skinMatrix)) * vec4(normal, 0)).xyz);\r\n v_uv = uv;\r\n gl_Position = u_MVP * skinMatrix * vec4(position, 1.0) ;\r\n}" +module.exports = "#version 300 es\r\n#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 WEIGHTS_0_LOCATION 4\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\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\r\n\r\nlayout(location = POSITION_LOCATION) in vec3 position;\r\nlayout(location = NORMAL_LOCATION) in vec3 normal;\r\nlayout(location = JOINTS_0_LOCATION) in vec4 joint;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight;\r\n\r\nout vec3 v_normal;\r\n\r\nvoid main()\r\n{\r\n mat4 skinMatrix = \r\n weight.x * u_jointMatrix.matrix[int(joint.x)] +\r\n weight.y * u_jointMatrix.matrix[int(joint.y)] +\r\n weight.z * u_jointMatrix.matrix[int(joint.z)] +\r\n weight.w * u_jointMatrix.matrix[int(joint.w)];\r\n\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}" /***/ }), /* 27 */ /***/ (function(module, exports) { -module.exports = "#version 300 es\r\n#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\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\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\nlayout(location = JOINTS_0_LOCATION) in vec4 joint0;\r\nlayout(location = JOINTS_1_LOCATION) in vec4 joint1;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight0;\r\nlayout(location = WEIGHTS_1_LOCATION) in vec4 weight1;\r\n\r\nout vec3 v_normal;\r\nout vec2 v_uv;\r\n\r\nvoid main()\r\n{\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 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 \r\n \r\n v_normal = normalize(( u_MVNormal * transpose(inverse(skinMatrix)) * vec4(normal, 0)).xyz);\r\n v_uv = uv;\r\n gl_Position = u_MVP * skinMatrix * vec4(position, 1.0) ;\r\n}" +module.exports = "#version 300 es\r\n#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\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\r\n\r\nlayout(location = POSITION_LOCATION) in vec3 position;\r\nlayout(location = NORMAL_LOCATION) in vec3 normal;\r\nlayout(location = JOINTS_0_LOCATION) in vec4 joint0;\r\nlayout(location = JOINTS_1_LOCATION) in vec4 joint1;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight0;\r\nlayout(location = WEIGHTS_1_LOCATION) in vec4 weight1;\r\n\r\nout vec3 v_normal;\r\n\r\nvoid main()\r\n{\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 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\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}" /***/ }), /* 28 */ /***/ (function(module, exports) { -module.exports = "#version 300 es\r\nprecision highp float;\r\nprecision highp int;\r\n\r\nuniform mat4 u_MVP;\r\n\r\nlayout(location = 0) in vec3 position;\r\n\r\nout vec3 texcoord;\r\n\r\nvoid main()\r\n{\r\n vec4 pos = u_MVP * vec4(position, 1.0);\r\n gl_Position = pos.xyww;\r\n texcoord = position;\r\n}" +module.exports = "#version 300 es\r\n#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 WEIGHTS_0_LOCATION 4\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\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\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\nlayout(location = JOINTS_0_LOCATION) in vec4 joint;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight;\r\n\r\nout vec3 v_normal;\r\nout vec2 v_uv;\r\n\r\nvoid main()\r\n{\r\n mat4 skinMatrix = \r\n weight.x * u_jointMatrix.matrix[int(joint.x)] +\r\n weight.y * u_jointMatrix.matrix[int(joint.y)] +\r\n weight.z * u_jointMatrix.matrix[int(joint.z)] +\r\n weight.w * u_jointMatrix.matrix[int(joint.w)];\r\n\r\n v_normal = normalize(( u_MVNormal * transpose(inverse(skinMatrix)) * vec4(normal, 0)).xyz);\r\n v_uv = uv;\r\n gl_Position = u_MVP * skinMatrix * vec4(position, 1.0) ;\r\n}" /***/ }), /* 29 */ /***/ (function(module, exports) { -module.exports = "#version 300 es\r\nprecision highp float;\r\nprecision highp int;\r\n\r\nuniform samplerCube u_environment;\r\n\r\nin vec3 texcoord;\r\n\r\nout vec4 color;\r\n\r\nvoid main()\r\n{\r\n color = texture(u_environment, texcoord);\r\n}" +module.exports = "#version 300 es\r\n#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\nuniform JointMatrix\r\n{\r\n mat4 matrix[32];\r\n} u_jointMatrix;\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\nlayout(location = JOINTS_0_LOCATION) in vec4 joint0;\r\nlayout(location = JOINTS_1_LOCATION) in vec4 joint1;\r\nlayout(location = WEIGHTS_0_LOCATION) in vec4 weight0;\r\nlayout(location = WEIGHTS_1_LOCATION) in vec4 weight1;\r\n\r\nout vec3 v_normal;\r\nout vec2 v_uv;\r\n\r\nvoid main()\r\n{\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 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 \r\n \r\n v_normal = normalize(( u_MVNormal * transpose(inverse(skinMatrix)) * vec4(normal, 0)).xyz);\r\n v_uv = uv;\r\n gl_Position = u_MVP * skinMatrix * vec4(position, 1.0) ;\r\n}" + +/***/ }), +/* 30 */ +/***/ (function(module, exports) { + +module.exports = "#version 300 es\r\n#define POSITION_LOCATION 0\r\n#define NORMAL_LOCATION 1\r\n#define TEXCOORD_0_LOCATION 2\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\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// TODO: tangents\r\n\r\nout vec3 v_normal;\r\nout vec2 v_uv;\r\n\r\nvoid main()\r\n{\r\n v_normal = normalize((u_MVNormal * vec4(normal, 0)).xyz);\r\n v_uv = uv;\r\n gl_Position = u_MVP * vec4(position, 1.0) ;\r\n}" + +/***/ }), +/* 31 */ +/***/ (function(module, exports) { + +module.exports = "#version 300 es\r\n#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\nuniform sampler2D u_baseColorTexture;\r\n\r\n// normal map\r\nuniform sampler2D u_normalTexture;\r\nuniform float u_normalScale;\r\n\r\n// emmisve map\r\nuniform sampler2D u_emissiveTexture;\r\nuniform vec3 u_emissiveFactor;\r\n\r\n// metal roughness\r\nuniform sampler2D u_metallicRoughnessTexture;\r\n\r\nuniform float u_metallicFactor;\r\nuniform float u_roughnessFactor;\r\n\r\n// occlusion texture\r\nuniform sampler2D u_occlusionTexture;\r\nuniform float u_occlusionStrength;\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 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 = textureCubeLodEXT(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 n = applyNormalMap( v_normal, texture(u_normalTexture, v_uv).rgb );\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}" /***/ }) /******/ ]); \ No newline at end of file diff --git a/src/index.js b/src/index.js index 2994237..8b81abc 100644 --- a/src/index.js +++ b/src/index.js @@ -224,6 +224,27 @@ import '../css/style.css'; gl.bindVertexArray(null); + var BRDF_LUT = { + texture: null, + + createTexture: function (img) { + this.texture = gl.createTexture(); + gl.bindTexture(gl.TEXTURE_2D, this.texture); + 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 + img + ); + gl.bindTexture(gl.TEXTURE_2D, null); + } + } + + // Environment maps var CUBE_MAP = { @@ -235,6 +256,9 @@ import '../css/style.css'; '../textures/environment/ny.jpg', '../textures/environment/pz.jpg', '../textures/environment/nz.jpg', + + // @tmp, ugly, load brdfLUT here + '../textures/brdfLUT.png' ], images: null, @@ -268,6 +292,9 @@ import '../css/style.css'; gl.bindTexture(gl.TEXTURE_CUBE_MAP, null); + // @tmp + BRDF_LUT.createTexture(this.images[6]); + if (this.finishLoadingCallback) { this.finishLoadingCallback(); } @@ -403,6 +430,32 @@ import '../css/style.css'; uniformNormalTextureLocation: gl.getUniformLocation(program, "u_normalTexture") }; + // @temp PBR + program = createProgram(gl, require('./shaders/vs-pbr.glsl'), require('./shaders/fs-pbr.glsl')); + var programPBR = { + program: program, + + uniformMvpLocation: gl.getUniformLocation(program, "u_MVP"), + uniformMvNormalLocation: gl.getUniformLocation(program, "u_MVNormal"), + uniformBaseColorFactorLocation: gl.getUniformLocation(program, "u_baseColorFactor"), + uniformBaseColorTextureLocation: gl.getUniformLocation(program, "u_baseColorTexture"), + + uniformNormalTextureScaleLocation: gl.getUniformLocation(program, "u_normalTextureScale"), + uniformNormalTextureLocation: gl.getUniformLocation(program, "u_normalTexture"), + + uniformDiffuseEnvSamplerLocation: gl.getUniformLocation(program, "u_DiffuseEnvSampler"), + uniformSpecularEnvSamplerLocation: gl.getUniformLocation(program, "u_SpecularEnvSampler"), + uniformBrdfLUTLocation: gl.getUniformLocation(program, "u_brdfLUT"), + + + uniformMetallicRoughnessTextureLocation: gl.getUniformLocation(program, "u_metallicRoughnessTexture"), + uniformMetallicFactorLocation: gl.getUniformLocation(program, "u_metallicFactor"), + uniformRoughnessFactorLocation: gl.getUniformLocation(program, "u_roughnessFactor"), + + uniformOcclusionTextureLocation: gl.getUniformLocation(program, "u_occlusionTexture"), + uniformOcclusionStrengthLocation: gl.getUniformLocation(program, "u_occlusionStrength") + }; + program = createProgram(gl, require('./shaders/vs-skin-normal.glsl'), require('./shaders/fs-base-color.glsl')); var programSkinBaseColor = { program: program, @@ -519,7 +572,7 @@ import '../css/style.css'; // var gltfUrl = '../glTFs/glTF_version_2/2CylinderEngine/glTF/2CylinderEngine.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/GearboxAssy/glTF/GearboxAssy.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/Buggy/glTF/Buggy.gltf'; - // var gltfUrl = '../glTFs/glTF_version_2/DamagedHelmet/glTF/DamagedHelmet.gltf'; + var gltfUrl = '../glTFs/glTF_version_2/DamagedHelmet/glTF/DamagedHelmet.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/Avocado/glTF/Avocado.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/BoomBox/glTF/BoomBox.gltf'; @@ -528,7 +581,7 @@ import '../css/style.css'; // var gltfUrl = '../glTFs/glTF_version_2/RiggedSimple/glTF/RiggedSimple.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/RiggedFigure/glTF/RiggedFigure.gltf'; - var gltfUrl = '../glTFs/glTF_version_2/BrainStem/glTF/BrainStem.gltf'; + // var gltfUrl = '../glTFs/glTF_version_2/BrainStem/glTF/BrainStem.gltf'; // var gltfUrl = '../glTFs/glTF_version_2/CesiumMan/glTF/CesiumMan.gltf'; // var gltfUrl = 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Monster/glTF/Monster.gltf'; diff --git a/src/shaders/fs-pbr.glsl b/src/shaders/fs-pbr.glsl new file mode 100644 index 0000000..135fd70 --- /dev/null +++ b/src/shaders/fs-pbr.glsl @@ -0,0 +1,260 @@ +#version 300 es +#define FRAG_COLOR_LOCATION 0 + +// reference: https://github.com/KhronosGroup/glTF-WebGL-PBR/blob/master/shaders/pbr-frag.glsl + +precision highp float; +precision highp int; + +// IBL +uniform samplerCube u_DiffuseEnvSampler; +uniform samplerCube u_SpecularEnvSampler; +uniform sampler2D u_brdfLUT; + +// Metallic-roughness material + +// TODO: use #define string for a full and dynamic support renderer + +// base color +uniform vec4 u_baseColorFactor; +uniform sampler2D u_baseColorTexture; + +// normal map +uniform sampler2D u_normalTexture; +uniform float u_normalScale; + +// emmisve map +uniform sampler2D u_emissiveTexture; +uniform vec3 u_emissiveFactor; + +// metal roughness +uniform sampler2D u_metallicRoughnessTexture; + +uniform float u_metallicFactor; +uniform float u_roughnessFactor; + +// occlusion texture +uniform sampler2D u_occlusionTexture; +uniform float u_occlusionStrength; + +in vec3 v_normal; +in vec2 v_uv; + +layout(location = FRAG_COLOR_LOCATION) out vec4 frag_color; + +struct PBRInfo +{ + float NdotL; // cos angle between normal and light direction + float NdotV; // cos angle between normal and view direction + float NdotH; // cos angle between normal and half vector + float LdotH; // cos angle between light direction and half vector + float VdotH; // cos angle between view direction and half vector + float perceptualRoughness; // roughness value, as authored by the model creator (input to shader) + float metalness; // metallic value at the surface + vec3 reflectance0; // full reflectance color (normal incidence angle) + vec3 reflectance90; // reflectance color at grazing angle + float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2]) + vec3 diffuseColor; // color contribution from diffuse lighting + vec3 specularColor; // color contribution from specular lighting +}; + + +vec3 applyNormalMap(vec3 geomnor, vec3 normap) { + normap = normap * 2.0 - 1.0; + vec3 up = normalize(vec3(0.001, 1, 0.001)); + vec3 surftan = normalize(cross(geomnor, up)); + vec3 surfbinor = cross(geomnor, surftan); + return normap.y * surftan + normap.x * surfbinor + normap.z * geomnor; +} + +const float M_PI = 3.141592653589793; +const float c_MinRoughness = 0.04; + + +vec3 getIBLContribution(PBRInfo pbrInputs, vec3 n, vec3 reflection) +{ + float mipCount = 9.0; // resolution of 512x512 + float lod = (pbrInputs.perceptualRoughness * mipCount); + // retrieve a scale and bias to F0. See [1], Figure 3 + vec3 brdf = texture(u_brdfLUT, vec2(pbrInputs.NdotV, 1.0 - pbrInputs.perceptualRoughness)).rgb; + vec3 diffuseLight = texture(u_DiffuseEnvSampler, n).rgb; + +// #ifdef USE_TEX_LOD +// vec3 specularLight = textureCubeLodEXT(u_SpecularEnvSampler, reflection, lod).rgb; +// #else + vec3 specularLight = texture(u_SpecularEnvSampler, reflection).rgb; +// #endif + + vec3 diffuse = diffuseLight * pbrInputs.diffuseColor; + vec3 specular = specularLight * (pbrInputs.specularColor * brdf.x + brdf.y); + + // // For presentation, this allows us to disable IBL terms + // diffuse *= u_ScaleIBLAmbient.x; + // specular *= u_ScaleIBLAmbient.y; + + return diffuse + specular; +} + +// Basic Lambertian diffuse +// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog +// See also [1], Equation 1 +vec3 diffuse(PBRInfo pbrInputs) +{ + return pbrInputs.diffuseColor / M_PI; +} + + +// The following equation models the Fresnel reflectance term of the spec equation (aka F()) +// Implementation of fresnel from [4], Equation 15 +vec3 specularReflection(PBRInfo pbrInputs) +{ + return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0); +} + + +// This calculates the specular geometric attenuation (aka G()), +// where rougher material will reflect less light back to the viewer. +// This implementation is based on [1] Equation 4, and we adopt their modifications to +// alphaRoughness as input as originally proposed in [2]. +float geometricOcclusion(PBRInfo pbrInputs) +{ + float NdotL = pbrInputs.NdotL; + float NdotV = pbrInputs.NdotV; + float r = pbrInputs.alphaRoughness; + + float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL))); + float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV))); + return attenuationL * attenuationV; +} + + +// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D()) +// Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz +// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3. +float microfacetDistribution(PBRInfo pbrInputs) +{ + float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness; + float f = (pbrInputs.NdotH * roughnessSq - pbrInputs.NdotH) * pbrInputs.NdotH + 1.0; + return roughnessSq / (M_PI * f * f); +} + + + + + + +void main() +{ + float perceptualRoughness = u_roughnessFactor; + float metallic = u_metallicFactor; + +// #ifdef HAS_METALROUGHNESSMAP + // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel. + // This layout intentionally reserves the 'r' channel for (optional) occlusion map data + vec4 mrSample = texture(u_metallicRoughnessTexture, v_uv); + perceptualRoughness = mrSample.g * perceptualRoughness; + metallic = mrSample.b * metallic; +// #endif + perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0); + metallic = clamp(metallic, 0.0, 1.0); + // Roughness is authored as perceptual roughness; as is convention, + // convert to material roughness by squaring the perceptual roughness [2]. + float alphaRoughness = perceptualRoughness * perceptualRoughness; + + + // The albedo may be defined from a base texture or a flat color +// #ifdef HAS_BASECOLORMAP + vec4 baseColor = texture(u_baseColorTexture, v_uv) * u_baseColorFactor; +// #else + // vec4 baseColor = u_baseColorFactor; +// #endif + + + + vec3 f0 = vec3(0.04); + vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0); + diffuseColor *= 1.0 - metallic; + vec3 specularColor = mix(f0, baseColor.rgb, metallic); + + // Compute reflectance. + float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b); + + + // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect. + // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%. + float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0); + vec3 specularEnvironmentR0 = specularColor.rgb; + vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90; + + + // vec3 n = getNormal(); // normal at surface point + vec3 n = applyNormalMap( v_normal, texture(u_normalTexture, v_uv).rgb ); + vec3 v = vec3( 0.0, 0.0, 1.0 ); // Vector from surface point to camera + // vec3 l = normalize(u_LightDirection); // Vector from surface point to light + vec3 l = vec3( 0.6, 0.8, 0.0 ); // Vector from surface point to light + vec3 h = normalize(l+v); // Half vector between both l and v + vec3 reflection = -normalize(reflect(v, n)); + + float NdotL = clamp(dot(n, l), 0.001, 1.0); + float NdotV = abs(dot(n, v)) + 0.001; + float NdotH = clamp(dot(n, h), 0.0, 1.0); + float LdotH = clamp(dot(l, h), 0.0, 1.0); + float VdotH = clamp(dot(v, h), 0.0, 1.0); + + PBRInfo pbrInputs = PBRInfo( + NdotL, + NdotV, + NdotH, + LdotH, + VdotH, + perceptualRoughness, + metallic, + specularEnvironmentR0, + specularEnvironmentR90, + alphaRoughness, + diffuseColor, + specularColor + ); + + // Calculate the shading terms for the microfacet specular shading model + vec3 F = specularReflection(pbrInputs); + float G = geometricOcclusion(pbrInputs); + float D = microfacetDistribution(pbrInputs); + + // Calculation of analytical lighting contribution + vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs); + vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV); + // vec3 color = NdotL * u_LightColor * (diffuseContrib + specContrib); + vec3 color = NdotL * (diffuseContrib + specContrib); // assume light color vec3(1, 1, 1) + + // Calculate lighting contribution from image based lighting source (IBL) +// #ifdef USE_IBL + color += getIBLContribution(pbrInputs, n, reflection); +// #endif + + + // Apply optional PBR terms for additional (optional) shading +// #ifdef HAS_OCCLUSIONMAP + float ao = texture(u_occlusionTexture, v_uv).r; + color = mix(color, color * ao, u_occlusionStrength); +// #endif + +// #ifdef HAS_EMISSIVEMAP + vec3 emissive = texture(u_emissiveTexture, v_uv).rgb * u_emissiveFactor; + color += emissive; +// #endif + + // // This section uses mix to override final color for reference app visualization + // // of various parameters in the lighting equation. + // color = mix(color, F, u_ScaleFGDSpec.x); + // color = mix(color, vec3(G), u_ScaleFGDSpec.y); + // color = mix(color, vec3(D), u_ScaleFGDSpec.z); + // color = mix(color, specContrib, u_ScaleFGDSpec.w); + + // color = mix(color, diffuseContrib, u_ScaleDiffBaseMR.x); + // color = mix(color, baseColor.rgb, u_ScaleDiffBaseMR.y); + // color = mix(color, vec3(metallic), u_ScaleDiffBaseMR.z); + // color = mix(color, vec3(perceptualRoughness), u_ScaleDiffBaseMR.w); + + frag_color = vec4(color, baseColor.a); +} \ No newline at end of file diff --git a/src/shaders/vs-pbr.glsl b/src/shaders/vs-pbr.glsl index 9915146..cdd96c8 100644 --- a/src/shaders/vs-pbr.glsl +++ b/src/shaders/vs-pbr.glsl @@ -12,6 +12,7 @@ uniform mat4 u_MVNormal; layout(location = POSITION_LOCATION) in vec3 position; layout(location = NORMAL_LOCATION) in vec3 normal; layout(location = TEXCOORD_0_LOCATION) in vec2 uv; +// TODO: tangents out vec3 v_normal; out vec2 v_uv; diff --git a/textures/brdfLUT.png b/textures/brdfLUT.png new file mode 100644 index 0000000..5f6541b Binary files /dev/null and b/textures/brdfLUT.png differ