Speed up the rendering of the solid view by caching support_angle property

This commit is contained in:
Jaime van Kessel 2019-04-19 14:55:28 +02:00
parent 9daa78a98f
commit 50ad4e4d4e
2 changed files with 40 additions and 13 deletions

View file

@ -1,4 +1,4 @@
# Copyright (c) 2015 Ultimaker B.V. # Copyright (c) 2019 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from UM.View.View import View from UM.View.View import View
@ -7,7 +7,6 @@ from UM.Scene.Selection import Selection
from UM.Resources import Resources from UM.Resources import Resources
from UM.Application import Application from UM.Application import Application
from UM.View.RenderBatch import RenderBatch from UM.View.RenderBatch import RenderBatch
from UM.Settings.Validator import ValidatorState
from UM.Math.Color import Color from UM.Math.Color import Color
from UM.View.GL.OpenGL import OpenGL from UM.View.GL.OpenGL import OpenGL
@ -20,9 +19,9 @@ import math
class SolidView(View): class SolidView(View):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
application = Application.getInstance()
Application.getInstance().getPreferences().addPreference("view/show_overhang", True) application.getPreferences().addPreference("view/show_overhang", True)
application.globalContainerStackChanged.connect(self._onGlobalContainerChanged)
self._enabled_shader = None self._enabled_shader = None
self._disabled_shader = None self._disabled_shader = None
self._non_printing_shader = None self._non_printing_shader = None
@ -30,6 +29,38 @@ class SolidView(View):
self._extruders_model = None self._extruders_model = None
self._theme = None self._theme = None
self._support_angle = 90
self._global_stack = None
Application.getInstance().engineCreatedSignal.connect(self._onGlobalContainerChanged)
def _onGlobalContainerChanged(self) -> None:
if self._global_stack:
try:
self._global_stack.propertyChanged.disconnect(self._onPropertyChanged)
except TypeError:
pass
for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
extruder_stack.propertyChanged.disconnect(self._onPropertyChanged)
self._global_stack = Application.getInstance().getGlobalContainerStack()
if self._global_stack:
self._global_stack.propertyChanged.connect(self._onPropertyChanged)
for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
extruder_stack.propertyChanged.connect(self._onPropertyChanged)
self._onPropertyChanged("support_angle", "value") # Force an re-evaluation
def _onPropertyChanged(self, key: str, property_name: str) -> None:
if key != "support_angle" or property_name != "value":
return
# As the rendering is called a *lot* we really, dont want to re-evaluate the property every time. So we store em!
global_container_stack = Application.getInstance().getGlobalContainerStack()
if global_container_stack:
support_extruder_nr = global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr")
support_angle_stack = Application.getInstance().getExtruderManager().getExtruderStack(support_extruder_nr)
if support_angle_stack:
self._support_angle = support_angle_stack.getProperty("support_angle", "value")
def beginRendering(self): def beginRendering(self):
scene = self.getController().getScene() scene = self.getController().getScene()
@ -63,14 +94,10 @@ class SolidView(View):
global_container_stack = Application.getInstance().getGlobalContainerStack() global_container_stack = Application.getInstance().getGlobalContainerStack()
if global_container_stack: if global_container_stack:
support_extruder_nr = global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr") if Application.getInstance().getPreferences().getValue("view/show_overhang"):
support_angle_stack = Application.getInstance().getExtruderManager().getExtruderStack(support_extruder_nr)
if support_angle_stack is not None and Application.getInstance().getPreferences().getValue("view/show_overhang"):
angle = support_angle_stack.getProperty("support_angle", "value")
# Make sure the overhang angle is valid before passing it to the shader # Make sure the overhang angle is valid before passing it to the shader
if angle is not None and angle >= 0 and angle <= 90: if self._support_angle is not None and self._support_angle >= 0 and self._support_angle <= 90:
self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - angle))) self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - self._support_angle)))
else: else:
self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0))) #Overhang angle of 0 causes no area at all to be marked as overhang. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0))) #Overhang angle of 0 causes no area at all to be marked as overhang.
else: else: