| 1 |
/* |
| 2 |
* Copyright (C) 2019 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <gltfio/MaterialProvider.h> |
| 18 |
|
| 19 |
#include <filamat/MaterialBuilder.h> |
| 20 |
|
| 21 |
#include <utils/Hash.h> |
| 22 |
|
| 23 |
#include <tsl/robin_map.h> |
| 24 |
|
| 25 |
#include <string> |
| 26 |
|
| 27 |
using namespace filamat; |
| 28 |
using namespace filament; |
| 29 |
using namespace gltfio; |
| 30 |
using namespace utils; |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
class MaterialGenerator : public MaterialProvider { |
| 35 |
public: |
| 36 |
explicit MaterialGenerator(filament::Engine* engine, bool optimizeShaders); |
| 37 |
~MaterialGenerator() override; |
| 38 |
|
| 39 |
MaterialSource getSource() const noexcept override { return GENERATE_SHADERS; } |
| 40 |
|
| 41 |
filament::MaterialInstance* createMaterialInstance(MaterialKey* config, UvMap* uvmap, |
| 42 |
const char* label) override; |
| 43 |
|
| 44 |
size_t getMaterialsCount() const noexcept override; |
| 45 |
const filament::Material* const* getMaterials() const noexcept override; |
| 46 |
void destroyMaterials() override; |
| 47 |
|
| 48 |
using HashFn = utils::hash::MurmurHashFn<MaterialKey>; |
| 49 |
tsl::robin_map<MaterialKey, filament::Material*, HashFn> mCache; |
| 50 |
std::vector<filament::Material*> mMaterials; |
| 51 |
filament::Engine* const mEngine; |
| 52 |
const bool mOptimizeShaders; |
| 53 |
}; |
| 54 |
|
| 55 |
MaterialGenerator::MaterialGenerator(Engine* engine, bool optimizeShaders) : mEngine(engine), |
| 56 |
mOptimizeShaders(optimizeShaders) { |
| 57 |
MaterialBuilder::init(); |
| 58 |
} |
| 59 |
|
| 60 |
MaterialGenerator::~MaterialGenerator() { |
| 61 |
MaterialBuilder::shutdown(); |
| 62 |
} |
| 63 |
|
| 64 |
size_t MaterialGenerator::getMaterialsCount() const noexcept { |
| 65 |
return mMaterials.size(); |
| 66 |
} |
| 67 |
|
| 68 |
const Material* const* MaterialGenerator::getMaterials() const noexcept { |
| 69 |
return mMaterials.data(); |
| 70 |
} |
| 71 |
|
| 72 |
void MaterialGenerator::destroyMaterials() { |
| 73 |
for (auto& iter : mCache) { |
| 74 |
mEngine->destroy(iter.second); |
| 75 |
} |
| 76 |
mMaterials.clear(); |
| 77 |
mCache.clear(); |
| 78 |
} |
| 79 |
|
| 80 |
std::string shaderFromKey(const MaterialKey& config) { |
| 81 |
std::string shader = "void material(inout MaterialInputs material) {\n"; |
| 82 |
|
| 83 |
if (config.hasNormalTexture && !config.unlit) { |
| 84 |
shader += "highp float2 normalUV = ${normal};\n"; |
| 85 |
if (config.hasTextureTransforms) { |
| 86 |
shader += "normalUV = (vec3(normalUV, 1.0) * materialParams.normalUvMatrix).xy;\n"; |
| 87 |
} |
| 88 |
shader += R"SHADER( |
| 89 |
material.normal = texture(materialParams_normalMap, normalUV).xyz * 2.0 - 1.0; |
| 90 |
material.normal.xy *= materialParams.normalScale; |
| 91 |
)SHADER"; |
| 92 |
} |
| 93 |
|
| 94 |
if (config.hasClearCoat && config.hasClearCoatNormalTexture && !config.unlit) { |
| 95 |
shader += "highp float2 clearCoatNormalUV = ${clearCoatNormal};\n"; |
| 96 |
if (config.hasTextureTransforms) { |
| 97 |
shader += "clearCoatNormalUV = (vec3(clearCoatNormalUV, 1.0) * " |
| 98 |
"materialParams.clearCoatNormalUvMatrix).xy;\n"; |
| 99 |
} |
| 100 |
shader += R"SHADER( |
| 101 |
material.clearCoatNormal = texture(materialParams_clearCoatNormalMap, clearCoatNormalUV).xyz * 2.0 - 1.0; |
| 102 |
material.clearCoatNormal.xy *= materialParams.clearCoatNormalScale; |
| 103 |
)SHADER"; |
| 104 |
} |
| 105 |
|
| 106 |
if (config.enableDiagnostics && !config.unlit) { |
| 107 |
shader += R"SHADER( |
| 108 |
if (materialParams.enableDiagnostics) { |
| 109 |
material.normal = vec3(0, 0, 1); |
| 110 |
} |
| 111 |
)SHADER"; |
| 112 |
} |
| 113 |
|
| 114 |
shader += R"SHADER( |
| 115 |
prepareMaterial(material); |
| 116 |
material.baseColor = materialParams.baseColorFactor; |
| 117 |
)SHADER"; |
| 118 |
|
| 119 |
if (config.hasBaseColorTexture) { |
| 120 |
shader += "highp float2 baseColorUV = ${color};\n"; |
| 121 |
if (config.hasTextureTransforms) { |
| 122 |
shader += "baseColorUV = (vec3(baseColorUV, 1.0) * " |
| 123 |
"materialParams.baseColorUvMatrix).xy;\n"; |
| 124 |
} |
| 125 |
shader += R"SHADER( |
| 126 |
material.baseColor *= texture(materialParams_baseColorMap, baseColorUV); |
| 127 |
)SHADER"; |
| 128 |
} |
| 129 |
|
| 130 |
if (config.enableDiagnostics) { |
| 131 |
shader += R"SHADER( |
| 132 |
#if defined(HAS_ATTRIBUTE_TANGENTS) |
| 133 |
if (materialParams.enableDiagnostics) { |
| 134 |
material.baseColor.rgb = vertex_worldNormal * 0.5 + 0.5; |
| 135 |
} |
| 136 |
#endif |
| 137 |
)SHADER"; |
| 138 |
} |
| 139 |
|
| 140 |
if (config.alphaMode == AlphaMode::BLEND) { |
| 141 |
shader += R"SHADER( |
| 142 |
material.baseColor.rgb *= material.baseColor.a; |
| 143 |
)SHADER"; |
| 144 |
} |
| 145 |
|
| 146 |
if (config.hasVertexColors) { |
| 147 |
shader += "material.baseColor *= getColor();\n"; |
| 148 |
} |
| 149 |
|
| 150 |
if (!config.unlit) { |
| 151 |
if (config.useSpecularGlossiness) { |
| 152 |
shader += R"SHADER( |
| 153 |
material.glossiness = materialParams.glossinessFactor; |
| 154 |
material.specularColor = materialParams.specularFactor; |
| 155 |
material.emissive = vec4(materialParams.emissiveFactor.rgb, 0.0); |
| 156 |
)SHADER"; |
| 157 |
} else { |
| 158 |
shader += R"SHADER( |
| 159 |
material.roughness = materialParams.roughnessFactor; |
| 160 |
material.metallic = materialParams.metallicFactor; |
| 161 |
material.emissive = vec4(materialParams.emissiveFactor.rgb, 0.0); |
| 162 |
)SHADER"; |
| 163 |
} |
| 164 |
if (config.hasMetallicRoughnessTexture) { |
| 165 |
shader += "highp float2 metallicRoughnessUV = ${metallic};\n"; |
| 166 |
if (config.hasTextureTransforms) { |
| 167 |
shader += "metallicRoughnessUV = (vec3(metallicRoughnessUV, 1.0) * " |
| 168 |
"materialParams.metallicRoughnessUvMatrix).xy;\n"; |
| 169 |
} |
| 170 |
if (config.useSpecularGlossiness) { |
| 171 |
shader += R"SHADER( |
| 172 |
vec4 sg = texture(materialParams_metallicRoughnessMap, metallicRoughnessUV); |
| 173 |
material.specularColor *= sg.rgb; |
| 174 |
material.glossiness *= sg.a; |
| 175 |
)SHADER"; |
| 176 |
} else { |
| 177 |
shader += R"SHADER( |
| 178 |
vec4 mr = texture(materialParams_metallicRoughnessMap, metallicRoughnessUV); |
| 179 |
material.roughness *= mr.g; |
| 180 |
material.metallic *= mr.b; |
| 181 |
)SHADER"; |
| 182 |
} |
| 183 |
} |
| 184 |
if (config.hasOcclusionTexture) { |
| 185 |
shader += "highp float2 aoUV = ${ao};\n"; |
| 186 |
if (config.hasTextureTransforms) { |
| 187 |
shader += "aoUV = (vec3(aoUV, 1.0) * materialParams.occlusionUvMatrix).xy;\n"; |
| 188 |
} |
| 189 |
shader += R"SHADER( |
| 190 |
material.ambientOcclusion = texture(materialParams_occlusionMap, aoUV).r * |
| 191 |
materialParams.aoStrength; |
| 192 |
)SHADER"; |
| 193 |
} |
| 194 |
if (config.hasEmissiveTexture) { |
| 195 |
shader += "highp float2 emissiveUV = ${emissive};\n"; |
| 196 |
if (config.hasTextureTransforms) { |
| 197 |
shader += "emissiveUV = (vec3(emissiveUV, 1.0) * " |
| 198 |
"materialParams.emissiveUvMatrix).xy;\n"; |
| 199 |
} |
| 200 |
shader += R"SHADER( |
| 201 |
material.emissive.rgb *= texture(materialParams_emissiveMap, emissiveUV).rgb; |
| 202 |
)SHADER"; |
| 203 |
} |
| 204 |
if (config.hasTransmission) { |
| 205 |
shader += R"SHADER( |
| 206 |
material.transmission = materialParams.transmissionFactor; |
| 207 |
|
| 208 |
// KHR_materials_transmission stipulates that baseColor be used for absorption, and |
| 209 |
// it says "the transmitted light will be modulated by this color as it passes", |
| 210 |
// which is inverted from Filament's notion of absorption. Note that Filament |
| 211 |
// clamps this value to [0,1]. |
| 212 |
material.absorption = 1.0 - material.baseColor.rgb; |
| 213 |
|
| 214 |
)SHADER"; |
| 215 |
if (config.hasTransmissionTexture) { |
| 216 |
shader += "highp float2 transmissionUV = ${transmission};\n"; |
| 217 |
if (config.hasTextureTransforms) { |
| 218 |
shader += "transmissionUV = (vec3(transmissionUV, 1.0) * " |
| 219 |
"materialParams.transmissionUvMatrix).xy;\n"; |
| 220 |
} |
| 221 |
shader += R"SHADER( |
| 222 |
material.transmission *= texture(materialParams_transmissionMap, transmissionUV).r; |
| 223 |
)SHADER"; |
| 224 |
} |
| 225 |
} |
| 226 |
if (config.hasClearCoat) { |
| 227 |
shader += R"SHADER( |
| 228 |
material.clearCoat = materialParams.clearCoatFactor; |
| 229 |
material.clearCoatRoughness = materialParams.clearCoatRoughnessFactor; |
| 230 |
)SHADER"; |
| 231 |
|
| 232 |
if (config.hasClearCoatTexture) { |
| 233 |
shader += "highp float2 clearCoatUV = ${clearCoat};\n"; |
| 234 |
if (config.hasTextureTransforms) { |
| 235 |
shader += "clearCoatUV = (vec3(clearCoatUV, 1.0) * " |
| 236 |
"materialParams.clearCoatUvMatrix).xy;\n"; |
| 237 |
} |
| 238 |
shader += R"SHADER( |
| 239 |
material.clearCoat *= texture(materialParams_clearCoatMap, clearCoatUV).r; |
| 240 |
)SHADER"; |
| 241 |
} |
| 242 |
|
| 243 |
if (config.hasClearCoatRoughnessTexture) { |
| 244 |
shader += "highp float2 clearCoatRoughnessUV = ${clearCoatRoughness};\n"; |
| 245 |
if (config.hasTextureTransforms) { |
| 246 |
shader += "clearCoatRoughnessUV = (vec3(clearCoatRoughnessUV, 1.0) * " |
| 247 |
"materialParams.clearCoatRoughnessUvMatrix).xy;\n"; |
| 248 |
} |
| 249 |
shader += R"SHADER( |
| 250 |
material.clearCoatRoughness *= texture(materialParams_clearCoatRoughnessMap, clearCoatRoughnessUV).g; |
| 251 |
)SHADER"; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if (config.hasSheen) { |
| 256 |
shader += R"SHADER( |
| 257 |
material.sheenColor = materialParams.sheenColorFactor; |
| 258 |
material.sheenRoughness = materialParams.sheenRoughnessFactor; |
| 259 |
)SHADER"; |
| 260 |
|
| 261 |
if (config.hasSheenColorTexture) { |
| 262 |
shader += "highp float2 sheenColorUV = ${sheenColor};\n"; |
| 263 |
if (config.hasTextureTransforms) { |
| 264 |
shader += "sheenColorUV = (vec3(sheenColorUV, 1.0) * " |
| 265 |
"materialParams.sheenColorUvMatrix).xy;\n"; |
| 266 |
} |
| 267 |
shader += R"SHADER( |
| 268 |
material.sheenColor *= texture(materialParams_sheenColorMap, sheenColorUV).rgb; |
| 269 |
)SHADER"; |
| 270 |
} |
| 271 |
|
| 272 |
if (config.hasSheenRoughnessTexture) { |
| 273 |
shader += "highp float2 sheenRoughnessUV = ${sheenRoughness};\n"; |
| 274 |
if (config.hasTextureTransforms) { |
| 275 |
shader += "sheenRoughnessUV = (vec3(sheenRoughnessUV, 1.0) * " |
| 276 |
"materialParams.sheenRoughnessUvMatrix).xy;\n"; |
| 277 |
} |
| 278 |
shader += R"SHADER( |
| 279 |
material.sheenRoughness *= texture(materialParams_sheenRoughnessMap, sheenRoughnessUV).a; |
| 280 |
)SHADER"; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
shader += "}\n"; |
| 286 |
return shader; |
| 287 |
} |
| 288 |
|
| 289 |
static Material* createMaterial(Engine* engine, const MaterialKey& config, const UvMap& uvmap, |
| 290 |
const char* name, bool optimizeShaders) { |
| 291 |
std::string shader = shaderFromKey(config); |
| 292 |
processShaderString(&shader, uvmap, config); |
| 293 |
MaterialBuilder builder = MaterialBuilder() |
| 294 |
.name(name) |
| 295 |
.flipUV(false) |
| 296 |
.specularAmbientOcclusion(MaterialBuilder::SpecularAmbientOcclusion::SIMPLE) |
| 297 |
.specularAntiAliasing(true) |
| 298 |
.clearCoatIorChange(false) |
| 299 |
.material(shader.c_str()) |
| 300 |
.doubleSided(config.doubleSided) |
| 301 |
.targetApi(filamat::targetApiFromBackend(engine->getBackend())); |
| 302 |
|
| 303 |
if (!optimizeShaders) { |
| 304 |
builder.optimization(MaterialBuilder::Optimization::NONE); |
| 305 |
} |
| 306 |
|
| 307 |
static_assert(std::tuple_size<UvMap>::value == 8, "Badly sized uvset."); |
| 308 |
int numUvSets = getNumUvSets(uvmap); |
| 309 |
if (numUvSets > 0) { |
| 310 |
builder.require(VertexAttribute::UV0); |
| 311 |
} |
| 312 |
if (numUvSets > 1) { |
| 313 |
builder.require(VertexAttribute::UV1); |
| 314 |
} |
| 315 |
|
| 316 |
if (config.enableDiagnostics) { |
| 317 |
builder.parameter(MaterialBuilder::UniformType::BOOL, "enableDiagnostics"); |
| 318 |
} |
| 319 |
|
| 320 |
// BASE COLOR |
| 321 |
builder.parameter(MaterialBuilder::UniformType::FLOAT4, "baseColorFactor"); |
| 322 |
if (config.hasBaseColorTexture) { |
| 323 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "baseColorMap"); |
| 324 |
if (config.hasTextureTransforms) { |
| 325 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "baseColorUvMatrix"); |
| 326 |
} |
| 327 |
} |
| 328 |
if (config.hasVertexColors) { |
| 329 |
builder.require(VertexAttribute::COLOR); |
| 330 |
} |
| 331 |
|
| 332 |
// METALLIC-ROUGHNESS |
| 333 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "metallicFactor"); |
| 334 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "roughnessFactor"); |
| 335 |
if (config.hasMetallicRoughnessTexture) { |
| 336 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "metallicRoughnessMap"); |
| 337 |
if (config.hasTextureTransforms) { |
| 338 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "metallicRoughnessUvMatrix"); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
// SPECULAR-GLOSSINESS |
| 343 |
if (config.useSpecularGlossiness) { |
| 344 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "glossinessFactor"); |
| 345 |
builder.parameter(MaterialBuilder::UniformType::FLOAT3, "specularFactor"); |
| 346 |
} |
| 347 |
|
| 348 |
// NORMAL MAP |
| 349 |
// In the glTF spec normalScale is in normalTextureInfo; in cgltf it is part of texture_view. |
| 350 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "normalScale"); |
| 351 |
if (config.hasNormalTexture) { |
| 352 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "normalMap"); |
| 353 |
if (config.hasTextureTransforms) { |
| 354 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "normalUvMatrix"); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// AMBIENT OCCLUSION |
| 359 |
// In the glTF spec aoStrength is in occlusionTextureInfo; in cgltf it is part of texture_view. |
| 360 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "aoStrength"); |
| 361 |
if (config.hasOcclusionTexture) { |
| 362 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "occlusionMap"); |
| 363 |
if (config.hasTextureTransforms) { |
| 364 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "occlusionUvMatrix"); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
// EMISSIVE |
| 369 |
builder.parameter(MaterialBuilder::UniformType::FLOAT3, "emissiveFactor"); |
| 370 |
if (config.hasEmissiveTexture) { |
| 371 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "emissiveMap"); |
| 372 |
if (config.hasTextureTransforms) { |
| 373 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "emissiveUvMatrix"); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
// CLEAR COAT |
| 378 |
if (config.hasClearCoat) { |
| 379 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "clearCoatFactor"); |
| 380 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "clearCoatRoughnessFactor"); |
| 381 |
if (config.hasClearCoatTexture) { |
| 382 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "clearCoatMap"); |
| 383 |
if (config.hasTextureTransforms) { |
| 384 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "clearCoatUvMatrix"); |
| 385 |
} |
| 386 |
} |
| 387 |
if (config.hasClearCoatRoughnessTexture) { |
| 388 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "clearCoatRoughnessMap"); |
| 389 |
if (config.hasTextureTransforms) { |
| 390 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "clearCoatRoughnessUvMatrix"); |
| 391 |
} |
| 392 |
} |
| 393 |
if (config.hasClearCoatNormalTexture) { |
| 394 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "clearCoatNormalScale"); |
| 395 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "clearCoatNormalMap"); |
| 396 |
if (config.hasTextureTransforms) { |
| 397 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "clearCoatNormalUvMatrix"); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
// SHEEN |
| 403 |
if (config.hasSheen) { |
| 404 |
builder.parameter(MaterialBuilder::UniformType::FLOAT3, "sheenColorFactor"); |
| 405 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "sheenRoughnessFactor"); |
| 406 |
if (config.hasSheenColorTexture) { |
| 407 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "sheenColorMap"); |
| 408 |
if (config.hasTextureTransforms) { |
| 409 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "sheenColorUvMatrix"); |
| 410 |
} |
| 411 |
} |
| 412 |
if (config.hasSheenRoughnessTexture) { |
| 413 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "sheenRoughnessMap"); |
| 414 |
if (config.hasTextureTransforms) { |
| 415 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "sheenRoughnessUvMatrix"); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
// TRANSMISSION |
| 421 |
if (config.hasTransmission) { |
| 422 |
|
| 423 |
// According to KHR_materials_transmission, the minimum expectation for a compliant renderer |
| 424 |
// is to at least render any opaque objects that lie behind transmitting objects. |
| 425 |
builder.refractionMode(RefractionMode::SCREEN_SPACE); |
| 426 |
|
| 427 |
// Thin refraction probably makes the most sense, given the language of the transmission |
| 428 |
// spec and its lack of an IOR parameter. This means that we would do a good job rendering a |
| 429 |
// window pane, but a poor job of rendering a glass full of liquid. |
| 430 |
builder.refractionType(RefractionType::THIN); |
| 431 |
|
| 432 |
builder.parameter(MaterialBuilder::UniformType::FLOAT, "transmissionFactor"); |
| 433 |
if (config.hasTransmissionTexture) { |
| 434 |
builder.parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "transmissionMap"); |
| 435 |
if (config.hasTextureTransforms) { |
| 436 |
builder.parameter(MaterialBuilder::UniformType::MAT3, "transmissionUvMatrix"); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
builder.blending(MaterialBuilder::BlendingMode::FADE); |
| 441 |
builder.depthWrite(true); |
| 442 |
|
| 443 |
} else { |
| 444 |
|
| 445 |
// BLENDING |
| 446 |
switch (config.alphaMode) { |
| 447 |
case AlphaMode::OPAQUE: |
| 448 |
builder.blending(MaterialBuilder::BlendingMode::OPAQUE); |
| 449 |
break; |
| 450 |
case AlphaMode::MASK: |
| 451 |
builder.blending(MaterialBuilder::BlendingMode::MASKED); |
| 452 |
break; |
| 453 |
case AlphaMode::BLEND: |
| 454 |
builder.blending(MaterialBuilder::BlendingMode::FADE); |
| 455 |
builder.depthWrite(true); |
| 456 |
break; |
| 457 |
default: |
| 458 |
// Ignore |
| 459 |
break; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
if (config.unlit) { |
| 464 |
builder.shading(Shading::UNLIT); |
| 465 |
} else if (config.useSpecularGlossiness) { |
| 466 |
builder.shading(Shading::SPECULAR_GLOSSINESS); |
| 467 |
} else { |
| 468 |
builder.shading(Shading::LIT); |
| 469 |
} |
| 470 |
|
| 471 |
Package pkg = builder.build(engine->getJobSystem()); |
| 472 |
return Material::Builder().package(pkg.getData(), pkg.getSize()).build(*engine); |
| 473 |
} |
| 474 |
|
| 475 |
MaterialInstance* MaterialGenerator::createMaterialInstance(MaterialKey* config, UvMap* uvmap, |
| 476 |
const char* label) { |
| 477 |
constrainMaterial(config, uvmap); |
| 478 |
auto iter = mCache.find(*config); |
| 479 |
if (iter == mCache.end()) { |
| 480 |
|
| 481 |
bool optimizeShaders = mOptimizeShaders; |
| 482 |
#ifndef NDEBUG |
| 483 |
optimizeShaders = false; |
| 484 |
#endif |
| 485 |
|
| 486 |
Material* mat = createMaterial(mEngine, *config, *uvmap, label, optimizeShaders); |
| 487 |
mCache.emplace(std::make_pair(*config, mat)); |
| 488 |
mMaterials.push_back(mat); |
| 489 |
return mat->createInstance(label); |
| 490 |
} |
| 491 |
return iter->second->createInstance(label); |
| 492 |
} |
| 493 |
|
| 494 |
} // anonymous namespace |
| 495 |
|
| 496 |
namespace gltfio { |
| 497 |
|
| 498 |
MaterialProvider* createMaterialGenerator(filament::Engine* engine, bool optimizeShaders) { |
| 499 |
return new MaterialGenerator(engine, optimizeShaders); |
| 500 |
} |
| 501 |
|
| 502 |
} // namespace gltfio |