Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MASK alpha mode rendering issues #242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyrender/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def _get_trimesh_props(mesh, smooth=False, material=None):
occlusionTexture=mat.occlusionTexture,
emissiveTexture=mat.emissiveTexture,
emissiveFactor=mat.emissiveFactor,
alphaMode='BLEND',
alphaMode=mat.alphaMode,
baseColorFactor=mat.baseColorFactor,
baseColorTexture=mat.baseColorTexture,
metallicFactor=mat.metallicFactor,
Expand Down
10 changes: 7 additions & 3 deletions pyrender/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,12 +564,14 @@ def _bind_and_draw_primitive(self, primitive, pose, program, flags):
program.set_uniform(b.format('glossiness_factor'),
material.glossinessFactor)

# Set blending options
# Set alpha options
glEnable(GL_BLEND)
if material.alphaMode == 'BLEND':
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
elif material.alphaMode == "MASK":
program.set_uniform(b.format('alpha_cutoff'), material.alphaCutoff)
glBlendFunc(GL_ONE, GL_ZERO)
else:
glEnable(GL_BLEND)
glBlendFunc(GL_ONE, GL_ZERO)

# Set wireframe mode
Expand Down Expand Up @@ -988,6 +990,8 @@ def _get_primitive_program(self, primitive, flags, program_flags):
defines['USE_METALLIC_MATERIAL'] = 1
elif isinstance(primitive.material, SpecularGlossinessMaterial):
defines['USE_GLOSSY_MATERIAL'] = 1
if primitive.material.alphaMode == 'MASK':
defines['IS_MASK_ALPHA_MODE'] = 1

program = self._program_cache.get_program(
vertex_shader=vertex_shader,
Expand Down
7 changes: 7 additions & 0 deletions pyrender/shaders/mesh.frag
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ struct Material {
#ifdef HAS_BASE_COLOR_TEX
sampler2D base_color_texture;
#endif
#ifdef IS_MASK_ALPHA_MODE
float alpha_cutoff;
#endif
#ifdef HAS_METALLIC_ROUGHNESS_TEX
sampler2D metallic_roughness_texture;
#endif
Expand Down Expand Up @@ -338,6 +341,10 @@ void main()
#ifdef HAS_BASE_COLOR_TEX
base_color = base_color * srgb_to_linear(texture(material.base_color_texture, uv_0));
#endif
#ifdef IS_MASK_ALPHA_MODE
// Discard fragment if alpha is less than the cutoff
if (base_color.a <= material.alpha_cutoff) discard;
#endif

// Compute specular and diffuse colors
vec3 dialectric_spec = vec3(min_roughness);
Expand Down