def apply_image_transform(self, x=0, y=0, scale=1, post_multiply=False, anchor=(0, 0)): # Test if the x move is valid t = Matrix() t = t.translate(x, 0, 0) if not self.test_valid(t.multiply(self.transform)): x = 0 # Test if the y move is valid t = Matrix() t = t.translate(0, y, 0) if not self.test_valid(t.multiply(self.transform)): y = 0 # Test if the scale is valid t = Matrix().translate(anchor[0], anchor[1], 0) t = t.scale(scale, scale, scale) t = t.multiply(Matrix().translate(-anchor[0], -anchor[1], 0)) if not self.test_valid(t.multiply(self.transform)): scale = 1 # Compile final matrix t = Matrix().translate(anchor[0], anchor[1], 0) t = t.translate(x, y, 0) t = t.scale(scale, scale, scale) t = t.multiply(Matrix().translate(-anchor[0], -anchor[1], 0)) if post_multiply: self.transform = self.transform.multiply(t) else: self.transform = t.multiply(self.transform)
def __init__(self, fs, length1, length2 = 1): size = (length1, length2) # it doesn't look like we can use float textures on mobile kivy, but people sometimes interconvert floats with 32bit rgba in shaders. # we would then have 3 shaders or texture rows or such, for x coord, y coord, angle, etc #Logger.info('float: ' + str(gl.getExtension('OES_texture_float'))) texture = Texture.create( size = size, #bufferfmt = 'float' ) self._fbo = Fbo( size = size, texture = texture, vs = default_vs, fs = header_fs + fs, ) # these matrices are to transform # window coordinates into data # coordinates centermat = Matrix() centermat.translate(-.5,-.5,-.5) idxscale = 1.0 / 255.0; idxmat = Matrix() idxmat.scale(idxscale, idxscale, idxscale) self._fbo['frag_coord2idx'] = idxmat.multiply(centermat) ratiomat = Matrix() ratiomat.scale(1.0 / length1, 1.0 / length2, 1.0) self._fbo['frag_coord2ratio'] = ratiomat self._texture_bindings = {} self._fbo.add_reload_observer(self._populate_fbo) self._populate_fbo(self._fbo)
def updateModelViewMatrix(self): m = Matrix().identity() hr = max(0.00001, (self.xmax - self.xmin)) vr = max(0.00001, (self.ymax - self.ymin)) m.scale(1.0 / hr, 1.0 / vr, 1.0) m.translate(-self.xmin / hr, -self.ymin / vr, 0.0) self.render_context['modelview_mat'] = m
def get_projection(self) -> Matrix: width = self.viewport_width / 2 height = self.viewport_height / 2 x = -self.pos_x y = -self.pos_y projection = Matrix() projection.view_clip(0, self.viewport_width, 0, self.viewport_height, -1.0, 1.0, 0) projection.scale(self.zoom, self.zoom, self.zoom) projection.translate(1 + x / width * self.zoom, 1 + y / height * self.zoom, 0) return projection
def get_window_matrix(self, x=0, y=0): '''Calculate the transformation matrix to convert between window and widget coordinates. :Parameters: `x`: float, defaults to 0 Translates the matrix on the x axis. `y`: float, defaults to 0 Translates the matrix on the y axis. ''' m = Matrix() m.translate(x, y, 0) m = self._apply_transform(m) return m
def get_window_matrix(self, x=0, y=0): '''Calculate the transformation matrix to convert between window and widget coordinates. :Parameters: `x`: float, defaults to 0 Translates the matrix on the x axis. `y`: float, defaults to 0 Translates the matrix on the y axis. ''' m = Matrix() m.translate(x, y, 0) m = self._apply_transform(m) return m
def updateProjectionMatrices(self): """ makes 0,0 the lower left corner and 1,1 the upper right """ w = float(Window.width) h = float(Window.height) m = Matrix().identity() p = rvit.core.BUTTON_BORDER_HEIGHT # self.projection_r = min(self.size[0],self.size[1]-p) # m.scale(self.projection_r/w*2, # self.projection_r/h*2,1.0) m.scale(2.0 * self.width / w, 2.0 * (self.height - p) / h, 1.0) m.translate(-1.0 + (self.pos[0]) * 2.0 / w, -1.0 + (self.pos[1]) * 2.0 / h, 0.0) self.render_context['projection_mat'] = m
def look_at(self, v): m = Matrix() pos = self._position * -1 m = m.translate(pos[0], pos[1], pos[2]) self.modelview_matrix = m self._look_at = v self.update()
def update_fbo(self, time): width = self.width if self.width > 1 else 100 height = self.height if self.height > 1 else 100 asp = (width / float(height)) proj = Matrix().view_clip(-asp, asp, -1, 1, 1, 1600, 1) proj = Matrix() proj.perspective(self.perspective_value, asp, 1, 1000) lightInvDir = (0.5, 2, 2) depthProjectionMatrix = Matrix().view_clip( -100 * self.shadow_threshold, 100 * self.shadow_threshold, -100 * self.shadow_threshold, 100 * self.shadow_threshold, -100 * self.shadow_threshold, 200 * self.shadow_threshold * 2, 0) _shadow_pos = self._shadow_pos _shadow_target = self._shadow_target depthViewMatrix = Matrix().look_at( _shadow_target[0], _shadow_target[1], _shadow_target[2] + self._shadow_offset, _shadow_pos[0], _shadow_pos[1], _shadow_pos[2], 0, 1, 0) depthModelMatrix = Matrix().identity() depthMVP = depthProjectionMatrix.multiply(depthViewMatrix).multiply( depthModelMatrix) self.fbo['projection_mat'] = proj self.fbo['depthMVP'] = depthMVP self.fbo['diffuse_light'] = (0.0, 1.0, 0.0) self.fbo['ambient_light'] = self.ambient_light for m_pos in range(len(self.nodes)): motion_matrix = Matrix().view_clip(-asp, asp, -1, 1, 1, 600, 1) angle = self.nodes[m_pos].rotate[0] * 3.14 / 180 pos = self.nodes[m_pos].get_pos() trans = self.nodes[m_pos].translate[:] result = [0, 0, 0] result[0] = 0.3 if trans[0] < pos[0] else -0.3 result[1] = 0.3 if trans[1] < pos[1] else -0.3 result[2] = 0.3 if trans[2] < pos[2] else -0.3 motion_matrix = motion_matrix.translate(trans[0] + 0.1, trans[1] + 0.1, trans[2]) self.motion_blur_fbo['oldTransformation{0}'.format( str(m_pos))] = motion_matrix self.motion_blur_fbo['projection_mat'] = proj self.motion_blur_fbo['depthMVP'] = depthMVP matrix_camera = Matrix().identity() matrix_camera = matrix_camera.look_at(*self.look_at) if self.picking_fbo: self.picking_fbo['projection_mat'] = proj self.picking_fbo['camera'] = matrix_camera self.alpha += 10 * time self.fbo['cond'] = (0.0, 0.7) self.fbo['val_sin'] = (self.alpha, 0.0)
def get_projection(self) -> Tuple[Matrix, Matrix]: viewport_w = self.viewport_width viewport_h = self.viewport_height width = viewport_w / 2 height = viewport_h / 2 x = -self.pos_x y = -self.pos_y projection = Matrix() projection.view_clip(0, viewport_w, 0, viewport_h, -1.0, 1.0, 0) projection.scale(self.zoom, self.zoom, self.zoom) projection.translate(1 + x / width * self.zoom, 1 + y / height * self.zoom, 0) model_view = Matrix() return projection, model_view
def __init__(self, *args, **kwargs): super().__init__(**kwargs) with self.canvas.before: PushMatrix() mi = MatrixInstruction() matrix = Matrix() matrix.translate(200, 200, 0) matrix.scale(1.4, 0.75, 1) print(matrix[0]) matrix[0] = 2 print(matrix[0]) mi.matrix = matrix with self.canvas: Rectangle(size=(100, 100)) with self.canvas.after: PopMatrix()
def update_glsl(self, *largs): global no_width_error_enable if self.player_velocity[0] != 0: self.camera_translate_instruction.x += self.player_velocity[0] if self.player_velocity[1] != 0: self.camera_translate_instruction.y += self.player_velocity[1] if self.player_velocity[2] != 0: self.camera_translate_instruction.z += self.player_velocity[2] if self.height > 0: asp = self.width / float(self.height) else: if no_width_error_enable: print("[ TestingKivy3D ] ERROR in update_glsl: Failed to get width.") no_width_error_enable = False clip_top = 0.06 # NOTE: 0.03 is ~1.72 degrees, if that matters # formerly field_of_view_factor # was changed to .03 when projection_near was changed from 1 to .1 # was .3 when projection_near was 1 clip_right = asp*clip_top # formerly overwrote asp self.projection_near = 0.1 projectionMatrix = Matrix().view_clip(-clip_right, clip_right, -1*clip_top, clip_top, self.projection_near, 100, 1) # last params: far, perspective #projectionMatrix = Matrix().view_clip(-asp, asp, -1, 1, 1, 100, 1) # last params: far, perspective modelViewMatrix = Matrix() modelViewMatrix.translate(self.camera_translate_instruction.x, self.camera_translate_instruction.y, self.camera_translate_instruction.z) if (self.camera_translate_instruction.x != self.look_point[0] or self.camera_translate_instruction.y != self.look_point[1] or self.camera_translate_instruction.z != self.look_point[2]): try: modelViewMatrix = modelViewMatrix.look_at(self.camera_translate_instruction.x, self.camera_translate_instruction.y, self.camera_translate_instruction.z, self.look_point[0], self.look_point[1], self.look_point[2], 0, 1, 0) # 0,1,0 is y-up orientation except: print("[ TestingKivy3D ] Could not finish modelViewMatrix.look_at:") else: print("[ TestingKivy3D ] Can't run modelViewMatrix.look_at since camera is at look_point") self.gl_widget.canvas['projection_mat'] = projectionMatrix self.gl_widget.canvas['modelview_mat'] = modelViewMatrix self.gl_widget.canvas["camera_world_pos"] = [self.camera_translate_instruction.x, self.camera_translate_instruction.y, self.camera_translate_instruction.z] self.gl_widget.canvas['ambient_light'] = (0.1, 0.1, 0.1)
def update_fbo(self, time): width = self.width if self.width > 1 else 100 height = self.height if self.height > 1 else 100 asp = (width / float(height)) proj = Matrix().view_clip(-asp, asp, -1, 1, 1, 600, 1) proj = Matrix() proj.perspective(self.perspective_value, asp, 1, 1000) lightInvDir = (0.5, 2, 2) depthProjectionMatrix = Matrix().view_clip(-100 * self.shadow_threshold, 100 * self.shadow_threshold, -100 * self.shadow_threshold, 100 * self.shadow_threshold, -100 * self.shadow_threshold, 200 * self.shadow_threshold * 2, 0) depthViewMatrix = Matrix().look_at(-0.5, -50, -100, 0, 0, 0, 0, 1, 0) depthModelMatrix = Matrix().identity() depthMVP = depthProjectionMatrix.multiply(depthViewMatrix).multiply(depthModelMatrix) self.fbo['projection_mat'] = proj self.fbo['depthMVP'] = depthMVP self.fbo['diffuse_light'] = (0.0, 1.0, 0.0) self.fbo['ambient_light'] = (0.1, 0.1, 0.1) for m_pos in range(len(self.nodes)): motion_matrix = Matrix().view_clip(-asp, asp, -1, 1, 1, 600, 1) angle = self.nodes[m_pos].rotate[0] * 3.14 / 180 pos = self.nodes[m_pos].get_pos() trans = self.nodes[m_pos].translate[:] result = [0, 0, 0] result[0] = 0.3 if trans[0] < pos[0] else -0.3 result[1] = 0.3 if trans[1] < pos[1] else -0.3 result[2] = 0.3 if trans[2] < pos[2] else -0.3 motion_matrix = motion_matrix.translate(trans[0] + 0.1, trans[1] + 0.1, trans[2]) self.motion_blur_fbo['oldTransformation{0}'.format(str(m_pos))] = motion_matrix self.motion_blur_fbo['projection_mat'] = proj self.motion_blur_fbo['depthMVP'] = depthMVP if self.picking_fbo: self.picking_fbo['projection_mat'] = proj self.alpha += 10 * time self.fbo['cond'] = (0.0, 0.7) self.fbo['val_sin'] = (self.alpha, 0.0)
def get_window_matrix(self, x=0, y=0): m = Matrix() m.translate(x, y, 0) return m
def get_window_matrix(self, x=0, y=0): m = Matrix() m.translate(x, y, 0) return m
def on_scale(self, instance, scale): matrix = Matrix() matrix.scale(scale, scale, 1) matrix.translate((-scale + 1) * 0.5 * self.width, -scale * self.height / 4, 0) self.instruction.matrix = matrix