WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Old 05-12-2010, 04:16 PM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
How to render a texture of the transparent object and then blur it

Hi,

I would like to have an object in Vizard that simulates blurred glass (glossy refraction) with varying degrees of blur. At low or zero blur, the object would be transparent, and at high blur it would be very blurry. I want the object to render such that if the object moves, whatever is behind the object is blurred.

I've tried to create a render texture and render node attached to the transparent object, and then apply a blur shader to the texture. I put some sample code provided by worldviz together, but can't seem to get it to behave appropriately. I've included it below. Is this the right approach or is there a better way?

Thanks in advance!

Code:
import viz
viz.go()

# add objects
viz.add('gallery.ive')

target = viz.add( 'box.wrl' ) 
target.setScale(1,1,0.001)
target.setPosition(0,2,1)
target.color(0.5,0.5,0.5)

# make it tranparent
target.alpha(0.4)


# shader programming
kernel = 50
size = 512

#Uniforms for shaders
srcImageUniform = viz.addUniformInt('srcImage',0)
blurScaleUniform = viz.addUniformFloat('blurScale',1.0)

blur_source = """
uniform sampler2D srcImage;
uniform float blurScale;
void main(void)
{
	vec4 color = vec4(0,0,0,0);
	%code%
	color.a = 1.0;
	gl_FragColor = color;
}"""
		
#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
total = sum(weights)
frac = 1.0 / total
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('color += ( texture2D( srcImage, gl_TexCoord[0].xy + vec2( %f*blurScale, 0.0 ) ) * %f );'%(o,w))
	vert_blur_code.append('color += ( texture2D( srcImage, gl_TexCoord[0].xy + vec2( 0.0, %f*blurScale ) ) * %f );'%(o,w))
			
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 ) ))


# apply shader on texture
for eye in [viz.RENDER_LEFT,viz.RENDER_RIGHT]:
			
	#Render textures for ping-ponging
	tex1 = viz.addRenderTexture()
	tex2 = viz.addRenderTexture()
			
	"""
	Pass 1 renders the normal scene to a texture
	"""
	pass1 = viz.addRenderNode(size=[size,size])
	pass1.attachTexture(tex1)
	pass1.setOrder(viz.PRE_RENDER,-5)
	pass1.disable(eye)
			
	"""
	Pass 2 renders the texture from pass 1 onto a quad.
	The quad has a shader attached which blurs the texture horizontally.
	"""
	pass2 = viz.addRenderNode()
	pass2.setHUD(0,200,0,200,renderQuad=True)
	pass2.setSize(size,size)
	pass2.setBuffer(viz.RENDER_FBO)
	pass2.setOrder(viz.PRE_RENDER,-4)
	pass2.attachTexture(tex2)
	pass2.texture(tex1)
	pass2.apply(horzBlurShader)
	pass2.apply(srcImageUniform)
	pass2.apply(blurScaleUniform)
	pass2.disable(eye)
			
	"""
	Pass 3 renders the texture from pass 2 onto a quad.
	The quad has a shader attached which blurs the texture vertically.
	"""
	pass3 = viz.addRenderNode()
	pass3.setHUD(0,200,0,200,renderQuad=True)
	pass3.setSize(size,size)
	pass3.setBuffer(viz.RENDER_FBO)
	pass3.setOrder(viz.PRE_RENDER,-3)
	pass3.attachTexture(tex1)
	pass3.texture(tex2)
	pass3.apply(vertBlurShader)
	pass3.apply(srcImageUniform)
	pass3.apply(blurScaleUniform)
	pass3.disable(eye)
			
	"""
	Pass 4 renders the final blurred texture to the screen
	"""
	pass4 = viz.addRenderNode()
	pass4.setHUD(0,200,0,200,renderQuad=True)
	pass4.setOrder(viz.POST_RENDER,-1)
	pass4.texture(tex1)
	pass4.disable(eye)
			
	#Save render nodes and blur uniform
	#d.passes += [pass1,pass2,pass3,pass4]
			

# apply shader	
target.apply(horzBlurShader)
target.apply(vertBlurShader)
target.apply(srcImageUniform)
target.apply(blurScaleUniform)
Reply With Quote
  #2  
Old 09-25-2012, 03:15 PM
jde jde is offline
Member
 
Join Date: Jul 2009
Location: Providence RI
Posts: 24
We're trying to accomplish the same thing. Any luck with this issue?
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:01 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC