Certainly! We can create an interactive 3D cube in Python that you can rotate with your mouse using the VPython library, which is designed for 3D simulations and visualizations. Below is a script that creates a cube of side length 1 unit that you can interact with.
Regarding the shadow simulation, VPython doesn't natively support shadows. However, we can simulate a basic shadow by projecting the cube onto the ground plane directly beneath it as if the light source is shining from directly above.
Here's the complete Python script:
from vpython import *
# Set up the scene
scene = canvas(title='Interactive 3D Cube with Shadow',
width=800, height=600,
center=vector(0,0,0), background=color.white)
scene.lights = []
# Add a distant light source directly above
distant_light(direction=vector(0, -1, 0), color=color.white)
# Create the ground plane
ground = box(pos=vector(0, -0.5, 0), size=vector(5, 0.01, 5), color=color.gray(0.8))
# Create the cube
cube = box(pos=vector(0, 0, 0), size=vector(1, 1, 1), color=color.red)
# Function to update the shadow
def update_shadow():
# Remove existing shadow if any
for obj in scene.objects:
if hasattr(obj, 'is_shadow') and obj.is_shadow:
obj.visible = False
del obj
# Project vertices onto ground to create shadow
vertices = [vertex.pos for vertex in cube.v]
shadow_polygon = []
for v in vertices:
# Projection onto ground plane
shadow_v = vector(v.x, ground.pos.y + 0.01, v.z)
shadow_polygon.append(shadow_v)
# Create the shadow as a quadrilateral (since cube faces are squares)
shadow = quad(vs=shadow_polygon, color=color.gray(0.5), opacity=0.5)
shadow.is_shadow = True # Mark this object as a shadow for easy removal later
# Initial shadow
update_shadow()
# Update the shadow whenever the cube is rotated
def on_rotate(evt):
update_shadow()
scene.bind('mousemove', on_rotate)
Explanation:
-
VPython Setup: We use the vpython library to create 3D objects. The canvas creates a window where we can render the 3D scene.
-
Lighting: Although VPython doesn't support shadows directly, we add a distant_light to simulate light coming from directly above.
-
Ground Plane: We create a ground plane using the box object to represent the surface on which the shadow will be cast.
-
Cube: The cube is created using the box object with a size of 1 unit in all directions.
-
Shadow Simulation:
-
Interactivity: We bind the on_rotate function to mouse movements (mousemove). Whenever the cube is rotated, the shadow updates accordingly.
Running the Script:
-
Install VPython:
Make sure you have VPython installed:
pip install vpython
-
Run the Script:
Save the script to a file named interactive_cube.py and run it:
python interactive_cube.py
-
Interact with the Cube:
- Click and drag with the mouse to rotate the cube.
- The shadow on the ground will update to reflect the cube's orientation.
Note on Shadows:
- The simulated shadow is a basic approximation and works best for simple rotations.
- For more complex scenes or realistic shadows, a more advanced 3D graphics library (such as OpenGL with PyOpenGL, or a game engine like Unity or Unreal Engine) would be necessary.
Dependencies:
- Ensure you have Python 3 installed.
- The
vpython library handles rendering in a browser window using WebGL, so you don't need to manage low-level graphics code.
Additional Enhancements:
- Camera Controls: You can adjust the camera's position and behavior by modifying
scene.camera.
- Lighting Effects: Experiment with different light sources, like point lights or spotlights.
- Textures and Materials: Apply textures to the cube or ground for a more realistic appearance.
Feel free to ask if you need any modifications or further assistance!