Jump to content

LUA TIP: Procedural Sphere with uvmap


shadmar
 Share

Recommended Posts


-- This will create an inverted sphere, meant for a skydome or similar

 

function Script:CreateSphere(latitudeBands, longitudeBands, radius)

 

local model = Model:Create()

local surface = model:AddSurface()

 

for latNumber=0,latitudeBands,1

do

local theta = latNumber * math.pi / latitudeBands

local sinTheta = math.sin(theta)

local cosTheta = math.cos(theta)

for longNumber = 0,longitudeBands,1

do

local phi = longNumber * 2 * math.pi / longitudeBands;

local sinPhi = math.sin(phi);

local cosPhi = math.cos(phi);

 

local x = cosPhi * sinTheta;

local y = cosTheta;

local z = sinPhi * sinTheta;

 

local u = 1 - (longNumber / longitudeBands);

local v = 1 - (latNumber / latitudeBands);

surface:AddVertex(radius * x, radius * y, radius * z, x, y, z, u, v)

end

end

 

for latNumber = 0,latitudeBands-1,1

do

for longNumber = 0,longitudeBands-1,1

do

local first = (latNumber * (longitudeBands + 1)) + longNumber;

local second = first + longitudeBands + 1;

 

surface:AddTriangle(second,second + 1,first + 1)

surface:AddTriangle(first + 1,first,second)

end

end

model:UpdateAABB(Entity.LocalAABB)

model:UpdateAABB(Entity.GlobalAABB)

return model

end

 

  • Upvote 2

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...