.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/validation/validate_ndc.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_validation_validate_ndc.py: NDC Coordinates =============== Example (and test) for the NDC coordinates. Draws a square that falls partly out of visible range. * The scene should show a band from the bottom left to the upper right. * The bottom-left (NDC -1 -1) must be green, the upper-right (NDC 1 1) blue. * The other corners must be black, cut off at exactly half way: the depth is 0-1. .. GENERATED FROM PYTHON SOURCE LINES 12-95 .. image-sg:: /_gallery/validation/images/sphx_glr_validate_ndc_001.webp :alt: validate ndc :srcset: /_gallery/validation/images/sphx_glr_validate_ndc_001.webp :class: sphx-glr-single-img .. code-block:: Python from rendercanvas.auto import RenderCanvas, loop from pygfx.renderers.wgpu import ( Binding, BaseShader, register_wgpu_render_function, ) import pygfx as gfx class Square(gfx.WorldObject): pass class SquareMaterial(gfx.Material): pass @register_wgpu_render_function(Square, SquareMaterial) class SquareShader(BaseShader): def get_bindings(self, wobject, shared, scene): binding = Binding("u_stdinfo", "buffer/uniform", shared.uniform_buffer) self.define_binding(0, 0, binding) return { 0: {0: binding}, } def get_pipeline_info(self, wobject, shared): return { "primitive_topology": "triangle-strip", "cull_mode": "none", } def get_render_info(self, wobject, shared): return { "indices": (4, 1), } def get_code(self): return """ {$ include 'pygfx.std.wgsl' $} @vertex fn vs_main(@builtin(vertex_index) index: u32) -> Varyings { var positions = array, 4>( vec3(-1.0, -1.0, 0.5), vec3(-1.0, 1.0, 1.5), vec3(1.0, -1.0, -0.5), vec3(1.0, 1.0, 0.5) ); var colors = array, 4>( vec3(0.0, 1.0, 0.0), vec3(0.0, 0.5, 0.5), vec3(0.0, 0.5, 0.5), vec3(0.0, 0.0, 1.0) ); var varyings: Varyings; varyings.position = vec4(positions[index], 1.0); varyings.color = vec4(colors[index], 1.0); return varyings; } @fragment fn fs_main(varyings: Varyings) -> FragmentOutput { var out: FragmentOutput; out.color = varyings.color; return out; } """ # Setup scene canvas = RenderCanvas() renderer = gfx.WgpuRenderer(canvas) scene = gfx.Scene() t1 = Square(None, SquareMaterial()) scene.add(t1) camera = gfx.NDCCamera() # This example does not even use the camera canvas.request_draw(lambda: renderer.render(scene, camera)) if __name__ == "__main__": print(__doc__) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.268 seconds) .. _sphx_glr_download__gallery_validation_validate_ndc.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: validate_ndc.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: validate_ndc.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: validate_ndc.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