eyes3d in Lua with TIC-80

Date: 2025-01-03
Tags: 3d, Lua, procedural generation, tic80

A simple tic-80 card in Lua, that display 3d eyes, following light or mouse, à la Xeyes 👀, but in 3d.

This does not use GL, it use 2d canvas with a bit of 3d computation, and TIC-80 tri function for drawing.

press several tume ESC key, then choose close game and then press ESC key again to go to source code, and play with it. Enjoy.

- CLICK TO PLAY -

This TIC-80 is also available on official tic80 website

Screen orientation The code of the procedural generation and it's management in main loop is not made to be really portable to any kind of shape. To change this, the best and simpliest solution, should be to create triangle faces at the time of the creation of the eyes. in the ``function buildeye(dotes)`` function.

In this scene, I choose as convention:

Eyes direction

The function calc_ang2(pd,ps) is used to compute the vector from center of eye to the light or the mouse cursor depending on mode used, each angle is computer after other components:

function calc_ang2(pd,ps)
  local x2,y2,z2 = pd[1]-ps[1],pd[2]-ps[2],pd[3]-ps[3]
  return -atan(y2,z2),
         -atan(x2,z2)
end

Lighting and shading

For the shading, oonly the is computed here. I compute the normal of the face with crossproduct of AB and AC vectors:

dX,dY,dZ = crossp(
  {B[1]-A[1],B[2]-A[2],B[3]-A[3]},
  {C[1]-A[1],C[2]-A[2],C[3]-A[3]}
)

Then only if dZ<=0 then (toward the viewer, backculling), the face will be drawn

For this face, the angle between:

a=ang({light[1]-A[1],light[2]-A[2],light[3]-A[3]},{dX,dY,dZ})

As a result, greater is the angle, lower is the light. with the computaton dependng on :

if fc ~=0 then fc=a/pi*16
else fc=a/pi*3 end
Tags: 3d, Lua, procedural generation, tic80