{ "cells": [ { "cell_type": "markdown", "id": "95b4836d", "metadata": {}, "source": [ "# PyGfx points and lines example\n", "\n", "**Note that this example depends on pygfx (`pip install -U pygfx`).**\n", "\n", "An example demonstrating points and lines, in two separate canvases." ] }, { "cell_type": "code", "execution_count": 1, "id": "8e9b85e0", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pygfx as gfx\n", "from wgpu.gui.jupyter import WgpuCanvas" ] }, { "cell_type": "code", "execution_count": 2, "id": "b912bb67", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d7fda6852cbf4567b25caa722554a3d8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "RFBOutputContext()" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "Forcing backend: Vulkan (4)\n" ] }, { "data": { "text/html": [ "
initial snapshot
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "90671226ee034aa6aa7d05cad339b65a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "JupyterWgpuCanvas()" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "canvas1 = WgpuCanvas()\n", "renderer1 = gfx.WgpuRenderer(canvas1)\n", "\n", "scene1 = gfx.Scene()\n", "\n", "x = np.linspace(20, 620, 200, dtype=np.float32)\n", "y = np.sin(x / 10) * 100 + 200\n", "\n", "positions = np.column_stack([x, y, np.zeros_like(x)])\n", "colors = np.random.uniform(0, 1, (x.size, 4)).astype(np.float32)\n", "geometry = gfx.Geometry(positions=positions, colors=colors)\n", "\n", "\n", "# Also see LineSegmentMaterial and LineThinSegmentMaterial\n", "material = gfx.LineSegmentMaterial(\n", " thickness=6.0, color=(0.0, 0.7, 0.3, 0.5), vertex_colors=True\n", ")\n", "line = gfx.Line(geometry, material)\n", "scene1.add(line)\n", "\n", "camera1 = gfx.ScreenCoordsCamera()\n", "\n", "canvas1.request_draw(lambda: renderer1.render(scene1, camera1))\n", "canvas1" ] }, { "cell_type": "code", "execution_count": 3, "id": "658ad769", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "40e9b815d4584933ab3f8463cc560a8f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "RFBOutputContext()" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "Forcing backend: Vulkan (4)\n" ] }, { "data": { "text/html": [ "
initial snapshot
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b7357203282145c581f4e9d37a5df812", "version_major": 2, "version_minor": 0 }, "text/plain": [ "JupyterWgpuCanvas()" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "canvas2 = WgpuCanvas()\n", "renderer2 = gfx.WgpuRenderer(canvas2)\n", "\n", "scene2 = gfx.Scene()\n", "\n", "positions = np.random.normal(0, 0.5, (100, 3)).astype(np.float32)\n", "geometry = gfx.Geometry(positions=positions)\n", "\n", "material = gfx.PointsMaterial(size=10, color=(0, 1, 0.5, 0.7))\n", "points = gfx.Points(geometry, material)\n", "scene2.add(points)\n", "scene2.add(gfx.Background(gfx.BackgroundMaterial((0.2, 0.0, 0, 1), (0, 0.0, 0.2, 1))))\n", "\n", "camera2 = gfx.NDCCamera()\n", "\n", "canvas2.request_draw(lambda: renderer2.render(scene2, camera2))\n", "canvas2" ] }, { "cell_type": "code", "execution_count": null, "id": "5db8f882", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.1" } }, "nbformat": 4, "nbformat_minor": 5 }