Last active
February 10, 2019 14:06
-
-
Save cortical-iv/e661dc12acb242ba430aaf4c05a38b51 to your computer and use it in GitHub Desktop.
Want both sinusoids to be full brightness, not half masked
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
from direct.showbase.ShowBase import ShowBase | |
from panda3d.core import Texture, CardMaker, TextureStage, TransparencyAttrib | |
import numpy as np | |
from direct.gui.OnscreenText import OnscreenText | |
from direct.task import Task | |
#Create sin | |
texSize = 512 | |
frequency = 8 | |
def sin3d(X, freq = 1): | |
return np.sin(X*freq) | |
def sin8bit(X, freq = 1): | |
sin_float = sin3d(X, freq=freq) | |
sin_pos = (sin_float+1)*127.5; #from 0-255 | |
return np.asarray(sin_pos, dtype = np.uint8) | |
x = np.linspace(0, 2*np.pi, texSize) #could try texSize+1 and then get meshgrid with x[0:-1] not perfect | |
y = np.linspace(0, 2*np.pi, texSize) | |
X, Y = np.meshgrid(x,y) | |
sinTex = sin8bit(X, freq = frequency); | |
#Create masks | |
leftMask = 255*np.ones((texSize,texSize), dtype=np.uint8) #127.5 | |
leftMask[:, :texSize//2 + bandRadius] = 0 | |
rightMask = 255*np.ones((texSize,texSize), dtype=np.uint8) #127.5 | |
rightMask[:, texSize//2 - bandRadius:] = 0 #Check index on this | |
#Properties of stimulus | |
bandRadius = 10 | |
rotation = 30 | |
class MyApp(ShowBase): | |
def __init__(self): | |
ShowBase.__init__(self) | |
#CREATE TEXTURE STAGES | |
#Sine | |
self.sinTexture = Texture("sin") | |
self.sinTexture.setup2dTexture(texSize, texSize, Texture.T_unsigned_byte, Texture.F_luminance) | |
self.sinTexture.setRamImage(sinTex) | |
self.sinTextureStage = TextureStage('sin') | |
#Mask left (with card 1) | |
self.leftMaskTex = Texture("left_mask") | |
self.leftMaskTex.setup2dTexture(texSize, texSize, Texture.T_unsigned_byte, Texture.F_luminance) | |
self.leftMaskTex.setRamImage(leftMask) | |
self.leftMaskTexStage = TextureStage('left_mask') | |
#Mask right (with card 2) | |
self.rightMaskTex = Texture("right_mask") | |
self.rightMaskTex.setup2dTexture(texSize, texSize, Texture.T_unsigned_byte, Texture.F_luminance) | |
self.rightMaskTex.setRamImage(rightMask) | |
self.rightMaskTexStage = TextureStage('right_mask') | |
#CREATE CARDS/SCENEGRAPH | |
cm = CardMaker('card1') | |
cm.setFrameFullscreenQuad() | |
self.card1 = self.aspect2d.attachNewNode(cm.generate()) | |
self.card2 = self.aspect2d.attachNewNode(cm.generate()) | |
#SET TEXTURE STAGES | |
self.card1.setTexture(self.sinTextureStage, self.sinTexture) #ts, tx | |
self.card1.setTexture(self.leftMaskTexStage, self.leftMaskTex) | |
self.card2.setTexture(self.sinTextureStage, self.sinTexture) #ts, tx | |
self.card2.setTexture(self.rightMaskTexStage, self.rightMaskTex) | |
#Set attributes so you can see both cards | |
#self.card2.setTransparency(TransparencyAttrib.MAlpha) #enable transparency | |
#self.card2.setAlphaScale(0.5) # | |
#Set attributes so both show brightly (do not use transparency attrib that's a trap) | |
self.setBackgroundColor((0, 0, 0, 1)) | |
self.card1.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.M_add)) | |
self.card2.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.M_add)) | |
#BASIC TRANSFORMS | |
self.card1.setScale(np.sqrt(2)) | |
self.card1.setR(rotation) | |
self.card2.setScale(np.sqrt(2)) | |
self.card2.setR(rotation) | |
self.title = OnscreenText("x", | |
style = 1, | |
fg = (1,1,1,1), | |
bg = (0, 0, 0, 1), | |
pos = (0,0), | |
scale = 0.06) | |
#Add texture move procedure to the task manager | |
self.taskMgr.add(self.moveTextureTask, "moveTextureTask") | |
#Procedure to move the textures | |
def moveTextureTask(self, task): | |
shiftMag = task.time*0.15 | |
self.card1.setTexPos(self.sinTextureStage, shiftMag, 0, 0) #u, v, w | |
self.card2.setTexPos(self.sinTextureStage, -shiftMag, 0, 0) #u, v, w | |
return Task.cont #as long as this is returned, the taskMgr will continue to call it | |
if __name__ == '__main__': | |
app = MyApp() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment