Skip to content

Commit

Permalink
docs: add lighting example in gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Sep 7, 2021
1 parent 3585c68 commit 14fada6
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,5 +384,6 @@
'examples/scales': 'examples/screenshot/scales.png',
'examples/moebius': 'examples/screenshot/moebius.png',
'examples/bars': 'examples/screenshot/bars.gif',
'examples/lighting': 'examples/screenshot/volume-rendering-specular.gif',
}
exclude_patterns = ['**.ipynb_checkpoints']
1 change: 1 addition & 0 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Examples
examples/scales
examples/moebius
examples/bars
examples/lighting

Feel free to contribute new examples:

Expand Down
289 changes: 289 additions & 0 deletions docs/source/examples/lighting.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lightning\n",
"By default ipyvolume uses a fake lighting model that works ok for depth perception. To get more realistic lighting effects ipyvolume supports a few light models from pythreejs/threejs.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-08-11T10:23:04.262467Z",
"start_time": "2020-08-11T10:23:02.527104Z"
}
},
"outputs": [],
"source": [
"import ipyvolume as ipv\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def scene():\n",
" f = ipv.figure(debug=True)\n",
" ipv.xyzlim(-1, 1)\n",
" x = np.array([0.1, 0.5], dtype=np.float32)\n",
" ipv.material_phong()\n",
" s = ipv.scatter(x, x, x, marker=\"sphere\", size=10);\n",
" k = ipv.examples.klein_bottle(show=False)\n",
" ipv.xyzlim(2)\n",
" m = ipv.plot_plane('bottom')\n",
" ipv.show()\n",
" return f"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ambient light\n",
"For global illumination, which will not cast shadows"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scene()\n",
"light = ipv.light_ambient(intensity=0.4);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hemisphere light\n",
"A light source positioned directly above the scene, with color fading from the sky color to the ground color.\n",
"This light cannot be used to cast shadows."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scene()\n",
"ipv.light_hemisphere(intensity=1);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Directional light\n",
"A Directional Light source illuminates all objects equally from a given direction. This light can be used to cast shadows. The light is recommended together with hemisphere."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-08-11T12:12:33.761830Z",
"start_time": "2020-08-11T12:12:33.685654Z"
}
},
"outputs": [],
"source": [
"f = scene()\n",
"ipv.light_ambient(intensity=0.4)\n",
"ipv.light_directional(position=[3, 10, 3]);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Spot light\n",
"A Spot Light produces a directed cone of light. The light becomes more intense closer to the spotlight source and to the center of the light cone. This light can be used to cast shadows."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scene()\n",
"ipv.light_hemisphere(intensity=0.2)\n",
"l = ipv.light_spot(position=[0, 3, 2]);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Point light\n",
"A Point Light originates from a single point and spreads outward in all directions. This light can be used to cast shadows."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scene()\n",
"ipv.light_hemisphere(intensity=0.2)\n",
"l = ipv.light_point(position=[0., 1.6, 1.0]);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Animation example"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipyvolume as ipv\n",
"import ipyvolume.datasets\n",
"stream = ipyvolume.datasets.animated_stream.fetch()\n",
"print(\"shape of steam data\", stream.data.shape) # first dimension contains x, y, z, vx, vy, vz, then time, then particle"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = ipv.figure()\n",
"# instead of doing x=stream.data[0], y=stream.data[1], ... vz=stream.data[5], use *stream.data\n",
"# limit to 50 timesteps to avoid having a huge notebook\n",
"ipv.material_physical()\n",
"# q = ipv.quiver(*stream.data[:,0:200:10,:2000:10], color=\"red\", size=7)\n",
"q = ipv.quiver(*stream.data, color=\"red\", size=7)\n",
"ipv.style.use(\"dark\") # looks better\n",
"m = ipv.plot_plane('bottom')\n",
"m = ipv.plot_plane('back')\n",
"m = ipv.plot_plane('left')\n",
"ipv\n",
"l = ipv.light_directional(position=[20, 50, 20], shadow_camera_orthographic_size=1, far=140, near=0.1);\n",
"ipv.animation_control(q, interval=200)\n",
"ipv.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ipv.light_ambient(intensity=0.4);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Volume rendering and lighting\n",
"For volumetric rendering we only support the [Phong lighting model](https://en.wikipedia.org/wiki/Phong_shading). No material needs to be set, only lights need to be setup.\n",
"\n",
"Note that volumetric regions do not cast, not receive shadows."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipyvolume as ipv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# no material and lighting\n",
"ipv.figure()\n",
"volume = ipv.examples.example_ylm(shape=64, show=False)\n",
"s = ipv.scatter(x=32, y=32, z=50, size=30, marker='sphere', color=\"green\");\n",
"\n",
"volume.tf.level1 = 0.24\n",
"volume.tf.opacity1 = 0.1\n",
"volume.brightness = 2\n",
"ipv.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipyvolume as ipv\n",
"f = ipv.figure(debug=True)\n",
"# we only add two lights\n",
"ipv.light_hemisphere(intensity=0.8)\n",
"ipv.light_directional()\n",
"\n",
"volume = ipv.examples.example_ylm(shape=64, show=False)\n",
"ipv.material_phong(specular='white')\n",
"s = ipv.scatter(x=32, y=32, z=50, size=30, marker='sphere', color=\"green\");\n",
"\n",
"volume.tf.level1 = 0.24\n",
"volume.tf.opacity1 = 0.1\n",
"volume.material.specular = 'white'\n",
"volume.brightness = 2\n",
"ipv.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[screenshot](screenshot/volume-rendering-specular.gif)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 14fada6

Please sign in to comment.