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
In this scene, I choose as convention:
- X goes to right
- y goes to up
- z goes toward viewer.
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:
- x depends on y and z
- y depends on x and z
- We don't need to rotate around z axis here. We artificially force the z value of these destinations to 30. We could follow the whole trajectory of light, when it goes behind, that was not the goal here.
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:
- The vector that goes from the source of the computed normal (A) to the light (light)
- The normal itself
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 :
- Black part of eye, if fc==0 then fc=a/pi*3
- Else, white part of eye, fc ~= 0 then fc=a/pi*16
if fc ~=0 then fc=a/pi*16
else fc=a/pi*3 end