def _getModifierMVP(self): modelTransform = self._getCurrentMainModelNode().getModelTransform() modifierSize = 0.05 if self._modifierMode == ModifierMode.ROTATE else 0.10 if self._modifierMode == ModifierMode.TRANSLATE: # Translation is in world space, simply emit world space axes nonScaledModelTransform = cgmath.Mat44.translate(modelTransform[12], modelTransform[13], modelTransform[14]) else: # Orthonormalize modelTransform (remove scale) modelRight = cgmath.Vec3(modelTransform[0], modelTransform[1], modelTransform[2]) modelUp = cgmath.Vec3(modelTransform[4], modelTransform[5], modelTransform[6]) modelForward = cgmath.Vec3(modelTransform[8], modelTransform[9], modelTransform[10]) modelRight.normalize() modelUp.normalize() modelForward.normalize() nonScaledModelTransform = cgmath.Mat44(modelRight[0], modelRight[1], modelRight[2], 0, modelUp[0], modelUp[1], modelUp[2], 0, modelForward[0], modelForward[1], modelForward[2], 0, modelTransform[12], modelTransform[13], modelTransform[14], 1) mv = nonScaledModelTransform * self._viewTransform mvp = cgmath.Mat44.scale(modifierSize * mv[14], modifierSize * mv[14], modifierSize * mv[14]) * mv * self._projection return mvp
def __init__(self): self._name = self.__class__.__name__[9:] self._translation = cgmath.Vec3(0.0, 0.0, 0.0) self._rotation = cgmath.Vec3(0.0, 0.0, 0.0) self._scale = 1.0 self._model = None self._subtractive = False
def __init__(self): super(Models, self).__init__() self._models = [] # Always start with a box m = self.addModel() b = m.addBox() b.size = cgmath.Vec3(0.2, 0.1, 0.1) b.translation = cgmath.Vec3(0.0, 0.5, 0.0) m.addBox()
def __init__(self, models): super(ModelerViewport, self).__init__() self.setLayout(vlayout()) self._updateMinMouseClickDist() self._primitives = Primitives() self._models = models self._currentModel = None self._currentModelNodes = set() self._cameraTransform = cgmath.Mat44.translate(0, 0, -2) self._modelTransform = cgmath.Mat44() self._viewTransform = cgmath.Mat44() self._cameraPivot = cgmath.Vec3(0.0,0.0,0.0) self._adjustingCamera = False self._adjustCameraMode = 0 self._selecting = False self._modifierMode = ModifierMode.SELECT self._modifierAxis = ModifierAxis.NONE self.setFocusPolicy(Qt.StrongFocus) self.installEventFilter(self)
def loadState(self): # save user camera position per scene userFile = currentProjectFilePath().ensureExt('user') if not userFile.exists(): return xUser = parseXMLWithIncludes(userFile) if xUser is None: return xMod = xUser.find('Modeler') if xMod is None: return if 'CurrentModel' in xMod.attrib: currentModelIndex = int(xMod.attrib['CurrentModel']) else: currentModelIndex = -1 if currentModelIndex >= 0 and currentModelIndex < len(self._models.models): self.selectedModelNodesChanged.emit(self._models.models[currentModelIndex], None) else: self.selectedModelNodesChanged.emit(None, None) ct = list(map(float, xMod.attrib['CameraTransform'].split(','))) self._cameraTransform = cgmath.Mat44(ct[0],ct[1],ct[2],ct[3],ct[4],ct[5],ct[6],ct[7],ct[8],ct[9],ct[10],ct[11],ct[12],ct[13],ct[14],ct[15]) ct = list(map(float, xMod.attrib['CameraPivot'].split(','))) self._cameraPivot = cgmath.Vec3(ct[0], ct[1], ct[2]) self.update()
def add(self, other): if isinstance(other, Bounds): self.add(other.min) self.add(other.max) return if self._min is None: self._min = other self._max = other return self._min = cgmath.Vec3(min(self._min[0], other[0]), min(self._min[1], other[1]), min(self._min[2], other[2])) self._max = cgmath.Vec3(max(self._max[0], other[0]), max(self._max[1], other[1]), max(self._max[2], other[2]))
def getBounds(self): modelTransform = self.getModelTransform() bounds = Bounds() for z in range(-1, 2, 2): for y in range(-1, 2, 2): for x in range(-1, 2, 2): p = cgmath.Vec4(float(x), float(y), float(z), 1.0) p = modelTransform * p bounds.add(cgmath.Vec3(p[0], p[1], p[2])) return bounds
def centerView(self): centerPos = cgmath.Vec3(0.0, 0.0, 0.0) radius = 1.0 # Do we have any current nodes? If so, center view on them if len(self._currentModelNodes) > 0: bounds = None for node in self._currentModelNodes: if bounds is None: bounds = node.getBounds() else: bounds.add(node.getBounds()) centerPos = bounds.center radius = (bounds.max - centerPos).length # Otherwise, is there a current model? If so, center view on it elif self._currentModel != None: bounds = self._currentModel.getBounds() centerPos = bounds.center radius = (bounds.max - centerPos).length translation = centerPos - self._cameraPivot self._cameraPivot = self._cameraPivot + translation # Move camera as well self._cameraTransform = self._cameraTransform * cgmath.Mat44.translate(translation[0], translation[1], translation[2]) # Pull back camera currentDist = (cgmath.Vec3(self._cameraTransform[12], self._cameraTransform[13], self._cameraTransform[14]) - self._cameraPivot).length fovH = math.radians(30) fovW = (self.width()/float(self.height()))*fovH desiredDist = 2.0*max(radius/math.tan(fovH), radius/math.tan(fovW)) self._cameraTransform = cgmath.Mat44.translate(0.0, 0.0, currentDist-desiredDist) * self._cameraTransform self.cameraChanged.emit(self._cameraTransform) self.update()
def getFieldFragmentShaderText(self): bounds = self.getBounds() boundsRange = bounds.max - bounds.min # Add 5% border to bounds bounds.add(bounds.min - boundsRange * 0.05) bounds.add(bounds.max + boundsRange * 0.05) boundsRange = bounds.max - bounds.min # Make bounds uniform maxBoundRange = max(max(boundsRange[0], boundsRange[1]), boundsRange[2]) boundsIncrease = cgmath.Vec3(maxBoundRange - boundsRange[0], maxBoundRange - boundsRange[1], maxBoundRange - boundsRange[2]) bounds.add(bounds.min - boundsIncrease * 0.5) bounds.add(bounds.max + boundsIncrease * 0.5) boundsRange = bounds.max - bounds.min # str = "uniform float uSlice;\n"+\ # "\n"+\ # "void main()\n"+\ # "{\n"+\ # "\tvec3 p = vec3(gl_FragCoord.x,gl_FragCoord.y,uSlice)/uResolution.x;\n"+\ # ("\tp = p * vec3(%f,%f,%f) + vec3(%f,%f,%f);\n" % (boundsRange[0],boundsRange[1],boundsRange[2],bounds.min[0],bounds.min[1],bounds.min[2])) +\ # "\tvec4 p4 = vec4(p, 1.0);\n"+\ # "\n"+\ # "\tfloat d = 99999.0;\n" funcName = self.name.replace(' ', '') str = ("float f%s(vec3 p)\n" % funcName)+\ "{\n"+\ "\tvec4 p4 = vec4(p, 1.0);\n"+\ "\n"+\ "\tfloat d = 99999.0;\n" for node in self.nodes: str += node.getFieldFragmentShaderText() str += "\treturn d;\n"+\ "}\n" return str
def translation(self): return cgmath.Vec3(self._translation)
def size(self, s): self._size = cgmath.Vec3(s) self._model._models.modelChanged.emit(self._model)
def __init__(self): super(ModelNodeBox, self).__init__() self._size = cgmath.Vec3(1.0, 1.0, 1.0)
def rotation(self, r): self._rotation = cgmath.Vec3(r) self._model._models.modelChanged.emit(self._model)
def mouseMoveEvent(self, mouseEvent): super(ModelerViewport, self).mouseMoveEvent(mouseEvent) # Panning/Rotating/Zooming? if self._adjustingCamera: # Panning? if self._adjustCameraMode == 0: panSpeed = 0.025 deltaMouse = mathutil.Vec2(mouseEvent.localPos().x(), mouseEvent.localPos().y()) - self._adjustCameraStartMousePos self._cameraTransform = cgmath.Mat44.translate(deltaMouse[0] * -panSpeed, deltaMouse[1] * panSpeed, 0.0) * self._adjustCameraStartCamera self._cameraPivot = self._adjustCameraPivot + cgmath.Vec3(self._cameraTransform[12] - self._adjustCameraStartCamera[12], self._cameraTransform[13] - self._adjustCameraStartCamera[13], self._cameraTransform[14] - self._adjustCameraStartCamera[14]) # Rotating? elif self._adjustCameraMode == 1: rotateSpeed = 0.010 deltaMouse = mathutil.Vec2(mouseEvent.localPos().x(), mouseEvent.localPos().y()) - self._adjustCameraStartMousePos # Remove pivot self._cameraTransform = cgmath.Mat44(self._adjustCameraStartCamera) * cgmath.Mat44.translate(-self._adjustCameraPivot[0], -self._adjustCameraPivot[1], -self._adjustCameraPivot[2]) # Rotate self._cameraTransform = self._cameraTransform * cgmath.Mat44.rotateY(deltaMouse[0] * rotateSpeed) self._cameraTransform = self._cameraTransform * self.axisAngle(cgmath.Vec3(1.0, 0.0, 0.0) * self._cameraTransform, deltaMouse[1] * -rotateSpeed) # Add pivot back self._cameraTransform = cgmath.Mat44(self._cameraTransform) * cgmath.Mat44.translate(self._adjustCameraPivot[0], self._adjustCameraPivot[1], self._adjustCameraPivot[2]) # Zooming? elif self._adjustCameraMode == 2: zoomSpeed = 0.025 deltaMouse = mathutil.Vec2(mouseEvent.localPos().x(), mouseEvent.localPos().y()) - self._adjustCameraStartMousePos self._cameraTransform = cgmath.Mat44.translate(0.0, 0.0, deltaMouse[1] * zoomSpeed) * self._adjustCameraStartCamera self.cameraChanged.emit(self._cameraTransform) # Dragging a translation modifier axis? elif self._modifierMode == ModifierMode.TRANSLATE and self._modifierAxis != ModifierAxis.NONE: deltaMouse = self._convertMousePosToScreenPos(mouseEvent.localPos().x(), mouseEvent.localPos().y()) - self._modifyStartMouseScreenPos if self._modifierAxis == ModifierAxis.X: axisDir = cgmath.Vec3(1.0,0.0,0.0) elif self._modifierAxis == ModifierAxis.Y: axisDir = cgmath.Vec3(0.0,1.0,0.0) else: axisDir = cgmath.Vec3(0.0,0.0,1.0) screenDir = self._getModifierAxisScreenDir(axisDir) delta = screenDir[0]*deltaMouse[0] + screenDir[1]*deltaMouse[1] for node in self._currentModelNodes: node.translation = axisDir * delta + self._modifyStartModelTranslation[node] # Dragging a rotation modifier axis? elif self._modifierMode == ModifierMode.ROTATE and self._modifierAxis != ModifierAxis.NONE: deltaMouse = self._convertMousePosToScreenPos(mouseEvent.localPos().x(), mouseEvent.localPos().y()) - self._modifyStartMouseScreenPos amount = deltaMouse[0] if self._modifierAxis == ModifierAxis.X: rot = cgmath.Mat44.rotateX(amount) elif self._modifierAxis == ModifierAxis.Y: rot = cgmath.Mat44.rotateY(amount) else: rot = cgmath.Mat44.rotateZ(amount) for node in self._currentModelNodes: node.rotation = (rot * self._modifyStartModelRotationMatrix[node]).eulerXYZ() # Dragging a scale modifier axis? elif self._modifierMode == ModifierMode.SCALE_NONUNIFORM and self._modifierAxis != ModifierAxis.NONE: deltaMouse = self._convertMousePosToScreenPos(mouseEvent.localPos().x(), mouseEvent.localPos().y()) - self._modifyStartMouseScreenPos if self._modifierAxis == ModifierAxis.X: axisDir = cgmath.Vec3(1.0,0.0,0.0) elif self._modifierAxis == ModifierAxis.Y: axisDir = cgmath.Vec3(0.0,1.0,0.0) else: axisDir = cgmath.Vec3(0.0,0.0,1.0) screenDir = self._getModifierAxisScreenDir(axisDir) delta = screenDir[0]*deltaMouse[0] + screenDir[1]*deltaMouse[1] for node in self._currentModelNodes: node.size = axisDir * delta * 0.5 + self._modifyStartModelSize[node] # Selecting nodes? elif self._selecting: self._selectCurrentMouseScreenPos = self._convertMousePosToScreenPos(mouseEvent.localPos().x(), mouseEvent.localPos().y()) self.update()
def translation(self, tr): self._translation = cgmath.Vec3(tr) self._model._models.modelChanged.emit(self._model)
def size(self): return cgmath.Vec3(self._size)
def rotation(self): return cgmath.Vec3(self._rotation)
def xmlAttribToVec3(attrib): vals = list(map(float, attrib.split(','))) return cgmath.Vec3(vals[0], vals[1], vals[2])