update examples
This commit is contained in:
parent
64e5995633
commit
aa5659f272
10 changed files with 6 additions and 8 deletions
136
examples/extra/camera.py
Normal file
136
examples/extra/camera.py
Normal file
|
@ -0,0 +1,136 @@
|
|||
from math import sin, cos
|
||||
import glm
|
||||
from raylib.dynamic import raylib as rl, ffi
|
||||
|
||||
|
||||
class CameraFly:
|
||||
def __init__(self, x=0, y=0, z=0, fov=70.0):
|
||||
self.last_mouse = ffi.new("struct Vector2 *", [0, 0])
|
||||
self.fov = fov
|
||||
self.yaw = -46.33
|
||||
self.pitch = 0.0
|
||||
self.front = glm.vec3()
|
||||
self.up = glm.vec3()
|
||||
self.right = glm.vec3()
|
||||
self.position = glm.vec3(x, y, z)
|
||||
self.world_up = glm.vec3(0, 1, 0)
|
||||
self.fps_facing = glm.vec3()
|
||||
self.movement_speed = 10
|
||||
self.mouse_sensitivity = 0.002
|
||||
self.last_time = rl.GetFrameTime()
|
||||
|
||||
def get_camera(self):
|
||||
return ffi.new(
|
||||
"struct Camera3D *",
|
||||
[
|
||||
self.get_ray_front(),
|
||||
self.get_ray_position(),
|
||||
self.get_ray_up(),
|
||||
self.fov,
|
||||
rl.CAMERA_PERSPECTIVE
|
||||
]
|
||||
)
|
||||
|
||||
def get_ray_front(self):
|
||||
return list(self.position + self.front)
|
||||
|
||||
def get_ray_up(self):
|
||||
return list(self.up)
|
||||
|
||||
def get_ray_position(self):
|
||||
return list(self.position)
|
||||
|
||||
def update_keyboard(self):
|
||||
boost = 4 if rl.IsKeyDown(rl.KEY_LEFT_SHIFT) else 1
|
||||
velocity = self.movement_speed * boost * rl.GetFrameTime()
|
||||
|
||||
if rl.IsKeyDown(rl.KEY_W):
|
||||
self.position = self.position - self.fps_facing * velocity
|
||||
|
||||
if rl.IsKeyDown(rl.KEY_S):
|
||||
self.position = self.position + self.fps_facing * velocity
|
||||
|
||||
if rl.IsKeyDown(rl.KEY_D):
|
||||
dirr = glm.normalize(glm.cross(self.fps_facing, self.world_up))
|
||||
self.position = self.position - dirr * velocity
|
||||
|
||||
if rl.IsKeyDown(rl.KEY_A):
|
||||
dirr = glm.normalize(glm.cross(self.fps_facing, self.world_up))
|
||||
self.position = self.position + dirr * velocity
|
||||
|
||||
if rl.IsKeyDown(rl.KEY_SPACE):
|
||||
self.position = self.position + self.world_up * velocity
|
||||
|
||||
if rl.IsKeyDown(rl.KEY_LEFT_CONTROL) or rl.IsKeyDown(rl.KEY_RIGHT_CONTROL):
|
||||
self.position = self.position - self.world_up * velocity
|
||||
|
||||
def update_mouse(self):
|
||||
pos = rl.GetMousePosition()
|
||||
width = rl.GetScreenWidth()
|
||||
height = rl.GetScreenHeight()
|
||||
|
||||
mouse_coordinates = ffi.new("struct Vector2 *", [
|
||||
pos.x - self.last_mouse.x,
|
||||
pos.y - self.last_mouse.y
|
||||
])
|
||||
|
||||
self.yaw += mouse_coordinates.x * self.mouse_sensitivity
|
||||
self.pitch += (mouse_coordinates.y * self.mouse_sensitivity)
|
||||
self.pitch = min(self.pitch, 1.5)
|
||||
self.pitch = max(self.pitch, -1.5)
|
||||
self.last_mouse = rl.GetMousePosition()
|
||||
|
||||
def update(self):
|
||||
# NOTE(pebaz): Comment out these lines to control them in a game loop
|
||||
self.update_keyboard()
|
||||
self.update_mouse()
|
||||
|
||||
# Update all camera vectors to reflect changes
|
||||
self.fps_facing = glm.vec3(
|
||||
cos((self.yaw)) * cos((0)),
|
||||
sin((0)),
|
||||
sin((self.yaw)) * cos((0))
|
||||
)
|
||||
self.fps_facing = glm.normalize(self.fps_facing)
|
||||
|
||||
front = glm.vec3(
|
||||
cos((self.yaw)) * cos((self.pitch)),
|
||||
sin((self.pitch)),
|
||||
sin((self.yaw)) * cos((self.pitch))
|
||||
)
|
||||
|
||||
self.front = glm.normalize(front)
|
||||
self.right = glm.normalize(glm.cross(self.front, self.world_up))
|
||||
self.up = glm.normalize(glm.cross(self.right, self.front))
|
||||
|
||||
|
||||
def camera_test():
|
||||
rl.SetTraceLogLevel(rl.LOG_ERROR)
|
||||
rl.SetConfigFlags(rl.FLAG_WINDOW_RESIZABLE)
|
||||
rl.InitWindow(512, 256, b'Test')
|
||||
rl.SetTargetFPS(60)
|
||||
rl.DisableCursor()
|
||||
|
||||
flycam = CameraFly()
|
||||
|
||||
|
||||
while not rl.WindowShouldClose():
|
||||
flycam.update()
|
||||
cam = flycam.get_camera()
|
||||
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground((0, 200, 255, 255))
|
||||
rl.BeginMode3D(cam[0])
|
||||
|
||||
# NOTE(pebaz): For whatever reason, this can solve a percentage of artifacts
|
||||
rl.DrawGizmo([100000000, 100000000, 100000000])
|
||||
|
||||
rl.DrawGrid(32, 1)
|
||||
|
||||
rl.EndMode3D()
|
||||
rl.EndDrawing()
|
||||
|
||||
rl.CloseWindow()
|
||||
|
||||
if __name__ == '__main__':
|
||||
camera_test()
|
1
examples/extra/camera.py.requirements.txt
Normal file
1
examples/extra/camera.py.requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
pyglm
|
140
examples/extra/flow-field.py
Normal file
140
examples/extra/flow-field.py
Normal file
|
@ -0,0 +1,140 @@
|
|||
"""
|
||||
RenderTexture example
|
||||
|
||||
Run with:
|
||||
|
||||
python3 flow-field
|
||||
flow-field bees
|
||||
"""
|
||||
|
||||
import sys, math, time, random
|
||||
import glm
|
||||
from raylib.dynamic import raylib as rl, ffi
|
||||
from raylib.colors import *
|
||||
|
||||
CTM = lambda: round(time.time() * 1000)
|
||||
|
||||
|
||||
|
||||
BEES = bool(sys.argv[1:])
|
||||
|
||||
|
||||
|
||||
|
||||
rl.SetTraceLogLevel(rl.LOG_ERROR)
|
||||
rl.SetConfigFlags(rl.FLAG_WINDOW_RESIZABLE)
|
||||
rl.InitWindow(512, 512, b'Friendly Bees')
|
||||
rl.SetTargetFPS(60)
|
||||
#rl.DisableCursor()
|
||||
|
||||
canvas = rl.LoadRenderTexture(rl.GetScreenWidth(), rl.GetScreenHeight())
|
||||
rl.SetTextureWrap(canvas.texture, rl.WRAP_MIRROR_REPEAT)
|
||||
|
||||
def random_point_in_circle(center, radius):
|
||||
a = random.random() * 2 * math.pi
|
||||
r = radius * math.sqrt(random.random())
|
||||
x = r * math.cos(a)
|
||||
z = r * math.sin(a)
|
||||
return glm.vec3(
|
||||
x + center.x,
|
||||
center.y,
|
||||
z + center.z
|
||||
)
|
||||
|
||||
class Particle:
|
||||
def __init__(self, pos):
|
||||
self.pos = pos
|
||||
self.scl = 16 if BEES else 2
|
||||
self.ang = math.radians(random.randint(0, 359))
|
||||
self.spd = random.randint(4, 10)
|
||||
self.start = CTM()
|
||||
self.clr = [255, 200, 0, 155] if BEES else [*(random.randint(55, 255) for i in range(3)), 55]
|
||||
self.mem = (0, 0)
|
||||
self.pre = glm.vec2(pos)
|
||||
|
||||
def process(self):
|
||||
if CTM() > self.start + 50:
|
||||
self.ang = math.radians(glm.simplex(self.pos) * 360.0)
|
||||
self.start = CTM()
|
||||
|
||||
self.rot = glm.vec2(
|
||||
glm.vec4(1) * glm.rotate(glm.mat4(), self.ang, [0, 0, 1])
|
||||
)
|
||||
self.pre = int(self.pos.x), int(self.pos.y)
|
||||
self.pos += self.rot * rl.GetFrameTime() * 64
|
||||
rl.DrawLine(
|
||||
int(self.pos.x), int(self.pos.y), *self.pre, self.clr
|
||||
)
|
||||
|
||||
if self.pos.x < 0: self.pos.x = rl.GetScreenWidth() - 1
|
||||
if self.pos.y < 0: self.pos.y = rl.GetScreenHeight() - 1
|
||||
if self.pos.x >= rl.GetScreenWidth(): self.pos.x = 0
|
||||
if self.pos.y >= rl.GetScreenHeight(): self.pos.y = 0
|
||||
|
||||
hlf = self.scl // 2
|
||||
self.mem = int(self.pos.x) - hlf, int(self.pos.y) - hlf
|
||||
rl.DrawRectangle(
|
||||
*self.mem,
|
||||
self.scl if not BEES else hlf,
|
||||
self.scl,
|
||||
self.clr
|
||||
)
|
||||
if BEES:
|
||||
rl.DrawRectangle(
|
||||
self.mem[0] + hlf, self.mem[1],
|
||||
hlf,
|
||||
self.scl,
|
||||
[0, 0, 0, 55]
|
||||
)
|
||||
|
||||
dims = rl.GetScreenWidth(), rl.GetScreenHeight()
|
||||
ini = False
|
||||
parts = [
|
||||
Particle(glm.vec2(random_point_in_circle(
|
||||
glm.vec3(
|
||||
rl.GetScreenWidth() / 2,
|
||||
rl.GetScreenHeight() / 2,
|
||||
0
|
||||
),
|
||||
64
|
||||
)))
|
||||
for i in range(125)
|
||||
]
|
||||
while not rl.WindowShouldClose():
|
||||
if (rl.GetScreenWidth(), rl.GetScreenHeight()) != dims:
|
||||
canvas = rl.LoadRenderTexture(rl.GetScreenWidth(), rl.GetScreenHeight())
|
||||
rl.SetTextureWrap(canvas.texture, rl.WRAP_MIRROR_REPEAT)
|
||||
ini = False
|
||||
dims = rl.GetScreenWidth(), rl.GetScreenHeight()
|
||||
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.BeginTextureMode(canvas)
|
||||
if not ini:
|
||||
rl.ClearBackground([200, 200, 0, 255] if BEES else WHITE)
|
||||
if BEES:
|
||||
rl.DrawRectangle(
|
||||
0, 0,
|
||||
dims[0], dims[1] // 2,
|
||||
(0, 200, 255, 255)
|
||||
)
|
||||
rl.DrawCircle(
|
||||
dims[0] - 72, 72, 32, [255, 200, 0, 255]
|
||||
)
|
||||
else:
|
||||
ini = True
|
||||
|
||||
for part in parts: part.process()
|
||||
|
||||
rl.EndTextureMode()
|
||||
|
||||
tex = canvas.texture
|
||||
rl.DrawTexturePro(
|
||||
tex, [0.0, 0.0, tex.width, -tex.height],
|
||||
[0, 0, tex.width, tex.height],
|
||||
[0, 0], 0.0, WHITE
|
||||
)
|
||||
|
||||
rl.EndDrawing()
|
||||
|
||||
rl.CloseWindow()
|
1
examples/extra/flow-field.py.requirements.txt
Normal file
1
examples/extra/flow-field.py.requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
pyglm
|
203
examples/extra/transparent_undecorated_window.py
Normal file
203
examples/extra/transparent_undecorated_window.py
Normal file
|
@ -0,0 +1,203 @@
|
|||
"""
|
||||
Requirements:
|
||||
- raylib
|
||||
- pytweening
|
||||
- pyglm
|
||||
|
||||
Windows-Only Requirements:
|
||||
- win32gui
|
||||
- win32con
|
||||
- pywintypes
|
||||
|
||||
Mac:
|
||||
- pyobjus
|
||||
- Cython
|
||||
"""
|
||||
|
||||
import sys, time
|
||||
import glm
|
||||
import pytweening as tween
|
||||
import screeninfo
|
||||
from raylib.dynamic import raylib as rl, ffi
|
||||
from raylib.colors import *
|
||||
|
||||
|
||||
Vec2 = lambda p: glm.vec2(p.x, p.y)
|
||||
CTM = lambda: round(time.perf_counter() * 1000)
|
||||
|
||||
monitors = screeninfo.get_monitors()
|
||||
drag = False
|
||||
offset = Vec2(rl.GetMousePosition())
|
||||
width = height = 160
|
||||
window_vel = glm.vec2()
|
||||
window_pos = glm.vec2(monitors[0].width - width, monitors[0].height - height)
|
||||
|
||||
rl.SetConfigFlags(
|
||||
rl.FLAG_WINDOW_TRANSPARENT
|
||||
| rl.FLAG_WINDOW_UNDECORATED
|
||||
| rl.FLAG_VSYNC_HINT
|
||||
| rl.FLAG_MSAA_4X_HINT
|
||||
)
|
||||
rl.InitWindow(width, height, b'')
|
||||
rl.SetWindowPosition(int(window_pos.x), int(window_pos.y))
|
||||
#rl.SetTargetFPS(500)
|
||||
target = rl.LoadRenderTexture(width, height)
|
||||
|
||||
# Top-Level Window Support Only On Windows
|
||||
if sys.platform == 'win32':
|
||||
import win32gui, win32con, pywintypes
|
||||
|
||||
# Set window to always top without moving it
|
||||
win32gui.SetWindowPos(
|
||||
pywintypes.HANDLE(ffi.cast('int', rl.GetWindowHandle())),
|
||||
win32con.HWND_TOPMOST,
|
||||
0, 0, 0, 0,
|
||||
win32con.SWP_NOSIZE | win32con.SWP_NOMOVE
|
||||
)
|
||||
|
||||
|
||||
#tint = glm.vec4(255, 255, 255, 255) When mouse over, brighten everything fade
|
||||
trans = 155
|
||||
elapsed = CTM()
|
||||
# Generate an 'L' pattern with pixels
|
||||
awd = rl.LoadTextureFromImage(rl.LoadImageEx(
|
||||
[
|
||||
WHITE, WHITE,
|
||||
WHITE, [0] * 4
|
||||
],
|
||||
2, 2
|
||||
))
|
||||
# start1 = [0, 0] ; end1 = [16, 16]
|
||||
# start2 = [0, 160] ; end2 = [16, 144]
|
||||
# start3 = [160, 160]; end3 = [144, 144]
|
||||
# start4 = [160, 0] ; end4 = [144, 16]
|
||||
|
||||
start1 = [0, 0] ; end1 = [8, 8]
|
||||
start2 = [0, 160] ; end2 = [8, 152]
|
||||
start3 = [160, 160]; end3 = [152, 152]
|
||||
start4 = [160, 0] ; end4 = [152, 8]
|
||||
|
||||
while not rl.WindowShouldClose():
|
||||
frame_start_time = CTM()
|
||||
|
||||
elapsed += 2 * rl.GetFrameTime()
|
||||
if elapsed > 1.0:
|
||||
elapsed = 0
|
||||
start1, end1 = end1, start1
|
||||
start2, end2 = end2, start2
|
||||
start3, end3 = end3, start3
|
||||
start4, end4 = end4, start4
|
||||
|
||||
mouse_pos = Vec2(rl.GetMousePosition())
|
||||
|
||||
if rl.IsKeyReleased(rl.KEY_ENTER):
|
||||
window_pos = glm.vec2()
|
||||
window_vel = glm.vec2()
|
||||
|
||||
if rl.CheckCollisionPointRec(list(mouse_pos), [0, 0, width, height]):
|
||||
if rl.IsMouseButtonPressed(rl.MOUSE_LEFT_BUTTON):
|
||||
drag = True
|
||||
offset = mouse_pos
|
||||
|
||||
trans = 255
|
||||
else:
|
||||
trans = 255
|
||||
|
||||
#mouse_pos += window_vel
|
||||
window_vel *= glm.vec2(0.9, 0.9)
|
||||
|
||||
if glm.length(window_vel):
|
||||
window_pos += window_vel
|
||||
#rl.SetWindowPosition(int(window_pos.x), int(window_pos.y))
|
||||
|
||||
|
||||
# Find which monitor the square is *currently within*
|
||||
wposx = window_pos.x + width // 2
|
||||
wposy = window_pos.y + height // 2
|
||||
found = False
|
||||
for monitor in reversed(monitors):
|
||||
if wposx > monitor.x and wposx < monitor.x + monitor.width:
|
||||
if wposy > monitor.y and wposy < monitor.y + monitor.height:
|
||||
found = True
|
||||
break
|
||||
|
||||
# If it is out of bounds, find the closest one
|
||||
if not found:
|
||||
sorted_monitors = list(
|
||||
sorted(
|
||||
monitors,
|
||||
key=lambda m: (m.x + m.width // 2) - window_pos.x + (m.y + m.height // 2) - window_pos.y
|
||||
)
|
||||
)
|
||||
monitor = sorted_monitors[0]
|
||||
|
||||
# Constrain window to bounds
|
||||
window_pos.x = max(monitor.x, window_pos.x)
|
||||
window_pos.x = min(monitor.x + monitor.width - width, window_pos.x)
|
||||
window_pos.y = max(monitor.y, window_pos.y)
|
||||
window_pos.y = min(monitor.y + monitor.height - height, window_pos.y)
|
||||
# Set here to "snap" and don't to enable "floating" back into position
|
||||
if not drag:
|
||||
rl.SetWindowPosition(int(window_pos.x), int(window_pos.y))
|
||||
|
||||
|
||||
if drag:
|
||||
window_pos += mouse_pos - offset
|
||||
drag = not rl.IsMouseButtonReleased(rl.MOUSE_LEFT_BUTTON)
|
||||
rl.SetWindowPosition(int(window_pos.x), int(window_pos.y))
|
||||
|
||||
if not drag:
|
||||
window_vel = (mouse_pos - offset) * glm.vec2(1, 0.6) * 0.8
|
||||
if any(glm.isnan(window_vel)):
|
||||
window_vel = glm.vec2()
|
||||
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground([0] * 4)
|
||||
|
||||
rl.BeginTextureMode(target)
|
||||
rl.ClearBackground([0] * 4)
|
||||
rl.DrawRectangle(0, 0, width, height, [0] * 4)
|
||||
|
||||
rl.DrawTextureEx(
|
||||
awd, tween.getPointOnLine(*start1, *end1, tween.easeInOutQuad(elapsed)), 0, 32,
|
||||
[255, 200, 0, trans] # Yellow
|
||||
)
|
||||
|
||||
rl.DrawTextureEx(
|
||||
awd, tween.getPointOnLine(*start2, *end2, tween.easeInOutQuad(elapsed)), -90, 32,
|
||||
[255, 0, 55, trans] # Red
|
||||
)
|
||||
|
||||
rl.DrawTextureEx(
|
||||
awd, tween.getPointOnLine(*start3, *end3, tween.easeInOutQuad(elapsed)), -180, 32,
|
||||
[100, 200, 55, trans] # Green
|
||||
)
|
||||
|
||||
rl.DrawTextureEx(
|
||||
awd, tween.getPointOnLine(*start4, *end4, tween.easeInOutQuad(elapsed)), 90, 32,
|
||||
[55, 155, 255, trans] # Blue
|
||||
)
|
||||
|
||||
rl.EndTextureMode()
|
||||
|
||||
rl.DrawTexturePro(
|
||||
target.texture,
|
||||
[0, 0, width, -height],
|
||||
[0, 0, width, height],
|
||||
[0, 0],
|
||||
0,
|
||||
WHITE
|
||||
)
|
||||
|
||||
for i in range(1000):
|
||||
rl.DrawTextureEx(awd, [-32, -32], 0, 32, WHITE)
|
||||
|
||||
#rl.DrawFPS(0, 0)
|
||||
|
||||
rl.EndDrawing()
|
||||
|
||||
# Sleep any leftover millis
|
||||
time_taken = CTM() - frame_start_time
|
||||
if time_taken < 1000 / 60:
|
||||
time.sleep((1000 / 60 - time_taken) / 1000)
|
||||
|
Reference in a new issue