|
2 | 2 |
|
3 | 3 | #include "Scene/Material.h" |
4 | 4 | #include "Renderer/Samplers.h" |
| 5 | +#include <glm/gtc/type_ptr.hpp> |
5 | 6 |
|
6 | 7 | PBRShader::PBRShader() : |
7 | 8 | baseColorFactorUniform(BGFX_INVALID_HANDLE), |
8 | | - metallicRoughnessFactorUniform(BGFX_INVALID_HANDLE), |
9 | | - hasTexturesUniform(BGFX_INVALID_HANDLE), |
| 9 | + metallicRoughnessNormalOcclusionFactorUniform(BGFX_INVALID_HANDLE), |
| 10 | + emissiveFactorUniform(BGFX_INVALID_HANDLE), |
| 11 | + hasTextures1Uniform(BGFX_INVALID_HANDLE), |
| 12 | + hasTextures2Uniform(BGFX_INVALID_HANDLE), |
10 | 13 | baseColorSampler(BGFX_INVALID_HANDLE), |
11 | 14 | metallicRoughnessSampler(BGFX_INVALID_HANDLE), |
12 | | - normalSampler(BGFX_INVALID_HANDLE) |
| 15 | + normalSampler(BGFX_INVALID_HANDLE), |
| 16 | + occlusionSampler(BGFX_INVALID_HANDLE), |
| 17 | + emissiveSampler(BGFX_INVALID_HANDLE) |
13 | 18 | { |
14 | 19 | } |
15 | 20 |
|
16 | 21 | void PBRShader::initialize() |
17 | 22 | { |
18 | 23 | baseColorFactorUniform = bgfx::createUniform("u_baseColorFactor", bgfx::UniformType::Vec4); |
19 | | - metallicRoughnessFactorUniform = bgfx::createUniform("u_metallicRoughnessFactor", bgfx::UniformType::Vec4); |
20 | | - hasTexturesUniform = bgfx::createUniform("u_hasTextures", bgfx::UniformType::Vec4); |
| 24 | + metallicRoughnessNormalOcclusionFactorUniform = bgfx::createUniform("u_metallicRoughnessNormalOcclusionFactor", bgfx::UniformType::Vec4); |
| 25 | + emissiveFactorUniform = bgfx::createUniform("u_emissiveFactorVec", bgfx::UniformType::Vec4); |
| 26 | + hasTextures1Uniform = bgfx::createUniform("u_hasTextures1", bgfx::UniformType::Vec4); |
| 27 | + hasTextures2Uniform = bgfx::createUniform("u_hasTextures2", bgfx::UniformType::Vec4); |
21 | 28 | baseColorSampler = bgfx::createUniform("s_texBaseColor", bgfx::UniformType::Sampler); |
22 | 29 | metallicRoughnessSampler = bgfx::createUniform("s_texMetallicRoughness", bgfx::UniformType::Sampler); |
23 | 30 | normalSampler = bgfx::createUniform("s_texNormal", bgfx::UniformType::Sampler); |
| 31 | + occlusionSampler = bgfx::createUniform("s_texOcclusion", bgfx::UniformType::Sampler); |
| 32 | + emissiveSampler = bgfx::createUniform("s_texEmissive", bgfx::UniformType::Sampler); |
24 | 33 | } |
25 | 34 |
|
26 | 35 | void PBRShader::shutdown() |
27 | 36 | { |
28 | 37 | bgfx::destroy(baseColorFactorUniform); |
29 | | - bgfx::destroy(metallicRoughnessFactorUniform); |
30 | | - bgfx::destroy(hasTexturesUniform); |
| 38 | + bgfx::destroy(metallicRoughnessNormalOcclusionFactorUniform); |
| 39 | + bgfx::destroy(emissiveFactorUniform); |
| 40 | + bgfx::destroy(hasTextures1Uniform); |
| 41 | + bgfx::destroy(hasTextures2Uniform); |
31 | 42 | bgfx::destroy(baseColorSampler); |
32 | 43 | bgfx::destroy(metallicRoughnessSampler); |
33 | 44 | bgfx::destroy(normalSampler); |
| 45 | + bgfx::destroy(occlusionSampler); |
| 46 | + bgfx::destroy(emissiveSampler); |
34 | 47 |
|
35 | | - baseColorFactorUniform = metallicRoughnessFactorUniform = hasTexturesUniform = baseColorSampler = |
36 | | - metallicRoughnessSampler = normalSampler = BGFX_INVALID_HANDLE; |
| 48 | + baseColorFactorUniform = metallicRoughnessNormalOcclusionFactorUniform = emissiveFactorUniform = hasTextures1Uniform = |
| 49 | + hasTextures2Uniform = baseColorSampler = metallicRoughnessSampler = normalSampler = occlusionSampler = |
| 50 | + emissiveSampler = BGFX_INVALID_HANDLE; |
37 | 51 | } |
38 | 52 |
|
39 | 53 | uint64_t PBRShader::bindMaterial(const Material& material) |
40 | 54 | { |
41 | | - float metallicRoughnessValues[4] = { material.metallicFactor, material.roughnessFactor, 0.0f, 0.0f }; |
42 | | - float hasTexturesValues[4] = { 1.0f, 1.0f, 1.0f, 0.0f }; |
43 | | - bgfx::setUniform(baseColorFactorUniform, &material.baseColorFactor[0]); |
44 | | - bgfx::setUniform(metallicRoughnessFactorUniform, &metallicRoughnessValues[0]); |
| 55 | + float factorValues[4] = { material.metallicFactor, material.roughnessFactor, material.normalScale, material.occlusionStrength }; |
| 56 | + bgfx::setUniform(baseColorFactorUniform, glm::value_ptr(material.baseColorFactor)); |
| 57 | + bgfx::setUniform(metallicRoughnessNormalOcclusionFactorUniform, factorValues); |
| 58 | + glm::vec4 emissiveFactor = glm::vec4(material.emissiveFactor, 0.0f); |
| 59 | + bgfx::setUniform(emissiveFactorUniform, glm::value_ptr(emissiveFactor)); |
45 | 60 |
|
| 61 | + float hasTextures1Values[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 62 | + float hasTextures2Values[4] = { 1.0f, 0.0f, 0.0f, 0.0f }; |
46 | 63 | if(bgfx::isValid(material.baseColorTexture)) |
47 | 64 | bgfx::setTexture(Samplers::PBR_BASECOLOR, baseColorSampler, material.baseColorTexture); |
48 | 65 | else |
49 | | - hasTexturesValues[0] = 0.0f; |
| 66 | + hasTextures1Values[0] = 0.0f; |
50 | 67 | if(bgfx::isValid(material.metallicRoughnessTexture)) |
51 | 68 | bgfx::setTexture(Samplers::PBR_METALROUGHNESS, metallicRoughnessSampler, material.metallicRoughnessTexture); |
52 | 69 | else |
53 | | - hasTexturesValues[1] = 0.0f; |
| 70 | + hasTextures1Values[1] = 0.0f; |
54 | 71 | if(bgfx::isValid(material.normalTexture)) |
55 | 72 | bgfx::setTexture(Samplers::PBR_NORMAL, normalSampler, material.normalTexture); |
56 | 73 | else |
57 | | - hasTexturesValues[2] = 0.0f; |
| 74 | + hasTextures1Values[2] = 0.0f; |
| 75 | + if(bgfx::isValid(material.occlusionTexture)) |
| 76 | + bgfx::setTexture(Samplers::PBR_OCCLUSION, occlusionSampler, material.occlusionTexture); |
| 77 | + else |
| 78 | + hasTextures1Values[3] = 0.0f; |
| 79 | + if(bgfx::isValid(material.emissiveTexture)) |
| 80 | + bgfx::setTexture(Samplers::PBR_EMISSIVE, emissiveSampler, material.emissiveTexture); |
| 81 | + else |
| 82 | + hasTextures2Values[0] = 0.0f; |
58 | 83 |
|
59 | | - bgfx::setUniform(hasTexturesUniform, &hasTexturesValues[0]); |
| 84 | + bgfx::setUniform(hasTextures1Uniform, hasTextures1Values); |
| 85 | + bgfx::setUniform(hasTextures2Uniform, hasTextures2Values); |
60 | 86 |
|
61 | 87 | uint64_t state = 0; |
62 | 88 | if(material.blend) |
|
0 commit comments