Skip to content

Instantly share code, notes, and snippets.

@plafone plafone/blur.py
Created Dec 1, 2015

Embed
What would you like to do?
GLSL - Blur shader + Panda3D
from panda3d.core import Shader
from direct.interval.LerpInterval import LerpFunc
vertex_shader = """
// Uniform inputs
uniform mat4 p3d_ModelViewProjectionMatrix;
// Vertex inputs
attribute vec4 p3d_Vertex;
attribute vec2 p3d_MultiTexCoord0;
// Output to fragment shader
varying vec2 texCoord;
varying vec2 v_blurTexCoords[15];
uniform float radius;
void main() {
gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;
texCoord = p3d_MultiTexCoord0;
v_blurTexCoords[ 0] = texCoord + vec2(-radius*7.0, 0.0);
v_blurTexCoords[ 1] = texCoord + vec2(-radius*6.0, 0.0);
v_blurTexCoords[ 2] = texCoord + vec2(-radius*5.0, 0.0);
v_blurTexCoords[ 3] = texCoord + vec2(-radius*4.0, 0.0);
v_blurTexCoords[ 4] = texCoord + vec2(-radius*3.0, 0.0);
v_blurTexCoords[ 5] = texCoord + vec2(-radius*2.0, 0.0);
v_blurTexCoords[ 6] = texCoord + vec2(-radius*1.0, 0.0);
v_blurTexCoords[ 7] = texCoord + vec2(-radius*0.0, 0.0);
v_blurTexCoords[ 8] = texCoord + vec2( radius*1.0, 0.0);
v_blurTexCoords[ 9] = texCoord + vec2( radius*2.0, 0.0);
v_blurTexCoords[10] = texCoord + vec2( radius*3.0, 0.0);
v_blurTexCoords[11] = texCoord + vec2( radius*4.0, 0.0);
v_blurTexCoords[12] = texCoord + vec2( radius*5.0, 0.0);
v_blurTexCoords[13] = texCoord + vec2( radius*6.0, 0.0);
v_blurTexCoords[14] = texCoord + vec2( radius*7.0, 0.0);
}
"""
fragment_shader = """
uniform sampler2D p3d_Texture0;
uniform float alpha;
varying vec2 v_blurTexCoords[15];
void main() {
gl_FragColor = vec4(0.0);
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 0])*0.0044299121055113265;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 1])*0.00895781211794;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 2])*0.0215963866053;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 3])*0.0443683338718;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 4])*0.0776744219933;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 5])*0.115876621105;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 6])*0.147308056121;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 7])*0.159576912161;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 8])*0.147308056121;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[ 9])*0.115876621105;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[10])*0.0776744219933;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[11])*0.0443683338718;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[12])*0.0215963866053;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[13])*0.00895781211794;
gl_FragColor += texture(p3d_Texture0, v_blurTexCoords[14])*0.0044299121055113265;
gl_FragColor = vec4(gl_FragColor.rgb, alpha);
}
"""
class BlurMan():
'''
This class allow to apply the blur effect to a set of object in a page.
If the blur is wanted in every page potentially every page has to have an
instance of this class.
Example of use:
blur_man.blur(True, self._background, radius=0.001, alpha=.6) to blur
blur_man.blur(False, self._background) to un-blur
'''
def __init__(self):
self._alpha_func = None
self._radius_func = None
self._objects = {}
def blur(self, state, node, radius=0.004, alpha=.8, duration=.1):
if self._alpha_func:
self._alpha_func.pause()
if self._radius_func:
self._radius_func.pause()
node.setShader(Shader.make(Shader.SLGLSL, vertex_shader, fragment_shader))
node.setShaderInput("alpha", 1.0)
node.setShaderInput("radius", 0.0)
if state:
self._alpha_func = LerpFunc(self.__set_alpha,
fromData=1.0,
toData=alpha,
duration=duration,
blendType='easeInOut',
extraArgs=[node])
self._alpha_func.start()
self._radius_func = LerpFunc(self.__set_radious,
fromData=0.0,
toData=radius,
duration=duration,
blendType='easeInOut',
extraArgs=[node])
self._radius_func.start()
self._objects[node] = (radius, alpha, duration)
else:
self._alpha_func = LerpFunc(self.__set_alpha,
fromData=self._objects[node][1],
toData=1.0,
duration=self._objects[node][2],
blendType='easeInOut',
extraArgs=[node])
self._alpha_func.start()
self._radius_func = LerpFunc(self.__set_radious,
fromData=self._objects[node][0],
toData=0.0,
duration=self._objects[node][2],
blendType='easeInOut',
extraArgs=[node])
self._radius_func.start()
def __set_alpha(self, value, obj):
obj.setShaderInput("alpha", value)
def __set_radious(self, value, obj):
obj.setShaderInput("radius", value)
def unblur_all(self):
for k in self._objects.keys():
self.blur(False, k,
self._objects[k][0],
self._objects[k][1],
self._objects[k][2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.