How do I create an Icosphere in script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CraftyManiac
:warning: Old Version Published before Godot 3 was released.

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.set_parameter(material.PARAM_DIFFUSE, Color(1,0,0,1))

surfTool.set_material(material)
surfTool.begin(VS.PRIMITIVE_TRIANGLES)

#Creat 12 vertices of a icosahedron
surfTool.add_vertex(Vector3(-1,t,0))
surfTool.add_vertex(Vector3(1,t,0))
surfTool.add_vertex(Vector3(-1,-t,0))
surfTool.add_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.

:bust_in_silhouette: Reply From: avencherus

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.

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?

CraftyManiac | 2017-01-08 05:57

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.

avencherus | 2017-01-08 06:14

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

CraftyManiac | 2017-01-08 06:20

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

https://drive.google.com/open?id=0BxM4Z1C-377uSHhUSkUyZXpTaHM

avencherus | 2017-01-08 06:35

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.

CraftyManiac | 2017-01-08 17:02

The repository doesn’t exists anymore

Afonso Lage | 2019-08-10 10:43

:bust_in_silhouette: Reply From: nic96

I just coded a solution for this problem: GitHub - nic96/godot-generate-icosphere: This Godot project procedurally generates an icosphere with user define levels of subdivision . 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.