Skip to content

Instantly share code, notes, and snippets.

@meshula
Created September 14, 2020 02:16
Show Gist options
  • Save meshula/3977db16b505c24b55491c69a83ed301 to your computer and use it in GitHub Desktop.
Save meshula/3977db16b505c24b55491c69a83ed301 to your computer and use it in GitHub Desktop.
fractalscape.pyxl
// Scripts, variables, and constants here are visible to all modes
let fake_phi = 0
def Render(p, ϕ, height, horizon, scale_height, distance, screen_width, screen_height):
// precalculate viewing angle parameters
const sinphi = sin(ϕ + fake_phi)
const cosphi = cos(ϕ + fake_phi)
fake_phi += 0.1
// initialize visibility array. Y position for each column on screen
const ybuffer = []
resize(ybuffer, screen_width)
for i < screen_width:
ybuffer[i] = screen_height
// Draw from front to the back (low z coordinate to high z coordinate)
let dz = 1.0
let z = 1.0
while z < distance:
// Find line on map. This calculation corresponds to a field of view of 90°
const pleft = xy(
(-cosphi*z - sinphi*z) + p.x,
( sinphi*z - cosphi*z) + p.y)
const pright = xy(
( cosphi*z - sinphi*z) + p.x,
(-sinphi*z - cosphi*z) + p.y)
// segment the line
let dx = (pright.x - pleft.x) / screen_width
let dy = (pright.y - pleft.y) / screen_width
// Raster line and draw a vertical line for each segment
for i < screen_width:
const heightmap_pos = xy(pleft.x, pleft.y)
let heightmap_val = abs(sin(pleft.x / 100)) // get_sprite_pixel_color(landscape_d2[0][0], heightmap_pos)
const color = rgba(heightmap_val, heightmap_val, heightmap_val, 1.0) //get_sprite_pixel_color(landscape_cw2[0][0], heightmap_pos)
heightmap_val *= 60
const height_on_screen = (height - heightmap_val) / z * scale_height + horizon
//DrawVerticalLine(i, height_on_screen, ybuffer[i], color)
draw_line(xy(i, height_on_screen), xy(i, ybuffer[i]), color, 1000, 1) // z 1000, w 1
if height_on_screen < ybuffer[i]:
ybuffer[i] = height_on_screen
pleft.x += dx
pleft.y += dy
// Go to next line and increase step size when you are far away
z += dz
dz += 0.2
// Call the render function with the camera parameters:
// position, viewing angle, height, horizon line position,
// scaling factor for the height, the largest distance,
// screen width and the screen height parameter
//Render( xy(0, 0), 0, 50, 120, 120, 300, 300, 200 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment