Planet Generation - 3

(BluePrintRandom) #1

Step by step how to generate a planet made of tiles, and it can generate multiple LOD’s we can store, to later use in BPY to make single tiles with multiple LOD levels.

if anything is unclear don’t hesitate to ask questions.

1 Like
(BluePrintRandom) #2


it looks like mathutils.geometry.normal is broken - ended up getting a solution from stack exchange using numpy

1 Like
(BluePrintRandom) #3

almost able to re-create meshes in bpy

import bpy
import pickle
import bmesh
from mathutils import Vector

try:
    path =  bpy.path.abspath("//")
    file = "Planet_data.txt"
    with open(path+file, 'rb') as f:
        MasterList = pickle.load(f)
    print('loaded patch data from disk')
except:
    print('failed to load')    

scn = bpy.context.scene

print(MasterList[0])
patch = bpy.data.objects['Patch']
for entry in MasterList:
    new_obj = patch.copy()
    new_obj.data = patch.data.copy()
    new_obj.name ="Gen_Patch_"+str(entry)
    scn.objects.link(new_obj)
    new_obj.location = MasterList[entry]["Pos"]
    new_obj.rotation_euler  = MasterList[entry]["Rot"]
    print('added a mesh')
    bpy.context.scene.objects.active = new_obj
    bpy.ops.object.mode_set(mode = 'EDIT')
    bm = bmesh.from_edit_mesh(new_obj.data)
    colors = bm.loops.layers.color.active
    
    bm.verts.ensure_lookup_table()
    if not colors:
        colors = bm.loops.layers.color.new("Col")
        
    for i in range(MasterList[entry]["L"]):
        data = MasterList[entry][i]
        bpy.ops.mesh.select_all(action='DESELECT')
        
        bm.verts[i].select = True
        verts = [ v for v in bm.verts if v.select ]
       
        if verts:
            colors = bm.loops.layers.color.active
            if not colors:
                colors = bm.loops.layers.color.new("Col")
                
            for v in verts:
                for loop in v.link_loops:
                    loop[colors] = data[1]
                v.normal = data[2]
                v.co = Vector(data[0])
                    
                    
    bmesh.update_edit_mesh(new_obj.data)


(Lyeb) #4

#InBPRWeTrust

(BluePrintRandom) #5

so - here is the code I am pickling the meshes with

        import pickle
        if 'saved' not in own:
            try:
                
                path = bge.logic.expandPath("//")
                file = "Planet_data.txt"
                with open(path+file, 'rb') as f:
                    MasterList = pickle.load(f)
                print('loaded patch data from disk')
                   
            except:
                print('failed to load')
                MasterList = {}
           
            for entry in own['quads']:
                target = own['quads'][entry][4]
                L = target.meshes[0].getVertexArrayLength(0)
                TileDict = {"Pos":tuple(target.worldPosition),"Rot":tuple(target.worldOrientation.to_euler()), "L":L }
                
                for index in range(L):
                    vert = target.meshes[0].getVertex(0,index)
                    pos = tuple(vert.XYZ)
                    color = tuple(vert.color)
                    norm = tuple(vert.normal)
                    TileDict[index] = (pos,color,norm)
                
                MasterList[entry] = TileDict
                
            print('generated patch data')
            own['saved']=True
            #print(MasterList)
           
            
            path = bge.logic.expandPath("//")
            file = "Planet_data.txt"
            with open(path+file, 'wb') as f: 
                pickle.dump(MasterList, f) 
            print('saved pickle')
            
(BluePrintRandom) #6

ok I have it working kinda, it looks like the vert index is different in bpy, will have to sort the patch array vertex and compare arrays to make a converter.

(BluePrintRandom) #7

ok I have generated a vertex array in bpy


now I can use a vertex array[x][y] to set it’s data instead of index, since bpy index seems a little different than upbge index.

(BluePrintRandom) #8

got it working !


I have a issue though in that the generated mesh vertex are invisible :stuck_out_tongue:

https://pastebin.com/RHQeFQ9X
code that is making ‘ghost’ meshes


getting close though, livin’ the dream.
(BluePrintRandom) #9

I did it!

mesh generated in game / manipulated in game can be saved and exported to bpy

(BluePrintRandom) #10

:smiley:

the key to matching up a bpy mesh, and bge mesh is sorting mesh into a vertex array

array[x][y][z] = vert_index

one in bge, one in bpy.

so now we can deform kit instances, build levels - save the level - regenerate in bpy.

(BluePrintRandom) #11

ok shit just got real.

(Lostscience) #12

can there be more one planet with gravity?Can I have spaceships that I can fly from one planet to other with this?

(BluePrintRandom) #13

I can add more planets etc, however now the focus is getting the 1 streaming at 60 fps at a decent speed.

(BluePrintRandom) #14

testing batch grouping the highest LOD level of tiles

(BluePrintRandom) #15
(BluePrintRandom) #16

:smiley:

viewport
engine 4,096 tiles

(BluePrintRandom) #17

current progress - trying to generate UV

(Lostscience) #18

what are you going to do to add houses and streets to the planets?that would be hard

to do without using an algorithm.

(BluePrintRandom) #19

Ok ! now the generated terrain has UV - so I can bake vertex color over to a cube map

here is 1/6th a cubesphere

perfs with 4096 tiles joined as 1 object

(BluePrintRandom) #20

uv’s cube map unwrap - baking of vertex color - second uv layer (cube projected unwrap)