K 4  >;  ̼̼̼̻̻̻̼˻̼ ˼˻̻ ˻ ̻ ̼ wxxxwwxwxwwwwwxwwxw wwwwwwwwxwwwxwwxw wwwwwwwwwwwwwwwxwwxw wwwwvwwwfwwwgvwwwvwwwxwwwwwwfwwgfvwwffwwwfwwwwxwwxwwwvwwffwgffvwfffwwffwwwwwwwxwxwwfvwfffgfffvfffvwffvwwfvwwwxwwx pwwgffwffffffffffffvfffwwffwwwwwwx pgvgffVfffVefffUffffffffwffvwwfwwwxw pgffffVUffVUeffUeffVefffvfffwwfvwwxw pgfVffVUefVUUffUUefVUeffVfffvwffwwwwww `gfVUfVUUeVUUUVUUUfVUUffVefffwffvwwvww`ffVUUVUUUUUUUUUUUUVUUefVUeffVfffwwfwwxw`VeVUUUUUUUUUUUUUUUUUUUeVUUffVeffvgfvwwxw`VUUUUUUUUUUUUUUUUUUUUUUVUUefVefffgfvwwwwxfVUUUUUUUUUUUUUUUUUUUUUUUUUUfVUffffffwwgwwxVVUUUUUUUUUUUUUUUUUUUUUUUUUUeUUeffVffvwfwwxVUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUffUfffwfvwwwVUUUUUUUUUEUUUEUUUUUUUUUUUUUUUUUefUeffvfvwwwwVUUUUUUTUUEDUUEDUUUTUUUUUUUUUUUUUfUUfffffwwvwxUUUUTUUDTUEDTUEDTUUDTUUEUUUUUUUUUUUUefVffvgvwwUUUUDUUDDTEDDTEDDUUDDUUETUUUUUUUUUUUefUefvgvwwUUUUDDUDDDEDDDEDDDUDDTUEDUUUUUUUUUUUUfUeffgfwwwUUDUEDDDDD4DDD4DDDDDDDUEDTUUDUUUUUUUUeUUffefwwwUUDDEDD4DD43DD4CDD4DDDTDDDUUDUUUUUUUUUUUffefvgwPUEDDDD4CD43CD43CD43DDDCDDTUDTUUUUUUUUUUeVefvgwPTED4CD43CD33C433D43CDD3DDDEDDUUEUUUUUUUUVUfffvwPDDDD3C433C333433C433DD3CDDDDDTUDTUUUUUUUVUfffvgPD4CD3333333333333333CD33DD4DDDUDDUUUUUUUUUfffvg@ED3C33333333333333333433CD3CDDTDDUUEUUUUUUeVfvgDD33333333333333333333333D33DD4DDTUDUUUUUUeVeff4C33333333333333333333333C33DD4DDDUDTUUUUUUUeffwD333333333333333333333333333CD3CDDEDTUUUUUUUeVfw@333333333333#333333333333333D3CDDDDDUEUUUUUeVf0433333#33#233233#33333333333333DDCDDUETUUUUeUf03333233"3#"33"23#23323333333333CD3DDTDTUUUUeUf3333#23"23""3""3#"33"3333333333C43DDTDTUUUUUef33#3#"2""2""2""2#"23"2333333333333CDDDTUTUUUef033"3"""""""""""#""3"23#2333333333CD3DDUTUUUeu033"""""""""""""""""""3#"333333333C43DDETUUUU32#""""""""""""""""""2#"33"333333343DDDTUUUU3"""""""""""""""""""""""23"333333333DDDTUUUU0#"""""""""""""""""""""""#"233333333D4DTEUU0"""""""""!"""""""""""""#"23"333333C3DTDUU""""""!""!"""""""""""""3"33333333DDDUU""""""!"!""""""""""#"33333333DDDEU """!!!"""!""""""""23233333DCTT""""!!"""""""""2#2333334CDT""!""""""""2"2333333DD "!!"""""""23233334D"!!!"""""""2#233333D !"!"""""""33333C!!""""""333334"!"""""#2333!""""""3333!"!""""323!""""2"33"""""23!""""#!""""3"!""2!"""!!"!"!!-- title: 3D Sphere -- author: Marioio -- desc: A 3D sphere rendered with points or triangles -- site: https://marioio333.neocities.org/ -- license: MIT License -- version: 1.0 -- script: lua RESOLUTION = 30 radius = 100/2 sphere_x = 240/2 --sphere_x = 240+radius+10 sphere_y = (136/2)+10 angleY = 0 angleX = 90 angleZ = 0 globe = {} mode = 1 modes = {"Points", "Dyson Sphere", "Solid"} colorMode = 1 colorModes = {"Depth", "Global Depth", "Random Colors", "Changing Random Colors", "Rainbow", "Rainbow Depth"} angleSpeed = 0.004 function BOOT () -- Convert initial angles to radians angleX = math.rad(angleX) angleY = math.rad(angleY) angleZ = math.rad(angleZ) for i=0, RESOLUTION+1 do globe[i] = {} for j=0, RESOLUTION+1 do globe[i][j] = {} end end calculateSphere() end function TIC() --angleY = angleY + 0.0005 --angleX = angleX + 0.0005 --angleZ = angleZ + 0.0005 --sphere_x = sphere_x - 0.2 if sphere_x < -radius-10 then sphere_x = 240+radius+10 end INPUT() DRAW() end function INPUT() if btn(0) then angleX = angleX-angleSpeed end if btn(1) then angleX = angleX+angleSpeed end if btn(2) then angleY = angleY+angleSpeed end if btn(3) then angleY = angleY-angleSpeed end if btnp(4) then mode = mode+1 if mode > #modes then mode = 1 end end if btnp(5) then colorMode = colorMode+1 if colorMode > #colorModes then colorMode = 1 end end end -- INPUT function DRAW() cls(0) -- UI print("Sphere Mode: "..modes[mode].." (A to change)",5,5,12) print("Color Mode: "..colorModes[colorMode].." (B to change)",5,15,12) print("Arrows to move",5,25,12) -- Sphere if mode == 1 then drawSphere() elseif mode == 2 then drawDysonSphere() elseif mode == 3 then drawSolidSphere() end end -- DRAW function drawSphere() -- background points for i=0, RESOLUTION do for j=0, RESOLUTION do x = globe[i][j].x y = globe[i][j].y z = globe[i][j].z -- sphere rotation sx, sy, sz = rotate3D(x, y, z, angleX, angleY, angleZ) if sz < 0 then sx = sphere_x + sx sy = sphere_y + sy color = getColor(x,y,z,sx,sy,sz) circ(sx,sy,0,color) end end end -- foreground points for i=0, RESOLUTION do for j=0, RESOLUTION do x = globe[i][j].x y = globe[i][j].y z = globe[i][j].z -- sphere rotation sx, sy, sz = rotate3D(x, y, z, angleX, angleY, angleZ) if sz > 0 then sx = sphere_x + sx sy = sphere_y + sy color = getColor(x,y,z,sx,sy,sz) circ(sx,sy,0,color) end end end end -- drawSphere function drawDysonSphere() -- Draw background points for i=0, RESOLUTION do for j=0, RESOLUTION do x = globe[i][j].x y = globe[i][j].y z = globe[i][j].z -- sphere rotation sx, sy, sz = rotate3D(x, y, z, angleX, angleY, angleZ) if sz < 0 then sx = sphere_x + sx sy = sphere_y + sy color = getColor(x,y,z,sx,sy,sz) circ(sx,sy,0,color) end end end circ(sphere_x, sphere_y, radius/2, 3) -- Draw foreground points for i=0, RESOLUTION do for j=0, RESOLUTION do x = globe[i][j].x y = globe[i][j].y z = globe[i][j].z -- sphere rotation sx, sy, sz = rotate3D(x, y, z, angleX, angleY, angleZ) if sz > 0 then sx = sphere_x + sx sy = sphere_y + sy color = getColor(x,y,z,sx,sy,sz) circ(sx,sy,0,color) end end end end -- drawDysonSphere function drawSolidSphere() for i=0, RESOLUTION do for j=0, RESOLUTION do v1 = globe[i][j] v2 = globe[i+1][j] v3 = globe[i][j+1] -- sphere rotation v1.sx, v1.sy, v1.sz = rotate3D(v1.x, v1.y, v1.z, angleX, angleY, angleZ) v2.sx, v2.sy, v2.sz = rotate3D(v2.x, v2.y, v2.z, angleX, angleY, angleZ) v3.sx, v3.sy, v3.sz = rotate3D(v3.x, v3.y, v3.z, angleX, angleY, angleZ) -- sphere translation v1.sx = sphere_x + v1.sx v1.sy = sphere_y + v1.sy v2.sx = sphere_x + v2.sx v2.sy = sphere_y + v2.sy v3.sx = sphere_x + v3.sx v3.sy = sphere_y + v3.sy color = getColor(v1.x,v1.y,v1.z,v1.sx,v1.sy,v1.sz) zavg = (v1.sz + v2.sz + v3.sz) / 3 if zavg > 0 then tri(v1.sx,v1.sy,v2.sx,v2.sy,v3.sx,v3.sy,color) end -- second triangle v1 = globe[i+1][j+1] v1.sx, v1.sy, v1.sz = rotate3D(v1.x, v1.y, v1.z, angleX, angleY, angleZ) v1.sx = sphere_x + v1.sx v1.sy = sphere_y + v1.sy color = getColor(v1.x,v1.y,v1.z,v1.sx,v1.sy,v1.sz) zavg = (v1.sz + v2.sz + v3.sz) / 3 if zavg > 0 then tri(v1.sx,v1.sy,v2.sx,v2.sy,v3.sx,v3.sy,color) end end end end -- drawSolidSphere function calculateSphere() for i=0, RESOLUTION+1 do lat = map(i,0,RESOLUTION, 0, math.pi) for j=0, RESOLUTION+1 do lon = map(j,0,RESOLUTION, 0, math.pi*2) -- point coords x = radius * math.sin(lat) * math.cos(lon) y = radius * math.sin(lat) * math.sin(lon) z = radius * math.cos(lat) globe[i][j] = {x=x,y=y,z=z} end end end -- calculateSphere -- HELPERS function getColor(x,y,z,sx,sy,sz) if colorMode == 1 then color = map(sz, -radius, radius,16, 12) elseif colorMode == 2 then color = map(z, -radius, radius,16, 12) elseif colorMode == 3 then color = ((x+y+z)%15)+1 elseif colorMode == 4 then color = ((sx+sy+sz)%15)+1 elseif colorMode == 5 then color = map(z, -radius, radius,1, 15) elseif colorMode == 6 then color = map(sz, -radius, radius,1, 15) end return color end function rotate3D(x, y, z, ax, ay, az) -- pre-calcular senos y cosenos local cx, sx = math.cos(ax), math.sin(ax) local cy, sy = math.cos(ay), math.sin(ay) local cz, sz = math.cos(az), math.sin(az) -- aplicar rotación global en una sola fórmula local nx = x*cy*cz + y*(sx*sy*cz - cx*sz) + z*(cx*sy*cz + sx*sz) local ny = x*cy*sz + y*(sx*sy*sz + cx*cz) + z*(cx*sy*sz - sx*cz) local nz = x*-sy + y*sx*cy + z*cx*cy return nx, ny, nz end function map(n, oldMin, oldMax, newMin, newMax) percentage = (n-oldMin)/(oldMax-oldMin) return newMin+ percentage * (newMax-newMin) end function isFrontFace(v1, v2, v3) return (v2.sx - v1.sx) * (v3.sy - v1.sy) - (v2.sy - v1.sy) * (v3.sx - v1.sx) < 0 end