internal: remove vertexScale from unlit material and multiply by alpha

This commit is contained in:
Nick Fisher
2025-01-02 13:16:42 +08:00
parent 242041c9f6
commit cc1b55b6c9
5 changed files with 31 additions and 197 deletions

View File

@@ -17,16 +17,12 @@ material {
{
type: float2,
name: uvScale
},
{
type: float,
name: vertexScale
}
],
depthWrite : true,
depthCulling : true,
shadingModel : unlit,
blending: opaque,
blending: transparent,
culling: none,
instanced: false,
vertexDomain: object,
@@ -36,6 +32,7 @@ fragment {
void material(inout MaterialInputs material) {
prepareMaterial(material);
material.baseColor = materialParams.baseColorFactor;
material.baseColor.rgb = material.baseColor.rgb * material.baseColor.a;
if (materialParams.baseColorIndex > -1) {
highp float2 uv = getUV0();
@@ -49,7 +46,6 @@ fragment {
vertex {
void materialVertex(inout MaterialVertexInputs material) {
float4 position = getPosition();
position.xyz *= materialParams.vertexScale;
material.worldPosition = getWorldFromModelMatrix() * position;
}
}

View File

@@ -1,178 +0,0 @@
material {
name : unlit_fade,
requires : [ uv0, uv1, color ],
shadingModel : ${SHADINGMODEL},
blending : ${BLENDING},
doubleSided : ${DOUBLESIDED},
transparency : ${TRANSPARENCY},
flipUV : false,
specularAmbientOcclusion : simple,
specularAntiAliasing : true,
clearCoatIorChange : false,
reflections : screenspace,
parameters : [
{ type : float3, name : specularFactor },
{ type : float, name : glossinessFactor },
// Base Color
{ type : int, name : baseColorIndex },
{ type : float4, name : baseColorFactor },
{ type : sampler2d, name : baseColorMap },
{ type : mat3, name : baseColorUvMatrix, precision: high },
// Metallic-Roughness Map
{ type : int, name : metallicRoughnessIndex },
{ type : float, name : metallicFactor },
{ type : float, name : roughnessFactor },
{ type : sampler2d, name : metallicRoughnessMap },
{ type : mat3, name : metallicRoughnessUvMatrix, precision: high },
// Normal Map
{ type : int, name : normalIndex },
{ type : float, name : normalScale },
{ type : sampler2d, name : normalMap },
{ type : mat3, name : normalUvMatrix, precision: high },
// Ambient Occlusion
{ type : int, name : aoIndex },
{ type : float, name : aoStrength },
{ type : sampler2d, name : occlusionMap },
{ type : mat3, name : occlusionUvMatrix, precision: high },
// Emissive Map
{ type : int, name : emissiveIndex },
{ type : float3, name : emissiveFactor },
{ type : float, name : emissiveStrength },
{ type : sampler2d, name : emissiveMap },
{ type : mat3, name : emissiveUvMatrix, precision: high },
// Clear coat
{ type : float, name : clearCoatFactor },
{ type : float, name : clearCoatRoughnessFactor },
{ type : int, name : clearCoatIndex },
{ type : sampler2d, name : clearCoatMap },
{ type : mat3, name : clearCoatUvMatrix, precision: high },
{ type : int, name : clearCoatRoughnessIndex },
{ type : sampler2d, name : clearCoatRoughnessMap },
{ type : mat3, name : clearCoatRoughnessUvMatrix, precision: high },
{ type : int, name : clearCoatNormalIndex },
{ type : sampler2d, name : clearCoatNormalMap },
{ type : mat3, name : clearCoatNormalUvMatrix, precision: high },
{ type : float, name : clearCoatNormalScale },
// Reflectance
{ type : float, name : reflectance }
${CUSTOM_PARAMS}
],
}
vertex {
void materialVertex(inout MaterialVertexInputs material) {
${CUSTOM_VERTEX}
}
}
fragment {
void material(inout MaterialInputs material) {
highp float2 uvs[2];
uvs[0] = getUV0();
uvs[1] = getUV1();
#if !defined(SHADING_MODEL_UNLIT)
if (materialParams.normalIndex > -1) {
highp float2 uv = uvs[materialParams.normalIndex];
uv = (vec3(uv, 1.0) * materialParams.normalUvMatrix).xy;
material.normal = texture(materialParams_normalMap, uv).xyz * 2.0 - 1.0;
material.normal.xy *= materialParams.normalScale;
}
#if defined(SHADING_MODEL_LIT)
if (materialParams.clearCoatNormalIndex > -1) {
highp float2 uv = uvs[materialParams.clearCoatNormalIndex];
uv = (vec3(uv, 1.0) * materialParams.clearCoatNormalUvMatrix).xy;
material.clearCoatNormal = texture(materialParams_clearCoatNormalMap, uv).xyz * 2.0 - 1.0;
material.clearCoatNormal.xy *= materialParams.clearCoatNormalScale;
}
#endif
#endif
prepareMaterial(material);
material.baseColor = materialParams.baseColorFactor;
if (materialParams.baseColorIndex > -1) {
highp float2 uv = uvs[materialParams.baseColorIndex];
uv = (vec3(uv, 1.0) * materialParams.baseColorUvMatrix).xy;
material.baseColor *= texture(materialParams_baseColorMap, uv);
}
#if defined(BLEND_MODE_TRANSPARENT)
material.baseColor.rgb *= material.baseColor.a;
#endif
material.baseColor *= getColor();
#if !defined(SHADING_MODEL_UNLIT)
#if defined(SHADING_MODEL_LIT)
material.roughness = materialParams.roughnessFactor;
material.metallic = materialParams.metallicFactor;
// KHR_materials_clearcoat forbids clear coat from
// being applied in the specular/glossiness model
material.clearCoat = materialParams.clearCoatFactor;
material.clearCoatRoughness = materialParams.clearCoatRoughnessFactor;
if (materialParams.clearCoatIndex > -1) {
highp float2 uv = uvs[materialParams.clearCoatIndex];
uv = (vec3(uv, 1.0) * materialParams.clearCoatUvMatrix).xy;
material.clearCoat *= texture(materialParams_clearCoatMap, uv).r;
}
if (materialParams.clearCoatRoughnessIndex > -1) {
highp float2 uv = uvs[materialParams.clearCoatRoughnessIndex];
uv = (vec3(uv, 1.0) * materialParams.clearCoatRoughnessUvMatrix).xy;
material.clearCoatRoughness *= texture(materialParams_clearCoatRoughnessMap, uv).g;
}
#endif
material.emissive = vec4(materialParams.emissiveStrength *
materialParams.emissiveFactor.rgb, 0.0);
#if defined(SHADING_MODEL_SPECULAR_GLOSSINESS)
material.glossiness = materialParams.glossinessFactor;
material.specularColor = materialParams.specularFactor;
#else
material.reflectance = materialParams.reflectance;
#endif
if (materialParams.metallicRoughnessIndex > -1) {
highp float2 uv = uvs[materialParams.metallicRoughnessIndex];
uv = (vec3(uv, 1.0) * materialParams.metallicRoughnessUvMatrix).xy;
#if defined(SHADING_MODEL_SPECULAR_GLOSSINESS)
vec4 sg = texture(materialParams_metallicRoughnessMap, uv);
material.specularColor *= sg.rgb;
material.glossiness *= sg.a;
#else
vec4 mr = texture(materialParams_metallicRoughnessMap, uv);
material.roughness *= mr.g;
material.metallic *= mr.b;
#endif
}
if (materialParams.aoIndex > -1) {
highp float2 uv = uvs[materialParams.aoIndex];
uv = (vec3(uv, 1.0) * materialParams.occlusionUvMatrix).xy;
float occlusion = texture(materialParams_occlusionMap, uv).r;
material.ambientOcclusion = 1.0 + materialParams.aoStrength * (occlusion - 1.0);
}
if (materialParams.emissiveIndex > -1) {
highp float2 uv = uvs[materialParams.emissiveIndex];
uv = (vec3(uv, 1.0) * materialParams.emissiveUvMatrix).xy;
material.emissive.rgb *= texture(materialParams_emissiveMap, uv).rgb;
}
#endif
${CUSTOM_FRAGMENT}
}
}

View File

@@ -149,7 +149,6 @@ class FFIAsset extends ThermionAsset {
await highlightMaterialInstance.setDepthCullingEnabled(false);
await highlightMaterialInstance.setParameterFloat("vertexScale", 1.03);
await highlightMaterialInstance.setParameterFloat4(
"baseColorFactor", r, g, b, 1.0);

View File

@@ -36,7 +36,6 @@ public:
const char* extras = nullptr) override {
auto instance = mUnlitMaterial->createInstance();
instance->setParameter("baseColorIndex", -1);
instance->setParameter("vertexScale", 1.0f);
instance->setParameter("uvScale", filament::math::float2 { 1.0f, 1.0f });
return instance;
}

View File

@@ -49,17 +49,35 @@ void main() async {
await viewer.setToneMapping(ToneMapper.LINEAR);
var materialInstance = await viewer.createUnlitMaterialInstance();
var cube = await viewer.createGeometry(GeometryHelper.cube(),
var cube = await viewer.createGeometry(
GeometryHelper.cube(normals: false, uvs: false),
materialInstances: [materialInstance]);
await materialInstance.setParameterFloat4(
"baseColorFactor", 0.0, 1.0, 0.0, 1.0);
await materialInstance.setParameterInt("baseColorIndex", -1);
// await viewer.render();
// await viewer.render();
// await viewer.render();
await testHelper.capture(viewer, "unlit_material_base_color");
});
await viewer.destroyMaterialInstance(materialInstance);
}, bg: kRed);
});
test('unlit material with color + alpha', () async {
await testHelper.withViewer((viewer) async {
await viewer.setPostProcessing(true);
await viewer.setToneMapping(ToneMapper.LINEAR);
var materialInstance = await viewer.createUnlitMaterialInstance();
var cube = await viewer.createGeometry(
GeometryHelper.cube(normals: false, uvs: false),
materialInstances: [materialInstance]);
await materialInstance.setParameterFloat4(
"baseColorFactor", 0.0, 1.0, 0.0, 0.1);
await materialInstance.setParameterInt("baseColorIndex", -1);
await testHelper.capture(viewer, "unlit_material_base_color_alpha");
await viewer.destroyMaterialInstance(materialInstance);
}, bg: kRed);
});
test('unlit fixed size material', () async {
@@ -105,7 +123,7 @@ void main() async {
await viewer.applyTexture(texture as ThermionFFITexture, cube.entity);
await testHelper.capture(
viewer, "geometry_cube_with_custom_material_ubershader_texture");
await viewer.removeEntity(cube);
await viewer.removeAsset(cube);
await viewer.destroyMaterialInstance(materialInstance);
await viewer.destroyTexture(texture);
await viewer.dispose();
@@ -131,7 +149,7 @@ void main() async {
await viewer.applyTexture(texture, cube.entity);
await testHelper.capture(
viewer, "geometry_cube_with_custom_material_unlit_texture_only");
await viewer.removeEntity(cube);
await viewer.removeAsset(cube);
cube = await viewer.createGeometry(GeometryHelper.cube(),
materialInstances: [materialInstance]);
@@ -141,7 +159,7 @@ void main() async {
"baseColorFactor", 0.0, 1.0, 0.0, 1.0);
await testHelper.capture(
viewer, "geometry_cube_with_custom_material_unlit_color_only");
await viewer.removeEntity(cube);
await viewer.removeAsset(cube);
cube = await viewer.createGeometry(GeometryHelper.cube(),
materialInstances: [materialInstance]);
@@ -154,7 +172,7 @@ void main() async {
await testHelper.capture(
viewer, "geometry_cube_with_custom_material_unlit_color_and_texture");
await viewer.removeEntity(cube);
await viewer.removeAsset(cube);
await viewer.destroyTexture(texture);
await viewer.destroyMaterialInstance(materialInstance);
@@ -400,7 +418,7 @@ void main() async {
// await testHelper.capture(viewer, "texture_applied_to_geometry");
// await viewer.removeEntity(cube);
// await viewer.removeAsset(cube);
// await viewer.destroyTexture(texture);
// await viewer.dispose();
// });
@@ -509,7 +527,7 @@ void main() async {
// await viewer.setToneMapping(ToneMapper.LINEAR);
// final unlit = await viewer.createUnlitMaterialInstance();
// await viewer.removeEntity(cube);
// await viewer.removeAsset(cube);
// cube = await viewer.createGeometry(GeometryHelper.cube(),
// materialInstance: unlit);
// var reconstructedTexture = await viewer.createTexture(pixelBufferPng);