MCPcopy Create free account
hub / github.com/ICE27182/Python-3D-renderer / add_lights

Function add_lights

pyrender.py:3390–3484  ·  view source on GitHub ↗

For debug use only. Must applied when other rendering process is done

(frame, cam:Camera, lights, direction)

Source from the content-addressed store, hash-verified

3388
3389
3390def add_lights(frame, cam:Camera, lights, direction):
3391 """
3392 For debug use only. Must applied when other rendering process is done
3393 """
3394 def add_line(M, N, color):
3395 if M[0] == N[0]:
3396 if 0 <= M[0] < cam.width:
3397 if M[1] > N[1]:
3398 M, N = N, M
3399 for y in range(max(0, M[1]), min(cam.height - 1, N[1])):
3400 frame[y][M[0]] = color
3401 elif abs(k:=(M[1] - N[1]) / (M[0] - N[0])) <= 1:
3402 b = M[1] - k * M[0]
3403 if M[0] > N[0]:
3404 M, N = N, M
3405 for x in range(max(0, M[0]), min(cam.width - 1, N[0])):
3406 # CodeUndone
3407 y = int(k * x + b)
3408 if 0 <= y < cam.height:
3409 frame[y][x] = color
3410 else:
3411 t = 1 / k
3412 b = M[0] - t * M[1]
3413 if M[1] > N[1]:
3414 M, N = N, M
3415 for y in range(max(0, M[1]), min(cam.height - 1, N[1])):
3416 x = int(t * y + b)
3417 if 0 <= x < cam.width:
3418 frame[y][x] = color
3419
3420 # CodeUndone
3421 light:Light
3422 for light in lights:
3423 if not light.hidden and light.z_in_cam > cam.z_near:
3424 x = cam.width // 2 + int(light.x_in_cam * cam.rendering_plane_z / light.z_in_cam)
3425 y = cam.height // 2 - int(light.y_in_cam * cam.rendering_plane_z / light.z_in_cam)
3426
3427 if light.type in (0, 2) and direction:
3428 x_axis = (
3429 light.x_in_cam + 4 * light.rotation0[0][0],
3430 light.y_in_cam + 4 * light.rotation0[0][1],
3431 light.z_in_cam + 4 * light.rotation0[0][2],
3432 )
3433 xx = cam.width // 2 + int(x_axis[0] * cam.rendering_plane_z / x_axis[2])
3434 xy = cam.height // 2 - int(x_axis[1] * cam.rendering_plane_z / x_axis[2])
3435 add_line((xx, xy), (x, y), (255, 0, 0))
3436
3437 y_axis = (
3438 light.x_in_cam + 4 * light.rotation0[1][0],
3439 light.y_in_cam + 4 * light.rotation0[1][1],
3440 light.z_in_cam + 4 * light.rotation0[1][2],
3441 )
3442 yx = cam.width // 2 + int(y_axis[0] * cam.rendering_plane_z / y_axis[2])
3443 yy = cam.height // 2 - int(y_axis[1] * cam.rendering_plane_z / y_axis[2])
3444 add_line((yx, yy), (x, y), (0, 255, 0))
3445
3446 z_axis = (
3447 light.x_in_cam + 4 * light.rotation0[2][0],

Callers

nothing calls this directly

Calls 1

add_lineFunction · 0.85

Tested by

no test coverage detected