0 votes

Hey so I'm trying to make a sphere in script using surface tool

func _ready():

var surfTool = SurfaceTool.new()
var mesh = Mesh.new()
var material =FixedMaterial.new()
var t = (1.0 + sqrt(5.0)) / 2.0

material.setparameter(material.PARAMDIFFUSE, Color(1,0,0,1))


#Creat 12 vertices of a icosahedron

I'm trying to convert this python script
which was derived from this page There is another version of this script in python in the comments by Miles Marx 5 months ago. That's the script I started to use.

I'm just a beginner so translating the code is rather difficult for me. I'm getting stuck on how to convert the classes and functions to work in gdscript I was looking for a hexagonal sphere but this was the next best thing. Then I have to figure out how to map it so that i can randomly generate nodes( or points of interest) on the surface. (it's going to be a planet that will only be viewed from afar) I'm almost wondering if there is a way to do this in blender and if that would be easier. Any help would be appreciated.

in Engine by (30 points)

2 Answers

+2 votes
Best answer

If you don't need to generate it dynamically an imported mesh would save much time.

There is also a plug-in for Godot for generating simple primitives: https://github.com/TheHX/add_primitives

And you can review it's source if you want specific examples of how to use the SurfaceTool.

by (5,240 points)
selected by

I suppose I don't have to do it dynamically. I could always create 3 or 4 different sizes of the sphere. Is there a way to plot several points on a sphere mesh at random?

Nothing that I know of built-in.

The solution you're looking for is in 3D math.

The simplest one I can think of is generating a random 3D vector, normalizing it, and then multiplying it by the sphere's radius. Then add the vector to the center position of the sphere, and it should be a point on the surface. I would test that though, that's just off the top of my head.

If it has to be something that matches some blocky sphere's collision bounds, then you may have to use a random raycast from center to find the surface point.

Ok I will see what I can come up with. Thank's for your help!

No problem. X)

I tested it, it works.

The code is something like:

var rand_vec = Vector3(rand_range(-1,1), rand_range(-1,1), rand_range(-1,1))

var rand_surface_point = sphere_position  + rand_vec.normalized() * radius


Ahh I see what you mean. I kind of meant how would i plot points on to the face of the icoscphere. like the black dots in the center of the faces in the image. Only populating some points at random not all.

The repository doesn't exists anymore

+1 vote

I just coded a solution for this problem: https://github.com/nic96/godot-generate-icosphere . This may not be the best solution since I'm not very good at math, but with enough research I was able to find the right formulas, equations and pseudo code to create it. I included a blender file that helped me visualize an icosahedron.

I'm posting this here in case anyone else is looking for a solution to this problem.

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.