From ef20eaf7c9ec881ef468587c4bf982ead36a612f Mon Sep 17 00:00:00 2001 From: Tim Fong 2 Date: Fri, 11 Aug 2023 23:41:31 +0700 Subject: [PATCH] added models waving cubes example --- examples/models/models_waving_cubes.py | 79 ++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 examples/models/models_waving_cubes.py diff --git a/examples/models/models_waving_cubes.py b/examples/models/models_waving_cubes.py new file mode 100644 index 0000000..a5b803a --- /dev/null +++ b/examples/models/models_waving_cubes.py @@ -0,0 +1,79 @@ +from pyray import * +import pyray +import math + +# Program main entry point +# Initialization +screenWidth = 800 +screenHeight = 450 + +init_window(screenWidth, screenHeight, "raylib [models] example - waving cubes") + +# Initialize the camera +camera = Camera3D() +camera.position = Vector3(30.0, 20.0, 30.0) # Camera position +camera.target = Vector3(0.0, 0.0, 0.0) # Camera looking at point +camera.up = Vector3(0.0, 1.0, 0.0) # Camera up vector (rotation towards target) +camera.fovy = 70.0 # Camera field-of-view Y +camera.projection = pyray.CAMERA_PERSPECTIVE # Camera projection type + +# Specify the amount of blocks in each direction +numBlocks = 15 + +set_target_fps(60) + +# Main game loop +while not window_should_close(): # Detect window close button or ESC key + # Update + time = get_time() + + # Calculate time scale for cube position and size + scale = (2.0 + math.sin(time)) * 0.7 + + # Move camera around the scene + cameraTime = time * 0.3 + camera.position.x = math.cos(cameraTime) * 40.0 + camera.position.z = math.sin(cameraTime) * 40.0 + + # Draw + begin_drawing() + + clear_background(RAYWHITE) + + begin_mode_3d(camera) + + draw_grid(10, 5.0) + + for x in range(numBlocks): + for y in range(numBlocks): + for z in range(numBlocks): + # Scale of the blocks depends on x/y/z positions + blockScale = (x + y + z) / 30.0 + + # Scatter makes the waving effect by adding blockScale over time + scatter = math.sin(blockScale * 20.0 + time * 4.0) + + # Calculate the cube position + cubePos = Vector3( + (x - numBlocks / 2) * (scale * 3.0) + scatter, + (y - numBlocks / 2) * (scale * 2.0) + scatter, + (z - numBlocks / 2) * (scale * 3.0) + scatter, + ) + + # Pick a color with a hue depending on cube position for the rainbow color effect + cubeColor = color_from_hsv(((x + y + z) * 18) % 360, 0.75, 0.9) + + # Calculate cube size + cubeSize = (2.4 - scale) * blockScale + + # And finally, draw the cube! + draw_cube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor) + + end_mode_3d() + + draw_fps(10, 10) + + end_drawing() + +# De-Initialization +close_window() # Close window and OpenGL context