Skip to content

Instantly share code, notes, and snippets.

@cortical-iv
Last active July 13, 2019 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cortical-iv/41e11ec102c8ef7893709662762725c7 to your computer and use it in GitHub Desktop.
Save cortical-iv/41e11ec102c8ef7893709662762725c7 to your computer and use it in GitHub Desktop.
Trying to get single circle to shrink or dilate in middle of screen
import numpy as np
from direct.showbase.ShowBase import ShowBase
from panda3d.core import Texture, CardMaker, TextureStage
from direct.task import Task
def make_circle_tex(texture_size = 512, circle_center = (0, 0), circle_radius = 50):
x = np.linspace(-texture_size/2, texture_size/2, texture_size)
y = np.linspace(-texture_size/2, texture_size/2, texture_size)
X, Y = np.meshgrid(x, y)
circle_texture = np.zeros((texture_size, texture_size), dtype = np.uint8)
circle_mask = (X - circle_center[0])**2 + (Y - circle_center[1])**2 <= circle_radius**2
circle_texture[circle_mask] = 255
return np.uint8(circle_texture)
class TexScaler(ShowBase):
"""
Show a single full-field texture that scales up or down in time, starting after 1 second.
"""
def __init__(self, texture_array, scale = 0.2, window_size = 512, texture_size = 512):
super().__init__()
self.scale = scale
self.current_scale = 1
self.texture_array = texture_array
self.texture_dtype = type(self.texture_array.flat[0])
self.ndims = self.texture_array.ndim
#Create texture stage
self.texture = Texture("Stimulus")
self.texture.setup2dTexture(texture_size, texture_size,
Texture.T_unsigned_byte, Texture.F_luminance)
self.texture.setRamImageAs(self.texture_array, "L")
self.textureStage = TextureStage("Stimulus")
#Create scenegraph
cm = CardMaker('card1')
cm.setFrameFullscreenQuad()
self.card1 = self.aspect2d.attachNewNode(cm.generate())
self.card1.setTexture(self.textureStage, self.texture) #ts, tx
if self.scale != 0:
self.taskMgr.add(self.scaleTextureTask, "scaleTextureTask")
#Scale the texture
def scaleTextureTask(self, task):
if task.time > 1:
self.current_scale *= self.scale
self.card1.setTexScale(self.textureStage, self.current_scale, self.current_scale) #u_scale, v
return Task.cont
if __name__ == '__main__':
circle_texture = make_circle_tex()
#Try scale < 1: expect (single) circle to shrink
circle_scaling = TexScaler(circle_texture, scale = 0.99)
#Try scale > 1: expect circle to dilate
#circle_scaling = TexScaler(circle_texture, scale = 1.002)
circle_scaling.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment