Unwrapping icospheres

Here’s a program that unwraps an icosphere, converting the x,y,z points to latitude and longitude and ploting the points. Run in landscape mode and change the level numbers for the icospheres 0 thru 5. The number of point are shown for each run. There’s no real purpose for this program, I just found it interesting to see the patterns. Levels 0 thru 5 will draw an icosphere, but anything above 5 will only show the points, and take awhile to run. I’m not sure if the values above 5 are correct.

displayMode(FULLSCREEN)

function setup()
    
    level=1     -- change level 0 thru 5
    
    assert(craft, "Please include Craft as a dependency")
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")        
    scene = craft.scene()
    v=scene.camera:add(OrbitViewer, vec3(0,0,0), 500, 0, 600)
    llTab={}    
    fill(0,255,0)
    createSphere()
    cnt=sph.model.indexCount//6+2
end

function draw()
    update(DeltaTime)
    scene:draw()
    for a,b in pairs(llTab) do
        point(b.x*2.6+500,b.y*1.5+560)
    end      
    text("Number of points  "..cnt,WIDTH/2,HEIGHT-20)  
end

function update(dt)
    scene:update(dt)
end

function createSphere()
    sph=scene:entity()
    sph.position=vec3(5,-80,0)
    sph.model = craft.model.icosphere(60,level,1)
    sph.material = craft.material("Materials:Standard")    
    for a,b in pairs(sph.model.colors) do
        sph.model:color(a,math.random(255),math.random(255),math.random(255),255)
    end
    -- convert x,y,z coords to latitude and longitude
    for a,b in pairs(sph.model.positions) do
        local c=b:normalize()
        local r=math.sqrt(c.x*c.x+c.y*c.y+c.z*c.z) 
        local lat=math.deg(math.asin(c.z))
        local lon=math.deg(math.atan2(c.y,c.x))
        table.insert(llTab,vec2(lon,lat))
    end
end

Note that in the proposed sphere texture, the longitude starts at a different place so that there is a clean “seam”.

(Also, the line local r = math.sqrt(...) isn’t needed; for one, because you don’t refer to r afterwards; and for two, because the line before normalises the vector so r is always equal to 1.)

@LoopSpace That r=math.sqrt was from code before I did the normalize(). The lat=math.deg did a divide by r, I just forgot to remove it because r then became 1. In your code in the other link, you were setting u,v to some value and then doing the Lat,Lon calculation. I figured that was to rotate your calculations to get you to start at one of the seams.