.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/mesh_toon2.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_toon2.py: Toon Rendering 2 ================ This example shows the toon rendering affect of materials with different colors and gradient maps. .. GENERATED FROM PYTHON SOURCE LINES 8-124 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_mesh_toon2_001.webp :alt: mesh toon2 :srcset: /_gallery/feature_demo/images/sphx_glr_mesh_toon2_001.webp :class: sphx-glr-single-img .. code-block:: Python import math from time import perf_counter import numpy as np from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx # Init canvas = RenderCanvas(size=(640, 480), title="gfx_toon") renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() # Lights scene.add(gfx.AmbientLight("#c1c1c1", 3)) point_light = gfx.PointLight("#fff", 2, distance=800, decay=0) scene.add(point_light) point_light.add(gfx.PointLightHelper(4)) background = gfx.Background.from_color("#444488") scene.add(background) # Now create spheres ... cube_width = 400 numbers_per_side = 5 sphere_radius = (cube_width / numbers_per_side) * 0.8 * 0.5 step_size = 1.0 / numbers_per_side geometry = gfx.sphere_geometry(sphere_radius, 32, 16) index = 0 alpha = 0.0 alpha_index = 0 while alpha <= 1.0: colors = np.arange(alpha_index + 2) / (alpha_index + 1) * 255 gradient_map_data = np.array(colors, dtype=np.uint8).reshape(1, -1, 1) # H,W,C gradient_map = gfx.Texture(gradient_map_data, dim=2) beta = 0.0 while beta <= 1.0: gamma = 0.0 while gamma <= 1.0: diffuse_color = gfx.Color.from_physical( *gfx.Color.from_hsl(alpha, 0.5, gamma * 0.5 + 0.1) ) diffuse_color = diffuse_color * (1.0 - beta * 0.2) material = gfx.MeshToonMaterial( color=diffuse_color, gradient_map=gradient_map, ) mesh = gfx.Mesh(geometry, material) mesh.local.position = ( alpha * 400 - 200, beta * 400 - 200, gamma * 400 - 200, ) scene.add(mesh) index += 1 gamma += step_size beta += step_size alpha += step_size alpha_index += 1 def add_label(text, pos): text = gfx.Text( text=text, font_size=20, text_align="center", anchor="middle-center", material=gfx.TextMaterial(outline_color="#000", outline_thickness=0.5), ) text.local.position = pos scene.add(text) add_label("-gradientMap", (-350, 0, 0)) add_label("+gradientMap", (350, 0, 0)) add_label("+diffuse", (0, 0, 300)) add_label("-diffuse", (0, 0, -300)) # Create camera and controls camera = gfx.PerspectiveCamera(45, 640 / 480) camera.show_object(scene, view_dir=(-2, -1.5, -3), scale=1) controller = gfx.OrbitController(camera, register_events=renderer) t0 = perf_counter() def animate(): t = perf_counter() - t0 t = t * 0.25 point_light.local.position = ( math.sin(t * 7) * 300, math.cos(t * 5) * 400, math.cos(t * 3) * 300, ) 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 23.449 seconds) .. _sphx_glr_download__gallery_feature_demo_mesh_toon2.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: mesh_toon2.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: mesh_toon2.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: mesh_toon2.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