{ "cells": [ { "cell_type": "raw", "id": "01e4e4d3", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _example_raycast:\n", "\n", "Raycasting a scene with OBJ models\n", "=======================================\n", "\n", "This example demonstrates how to render a scene with OBJ models using raycasting." ] }, { "cell_type": "code", "execution_count": 1, "id": "5d720104", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:13.203367Z", "iopub.status.busy": "2021-10-22T11:24:13.202689Z", "iopub.status.idle": "2021-10-22T11:24:13.297455Z", "shell.execute_reply": "2021-10-22T11:24:13.298317Z" } }, "outputs": [], "source": [ "import lmenv\n", "env = lmenv.load('.lmenv')" ] }, { "cell_type": "code", "execution_count": 2, "id": "c396924b", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:13.302243Z", "iopub.status.busy": "2021-10-22T11:24:13.301153Z", "iopub.status.idle": "2021-10-22T11:24:13.511162Z", "shell.execute_reply": "2021-10-22T11:24:13.510661Z" } }, "outputs": [], "source": [ "import os\n", "import numpy as np\n", "import imageio\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import lightmetrica as lm\n", "%load_ext lightmetrica_jupyter" ] }, { "cell_type": "code", "execution_count": 3, "id": "a7171914", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:13.514939Z", "iopub.status.busy": "2021-10-22T11:24:13.514469Z", "iopub.status.idle": "2021-10-22T11:24:13.517654Z", "shell.execute_reply": "2021-10-22T11:24:13.517272Z" }, "nbsphinx": "hidden" }, "outputs": [], "source": [ "if not lm.Release:\n", " lm.debug.attach_to_debugger()" ] }, { "cell_type": "code", "execution_count": 4, "id": "76f44008", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:13.523224Z", "iopub.status.busy": "2021-10-22T11:24:13.522006Z", "iopub.status.idle": "2021-10-22T11:24:13.525276Z", "shell.execute_reply": "2021-10-22T11:24:13.524874Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.000] Lightmetrica -- Version 3.0.0 (rev. 70601db) Linux x64\n" ] } ], "source": [ "lm.init()\n", "lm.log.init('jupyter')\n", "lm.progress.init('jupyter')\n", "lm.info()" ] }, { "cell_type": "raw", "id": "9fff84e6", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Following is the definition of assets. To load an OBJ model, we can use ``model::wavefrontobj`` asset. This asset internally creates meshes and materials by reading the associated MTL file.\n", "\n", ".. note::\n", " A model asset can be considered as a special asset containing (a part of) the scene graph and assets reference by the structure. " ] }, { "cell_type": "code", "execution_count": 5, "id": "8ca97931", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:14.116057Z", "iopub.status.busy": "2021-10-22T11:24:14.063855Z", "iopub.status.idle": "2021-10-22T11:24:14.118865Z", "shell.execute_reply": "2021-10-22T11:24:14.119270Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.009] Loading asset [name='film']\n", "[I|0.086] Loading asset [name='camera']\n", "[I|0.087] Loading asset [name='model']\n", "[I|0.087] .. Loading OBJ file [path='fireplace_room.obj']\n", "[I|0.087] .. Loading MTL file [path='fireplace_room.mtl']\n", "[I|0.417] .. Loading texture [path='wood.ppm']\n", "[I|0.508] .. Loading texture [path='leaf.ppm']\n", "[I|0.511] .. Loading texture [path='picture8.ppm']\n", "[I|0.543] .. Loading texture [path='wood5.ppm']\n" ] } ], "source": [ "# Film for the rendered image\n", "film = lm.load_film('film', 'bitmap', w=1920, h=1080)\n", "\n", "# Pinhole camera\n", "camera = lm.load_camera('camera', 'pinhole',\n", " position=[5.101118, 1.083746, -2.756308],\n", " center=[4.167568, 1.078925, -2.397892],\n", " up=[0,1,0],\n", " vfov=43.001194,\n", " aspect=16/9)\n", "\n", "# OBJ model\n", "model = lm.load_model('model', 'wavefrontobj',\n", " path=os.path.join(env.scene_path, 'fireplace_room/fireplace_room.obj'))" ] }, { "cell_type": "raw", "id": "45d33282", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "We can create primitives from the loaded mode using ``model` parameter for the :cpp:func:`lm::Scene::add_primitive` function." ] }, { "cell_type": "code", "execution_count": 6, "id": "10272f46", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:14.128037Z", "iopub.status.busy": "2021-10-22T11:24:14.127083Z", "iopub.status.idle": "2021-10-22T11:24:14.780998Z", "shell.execute_reply": "2021-10-22T11:24:14.780590Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.602] Loading asset [name='accel']\n", "[I|0.603] Loading asset [name='scene']\n", "[I|0.606] Building acceleration structure [name='accel']\n", "[I|0.607] .. Flattening scene\n", "[I|0.635] .. Building\n" ] } ], "source": [ "accel = lm.load_accel('accel', 'sahbvh')\n", "scene = lm.load_scene('scene', 'default', accel=accel)\n", "scene.add_primitive(camera=camera)\n", "scene.add_primitive(model=model)\n", "scene.build()" ] }, { "cell_type": "raw", "id": "33add500", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Executing the renderer will produce the following image." ] }, { "cell_type": "code", "execution_count": 7, "id": "6490cafc", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:14.785519Z", "iopub.status.busy": "2021-10-22T11:24:14.784927Z", "iopub.status.idle": "2021-10-22T11:24:17.602891Z", "shell.execute_reply": "2021-10-22T11:24:17.603280Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|1.264] Loading asset [name='renderer']\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2dfe2705123645ce98d0235aa96b692b", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2073600 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "img = np.copy(film.buffer())\n", "f = plt.figure(figsize=(15,15))\n", "ax = f.add_subplot(111)\n", "ax.imshow(np.clip(np.power(img,1/2.2),0,1), origin='lower')\n", "plt.show()" ] } ], "metadata": { "jupytext": { "cell_metadata_json": true, "formats": "ipynb,py:light", "text_representation": { "extension": ".py", "format_name": "light", "format_version": "1.5", "jupytext_version": "1.3.3" } }, "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.7.10" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "011e380f55d0448fa327eee990464217": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "0299067579bb4e5d801423aa02148ec6": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "03a756c55c6f4e10bfc3ef08358004c0": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "16d79dcdd5494bbd80a48a7423b03ce5": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "2dfe2705123645ce98d0235aa96b692b": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HBoxModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_500134ba16914b0aa7643d4c1f4b3239", "IPY_MODEL_41963a2d43cd4f7ca7ead53228b77632", "IPY_MODEL_d4e33875b56f49d4a8095b8fa6f44c1a" ], "layout": "IPY_MODEL_03a756c55c6f4e10bfc3ef08358004c0" } }, "41963a2d43cd4f7ca7ead53228b77632": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "FloatProgressModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_16d79dcdd5494bbd80a48a7423b03ce5", "max": 2073600.0, "min": 0.0, "orientation": "horizontal", "style": "IPY_MODEL_dc397ad6939e408da05d90711bcbde39", "value": 2073600.0 } }, "47ce2ac1c80445029f0af2978da980a8": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "500134ba16914b0aa7643d4c1f4b3239": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HTMLModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_011e380f55d0448fa327eee990464217", "placeholder": "​", "style": "IPY_MODEL_b9dc5a54e5f441989afea87eebc5bfce", "value": "100%" } }, "b9dc5a54e5f441989afea87eebc5bfce": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "d4e33875b56f49d4a8095b8fa6f44c1a": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HTMLModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_0299067579bb4e5d801423aa02148ec6", "placeholder": "​", "style": "IPY_MODEL_47ce2ac1c80445029f0af2978da980a8", "value": " 2073600/2073600 [00:02<00:00, 843680.36it/s]" } }, "dc397ad6939e408da05d90711bcbde39": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "ProgressStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }