def __init__(self): vispy.app.Canvas.__init__(self, keys='interactive', size=(800, 800)) self.images = [visuals.ImageVisual(image, method='impostor') for i in range(4)] self.images[0].transform = (STTransform(scale=(30, 30), translate=(600, 600)) * SineTransform() * STTransform(scale=(0.1, 0.1), translate=(-5, -5))) tr = AffineTransform() tr.rotate(30, (0, 0, 1)) tr.scale((3, 3)) self.images[1].transform = (STTransform(translate=(200, 600)) * tr * STTransform(translate=(-50, -50))) self.images[2].transform = (STTransform(scale=(3, -150), translate=(200, 100)) * LogTransform((0, 2, 0)) * STTransform(scale=(1, -0.01), translate=(-50, 1.3))) self.images[3].transform = (STTransform(scale=(400, 400), translate=(600, 300)) * PolarTransform() * STTransform(scale=(np.pi/200, 0.005), translate=(-3*np.pi/4., 0.1))) for img in self.images: img.tr_sys = TransformSystem(self) img.tr_sys.visual_to_document = img.transform self.show()
def __init__(self, texture, pos): self.vshader = Function(""" void line_of_sight() { vec4 polar_pos = $transform(vec4($pos, 1)); if( polar_pos.x > 0.999 ) { polar_pos.x = 0.001; } vec4 c = texture2D($texture, vec2(polar_pos.x, 0.5)); float depth = c.r; if( polar_pos.y > depth+0.5 ) { $mask = vec3(0.5, 0.5, 1); // out-of-sight objects turn blue } else { $mask = vec3(1, 1, 1); } } """) self.fshader = Function(""" void apply_texture_mask() { gl_FragColor *= vec4($mask,1); } """) self.center = STTransform() self.transform = STTransform( scale=(0.5 / np.pi, 1, 0), translate=(0.5, 0, 0)) * PolarTransform().inverse * self.center self.vshader['pos'] = pos self.vshader['transform'] = self.transform self.vshader['texture'] = texture self.vshader['mask'] = Varying('mask', dtype='vec3') self.fshader['mask'] = self.vshader['mask']
def __init__(self): vispy.app.Canvas.__init__(self, keys='interactive', size=(800, 800)) # Create 4 copies of an image to be displayed with different transforms image = get_image() self.images = [visuals.ImageVisual(image, method='impostor') for i in range(4)] # Transform all images to a standard size / location (because # get_image() might return unexpected sizes) s = 100. / max(self.images[0].size) tx = 0.5 * (100 - (self.images[0].size[0] * s)) ty = 0.5 * (100 - (self.images[0].size[1] * s)) base_tr = STTransform(scale=(s, s), translate=(tx, ty)) self.images[0].transform = (STTransform(scale=(30, 30), translate=(600, 600)) * SineTransform() * STTransform(scale=(0.1, 0.1), translate=(-5, -5)) * base_tr) tr = MatrixTransform() tr.rotate(40, (0, 0, 1)) tr.rotate(30, (1, 0, 0)) tr.translate((0, -20, -60)) p = MatrixTransform() p.set_perspective(0.5, 1, 0.1, 1000) tr = p * tr tr1 = (STTransform(translate=(200, 600)) * tr * STTransform(translate=(-50, -50)) * base_tr) self.images[1].transform = tr1 tr2 = (STTransform(scale=(3, -100), translate=(200, 50)) * LogTransform((0, 2, 0)) * STTransform(scale=(1, -0.01), translate=(-50, 1.1)) * base_tr) self.images[2].transform = tr2 tr3 = (STTransform(scale=(400, 400), translate=(570, 400)) * PolarTransform() * STTransform(scale=(np.pi/150, -0.005), translate=(-3.3*np.pi/4., 0.7)) * base_tr) self.images[3].transform = tr3 text = visuals.TextVisual( text=['logarithmic', 'polar', 'perspective', 'custom (sine)'], pos=[(100, 20), (500, 20), (100, 410), (500, 410)], color='k', font_size=16) self.visuals = self.images + [text] self.show()
def __init__(self, scene, los_tex, size, supersample=4): vert = """ #version 120 attribute vec2 pos; varying vec2 v_pos; void main(void) { gl_Position = vec4(pos, 0, 1); v_pos = $transform(gl_Position).xy; } """ frag = """ #version 120 varying vec2 v_pos; uniform sampler2D los_tex; void main(void) { vec2 polar_pos = $transform(vec4(v_pos, 0, 1)).xy; float los_depth = texture2D(los_tex, vec2(polar_pos.x, 0.5)).r; float diff = (los_depth+1 - polar_pos.y); gl_FragColor = vec4(diff, diff, diff, 1); } """ self.scene = scene self.size = (size[0] * supersample, size[1] * supersample) self.vertices = np.array( [[-1, -1], [1, -1], [-1, 1], [-1, 1], [1, -1], [1, 1]], dtype='float32') self.program = ModularProgram(vert, frag) self.program['pos'] = self.vertices self.program['los_tex'] = los_tex self.program.vert['transform'] = STTransform( scale=(size[1] / 2., size[0] / 2.)) * STTransform(translate=(1, 1)) self.center = STTransform() self.program.frag['transform'] = STTransform( scale=(0.5 / np.pi, 1, 1), translate=(0.5, 0, 0)) * PolarTransform().inverse * self.center self.tex = vispy.gloo.Texture2D(shape=self.size + (4, ), format='rgba', interpolation='linear') self.fbo = vispy.gloo.FrameBuffer(color=self.tex, depth=vispy.gloo.RenderBuffer( self.size))
def __init__(self): # Define several Line visuals that use the same position data # but have different colors and transformations colors = [color, (1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 1)] self.lines = [visuals.LineVisual(pos=pos, color=colors[i]) for i in range(6)] center = STTransform(translate=(400, 400)) self.lines[0].transform = center self.lines[1].transform = (center * STTransform(scale=(1, 0.1, 1))) self.lines[2].transform = (center * STTransform(translate=(200, 200, 0)) * STTransform(scale=(0.3, 0.5, 1))) self.lines[3].transform = (center * STTransform(translate=(-200, -200, 0), scale=(200, 1)) * LogTransform(base=(10, 0, 0)) * STTransform(translate=(1, 0, 0))) self.lines[4].transform = AffineTransform() self.lines[4].transform.rotate(45, (0, 0, 1)) self.lines[4].transform.scale((0.3, 0.3, 1)) self.lines[4].transform.translate((200, 200, 0)) self.lines[5].transform = (STTransform(translate=(200, 600, 0), scale=(5, 5)) * PolarTransform() * LogTransform(base=(2, 0, 0)) * STTransform(scale=(0.01, 0.1), translate=(4, 20))) app.Canvas.__init__(self, keys='interactive') self.size = (800, 800) self.show() for line in self.lines: tr_sys = visuals.transforms.TransformSystem(self) tr_sys.visual_to_document = line.transform line.tr_sys = tr_sys
def __init__(self, scene, opacity, size=(100, 1000)): self.scene = scene self.size = size self.tex = vispy.gloo.Texture2D(shape=size + (4, ), format='rgba', interpolation='linear', wrapping='repeat') self.fbo = vispy.gloo.FrameBuffer(color=self.tex, depth=vispy.gloo.RenderBuffer(size)) vert = """ #version 120 attribute vec3 position; uniform float scale; uniform float underfill; varying vec3 depth; uniform sampler2D opacity; uniform vec2 opacity_size; void main (void) { vec3 cpos = position; float alpha = texture2D(opacity, (cpos.xy+vec2(0.5, 0.5)) / opacity_size).r; if( alpha > 0 ) { vec4 center = $transform(vec4(cpos, 1)); // Determine the min/max azimuthal angle occupied by this wall float min_theta = center.x; float max_theta = center.x; // Check for connection with adjacent walls, extend for( int i=0; i<2; i++ ) { for( int j=-1; j<2; j+=2 ) { vec3 dx = vec3(0, 0, 0); dx[i] = j; vec3 pos2 = cpos + dx; float alpha2 = texture2D(opacity, (pos2.xy+vec2(0.5, 0.5)) / opacity_size).r; if( alpha2 > 0 ) { dx[i] = j/1.95; // 1.95 gives a small amount of overlap to prevent gaps } else { dx[i] = j/4.; // unconnected walls still have some width } vec4 polar_pos = $transform(vec4(cpos + dx, 1)); if( polar_pos.x - center.x > 1 ) { // point wraps around between -pi and +pi polar_pos.x -= 2; } else if( center.x - polar_pos.x > 1 ) { polar_pos.x += 2; } min_theta = min(min_theta, polar_pos.x); max_theta = max(max_theta, polar_pos.x); } } if( min_theta < -1 ) { min_theta += 2; max_theta += 2; center.x += 2; } float theta = (min_theta + max_theta) / 2.0; theta = (theta + 1) * underfill - 1; // compress theta range to avoid overflowing the right edge gl_Position = vec4(theta, 0, center.y/1000., 1); gl_PointSize = scale * underfill * abs(max_theta - min_theta); // encode depth as rgb float r = int(center.y / 256.) / 255.; float g = int(center.y - (r*256)) / 255.; float b = center.y - int(center.y); depth = vec3(r, g, b); } else { // Not a wall gl_Position = vec4(-2, -2, -2, 1); gl_PointSize = 0; } } """ frag = """ #version 120 varying vec3 depth; void main (void) { gl_FragColor = vec4(depth, 1); } """ self.program = ModularProgram(vert, frag) self.underfill = 0.9 # Need to underfill the x axis because some points straddle the border between -pi and +pi self.program['underfill'] = self.underfill self.center = STTransform() self.transform = STTransform( scale=(1. / np.pi, 1, 1)) * PolarTransform().inverse * self.center self.program.vert['transform'] = self.transform self.program['scale'] = self.size[1] / 2.0 self.program['wrap'] = self.underfill self.program['opacity'] = opacity self.program['opacity_size'] = opacity.shape[:2][::-1]