Skip to content

Instantly share code, notes, and snippets.

@aw-junaid
Created March 21, 2025 01:56
Show Gist options
  • Select an option

  • Save aw-junaid/69e768d63f6e0eba9622a60ab0dfc67d to your computer and use it in GitHub Desktop.

Select an option

Save aw-junaid/69e768d63f6e0eba9622a60ab0dfc67d to your computer and use it in GitHub Desktop.
3D model Sunlight Movement
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"private_outputs": true,
"provenance": [],
"authorship_tag": "ABX9TyOIMbWspBLGd3OPNDX7nfTx",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/aw-junaid/69e768d63f6e0eba9622a60ab0dfc67d/3d-model-sunlight-movement.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8n9M3IB9Ybh3"
},
"outputs": [],
"source": [
"# Install necessary libraries\n",
"!pip install numpy matplotlib\n",
"\n",
"# Import libraries\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.animation import FuncAnimation\n",
"from IPython.display import HTML\n",
"\n",
"# Set up the figure and 3D axis\n",
"fig = plt.figure(figsize=(10, 8))\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"\n",
"# Define the cave and open space\n",
"cave_width = 5\n",
"cave_depth = 10\n",
"open_space_length = 20\n",
"\n",
"# Create the cave (a box)\n",
"cave_x = np.array([0, 0, cave_width, cave_width, 0, 0, cave_width, cave_width])\n",
"cave_y = np.array([0, cave_depth, cave_depth, 0, 0, cave_depth, cave_depth, 0])\n",
"cave_z = np.array([0, 0, 0, 0, 1, 1, 1, 1]) # Cave height\n",
"\n",
"# Create the open space (a flat plane)\n",
"ground_x, ground_y = np.meshgrid(np.linspace(-open_space_length, open_space_length, 50),\n",
" np.linspace(-open_space_length, open_space_length, 50))\n",
"ground_z = np.zeros_like(ground_x)\n",
"\n",
"# Plot the cave and open space\n",
"ax.plot_surface(ground_x, ground_y, ground_z, color='lightgreen', alpha=0.5) # Ground\n",
"ax.plot(cave_x, cave_y, cave_z, color='brown', label='Cave') # Cave\n",
"\n",
"# Sun position parameters\n",
"sun_radius = 15\n",
"sun_height = 20\n",
"\n",
"# Function to update the sun's position and shadows\n",
"def update(frame):\n",
" ax.clear()\n",
" ax.set_xlim(-open_space_length, open_space_length)\n",
" ax.set_ylim(-open_space_length, open_space_length)\n",
" ax.set_zlim(0, sun_height + 5)\n",
" ax.set_xlabel('X')\n",
" ax.set_ylabel('Y')\n",
" ax.set_zlabel('Z')\n",
" ax.set_title(f'Sun Movement (Frame {frame})')\n",
"\n",
" # Replot the cave and open space\n",
" ax.plot_surface(ground_x, ground_y, ground_z, color='lightgreen', alpha=0.5)\n",
" ax.plot(cave_x, cave_y, cave_z, color='brown', label='Cave')\n",
"\n",
" # Calculate the sun's position (moving from right to left)\n",
" sun_x = sun_radius * np.cos(np.radians(frame))\n",
" sun_y = sun_radius * np.sin(np.radians(frame))\n",
" sun_z = sun_height\n",
"\n",
" # Plot the sun\n",
" ax.scatter(sun_x, sun_y, sun_z, color='yellow', s=200, label='Sun')\n",
"\n",
" # Draw sunlight rays (from sun to cave and open space)\n",
" for x in np.linspace(-open_space_length, open_space_length, 10):\n",
" for y in np.linspace(-open_space_length, open_space_length, 10):\n",
" ax.plot([sun_x, x], [sun_y, y], [sun_z, 0], color='orange', alpha=0.2)\n",
"\n",
" # Highlight shaded area (behind the cave)\n",
" if sun_x > 0: # Sun is on the right\n",
" shade_x = np.linspace(-open_space_length, 0, 10)\n",
" shade_y = np.linspace(-open_space_length, open_space_length, 10)\n",
" shade_x, shade_y = np.meshgrid(shade_x, shade_y)\n",
" shade_z = np.zeros_like(shade_x)\n",
" ax.plot_surface(shade_x, shade_y, shade_z, color='gray', alpha=0.3)\n",
"\n",
"# Animate the sun's movement\n",
"frames = np.arange(0, 360, 5) # Sun moves from 0 to 360 degrees\n",
"ani = FuncAnimation(fig, update, frames=frames, interval=200)\n",
"\n",
"# Display the animation in Colab\n",
"HTML(ani.to_jshtml())"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment