|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Subsurface scattering\n", |
| 8 | + "\n", |
| 9 | + "This example shows how to:\n", |
| 10 | + " - setup a glass-like material for subsurface scattering\n", |
| 11 | + " - enable light emmision in the volume\n", |
| 12 | + "\n", |
| 13 | + "\n", |
| 14 | + "\n", |
| 15 | + "Glass-like material shader in PlotOptiX can simulate light propagation in a volume with a diffuse scattering. The free path length of the light is set with ``radiation_length``, and the diffusion color is ``subsurface_color``. You can add some color to the surface with ``surface_albedo`` or enable a light emission in the volume to obtain the finest quality materials." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "markdown", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "Make some data for a simple scene first:" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 1, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "import numpy as np\n", |
| 32 | + "\n", |
| 33 | + "rx = (-20, 20)\n", |
| 34 | + "rz = (-20, 20)\n", |
| 35 | + "n = 100\n", |
| 36 | + "\n", |
| 37 | + "x = np.linspace(rx[0], rx[1], n)\n", |
| 38 | + "z = np.linspace(rz[0], rz[1], n)\n", |
| 39 | + "\n", |
| 40 | + "X, Z = np.meshgrid(x, z)\n", |
| 41 | + "\n", |
| 42 | + "# positions of blocks\n", |
| 43 | + "data = np.stack((X.flatten(), np.full(n*n, -2), Z.flatten())).T\n", |
| 44 | + "# XZ sizes\n", |
| 45 | + "size_u = 0.96 * (rx[1] - rx[0]) / (n - 1)\n", |
| 46 | + "size_w = 0.96 * (rz[1] - rz[0]) / (n - 1)" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "Setup the raytracer using Tkinter GUI as the output target. Note, it is important to select the **background mode** which supports scattering in volumes: ``AmbientAndVolume``, ``TextureFixed``, or ``TextureEnvironment``." |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 2, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "from plotoptix import TkOptiX\n", |
| 63 | + "\n", |
| 64 | + "optix = TkOptiX()\n", |
| 65 | + "optix.set_param(min_accumulation_step=4, # set more accumulation frames to get rid of the noise\n", |
| 66 | + " max_accumulation_frames=512,\n", |
| 67 | + " light_shading=\"Hard\") # use ligth shading best for caustics\n", |
| 68 | + "\n", |
| 69 | + "optix.set_uint(\"path_seg_range\", 15, 40) # more path segments to improve simulation in the volume\n", |
| 70 | + "\n", |
| 71 | + "optix.set_background_mode(\"AmbientAndVolume\") # need one of modes supporting scattering in volumes" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "Only *diffuse* material is available by default. Other materials need to be configured before using." |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": 3, |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [], |
| 86 | + "source": [ |
| 87 | + "from plotoptix.materials import m_clear_glass, m_matt_glass\n", |
| 88 | + "import copy\n", |
| 89 | + "\n", |
| 90 | + "m_clear_glass_2 = copy.deepcopy(m_clear_glass)\n", |
| 91 | + "m_clear_glass_3 = copy.deepcopy(m_clear_glass)\n", |
| 92 | + "m_clear_glass_4 = copy.deepcopy(m_clear_glass)\n", |
| 93 | + "m_clear_glass_5 = copy.deepcopy(m_clear_glass)\n", |
| 94 | + "\n", |
| 95 | + "m_matt_glass_2 = copy.deepcopy(m_matt_glass)\n", |
| 96 | + "\n", |
| 97 | + "m_light_1 = copy.deepcopy(m_clear_glass)\n", |
| 98 | + "m_light_2 = copy.deepcopy(m_clear_glass)\n", |
| 99 | + "\n", |
| 100 | + "optix.setup_material(\"glass\", m_clear_glass) # clear glass\n", |
| 101 | + "optix.setup_material(\"glass_2\", m_clear_glass_2) # diffuse color\n", |
| 102 | + "optix.setup_material(\"glass_3\", m_clear_glass_3) # diffuse and albedo color\n", |
| 103 | + "optix.setup_material(\"glass_4\", m_clear_glass_4) # diffuse slight\n", |
| 104 | + "optix.setup_material(\"glass_5\", m_clear_glass_5) # diffuse slight, textured\n", |
| 105 | + "optix.setup_material(\"matt_glass\", m_matt_glass) # matt surface\n", |
| 106 | + "optix.setup_material(\"matt_glass_2\", m_matt_glass_2) # matt surface, diffuse volume\n", |
| 107 | + "optix.setup_material(\"light_1\", m_light_1) # emissive\n", |
| 108 | + "optix.setup_material(\"light_2\", m_light_2) # emissive, textured" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "markdown", |
| 113 | + "metadata": {}, |
| 114 | + "source": [ |
| 115 | + "Add objects to the scene." |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": 4, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "optix.set_data(\"blocks\", pos=data,\n", |
| 125 | + " c=0.85 + 0.1*np.random.randint(3, size=data.shape[0]),\n", |
| 126 | + " u=[size_u, 0, 0], v=[0, -1, 0], w=[0, 0, size_w],\n", |
| 127 | + " geom=\"Parallelepipeds\")\n", |
| 128 | + "\n", |
| 129 | + "optix.set_data(\"c_clear\", pos=[-3.5, 0, -5], u=[0.25, 0, 0], v=[0, 4, 0], w=[0, 0, 4], c=10, mat=\"glass\", geom=\"Parallelepipeds\")\n", |
| 130 | + "optix.rotate_geometry(\"c_clear\", [0, 0, -np.pi/4])\n", |
| 131 | + "optix.set_data(\"c_diffuse\", pos=[-0.5, 0, -5], u=[0.25, 0, 0], v=[0, 4, 0], w=[0, 0, 4], c=10, mat=\"glass_2\", geom=\"Parallelepipeds\")\n", |
| 132 | + "optix.rotate_geometry(\"c_diffuse\", [0, 0, -np.pi/4])\n", |
| 133 | + "optix.set_data(\"c_light\", pos=[2.5, 0, -5], u=[0.25, 0, 0], v=[0, 4, 0], w=[0, 0, 4], c=10, mat=\"light_1\", geom=\"Parallelepipeds\")\n", |
| 134 | + "optix.rotate_geometry(\"c_light\", [0, 0, -np.pi/4])\n", |
| 135 | + "\n", |
| 136 | + "optix.set_data(\"s_light_2\", pos=[-3.1, 1.5, 1], r=1.5, c=10, mat=\"light_2\", geom=\"ParticleSetTextured\")\n", |
| 137 | + "optix.set_data(\"s_diffuse\", pos=[0, 1.5, 1], r=1.5, c=10, mat=\"glass_2\")\n", |
| 138 | + "optix.set_data(\"s_diffuse_colored\", pos=[3.1, 1.5, 1], r=1.5, c=10, mat=\"glass_3\")\n", |
| 139 | + "\n", |
| 140 | + "optix.set_data(\"s_matt\", pos=[-3.1, 1.5, 4.1], r=1.5, c=10, mat=\"matt_glass\")\n", |
| 141 | + "optix.set_data(\"s_diffuse_less\", pos=[0, 1.5, 4.1], r=1.5, c=10, mat=\"glass_4\")\n", |
| 142 | + "optix.set_data(\"s_diffuse_tex\", pos=[3.1, 1.5, 4.1], r=1.5, c=10, mat=\"glass_5\", geom=\"ParticleSetTextured\")\n", |
| 143 | + "\n", |
| 144 | + "optix.set_data(\"s_matt_colored\", pos=[-3.1, 1.5, 7.2], r=1.5, c=10, mat=\"matt_glass_2\")\n", |
| 145 | + "optix.set_data(\"s_light_1\", pos=[0, 1.5, 7.2], r=1.5, c=10, mat=\"light_1\")\n", |
| 146 | + "optix.set_data(\"s_clear\", pos=[3.1, 1.5, 7.2], r=1.5, c=10, mat=\"glass\")" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "Setup a good point of view, set background and lights." |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": 5, |
| 159 | + "metadata": {}, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "optix.setup_camera(\"cam1\", cam_type=\"DoF\",\n", |
| 163 | + " eye=[0, 15, 1.55], target=[0, 0, 1.55], up=[1, 0, 0],\n", |
| 164 | + " aperture_radius=0.01, fov=45, focal_scale=0.8)\n", |
| 165 | + "\n", |
| 166 | + "optix.setup_light(\"light1\", pos=[5, 8, 7], color=[10, 10, 10], radius=1.9)\n", |
| 167 | + "optix.setup_light(\"light2\", pos=[-6, 8, -5], color=[10, 11, 12], radius=1.3)\n", |
| 168 | + "\n", |
| 169 | + "exposure = 1.1; gamma = 2.2 \n", |
| 170 | + "optix.set_float(\"tonemap_exposure\", exposure)\n", |
| 171 | + "optix.set_float(\"tonemap_gamma\", gamma)\n", |
| 172 | + "optix.set_float(\"denoiser_blend\", 0.2)\n", |
| 173 | + "optix.add_postproc(\"Denoiser\") # apply AI denoiser, or\n", |
| 174 | + "#optix.add_postproc(\"Gamma\") # use gamma correction\n", |
| 175 | + "\n", |
| 176 | + "optix.set_background(0)\n", |
| 177 | + "optix.set_ambient(0)" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "Open the GUI." |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": 6, |
| 190 | + "metadata": { |
| 191 | + "scrolled": true |
| 192 | + }, |
| 193 | + "outputs": [], |
| 194 | + "source": [ |
| 195 | + "optix.start()" |
| 196 | + ] |
| 197 | + }, |
| 198 | + { |
| 199 | + "cell_type": "markdown", |
| 200 | + "metadata": {}, |
| 201 | + "source": [ |
| 202 | + "Modify materials:" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": 8, |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [], |
| 210 | + "source": [ |
| 211 | + "m_clear_glass_2[\"VarFloat\"][\"radiation_length\"] = 0.1 # short w.r.t. the object size\n", |
| 212 | + "m_clear_glass_2[\"VarFloat3\"][\"subsurface_color\"] = [ 0.7, 0.85, 1 ]\n", |
| 213 | + "optix.update_material(\"glass_2\", m_clear_glass_2)\n", |
| 214 | + "\n", |
| 215 | + "m_clear_glass_3[\"VarFloat\"][\"radiation_length\"] = 0.1\n", |
| 216 | + "m_clear_glass_3[\"VarFloat3\"][\"subsurface_color\"] = [ 0.7, 0.85, 1 ]\n", |
| 217 | + "m_clear_glass_3[\"VarFloat3\"][\"surface_albedo\"] = [ 0.6, 0.8, 1 ] # add some color to reflections\n", |
| 218 | + "optix.update_material(\"glass_3\", m_clear_glass_3)\n", |
| 219 | + "\n", |
| 220 | + "m_clear_glass_4[\"VarFloat\"][\"radiation_length\"] = 1.0 # comparable to the object size\n", |
| 221 | + "m_clear_glass_4[\"VarFloat3\"][\"subsurface_color\"] = [ 1, 0.85, 0.7 ]\n", |
| 222 | + "optix.update_material(\"glass_4\", m_clear_glass_4)\n", |
| 223 | + "\n", |
| 224 | + "optix.load_texture(\"rainbow\", r\"data/rainbow.jpg\")\n", |
| 225 | + "m_clear_glass_5[\"VarFloat\"][\"radiation_length\"] = 1.0\n", |
| 226 | + "m_clear_glass_5[\"VarFloat3\"][\"subsurface_color\"] = [ 1, 0.85, 0.7 ]\n", |
| 227 | + "m_clear_glass_5[\"VarFloat3\"][\"surface_albedo\"] = [ 0.9, 1, 1 ]\n", |
| 228 | + "m_clear_glass_5[\"ColorTextures\"] = [ \"rainbow\" ]\n", |
| 229 | + "optix.update_material(\"glass_5\", m_clear_glass_5)\n", |
| 230 | + "\n", |
| 231 | + "m_matt_glass_2[\"VarFloat\"][\"radiation_length\"] = 1.0\n", |
| 232 | + "m_matt_glass_2[\"VarFloat3\"][\"subsurface_color\"] = [ 1, 0.8, 1 ]\n", |
| 233 | + "optix.update_material(\"matt_glass_2\", m_matt_glass_2)\n", |
| 234 | + "\n", |
| 235 | + "m_light_1[\"VarFloat\"][\"radiation_length\"] = 1.5\n", |
| 236 | + "m_light_1[\"VarFloat\"][\"light_emission\"] = 0.1 # add light on each scattering\n", |
| 237 | + "m_light_1[\"VarFloat3\"][\"subsurface_color\"] = [ 0.9, 1, 1 ] # diffuse and emission base color\n", |
| 238 | + "optix.update_material(\"light_1\", m_light_1)\n", |
| 239 | + "\n", |
| 240 | + "optix.load_texture(\"wood\", r\"data/wood.jpg\")\n", |
| 241 | + "m_light_2[\"VarFloat\"][\"radiation_length\"] = 1.5\n", |
| 242 | + "m_light_2[\"VarFloat\"][\"light_emission\"] = 0.1\n", |
| 243 | + "m_light_2[\"VarFloat3\"][\"subsurface_color\"] = [ 1, 1, 1 ] # leave the (default) neutral color\n", |
| 244 | + "m_light_2[\"ColorTextures\"] = [ \"wood\" ]\n", |
| 245 | + "optix.update_material(\"light_2\", m_light_2, refresh=True)" |
| 246 | + ] |
| 247 | + }, |
| 248 | + { |
| 249 | + "cell_type": "markdown", |
| 250 | + "metadata": {}, |
| 251 | + "source": [ |
| 252 | + "Close GUI window, release resources." |
| 253 | + ] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "code", |
| 257 | + "execution_count": 9, |
| 258 | + "metadata": {}, |
| 259 | + "outputs": [], |
| 260 | + "source": [ |
| 261 | + "optix.close()" |
| 262 | + ] |
| 263 | + } |
| 264 | + ], |
| 265 | + "metadata": { |
| 266 | + "kernelspec": { |
| 267 | + "display_name": "Python 3", |
| 268 | + "language": "python", |
| 269 | + "name": "python3" |
| 270 | + }, |
| 271 | + "language_info": { |
| 272 | + "codemirror_mode": { |
| 273 | + "name": "ipython", |
| 274 | + "version": 3 |
| 275 | + }, |
| 276 | + "file_extension": ".py", |
| 277 | + "mimetype": "text/x-python", |
| 278 | + "name": "python", |
| 279 | + "nbconvert_exporter": "python", |
| 280 | + "pygments_lexer": "ipython3", |
| 281 | + "version": "3.7.4" |
| 282 | + } |
| 283 | + }, |
| 284 | + "nbformat": 4, |
| 285 | + "nbformat_minor": 2 |
| 286 | +} |
0 commit comments