Skip to content

Commit

Permalink
🅱️ More builder updates
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Mar 1, 2024
1 parent 648e6bf commit 26d1515
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 33 deletions.
32 changes: 18 additions & 14 deletions dist-single/gsots3d.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist-single/gsots3d.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/builder/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ctx.start()
const builder = new RenderableBuilder()

const brick = Material.createBasicTexture('../_textures/brickwall.jpg', true)
const crate = Material.createBasicTexture('../_textures/crate.png', true)
const crate = Material.createBasicTexture('../_textures/STARG2.png', true)
const base = builder.newPart('pyramid-base', crate)
const sides = builder.newPart('pyramid-side', brick)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gsots3d",
"version": "0.0.5-alpha.8",
"version": "0.0.5-alpha.9",
"description": "Getting S**t On The Screen in 3D. A library for doing 3D graphics in the browser.",
"author": "Ben Coleman",
"license": "MIT",
Expand Down
7 changes: 5 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ export class Context {
this.instancesParticles.set(instance.id, instance)
Stats.instances++

log.debug(`✨ Created particle system`)

return { instance, particleSystem }
}

Expand Down Expand Up @@ -796,10 +798,11 @@ export class Context {
}

/**
*
* Build a instance of a custom renderable from a builder and add it to the scene
* @param builder Builder with
*/
createCustomInstance(builder: RenderableBuilder) {
const renderable = builder.buildAllParts(this.gl)
const renderable = builder.build(this.gl)
const instance = new Instance(renderable)

this.instances.set(instance.id, instance)
Expand Down
28 changes: 14 additions & 14 deletions src/renderable/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ import { ModelPart } from './model.ts'
* const renderable = builder.build(gl)
*/
export class RenderableBuilder {
private builderParts: Map<string, BuilderPart>
public readonly parts: Map<string, BuilderPart>
private materials: Map<string, Material>

constructor() {
this.builderParts = new Map<string, BuilderPart>()
this.parts = new Map<string, BuilderPart>()
this.materials = new Map<string, Material>()
}

/**
* Create and add a 'part' each part should have a unique name, and material to apply to that part
* Vertex mesh data is then added to the part
* Create and add a 'part', each part should have a unique name, and material to apply to it
* Vertex mesh data is then added to the part, with addQuad and addTriangle
* @param name Name of this part, just a string can be anything
* @param material Material to attach and apply to all surfaces in this part
*/
newPart(name: string, material: Material): BuilderPart {
if (this.builderParts.has(name)) {
if (this.parts.has(name)) {
throw new Error('Builder part name exists!')
}

const builderPart = new BuilderPart()

this.builderParts.set(name, builderPart)
this.parts.set(name, builderPart)
this.materials.set(name, material)

return builderPart
Expand All @@ -58,15 +58,15 @@ export class RenderableBuilder {
* Called after all parts are ready, to generate a CustomRenderable
* @param gl A WebGL2RenderingContext
*/
buildAllParts(gl: WebGL2RenderingContext): CustomRenderable {
build(gl: WebGL2RenderingContext): CustomRenderable {
const buffers = new Map<string, twgl.BufferInfo>()

for (const [name, builderPart] of this.builderParts) {
buffers.set(name, builderPart.build(gl))
for (const [name, builderPart] of this.parts) {
const partBuffers = builderPart.build(gl)
if (!partBuffers) continue
buffers.set(name, partBuffers)
}

console.log(buffers)

return new CustomRenderable(buffers, this.materials)
}
}
Expand Down Expand Up @@ -158,17 +158,17 @@ export class BuilderPart {
* @param gl A WebGL2 rendering context
* @returns BufferInfo used by twgl
*/
build(gl: WebGL2RenderingContext): twgl.BufferInfo {
build(gl: WebGL2RenderingContext): twgl.BufferInfo | null {
let bufferInfo: twgl.BufferInfo
if (this._customArrayData) {
bufferInfo = twgl.createBufferInfoFromArrays(gl, this._customArrayData)
} else {
if (this.vertexData.length === 0) {
throw new Error('No vertices added to renderable')
return null
}

if (this.indexData.length === 0) {
throw new Error('No indices added to renderable')
return null
}

// This is where the magic happens
Expand Down

0 comments on commit 26d1515

Please sign in to comment.