Discord
Login
Community
DARK THEME

Mapping an IMG on a sphere

Anyone knows how to Map a Image on a sphere (circle). I would like to make some kind of surface, but I have an image, which I would like to load on some kind of circle (planet), so i can spin arround that.

https://microstudio.dev/i/TinkerSmith/planet/

or Babylon or PIXI3D

thx for ur answer. Do u know how it is possible with an "Image", after I loaded this? And even better would be with M3D graphics, like https://microstudio.dev/i/gilles/m3dtest/

Render on an Image object. And then rotate it as you draw.

//******************************************************************
// the texture size ratio has to be 3:1 (width/height)
// sector 1 & 2 in x direction are the texture to be projected
// sector 3 is a repeat of sector 1 for the wrap around
//
// could be done by splitting the wrap routine, but easier this way
//******************************************************************

drawPlanet = function(img,planet,texpos,px=0,py=0,size)
  local tsize  = sprites[planet].height  // texture height/width
  local radius = tsize/2                // radius = half width
  local scale  = size/tsize             // scale
  img.setTranslation( img.width/2, img.height/2 )
  img.setDrawAnchor( 0, 0 )
  for y=-radius to radius by 1/scale
    local dc = 2*sqrt(radius^2-y^2)     // chord at y = x-scale
    img.setDrawScale(dc/tsize,1)
    img.drawSpritePart(
                planet,             // texture reference
                texpos,radius-y,    // texture x/y
                tsize,scale,        // texture width/height
                px*scale,py+y*scale,      // draw x/y
                tsize*scale,scale)  // draw width/height
  end
  img.setDrawScale(1,1)
end

Main file

init = function()
  texture = "dstar"
  shading = true
  clouds  = true
  size    = 64        // projected size
  posx    = 0         // for future use
  posy    = 0         // position of the sphere on screen
  image = new Image( 200, 100 )
  image.setTranslation()
  image.setColor("red")
  image.drawRect( 0, 0, image.width, image.height )
  drawPlanet(image,texture,128,posx,posy,64)  
  angle = 0
end

update = function()
  t1 = (t1+.06)%128   // texture speed 1
  t2 = (t2+.04)%128   // texture speed clouds
  
  if keyboard.press.S then 
    shading = not shading 
  elsif keyboard.press.C then
    clouds  = not clouds
  end
  
  if keyboard.press.1 then 
    texture = "earth" 
  elsif keyboard.press.2 then
    texture = "dstar"
  elsif keyboard.press.3 then
    texture = "planet"
  elsif keyboard.press.4 then
    texture = "tinker"
  end
  
  if keyboard.ARROW_UP then 
    size += 1 if size>128 then size =128 end
  elsif keyboard.ARROW_DOWN then
    size -= 1 if size<16 then size =16 end
  end
  angle += 0.1
end

draw = function()
  screen.clear("rgb(0,0,0)")
  drawPlanet(image,texture,128-t1,posx,posy,size) 

  screen.setDrawRotation( angle )
  screen.drawImage( image, 0, 0, image.width, image.height )
  screen.setDrawRotation( 0 )
  screen.drawText("[S] - Shading On/Off",0,90,8,"rgb(50,227,227)")
  screen.drawText("[C] - Clouds On/Off",0,80,8,"rgb(50,227,227)")
  screen.drawText("[UP][DOWN] - change size",0,-80,8,"rgb(50,227,227)")
  screen.drawText("[1]..[4] - change texture",0,-90,8,"rgb(50,227,227)")
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community