vorg / geom-builder

Simplicial-complex-like geometry builder backed by typed arrays
MIT License
14 stars 2 forks source link

Ideas for generalised version 3 #3

Open vorg opened 4 years ago

vorg commented 4 years ago

Currently we come with assumptions about supported attributes e.g. that positions are always enabled

const builder = createGeomBuilder({ colors: 4, cells: 3 })

builder.addPosition([0, 0, 0])
builder.addColor([0, 0, 0, 1])
builder.count //1
builder.positions //Float32Array [0, 0, 0]

a more generic API could be as follows:

const builder = createGeomBuilder({ positions: 3, colors: 4, cells: { stride: 3, type: Uint32Array }})

builder.add('positions', [0.0, 0.0, 0.0])
builder.add('colors', [0.0, 0.0, 0.0, 1.0])
builder.add('cells', [0, 1, 2])

buidler.positions //Float32Array [0, 0, 0]

The builder.add('positions', []) fees weird, it should be builder.add('position', []) but then how do we guess the name? By adding s automatically?

Also

builder.positions

//vs 

builder.get('position')

Also if we change API so much this should be new module e.g. auto-expanding-buffer. What makes it geometry specific is assumption that all attributes grow at the same rate which is actually not always true (e.g. instancing). Would the api be then builder.count('position') and or builder.count('cells')?

auto-buffer

auto-buffer auto expanding buffer. Is there something similar on npm already?

That also leads to builder.resetAll() and more granular builder.reset('position')... which leads to fact that maybe we need builder per buffer?

const autoBuffer = require('auto-buffer') //free npm name!
const positions = autoBuffer() //no predefined stride/size, probably starts with length 32
positions.add([0, 0, 0])
positions.count //undefined
positions.length //3, actual used length since last reset, not allocated buffer size
let count = positions.length / 3 //diy
positions.reset()
vorg commented 4 years ago

@dmnsgn would you find proposed auto-buffer useful?

dmnsgn commented 4 years ago

Yes I would. Any way to have it as a bunch of static functions like pex-math (but also keeping a reference to build buffer and update count)?

I imagine there's a package already but couldn't find it.

vorg commented 4 years ago

I imagine having only two functions add/reset and the object itself as {data, length}. Not sure if API like this is much helpful:

var gar = require('growing-array')
var arr = gar.create({ type: Float32Array, count: 32 }) //{data, length}
gar.push(arr, [0.0, 1.0, 2.0])
console.log(arr.length) //3
console.log(arr.data.length) //32
gar.reset(arr)

Because vec3 as [x, y, z] is standalone and useful while {data, length} is not.

vorg commented 4 years ago

One has to be also careful with name choosing. We use it for buffers that are actually (typed)arrays.