Jump to content

LUA TIP: Box to Sphere


shadmar
 Share

Recommended Posts

--Scale of box has to be less or equal to 1, you can scale up after

function Script:sphereit(x,y,z)

sx = x * math.sqrt(1.0 - (y*y*0.5) - (z*z*0.5) + (y*y*z*z/3.0));

sy = y * math.sqrt(1.0 - (z*z*0.5) - (x*x*0.5) + (z*z*x*x/3.0));

sz = z * math.sqrt(1.0 - (x*x*0.5) - (y*y*0.5) + (x*x*y*y/3.0));

return sx,sy,sz

end

 

 

Example usage on a 1x1x1 box mesh (has to be abit tesselated to have enough vertices to transform)

 

function Script:Start()

surface=self.entity:GetSurface(0)

for v=0,surface:CountVertices()-1 do

surface:SetVertexColor(v,v/1000,1000-v,v/1000,1)

vpos = surface:GetVertexPosition(v)

x,y,z = self:sphereit(vpos.x,vpos.y,vpos.z)

surface:SetVertexPosition(v,Vec3(x,y,z))

end

surface:UpdateNormals(true,0.01,180)

end

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...