Skip to content

Commit 8f6e156

Browse files
committed
fixed mesh shader bug triggered by trimesh version bump
1 parent ad44071 commit 8f6e156

6 files changed

Lines changed: 70 additions & 59 deletions

File tree

coperniFUS/__init__.py

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import pyqtgraph.opengl as gl
1212
import numpy as np
1313

14+
# Handle multiple GLview instances
15+
pyqtc.QCoreApplication.setAttribute(pyqtc.Qt.ApplicationAttribute.AA_ShareOpenGLContexts)
1416

1517
from coperniFUS.modules import _jsonshelve
1618

@@ -701,53 +703,45 @@ def get_gl_item_from_name(self, gl_item_name):
701703
else:
702704
return None
703705

704-
# ----- Custom Mesh Shaders -----
705-
# Src: https://stackoverflow.com/a/68989314/9645937
706-
707-
gl.shaders.Shaders.append(gl.shaders.ShaderProgram('boneShader', [
708-
gl.shaders.VertexShader("""
709-
varying vec3 normal;
710-
void main() {
711-
// compute here for use in fragment shader
712-
normal = normalize(gl_NormalMatrix * gl_Normal);
713-
gl_FrontColor = gl_Color;
714-
gl_BackColor = gl_Color;
715-
gl_Position = ftransform();
716-
}
717-
"""),
718-
gl.shaders.FragmentShader("""
719-
varying vec3 normal;
720-
void main() {
721-
vec4 color = gl_Color;
722-
color.x = (normal.y + 1.0) * 0.972 * .4;
723-
color.y = (normal.y + 1.0) * 0.760 * .4;
724-
color.z = (normal.y + 1.0) * 0.568 * .4;
725-
color.w = 0.5;
726-
gl_FragColor = color;
727-
}
728-
""")
706+
# ----- Custom Mesh Shader -----
707+
# Src: https://github.com/pyqtgraph/pyqtgraph/discussions/3230
708+
709+
vert_shader = """
710+
#version 120
711+
712+
uniform mat4 u_mvp;
713+
uniform mat3 u_normal;
714+
715+
attribute vec4 a_position;
716+
attribute vec3 a_normal;
717+
attribute vec4 a_color;
718+
719+
varying vec4 v_color;
720+
varying vec3 v_normal;
721+
722+
void main()
723+
{
724+
v_normal = normalize(u_normal * a_normal);
725+
v_color = a_color;
726+
gl_Position = u_mvp * a_position;
727+
}
728+
"""
729+
frag_shader = """
730+
#version 120
731+
732+
varying vec4 v_color;
733+
varying vec3 v_normal;
734+
735+
void main()
736+
{
737+
float shade = (v_normal.y + 1.0) * 0.4;
738+
gl_FragColor = vec4(v_color.rgb * shade, v_color.a);
739+
}
740+
"""
741+
742+
gl.shaders.Shaders.append(gl.shaders.ShaderProgram('softShade', [
743+
gl.shaders.VertexShader(vert_shader),
744+
gl.shaders.FragmentShader(frag_shader),
729745
]))
730746

731-
gl.shaders.Shaders.append(gl.shaders.ShaderProgram('bwShader', [
732-
gl.shaders.VertexShader("""
733-
varying vec3 normal;
734-
void main() {
735-
// compute here for use in fragment shader
736-
normal = normalize(gl_NormalMatrix * gl_Normal);
737-
gl_FrontColor = gl_Color;
738-
gl_BackColor = gl_Color;
739-
gl_Position = ftransform();
740-
}
741-
"""),
742-
gl.shaders.FragmentShader("""
743-
varying vec3 normal;
744-
void main() {
745-
vec4 color = gl_Color;
746-
color.x = (normal.y + 1.0) * .4;
747-
color.y = (normal.y + 1.0) * .4;
748-
color.z = (normal.y + 1.0) * .4;
749-
color.w = 0.8;
750-
gl_FragColor = color;
751-
}
752-
""")
753-
]))
747+
AVAILABLE_SHADER_NAMES = [name for name in gl.shaders.Shaders[0].names if name is not None]

coperniFUS/examples/Dual arms FUS + rec electrode rat tutorial config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126
"transform_str": "Rz90deg Tz-0.292m Tx0.415m Ry4.2deg S.09",
127127
"ignore_plane_slicing": false,
128128
"ignore_anatomical_landmarks_calibration": false,
129-
"gl_mesh_shader": "boneShader",
129+
"gl_mesh_shader": "softShade",
130+
"gl_mesh_color": [0.972, 0.76, 0.568, 1.0],
130131
"gl_mesh_drawEdges": false,
131132
"gl_mesh_drawFaces": true,
132133
"gl_mesh_edgeColor": [
@@ -145,7 +146,8 @@
145146
"mesh_handler.Pohl2013_coarse.file_path": "/Users/tomaubier/Documents/US_Neurostim/Analysis_scripts/CoperniFUS/coperniFUS/examples/assets/Pohl2013_coarse.stl",
146147
"mesh_handler.Pohl2013_coarse.ignore_anatomical_landmarks_calibration": false,
147148
"mesh_handler.Pohl2013_coarse.ignore_plane_slicing": false,
148-
"mesh_handler.Pohl2013_coarse.gl_mesh_shader": "boneShader",
149+
"mesh_handler.Pohl2013_coarse.gl_mesh_shader": "softShade",
150+
"mesh_handler.Pohl2013_coarse.gl_mesh_color": [0.972, 0.76, 0.568, 1.0],
149151
"mesh_handler.Pohl2013_coarse.gl_mesh_drawEdges": false,
150152
"mesh_handler.Pohl2013_coarse.gl_mesh_drawFaces": true,
151153
"mesh_handler.Pohl2013_coarse.gl_mesh_edgeColor": [

coperniFUS/modules/armatures/mesh_armatures.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,11 @@ class STLMeshBooleanArmature(STLMeshArmature):
295295
'transform_str': 'S.11 Rz90deg Tz-31.9mm Tx46mm Ry3.7deg',
296296
'ignore_plane_slicing': False,
297297
'ignore_anatomical_landmarks_calibration': False,
298-
'gl_mesh_shader': 'boneShader',
298+
'gl_mesh_shader': 'softShade',
299+
'gl_mesh_color': [0.972, 0.76, 0.568, 1.],
299300
'gl_mesh_drawEdges': False,
300301
'gl_mesh_drawFaces': True,
301-
'gl_mesh_edgeColor': (.5, .5, .5, .7),
302+
'gl_mesh_edgeColor': [.5, .5, .5, .7],
302303
'gl_mesh_glOptions': 'opaque',
303304
'gl_mesh_smooth': False,
304305
'gl_mesh_edgeWidth': 2

coperniFUS/modules/interfaces/trimesh_interfaces.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class TrimeshHandler:
99
'gl_mesh_shader': 'viewNormalColor',
1010
'gl_mesh_drawEdges': False,
1111
'gl_mesh_drawFaces': True,
12-
'gl_mesh_edgeColor': (.5, .5, .5, .7),
12+
'gl_mesh_color': [.5, .5, .5, 1.],
13+
'gl_mesh_edgeColor': [.5, .5, .5, .7],
1314
'gl_mesh_glOptions': 'opaque',
1415
'gl_mesh_smooth': False,
1516
'gl_mesh_edgeWidth': 5,
@@ -103,13 +104,22 @@ def stl_item_tmat(self, value):
103104

104105
def add_rendered_object(self):
105106
""" Called when populating the viewer with the module rendered objects """
107+
108+
def handle_unavailable_shade_names(shader_name):
109+
if shader_name is not None and shader_name not in AVAILABLE_SHADER_NAMES:
110+
warnings.warn(f'{shader_name} does not exist. Please use one of these:\n\t{"\n\t".join(AVAILABLE_SHADER_NAMES)}.')
111+
shader_name = 'shaded'
112+
return shader_name
113+
106114
def add_mesh_render(mesh):
107115
stl_item_gl_mesh_data = gl.MeshData(vertexes=mesh.vertices, faces=mesh.faces)
116+
108117
self.stl_glitem.append(
109118
gl.GLMeshItem(
110119
meshdata=stl_item_gl_mesh_data,
111-
shader=self.get_user_param('gl_mesh_shader'), # TODO get_user_param -> redondant -> transfer to stl dock
120+
shader=handle_unavailable_shade_names(self.get_user_param('gl_mesh_shader')), # TODO get_user_param -> redondant -> transfer to stl dock
112121
smooth=self.get_user_param('gl_mesh_smooth'),
122+
color=self.get_user_param('gl_mesh_color'),
113123
drawFaces=self.get_user_param('gl_mesh_drawFaces'),
114124
drawEdges=self.get_user_param('gl_mesh_drawEdges'),
115125
edgeColor=self.get_user_param('gl_mesh_edgeColor'),
@@ -200,7 +210,8 @@ class StlHandler(TrimeshHandler):
200210
'gl_mesh_shader': 'viewNormalColor',
201211
'gl_mesh_drawEdges': False,
202212
'gl_mesh_drawFaces': True,
203-
'gl_mesh_edgeColor': (.5, .5, .5, .7),
213+
'gl_mesh_color': [.5, .5, .5, 1.],
214+
'gl_mesh_edgeColor': [.5, .5, .5, .7],
204215
'gl_mesh_glOptions': 'opaque',
205216
'gl_mesh_smooth': False,
206217
'gl_mesh_edgeWidth': 5,
@@ -319,7 +330,8 @@ class StlHandler(TrimeshHandler):
319330
'gl_mesh_shader': 'viewNormalColor',
320331
'gl_mesh_drawEdges': False,
321332
'gl_mesh_drawFaces': True,
322-
'gl_mesh_edgeColor': (.5, .5, .5, .7),
333+
'gl_mesh_color': [.5, .5, .5, 1.],
334+
'gl_mesh_edgeColor': [.5, .5, .5, .7],
323335
'gl_mesh_glOptions': 'opaque',
324336
'gl_mesh_smooth': False,
325337
'gl_mesh_edgeWidth': 5,

docs/contents/tutorial.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ Based on the affine transformation string determined earlier the following confi
122122
'transform_str': 'Rz90deg Tz-0.292m Tx0.415m Ry4.2deg S.09',
123123
'ignore_plane_slicing': False,
124124
'ignore_anatomical_landmarks_calibration': False,
125-
'gl_mesh_shader': 'boneShader',
125+
'gl_mesh_shader': 'softShade',
126+
'gl_mesh_color': [0.972, 0.76, 0.568, 1.],
126127
'gl_mesh_drawEdges': False,
127128
'gl_mesh_drawFaces': True,
128129
'gl_mesh_edgeColor': [0.9, 0.9, 0.9, 0.7],

tests/tutorial_config_generator_script.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
'transform_str': 'Rz90deg Tz-0.292m Tx0.415m Ry4.2deg S.09',
6060
'ignore_plane_slicing': False,
6161
'ignore_anatomical_landmarks_calibration': False,
62-
'gl_mesh_shader': 'boneShader',
62+
'gl_mesh_shader': 'softShade',
63+
'gl_mesh_color': [0.972, 0.76, 0.568, 1.],
6364
'gl_mesh_drawEdges': False,
6465
'gl_mesh_drawFaces': True,
6566
'gl_mesh_edgeColor': [0.9, 0.9, 0.9, 0.7],
@@ -672,7 +673,7 @@
672673
'rho_2': 1850,
673674
'alpha_2': 2.693,
674675
'alpha_power_2': 1.18,
675-
'alpha_mode': None,
676+
# 'alpha_mode': None,
676677
'source_f0': 1000000.0,
677678
'source_roc': 0.015,
678679
'source_diameter': 0.015,

0 commit comments

Comments
 (0)