Last active
March 9, 2019 18:26
-
-
Save cortical-iv/838d37d559ccc3de198a742ee252f62a to your computer and use it in GitHub Desktop.
Attempt to setCombineRgb across cards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Want to combine (CMModulate) two texture stages from two cards, | |
but am just seeing the top card's texture stage. | |
""" | |
import numpy as np | |
from scipy import signal #for grating (square wave) | |
from direct.showbase.ShowBase import ShowBase | |
from panda3d.core import Texture, CardMaker, TextureStage | |
from panda3d.core import TransparencyAttrib | |
#Define texture (simple grating) | |
def grating_byte(X, freq = 1): | |
grating_float = signal.square(X*freq) | |
grating_transformed = (grating_float+1)*127.5; #from 0-255 | |
return np.uint8(grating_transformed) | |
def grating_texture(texture_size = 512, spatial_frequency = 10): | |
x = np.linspace(0, 2*np.pi, texture_size+1) | |
y = np.linspace(0, 2*np.pi, texture_size+1) | |
X, Y = np.meshgrid(x[:texture_size],y[:texture_size]) | |
return grating_byte(X, freq = spatial_frequency) | |
class CombineCards(ShowBase): | |
"""Trying to draw a mask card over a texture card and combine them (modulate)""" | |
def __init__(self, texture_array, texture_angle = 0, mask_angle = 0, | |
window_size = 512, texture_size = 512): | |
super().__init__() | |
self.texture_array = texture_array | |
self.left_angle = texture_angle | |
self.mask_angle = mask_angle | |
#CREATE MASK | |
self.right_mask = 255*np.ones((texture_size,texture_size), dtype=np.uint8) #should set to 0/1 not 255 | |
self.right_mask[:, texture_size//2: ] = 0 #texture_size//2:] = 0 | |
#CREATE TEXTURE STAGES | |
#Texture | |
self.grating_texture = Texture("Grating") #T_unsigned_byte | |
self.grating_texture.setup2dTexture(texture_size, texture_size, | |
Texture.T_unsigned_byte, Texture.F_luminance) | |
self.grating_texture.setRamImage(self.texture_array) | |
self.left_texture_stage = TextureStage('grating') | |
self.left_texture_stage.setSort(1) | |
#Mask | |
self.right_mask_texture = Texture("right_mask") | |
self.right_mask_texture.setup2dTexture(texture_size, texture_size, | |
Texture.T_unsigned_byte, Texture.F_luminance) | |
self.right_mask_texture.setRamImage(self.right_mask) | |
self.right_mask_stage = TextureStage('right_mask') | |
self.right_mask_stage.setSort(0) | |
#CREATE CARDS/SCENEGRAPH | |
cm = CardMaker('card') | |
cm.setFrameFullscreenQuad() | |
#left texture card | |
self.left_texture_card = self.aspect2d.attachNewNode(cm.generate()) | |
self.left_texture_card.setTexture(self.left_texture_stage, self.grating_texture) | |
self.left_texture_card.setScale(np.sqrt(8)) #so it can handle arbitrary rotations and shifts | |
self.left_texture_card.setR(self.left_angle) | |
self.left_texture_card.setTransparency(TransparencyAttrib.MAlpha) | |
#mask card | |
self.right_mask_card = self.aspect2d.attachNewNode(cm.generate()) | |
self.right_mask_card.setTexture(self.right_mask_stage, self.right_mask_texture) | |
self.right_mask_card.setScale(np.sqrt(8)) | |
self.right_mask_card.setR(self.mask_angle) | |
self.right_mask_card.setTransparency(TransparencyAttrib.MAlpha) | |
#trying to combine texture stages | |
self.right_mask_stage.setCombineRgb(TextureStage.CMModulate, | |
TextureStage.CSTexture, #source0 | |
TextureStage.COSrcColor, #operand0 | |
TextureStage.CSPrevious, #source1 | |
TextureStage.COSrcColor) #operand1 | |
#%% | |
if __name__ == '__main__': | |
mask_orientation = 0 | |
texture_orientation = 90 | |
stim_params = {'spatial_freq': 20, 'stim_angle': texture_orientation} | |
animal_angle = mask_orientation | |
texture_size = 512 | |
window_size = 512 | |
grating_texture = grating_texture(texture_size, stim_params['spatial_freq']) | |
binocular_drifting = CombineCards(grating_texture, | |
texture_angle = stim_params["stim_angle"], | |
mask_angle = mask_orientation, | |
window_size = window_size, | |
texture_size = texture_size) | |
binocular_drifting.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment