Created
April 20, 2023 10:20
-
-
Save almarklein/95af9966481f62848c629794266f7603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wgpu.gui.auto import WgpuCanvas, run | |
import pygfx as gfx | |
import pylinalg as la | |
import numpy as np | |
def set_position(ob, pos): | |
# compat between old and new api | |
if hasattr(ob, "local"): | |
ob.local.position = pos | |
else: | |
ob.position.set(*pos) | |
renderer = gfx.WgpuRenderer(WgpuCanvas()) | |
scene = gfx.Scene() | |
# Box in which the scene is contained | |
box = gfx.Mesh( | |
gfx.box_geometry(600, 600, 600), | |
gfx.MeshPhongMaterial(color="#808080", side="Back"), | |
) | |
box.receive_shadow = True | |
box.cast_shadow = False | |
scene.add(box) | |
# A cube that will cast the shadows | |
cube = gfx.Mesh( | |
gfx.box_geometry(50, 50, 50), | |
gfx.MeshPhongMaterial(), | |
) | |
cube.cast_shadow = True | |
cube.receive_shadow = True | |
scene.add(cube) | |
# Add some more cubes to make the scene asymetric | |
for i in range(3): | |
s = 5 + 5 * i | |
sub_cube = gfx.Mesh(gfx.box_geometry(s, s, s), gfx.MeshPhongMaterial()) | |
sub_cube.cast_shadow = True | |
sub_cube.receive_shadow = True | |
subpos = [0, 0, 0] | |
subpos[i] += 50 | |
set_position(sub_cube, subpos) | |
cube.add(sub_cube) | |
# Create a point light | |
light = gfx.PointLight("#9999ff", 500000, decay=2) | |
light.cast_shadow = True | |
# Camera and controller | |
camera = gfx.PerspectiveCamera(90) | |
camera.show_object(cube, scale=2) | |
controller = gfx.OrbitController(camera, register_events=renderer) | |
# We attach the light to the camea, with a small offset (otherwise the objects will simply obscure the shadow) | |
scene.add(camera.add(light.add(gfx.PointLightHelper(5)))) | |
set_position(light, (10, 30, -30)) | |
renderer.request_draw(lambda: renderer.render(scene, camera)) | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment