.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/light_directional_shadow.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_light_directional_shadow.py: Directional Shadow 1 ==================== This example demonstrates directional light shadows and their helper. The cubes within the view frustum of the shadow camera have complete shadows, the cubes at the edge of the view frustum of the camera have partial shadows, while the cubes outside the view frustum of the camera will not cast shadows. .. GENERATED FROM PYTHON SOURCE LINES 11-71 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_light_directional_shadow_001.webp :alt: light directional shadow :srcset: /_gallery/feature_demo/images/sphx_glr_light_directional_shadow_001.webp :class: sphx-glr-single-img .. code-block:: Python import math import pylinalg as la from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx renderer = gfx.renderers.WgpuRenderer(RenderCanvas()) scene = gfx.Scene() floor = gfx.Mesh( gfx.plane_geometry(2000, 2000), gfx.MeshPhongMaterial(color="#808080", side="Front"), ) floor.local.rotation = la.quat_from_euler(-math.pi / 2, order="X") floor.receive_shadow = True scene.add(floor) ambient = gfx.AmbientLight("#fff", 0.1) scene.add(ambient) light = gfx.DirectionalLight("#aaaaaa") light.local.x = -50 light.local.y = 50 light.cast_shadow = True light.shadow.camera.width = 100 light.shadow.camera.height = 100 scene.add(light.add(gfx.DirectionalLightHelper(30, show_shadow_extent=True))) box = gfx.box_geometry(20, 20, 20) material = gfx.MeshPhongMaterial() for i in range(5): cube = gfx.Mesh(box, material) cube.local.position = (0, 10, i * 50 - 100) cube.cast_shadow = True scene.add(cube) camera = gfx.PerspectiveCamera(70, 16 / 9) camera.local.y = 100 camera.local.z = 350 camera.show_pos((0, 0, 0)) 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.640 seconds) .. _sphx_glr_download__gallery_feature_demo_light_directional_shadow.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: light_directional_shadow.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: light_directional_shadow.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: light_directional_shadow.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