def __init__(self): # open the fragment shader, assuming it's relative to this code file vertCode = "" with open( os.path.join(os.path.dirname(__file__), 'view_projector.vert'), 'r') as vertFile: vertCode = vertFile.read() fragCode = "" with open( os.path.join(os.path.dirname(__file__), 'view_projector.frag'), 'r') as fragFile: fragCode = fragFile.read() # Make a new shader, and assign some default values for the uniforms. # Thesewill be replaced on update. self._shader = viz.addShader(frag=fragCode, vert=vertCode) # temp mat to default set the uniforms mat = vizmat.Transform() # Holds the inverse transform of the main view. # This will help to calculate the model matrix inside the shader. # Such that vertices can be re-projected into view space of the projector. self._inv_view_uni = viz.addUniformMatrix('mainViewMat_inv', mat.get()) # View matrix of the projector. self._view_uni = viz.addUniformMatrix('viewMat', mat.get()) # Projection matrix of the projector. self._proj_uni = viz.addUniformMatrix('projMat', mat.get()) # Holds the depth texture to perform inverse shadow mapping. # This will allow for projecting only onto first fragment hit. self._depth_texture_uni = viz.addUniformInt('depth_tex', 3) # This allows for accumulation of the previous frames projection # along with the actual frames projection. self._prev_texture_uni = viz.addUniformInt('prev_frame_tex', 4) # specifies an additional scaling factor for frame based accumulation (value range [0.1,1.0]) self._frame_weight_uni = viz.addUniformFloat('frame_weight', 1.0) # specifies a scaling factor for the view cone aperture (value range [0.0,1.0]) self._aperture_scale_uni = viz.addUniformFloat('aperture_scale', 1.0) # attach all uniforms self._shader.attach([ self._inv_view_uni, self._view_uni, self._proj_uni, self._depth_texture_uni, self._prev_texture_uni, self._frame_weight_uni, self._aperture_scale_uni ]) # Camera used to capture the depth texture. self._depth_cam = viz.addRenderNode(inheritView=False) self._depth_cam.drawOrder(1000) self._depth_cam.setAutoClip(False) self._depth_cam.setRenderTexture( viz.addRenderTexture(format=viz.TEX_DEPTH), buffer=viz.RENDER_DEPTH)
def addMirror(mirror,mat=None,eye=viz.BOTH_EYE): #If mirror matrix is not specifed, get matrix of mirror object if mat is None: mat = mirror.getMatrix() #Position of mirror pos = viz.Vector(mat.getPosition()) #Direction mirror is pointing dir = viz.Vector(mat.getForward()) dir.normalize() #Quaternion rotation of mirror quat = mat.getQuat() #Create render texture tex = viz.addRenderTexture() #Create render node for rendering reflection lens = viz.addRenderNode(size=[1024*2,512*2]) lens.setMultiSample(4) lens.attachTexture(tex) lens.setInheritView(True,viz.POST_MULT) lens.disable(viz.CULL_FACE,op=viz.OP_OVERRIDE) #Test this to drop frames #lens.setRenderLimit( viz.RENDER_LIMIT_FRAME ) lens.setCullMask(REFLECT_MASK) if eye == viz.LEFT_EYE: lens.disable(viz.RENDER_RIGHT) mirror.disable(viz.RENDER_RIGHT) mirror.setMask(REFLECT_MASK|viz.RIGHT_MASK,mode=viz.MASK_REMOVE) elif eye == viz.RIGHT_EYE: lens.disable(viz.RENDER_LEFT) mirror.disable(viz.RENDER_LEFT) mirror.setMask(REFLECT_MASK|viz.LEFT_MASK,mode=viz.MASK_REMOVE) mirror.setMask(REFLECT_MASK,mode=viz.MASK_REMOVE) mirror.renderToAllRenderNodesExcept([lens]) #Setup reflection matrix rot = viz.Matrix.quat(quat) invRot = rot.inverse() lens.setMatrix(viz.Matrix.translate(-pos)*invRot*viz.Matrix.scale(1,1,-1)*rot*viz.Matrix.translate(pos)) #Setup reflection clip plane s = viz.sign(viz.Vector(dir) * viz.Vector(pos)) plane = vizmat.Plane(pos=pos,normal=dir) dist = plane.distance([0,0,0]) lens.clipPlane([dir[0],dir[1],dir[2],s*dist-0.01]) #Project reflection texture onto mirror mirror.texture(tex) mirror.texGen(viz.TEXGEN_PROJECT_EYE)
def addMirror(mirror,mat=None,eye=viz.BOTH_EYE): #If mirror matrix is not specifed, get matrix of mirror object if mat is None: mat = mirror.getMatrix() #Position of mirror pos = viz.Vector(mat.getPosition()) #Direction mirror is pointing dir = viz.Vector(mat.getForward()) dir.normalize() #Quaternion rotation of mirror quat = mat.getQuat() #Create render texture tex = viz.addRenderTexture() #Create render node for rendering reflection lens = viz.addRenderNode(size=[512,512]) lens.attachTexture(tex) lens.setInheritView(True,viz.POST_MULT) lens.disable(viz.CULL_FACE,op=viz.OP_OVERRIDE) lens.setCullMask(REFLECT_MASK) if eye == viz.LEFT_EYE: lens.disable(viz.RENDER_RIGHT) mirror.setMask(REFLECT_MASK|viz.RIGHT_MASK,mode=viz.MASK_REMOVE) elif eye == viz.RIGHT_EYE: lens.disable(viz.RENDER_LEFT) mirror.setMask(REFLECT_MASK|viz.LEFT_MASK,mode=viz.MASK_REMOVE) else: mirror.setMask(REFLECT_MASK,mode=viz.MASK_REMOVE) #Setup reflection matrix rot = viz.Matrix.quat(quat) invRot = rot.inverse() lens.setMatrix(viz.Matrix.translate(-pos)*invRot*viz.Matrix.scale(1,1,-1)*rot*viz.Matrix.translate(pos)) #Setup reflection clip plane s = viz.sign(viz.Vector(dir) * viz.Vector(pos)) plane = vizmat.Plane(pos=pos,normal=dir) dist = plane.distance([0,0,0]) lens.clipPlane([dir[0],dir[1],dir[2],s*dist-0.01]) #Project reflection texture onto mirror mirror.texture(tex) mirror.texGen(viz.TEXGEN_PROJECT_EYE)
def __init__(self, frame_weight=0.5, aperture_scale=0.5, node=None, **kwargs): # node to reference this instance in the scenegraph self._node = node if self._node == None: self._node = viz.addGroup() viz.VizNode.__init__(self, id=self._node.id, **kwargs) # Create two render textures which we will swap between when updating self._output_texture = viz.addRenderTexture() self._last_frame_texture = viz.addRenderTexture() ### INIT CAMERA # Create render node for camera self._cam = viz.addRenderNode(size=(1024, 1024)) self._cam.renderOnlyToWindows([viz.MainWindow]) self._cam.setInheritView(False) self._cam.drawOrder(1000) self._cam.setFov(90.0, 1.0, 0.1, 1000.0) # Only render once per frame, in case stereo is enabled self._cam.setRenderLimit(viz.RENDER_LIMIT_FRAME) # set the starting render textures for output and input self._cam.setRenderTexture(self._output_texture) self._cam.texture(self._last_frame_texture, unit=4) # link camera to capture viz.link(self._node, self._cam) # affect camera so its render texture will be computed using the defined shading pipeline self._projector = ViewProjector() self._projector.setFrameWeight(frame_weight) self._projector.setApertureScale(aperture_scale) self._projector.affect(self._cam) self._update_event = vizact.onupdate(100, self.update)
def __init__(self, kernel=9, alpha=0.8, size=256, pos=(0, 10, 0), euler=(0, 90, 0), area=[5, 5], scene=viz.MainScene): #Create render texture self.shadowTex = viz.addRenderTexture(wrap=viz.CLAMP_TO_BORDER, borderColor=viz.WHITE) #Create render node to render shadow caster to texture self.shadowPass = viz.addRenderNode(scene=scene, size=[size, size], inheritView=False, autoClip=False) self.shadowPass.setClearColor(viz.WHITE) self.shadowPass.attachTexture(self.shadowTex) self.shadowPass.setScene(None) #Apply shader to render node so everything is black code = """ #define I %alpha% void main() { gl_FragColor = vec4(I,I,I,1.0); } """ shader = viz.addShader(frag=code.replace('%alpha%', str(1.0 - alpha))) self.shadowPass.apply(shader, op=viz.OP_OVERRIDE) #Create projector for projecting shadow texture onto receivers self.texProj = projector.add(self.shadowTex, scene=scene, cubemap=False) #Save all render node passes self.passes = [self.shadowPass] #Setup render nodes to blur shadow texture if kernel: blur_source = """ uniform sampler2D srcImage; void main(void) { vec4 color = vec4(0,0,0,0); %code% color.a = 1.0; gl_FragColor = color; }""" horz_source = 'color += texture2D( srcImage, gl_TexCoord[0].xy + vec2( %f, 0.0 ) ) * %f;' vert_source = 'color += texture2D( srcImage, gl_TexCoord[0].xy + vec2( 0.0, %f ) ) * %f;' #Calculate weight and offsets for blur code weights = [] offsets = [] mid = float(kernel - 1) kernel *= 2 for i in xrange(0, kernel, 2): offsets.append(((i - mid) / 2.0) * (1.0 / size)) x = (i - mid) / mid weights.append(0.05 + ((-(x * x) + 1.0) / 4.0)) #Normalize weights frac = 1.0 / sum(weights) weights = [frac * x for x in weights] #Create blur code for shaders horz_blur_code = [] vert_blur_code = [] for w, o in zip(weights, offsets): horz_blur_code.append(horz_source % (o, w)) vert_blur_code.append(vert_source % (o, w)) #Create shaders horzBlurShader = viz.addShader( frag=blur_source.replace('%code%', '\n'.join(horz_blur_code))) vertBlurShader = viz.addShader( frag=blur_source.replace('%code%', '\n'.join(vert_blur_code))) srcImageUniform = viz.addUniformInt('srcImage', 0) #Render texture for ping-ponging tex2 = viz.addRenderTexture() """ Pass 2 renders the shadow texture onto a quad. The quad has a shader attached which blurs the texture horizontally. """ self.blurPass1 = viz.addRenderNode(scene=scene) self.blurPass1.setHUD(0, 200, 0, 200, renderQuad=True) self.blurPass1.setSize(size, size) self.blurPass1.setBuffer(viz.RENDER_FBO) self.blurPass1.setOrder(viz.PRE_RENDER, 1) self.blurPass1.attachTexture(tex2) self.blurPass1.texture(self.shadowTex) self.blurPass1.apply(horzBlurShader) self.blurPass1.apply(srcImageUniform) self.passes.append(self.blurPass1) """ Pass 3 renders the texture from pass 2 onto a quad. The quad has a shader attached which blurs the texture vertically. """ self.blurPass2 = viz.addRenderNode(scene=scene) self.blurPass2.setHUD(0, 200, 0, 200, renderQuad=True) self.blurPass2.setSize(size, size) self.blurPass2.setBuffer(viz.RENDER_FBO) self.blurPass2.setOrder(viz.PRE_RENDER, 2) self.blurPass2.attachTexture(self.shadowTex) self.blurPass2.texture(tex2) self.blurPass2.apply(vertBlurShader) self.blurPass2.apply(srcImageUniform) self.passes.append(self.blurPass2) #Remove texture/shader/uniforms (already applied) horzBlurShader.remove() vertBlurShader.remove() srcImageUniform.remove() tex2.remove() #Initialize shadow area/pos/euler self.setArea(area) self.setPosition(pos) self.setEuler(euler)
pit.audio_running.play() pit.runAction(vizact.moveTo(pos,speed=2.0)) pit.addAction(vizact.call(pit.audio_stop.play)) pit.addAction(vizact.call(pit.audio_running.pause)) # Use pit color to blend between lower/upper lightmaps duration = pit.getActionInstance().getDuration() color = pit.colors[pit.lowered] pit.runAction(vizact.fadeTo(color,time=duration),pool=1) vizact.onkeydown('2',TogglePit) # Create render texture for camera video feed video = viz.addRenderTexture() # Create render node for camera cam = viz.addRenderNode() cam.fov = 30.0 cam.setSize(1280,720) cam.setInheritView(False) cam.setPosition([-10.94835, 11.09378, 13.61334]) cam.setRenderTexture(video) cam.setMultiSample(viz.AUTO_COMPUTE) cam.setRenderLimit(viz.RENDER_LIMIT_FRAME) # Get handle to screen object and apply video feed to it screen = model.getChild('screen') screen.texture(video) cam.renderOnlyIfNodeVisible([screen]) # Use up/down keys to zoom camera in/out def CameraZoom(inc):
def __init__(self,kernel=9,alpha=0.8,size=256,pos=(0,10,0),euler=(0,90,0),area=[5,5],scene=viz.MainScene): #Create render texture self.shadowTex = viz.addRenderTexture(wrap=viz.CLAMP_TO_BORDER,borderColor=viz.WHITE) #Create render node to render shadow caster to texture self.shadowPass = viz.addRenderNode(scene=scene,size=[size,size],inheritView=False,autoClip=False) self.shadowPass.setClearColor(viz.WHITE) self.shadowPass.attachTexture(self.shadowTex) self.shadowPass.setScene(None) #Apply shader to render node so everything is black code = """ #define I %alpha% void main() { gl_FragColor = vec4(I,I,I,1.0); } """ shader = viz.addShader(frag=code.replace('%alpha%',str(1.0-alpha))) self.shadowPass.apply(shader,op=viz.OP_OVERRIDE) #Create projector for projecting shadow texture onto receivers self.texProj = projector.add(self.shadowTex,scene=scene,cubemap=False) #Save all render node passes self.passes = [self.shadowPass] #Setup render nodes to blur shadow texture if kernel: blur_source = """ uniform sampler2D srcImage; void main(void) { vec4 color = vec4(0,0,0,0); %code% color.a = 1.0; gl_FragColor = color; }""" horz_source = 'color += texture2D( srcImage, gl_TexCoord[0].xy + vec2( %f, 0.0 ) ) * %f;' vert_source = 'color += texture2D( srcImage, gl_TexCoord[0].xy + vec2( 0.0, %f ) ) * %f;' #Calculate weight and offsets for blur code weights = [] offsets = [] mid = float(kernel - 1) kernel *= 2 for i in xrange(0,kernel,2): offsets.append( ((i-mid) / 2.0) * (1.0 / size) ) x = (i - mid) / mid weights.append( 0.05 + ((-(x*x) + 1.0) / 4.0) ) #Normalize weights frac = 1.0 / sum(weights) weights = [ frac * x for x in weights ] #Create blur code for shaders horz_blur_code = [] vert_blur_code = [] for w,o in zip(weights,offsets): horz_blur_code.append(horz_source%(o,w)) vert_blur_code.append(vert_source%(o,w)) #Create shaders horzBlurShader = viz.addShader(frag=blur_source.replace('%code%','\n'.join(horz_blur_code))) vertBlurShader = viz.addShader(frag=blur_source.replace('%code%','\n'.join(vert_blur_code))) srcImageUniform = viz.addUniformInt('srcImage',0) #Render texture for ping-ponging tex2 = viz.addRenderTexture() """ Pass 2 renders the shadow texture onto a quad. The quad has a shader attached which blurs the texture horizontally. """ self.blurPass1 = viz.addRenderNode(scene=scene) self.blurPass1.setHUD(0,200,0,200,renderQuad=True) self.blurPass1.setSize(size,size) self.blurPass1.setBuffer(viz.RENDER_FBO) self.blurPass1.setOrder(viz.PRE_RENDER,1) self.blurPass1.attachTexture(tex2) self.blurPass1.texture(self.shadowTex) self.blurPass1.apply(horzBlurShader) self.blurPass1.apply(srcImageUniform) self.passes.append(self.blurPass1) """ Pass 3 renders the texture from pass 2 onto a quad. The quad has a shader attached which blurs the texture vertically. """ self.blurPass2 = viz.addRenderNode(scene=scene) self.blurPass2.setHUD(0,200,0,200,renderQuad=True) self.blurPass2.setSize(size,size) self.blurPass2.setBuffer(viz.RENDER_FBO) self.blurPass2.setOrder(viz.PRE_RENDER,2) self.blurPass2.attachTexture(self.shadowTex) self.blurPass2.texture(tex2) self.blurPass2.apply(vertBlurShader) self.blurPass2.apply(srcImageUniform) self.passes.append(self.blurPass2) #Remove texture/shader/uniforms (already applied) horzBlurShader.remove() vertBlurShader.remove() srcImageUniform.remove() tex2.remove() #Initialize shadow area/pos/euler self.setArea(area) self.setPosition(pos) self.setEuler(euler)
#viz.add( 'tut_ground.wrl' ) room = viz.add('../models/room/crappy_smallroom1.wrl',viz.WORLD,scene=viz.Scene2) #room = viz.add('../models/room/crappy_smallroom1.wrl',viz.WORLD) room.setPosition( [0,-2,0] ) room.setEuler( [0, 90, 0] ); room.setScale( .05, .05, .05 ) node = viz.addRenderNode() node.setScene( viz.Scene2 ) node.setBuffer( viz.RENDER_FRAME_BUFFER ) node.setOrder( viz.POST_RENDER ) node.setInheritView( 0 ) node.setClearMask( 0 ) def setARfov( val ): global node, ARfov_vert ARfov_vert = val ARheight = HMDheight / HMDfov_vert * ARfov_vert ARwidth = ARheight * HMDaspect ARx = (HMDwidth - ARwidth)/2 ARy = (HMDheight - ARheight)/2 print ARwidth,ARheight,ARx,ARy node.setFov( ARfov_vert, HMDaspect, 0.1, 10 )
HMDaspect = HMDwidth/HMDheight viz.fov( HMDfov_vert, HMDaspect ) #viz.clip( HMDnear, HMDfar ) viz.window.setSize( HMDwidth, HMDheight ) ground = viz.add('tut_ground.wrl') ground.setScale([5,1,5]) #viz.translate(viz.HEAD_POS,0,20,-20) #viz.lookat(0,0,0) viz.clearcolor(viz.SKYBLUE) #window = viz.add(viz.WINDOW) #window.setPosition(.5,.5) node = viz.addRenderNode() node.setScene( viz.Scene2 ) node.setBuffer( viz.RENDER_FRAME_BUFFER ) node.setOrder( viz.POST_RENDER ) node.setInheritView( 0 ) node.setClearMask( 0 ) #node.disable( viz.DEPTH_TEST ); hmdview = 0 def setARfov( val ): global node, ARfov_vert, hmdview ARfov_vert = val #ARfov_vert = HMDfov_vert #ARfov_vert = 20 ARheight = (int) (HMDheight / HMDfov_vert * ARfov_vert) ARwidth = ARheight * HMDaspect
def addWaterReflection(plane, height): SIZE = [512, 512] REFLECT_MASK = viz.LAST_MASK << 1 #Use same septh texture for both render nodes depth = viz.addRenderTexture(format=viz.TEX_DEPTH) #Setup reflection texture reflectTex = viz.addRenderTexture() reflect = viz.addRenderNode(size=SIZE) reflect.attachTexture(reflectTex) reflect.attachTexture(depth, viz.RENDER_DEPTH) reflect.setMatrix( viz.Matrix.translate(0, -height, 0) * viz.Matrix.scale(1, -1, 1) * viz.Matrix.translate(0, height, 0)) reflect.setInheritView(True, viz.POST_MULT) reflect.disable(viz.CULL_FACE, op=viz.OP_OVERRIDE) reflect.clipPlane([0, -1, 0, -height]) #OP_OVERRIDE reflect.setCullMask(REFLECT_MASK) #Setup refraction texture refractTex = viz.addRenderTexture() refract = viz.addRenderNode(size=SIZE) refract.attachTexture(refractTex) refract.attachTexture(depth, viz.RENDER_DEPTH) refract.clipPlane([0, -1, 0, -height]) #OP_OVERRIDE refract.setCullMask(REFLECT_MASK) vert = """ attribute vec3 Tangent; uniform float osg_FrameTime; #define WAVE_SCALE 0.01 #define WAVE_SPEED 0.01 void main(void) { gl_Position = ftransform(); vec2 fTranslation= vec2(mod(osg_FrameTime, 100.0)*WAVE_SPEED, 0.0); vec2 vTexCoords = gl_Vertex.xz*WAVE_SCALE; // Scale texture coordinates to get mix of low/high frequency details gl_TexCoord[1].xy = vTexCoords.xy+fTranslation*2.0; gl_TexCoord[2].xy = vTexCoords.xy*2.0+fTranslation*4.0; gl_TexCoord[3].xy = vTexCoords.xy*4.0+fTranslation*2.0; gl_TexCoord[4].xy = vTexCoords.xy*8.0+fTranslation; // perspective corrected projection gl_TexCoord[1].zw = gl_Position.w; gl_TexCoord[5].xy = (gl_Position.xy + gl_Position.w)*0.5; gl_TexCoord[5].zw = vec2(1, gl_Position.w); // get tangent space basis vec3 n = normalize(gl_NormalMatrix * gl_Normal); vec3 t = normalize(gl_NormalMatrix * Tangent); vec3 b = cross(n, t); // compute tangent space eye vector vec3 tmpVec = -vec3(gl_ModelViewMatrix * gl_Vertex); gl_TexCoord[0].x = dot(tmpVec, t); gl_TexCoord[0].y = dot(tmpVec, b); gl_TexCoord[0].z = dot(tmpVec, n); } """ frag = """ uniform sampler2D water_normal; uniform sampler2D water_reflection; uniform sampler2D water_refraction; #define FADE_DIST 10.0 void main(void) { vec3 vEye = normalize(gl_TexCoord[0].xyz); // Get bump layers vec3 vBumpTexA = texture2D(water_normal, gl_TexCoord[1].xy).xyz; vec3 vBumpTexB = texture2D(water_normal, gl_TexCoord[2].xy).xyz; vec3 vBumpTexC = texture2D(water_normal, gl_TexCoord[3].xy).xyz; vec3 vBumpTexD = texture2D(water_normal, gl_TexCoord[4].xy).xyz; // Average bump layers vec3 vBumpTex = normalize(2.0 * (vBumpTexA + vBumpTexB + vBumpTexC + vBumpTexD)-4.0); // Apply individual bump scale for refraction and reflection vec3 vRefrBump = vBumpTex * vec3(0.02, 0.02, 1.0); vec3 vReflBump = vBumpTex * vec3(0.1, 0.1, 1.0); // Compute projected coordinates vec2 vProj = (gl_TexCoord[5].xy/gl_TexCoord[5].w); vec4 vReflection = texture2D(water_reflection, vProj.xy + vReflBump.xy); vec4 vRefraction = texture2D(water_refraction, vProj.xy + vRefrBump.xy); // Compute Fresnel term float NdotL = max(dot(vEye, vReflBump), 0.0); float facing = (1.0 - NdotL); float fresnelBias = 0.2; float fresnelPow = 5.0; float fresnel = max(fresnelBias + (1.0-fresnelBias)*pow(facing, fresnelPow), 0.0); // Use distance to lerp between refraction and deep water color float fDistScale = clamp(FADE_DIST/gl_TexCoord[1].w,0.0,1.0); vec3 WaterDeepColor = (vRefraction.xyz * fDistScale + (1.0 - fDistScale) * vec3(0.0, 0.1, 0.125)); // Lerp between water color and deep water color vec3 WaterColor = vec3(0, 0.1, 0.15); vec3 waterColor = (WaterColor * facing + WaterDeepColor * (1.0 - facing)); vec3 cReflect = fresnel * vReflection; // final water = reflection_color * fresnel + water_color gl_FragColor = vec4(cReflect + waterColor, 1); } """ shader = viz.addShader(vert=vert, frag=frag) shader.attach(viz.addUniformInt('water_normal', 0)) shader.attach(viz.addUniformInt('water_reflection', 1)) shader.attach(viz.addUniformInt('water_refraction', 2)) plane.apply(shader) #Apply reflection/refraction/normal texture to plane plane.texture(viz.add('art/waves.dds', wrap=viz.REPEAT), unit=0) plane.texture(reflectTex, unit=1) plane.texture(refractTex, unit=2) #Remove reflect mask from plane so it isn't drawn during reflect/refract stage plane.setMask(REFLECT_MASK, mode=viz.MASK_REMOVE)
vizact.ontimer(0, updatecar) # Add an object to scene. duck = viz.add('duck.wrl', viz.WORLD, 1) duck.setPosition(1, 7, 5) # Warehouse environment ware = viz.add('pit.osgb') # Create render texture for camera video feed video = viz.addRenderTexture() # Create render node for camera cam = viz.addRenderNode(size=(1280, 720)) # Render to video feed texture cam.setRenderTexture(video) # Only render once per frame, in case stereo is enabled cam.setRenderLimit(viz.RENDER_LIMIT_FRAME) # Get handle to screen object and apply video feed to it screen = ware.getChild('screen') screen.texture(video) # Create attachment point for following avatar car_camera = viz.link(car, viz.NullLinkable) car_camera.preTrans([0, 2, -2]) car_camera.preEuler([0, 10, 0])
def __init__(self, textures, auto_update = True, node = None, **kwargs): # node to reference this instance in the scenegraph self._node = node if self._node == None: self._node = viz.addGroup() viz.VizNode.__init__(self, id = self._node.id, **kwargs) # list of models affected by the shader effect self._affected_models = [] # save projective textures self._textures = textures ## Initialize depth cameras. # They are used to capture a depth buffer for each face. # Inverse shadow mapping is performed based on these buffers, # such that the projection only hits the fisrt surface along the projection direction. self._depth_cams = { viz.POSITIVE_X : viz.addRenderNode(inheritView = False), viz.NEGATIVE_X : viz.addRenderNode(inheritView = False), viz.POSITIVE_Y : viz.addRenderNode(inheritView = False), viz.NEGATIVE_Y : viz.addRenderNode(inheritView = False), viz.POSITIVE_Z : viz.addRenderNode(inheritView = False), viz.NEGATIVE_Z : viz.addRenderNode(inheritView = False) } # save projective depth textures self._depth_textures = { viz.POSITIVE_X : None, viz.NEGATIVE_X : None, viz.POSITIVE_Y : None, viz.NEGATIVE_Y : None, viz.POSITIVE_Z : None, viz.NEGATIVE_Z : None } cube_euler = { viz.POSITIVE_X : [90,0,0], viz.NEGATIVE_X : [-90,0,0], viz.POSITIVE_Y : [0,-90,0], viz.NEGATIVE_Y : [0,90,0], viz.POSITIVE_Z : [0,0,0], viz.NEGATIVE_Z : [-180,0,0] } proj_mat = viz.Matrix.perspective(90.0,1.0,0.1,100.0) for cam in self._depth_cams: self._depth_cams[cam].drawOrder(1000) self._depth_cams[cam].setRenderTexture( viz.addRenderTexture(format = viz.TEX_DEPTH), buffer = viz.RENDER_DEPTH ) self._depth_cams[cam].setPosition(self.getPosition()) self._depth_cams[cam].setEuler(cube_euler[cam]) self._depth_cams[cam].setProjectionMatrix(proj_mat) self._depth_cams[cam].setAutoClip(viz.OFF) viz.grab(self, self._depth_cams[cam]) self._depth_textures[cam] = self._depth_cams[cam].getRenderTexture(viz.RENDER_DEPTH) # load effect code = self._getShaderCode() self._effect = viz.addEffect(code) # set initial property values self._effect.setProperty("Tex_ProjMat", proj_mat) self._effect.setProperty("Inv_ViewMat", toGL(viz.MainView.getMatrix().inverse())) self._effect.setProperty("Tex_ViewMat_px", toGL(eulerMat(90,0,0)*self.getMatrix())) self._effect.setProperty("Tex_ViewMat_nx", toGL(eulerMat(-90,0,0)*self.getMatrix())) self._effect.setProperty("Tex_ViewMat_py", toGL(eulerMat(0,-90,0)*self.getMatrix())) self._effect.setProperty("Tex_ViewMat_ny", toGL(eulerMat(0,90,0)*self.getMatrix())) self._effect.setProperty("Tex_ViewMat_pz", toGL(eulerMat(0,0,0)*self.getMatrix())) self._effect.setProperty("Tex_ViewMat_nz", toGL(eulerMat(180,0,0)*self.getMatrix())) self._effect.setProperty("Tex_px", self._textures[viz.POSITIVE_X]) self._effect.setProperty("Tex_nx", self._textures[viz.NEGATIVE_X]) self._effect.setProperty("Tex_py", self._textures[viz.POSITIVE_Y]) self._effect.setProperty("Tex_ny", self._textures[viz.NEGATIVE_Y]) self._effect.setProperty("Tex_pz", self._textures[viz.POSITIVE_Z]) self._effect.setProperty("Tex_nz", self._textures[viz.NEGATIVE_Z]) self._effect.setProperty("TexDepth_px", self._depth_textures[viz.POSITIVE_X]) self._effect.setProperty("TexDepth_nx", self._depth_textures[viz.NEGATIVE_X]) self._effect.setProperty("TexDepth_py", self._depth_textures[viz.POSITIVE_Y]) self._effect.setProperty("TexDepth_ny", self._depth_textures[viz.NEGATIVE_Y]) self._effect.setProperty("TexDepth_pz", self._depth_textures[viz.POSITIVE_Z]) self._effect.setProperty("TexDepth_nz", self._depth_textures[viz.NEGATIVE_Z]) # init auto update self._auto_update = vizact.onupdate(0, self.update) self._auto_update.setEnabled(auto_update)