WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 08-03-2016, 09:48 PM
Vaquero Vaquero is offline
Member
 
Join Date: Nov 2015
Posts: 62
Red face Crosshatch Post-Processing Effect give away

He there!
As I was digging more into GLSL shader programming and its usage inside Vizard, I implemented the code for a crosshatch post-processing effect as a practice. But I thought, maybe it's useful for anybody, so I share it here.
It also demonstrates how to partially overlay the original image.

Vizard Script:
Code:
"""
Crosshatch Post Processing Effect
with adjustable split screen.
"""

import viz
viz.go()

import vizcam
vizcam.PivotNavigate(center=[0,1,0],distance=10)

viz.add('piazza.osgb')

import vizinfo
# Setup info panel
info = vizinfo.InfoPanel()

import vizfx.postprocess

class CrosshatchEffect(vizfx.postprocess.BaseShaderEffect):
	
	def _getFragmentCode(self):
		return """
		uniform sampler2D vizpp_InputTex;
		uniform float scale;
		uniform float vx_offset;
		uniform float hatch_y_offset; // 5.0
		uniform float lum_threshold_1; // 1.0
		uniform float lum_threshold_2; // 0.7
		uniform float lum_threshold_3; // 0.5
		uniform float lum_threshold_4; // 0.3
		void main() 
		{ 
		  vec2 uv = gl_TexCoord[0].xy;
		  
		  vec3 tc = vec3(1.0, 0.0, 0.0);
		  if (uv.x < (vx_offset-0.005))
		  {
		    float lum_r = texture2D(vizpp_InputTex, uv).r * 0.2126;
			float lum_g = texture2D(vizpp_InputTex, uv).g*0.7152;
			float lum_b = texture2D(vizpp_InputTex, uv).b*0.0722;
			float lum = scale * length(lum_r + lum_g + lum_b);
			tc = vec3(1.0, 1.0, 1.0);
		  
			if (lum < lum_threshold_1) 
			{
			  if (mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0) 
				tc = vec3(0.0, 0.0, 0.0);
			}  
		  
			if (lum < lum_threshold_2) 
			{
			  if (mod(gl_FragCoord.x - gl_FragCoord.y, 10.0) == 0.0) 
				tc = vec3(0.0, 0.0, 0.0);
			}  
		  
			if (lum < lum_threshold_3) 
			{
			  if (mod(gl_FragCoord.x + gl_FragCoord.y - hatch_y_offset, 10.0) == 0.0) 
				tc = vec3(0.0, 0.0, 0.0);
			}  
		  
			if (lum < lum_threshold_4) 
			{
			  if (mod(gl_FragCoord.x - gl_FragCoord.y - hatch_y_offset, 10.0) == 0.0) 
				tc = vec3(0.0, 0.0, 0.0);
			}
		  }
		  else if (uv.x>=(vx_offset+0.005))
		  {
			tc = texture2D(vizpp_InputTex, uv).rgb;
		  }
		  
		  gl_FragColor = vec4(tc, 1.0);
		}
		"""
	
	def _createUniforms(self):
		self.uniforms.addFloat('scale',1.0)
		self.uniforms.addFloat('vx_offset',0.5)
		self.uniforms.addFloat('hatch_y_offset',5.0)
		self.uniforms.addFloat('lum_threshold_1',1.0)
		self.uniforms.addFloat('lum_threshold_2',0.7)
		self.uniforms.addFloat('lum_threshold_3',0.5)
		self.uniforms.addFloat('lum_threshold_4',0.3)
	
	def setScale(self,scale):
		self.uniforms.setValue('scale',scale)
	
	def getScale(self):
		return self.uniforms.getValue('scale')
		
	def setvx_offset(self,vx_offset):
		self.uniforms.setValue('vx_offset',vx_offset)
	
	def getvx_offset(self):
		return self.uniforms.getValue('vx_offset')
		
effect = CrosshatchEffect()
vizfx.postprocess.addEffect(effect)

import vizconfig
# Setup config options for post processing effect
ppConfig = vizconfig.BasicConfigurable('Crosshatch')
ppConfig.addBoolItem('Enable', fget=effect.getEnabled, fset=effect.setEnabled)
ppConfig.addFloatRangeItem('Brightness', [0.5,5.0], fget=lambda: effect.getScale(), fset=lambda v: effect.setScale(v))
ppConfig.addFloatRangeItem('Split Screen', [0.0,1.0], fget=lambda: effect.getvx_offset(), fset=lambda v: effect.setvx_offset(v))
vizconfig.register(ppConfig)

vizconfig.getConfigWindow().setWindowVisible(True)
Source: http://www.geeks3d.com/20110219/shad...g-glsl-filter/
Reply With Quote
Reply

Tags
crosshatch, effect, glsl, post-process, shader

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating a Postprocessing effect MissingAFew Vizard 2 05-07-2015 09:58 AM


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


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