Skip to content

Commit

Permalink
Making bvh on mesh generate in bg
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentFarris committed Mar 21, 2024
1 parent ca69e00 commit 1ec6774
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/editor/content/content_opener/obj_opener.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"kaiju/engine"
"kaiju/matrix"
"kaiju/rendering"
"kaiju/rendering/loaders/load_result"
"sync"
)

Expand All @@ -56,6 +57,37 @@ func (o ObjOpener) Handles(adi asset_info.AssetDatabaseInfo) bool {
return adi.Type == editor_config.AssetTypeObj
}

func buildBVH(m load_result.Mesh, e *engine.Entity) {
tris := make([]collision.DetailedTriangle, len(m.Indexes)/3)
group := sync.WaitGroup{}
construct := func(from, to int) {
for i := from; i < to; i += 3 {
for i := 0; i < len(m.Indexes); i += 3 {
points := [3]matrix.Vec3{
m.Verts[m.Indexes[i]].Position,
m.Verts[m.Indexes[i+1]].Position,
m.Verts[m.Indexes[i+2]].Position,
}
tris[i/3] = collision.DetailedTriangleFromPoints(points)
}
}
group.Done()
}
group.Add(1)
if len(tris) > 100 {
group.Add(9)
for i := 0; i < 10; i++ {
go construct(i*len(tris)/10, (i+1)*len(tris)/10)
}
} else {
construct(0, len(tris))
group.Done()
}
group.Wait()
bvh := collision.BVHBottomUp(tris)
e.EditorBindings.Set("bvh", bvh)
}

func load(host *engine.Host, adi asset_info.AssetDatabaseInfo, e *engine.Entity) error {
texId := assets.TextureSquare
if t, ok := adi.Metadata["texture"]; ok {
Expand Down Expand Up @@ -89,34 +121,7 @@ func load(host *engine.Host, adi asset_info.AssetDatabaseInfo, e *engine.Entity)
return err
}
mesh = rendering.NewMesh(adi.ID, m.Verts, m.Indexes)
tris := make([]collision.DetailedTriangle, len(m.Indexes)/3)
group := sync.WaitGroup{}
construct := func(from, to int) {
for i := from; i < to; i += 3 {
for i := 0; i < len(m.Indexes); i += 3 {
points := [3]matrix.Vec3{
m.Verts[m.Indexes[i]].Position,
m.Verts[m.Indexes[i+1]].Position,
m.Verts[m.Indexes[i+2]].Position,
}
tris[i/3] = collision.DetailedTriangleFromPoints(points)
}
}
group.Done()
}
group.Add(1)
if len(tris) > 100 {
group.Add(9)
for i := 0; i < 10; i++ {
go construct(i*len(tris)/10, (i+1)*len(tris)/10)
}
} else {
construct(0, len(tris))
group.Done()
}
group.Wait()
bvh := collision.BVHBottomUp(tris)
e.EditorBindings.Set("bvh", bvh)
go buildBVH(m, e)
}
host.MeshCache().AddMesh(mesh)
drawing := rendering.Drawing{
Expand Down

0 comments on commit 1ec6774

Please sign in to comment.