.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/mesh_depth_material.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_mesh_depth_material.py: Depth Material ============== A custom material for drawing geometry by depth .. GENERATED FROM PYTHON SOURCE LINES 7-118 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_mesh_depth_material_001.webp :alt: mesh depth material :srcset: /_gallery/feature_demo/images/sphx_glr_mesh_depth_material_001.webp :class: sphx-glr-single-img .. code-block:: Python import wgpu from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx from pygfx.renderers.wgpu import Binding, register_wgpu_render_function from pygfx.renderers.wgpu.shaders.meshshader import MeshShader class DepthMaterial(gfx.MeshBasicMaterial): pass @register_wgpu_render_function(gfx.Mesh, DepthMaterial) class DepthShader(MeshShader): # Mark as render-shader (as opposed to compute-shader) type = "render" def get_bindings(self, wobject, shared, scene): geometry = wobject.geometry bindings = { 0: Binding("u_stdinfo", "buffer/uniform", shared.uniform_buffer), 1: Binding("u_wobject", "buffer/uniform", wobject.uniform_buffer), } bindings[2] = Binding( "s_indices", "buffer/read_only_storage", geometry.indices, "VERTEX" ) bindings[3] = Binding( "s_positions", "buffer/read_only_storage", geometry.positions ) self.define_bindings(0, bindings) return { 0: bindings, } def get_pipeline_info(self, wobject, shared): # We draw triangles, no culling return { "primitive_topology": wgpu.PrimitiveTopology.triangle_list, "cull_mode": wgpu.CullMode.none, } def get_render_info(self, wobject, shared): geometry = wobject.geometry n = geometry.indices.data.size return { "indices": (n, 1), } def get_code(self): return """ {$ include 'pygfx.std.wgsl' $} struct VertexInput { @builtin(vertex_index) vertex_index : u32, }; @vertex fn vs_main(in: VertexInput) -> Varyings { let vertex_index = i32(in.vertex_index); let face_index = vertex_index / 3; var sub_index = vertex_index % 3; let ii = load_s_indices(face_index); let i0 = i32(ii[sub_index]); let position = load_s_positions(i0); let u_mvp = u_stdinfo.projection_transform * u_stdinfo.cam_transform * u_wobject.world_transform; let pos = u_mvp * vec4(position, 1.0); var varyings: Varyings; varyings.position = vec4(pos); return varyings; } @fragment fn fs_main(varyings: Varyings) -> FragmentOutput { var out: FragmentOutput; let depth = 1.0 - varyings.position.z; // Invert depth - could also do logarithmic depth out.color = vec4(depth); return out; } """ # Setup scene renderer = gfx.WgpuRenderer(RenderCanvas(size=(640, 480))) camera = gfx.PerspectiveCamera(45, 640 / 480, depth_range=(8, 12)) camera.local.z = 10 camera.show_pos((0, 0, 0)) t = gfx.Mesh(gfx.torus_knot_geometry(1, 0.3, 128, 32), DepthMaterial()) scene = gfx.Scene() scene.add(t) controller = gfx.OrbitController(camera, register_events=renderer) def animate(): renderer.render(scene, camera) renderer.request_draw() if __name__ == "__main__": renderer.request_draw(animate) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.248 seconds) .. _sphx_glr_download__gallery_feature_demo_mesh_depth_material.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: mesh_depth_material.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: mesh_depth_material.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: mesh_depth_material.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