.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/posteffect2.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_feature_demo_posteffect2.py: Post processing effects 2 ========================= Example post-processing effects, showing a custom effect. .. GENERATED FROM PYTHON SOURCE LINES 7-75 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_posteffect2_001.webp :alt: posteffect2 :srcset: /_gallery/feature_demo/images/sphx_glr_posteffect2_001.webp :class: sphx-glr-single-img .. code-block:: Python from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx from pygfx.renderers.wgpu import EffectPass, NoisePass canvas = RenderCanvas(update_mode="continuous") renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() obj = gfx.Mesh( gfx.torus_knot_geometry(1, 0.3, 128, 32), gfx.MeshPhongMaterial(map=None), ) scene.add(obj) camera = gfx.PerspectiveCamera(70, 1) camera.show_object(scene) scene.add(gfx.AmbientLight(), camera.add(gfx.DirectionalLight())) controller = gfx.TrackballController(camera, register_events=renderer) class SineEffectPass(EffectPass): """A custom effect by subclassing the base EffectPass. See how we implement the fragment shader entry point. Variables that already exist are: * varyings.position * varyings.texCoord * colorTex * depthText * texSamper * u_effect.time Extra uniforms can be added by overloading the uniform_type class variable. This works in the same way as in custom materials. """ uniform_type = dict( EffectPass.uniform_type, magnitude="f4", ) wgsl = """ @fragment fn fs_main(varyings: Varyings) -> @location(0) vec4 { let texCoord = varyings.texCoord.xy; let v = sin(texCoord.y * 20.0 + u_effect.time * 2.0) * u_effect.magnitude; let uv = vec2f(texCoord.x + v, texCoord.y); return textureSample(colorTex, texSampler, uv); } """ def __init__(self, magnitude): super().__init__() self._uniform_data["magnitude"] = float(magnitude) renderer.effect_passes = [SineEffectPass(0.01), NoisePass(0.2)] if __name__ == "__main__": canvas.request_draw(lambda: renderer.render(scene, camera)) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.298 seconds) .. _sphx_glr_download__gallery_feature_demo_posteffect2.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: posteffect2.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: posteffect2.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: posteffect2.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_ .. only:: html Interactive example ------------------- Try this example in your browser using Pyodide. Might not work with all examples and all devices. Check the output and your browser's console for details. .. raw:: html