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

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

surfTool.setmaterial(material)
surfTool.begin(VS.PRIMITIVE
TRIANGLES)

#Creat 12 vertices of a icosahedron
vertex(Vector3(1,t,0))
vertex(Vector3(1,-t,0))
........etc

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

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

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