-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved around the batching code to use the mesh, geometry, material cl…
…asses
- Loading branch information
Showing
6 changed files
with
291 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import BatchInstance from "./batch-instance.mjs"; | ||
import { Geometry } from "./geometry.mjs"; | ||
import MeshHandler from "./mesh-handler.mjs"; | ||
|
||
/** | ||
* Class for managing Buffers | ||
*/ | ||
class BatchGeometry extends Geometry { | ||
|
||
/** @type {Array<BatchInstance>} */ | ||
batchInstances; | ||
/** @type {Number} */ | ||
batchSize; | ||
|
||
/** | ||
* Constructs a new BufferManager | ||
* | ||
* @param {WebGLRenderingContext} gl Rendering Context | ||
* @param {MeshHandler} meshHandler Mesh handler for data of each batch instance | ||
* @param {Number} batchSize Maximum number of batch instances that be can loaded in a single draw call | ||
*/ | ||
constructor(gl, meshHandler, batchSize) { | ||
super(gl); | ||
|
||
this.batchSize = batchSize; | ||
|
||
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer); | ||
gl.bufferData(gl.ARRAY_BUFFER, (meshHandler.vertexArray.length * Float32Array.BYTES_PER_ELEMENT) * batchSize, gl.DYNAMIC_DRAW); | ||
gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer); | ||
gl.bufferData(gl.ARRAY_BUFFER, (meshHandler.normalArray.length * Float32Array.BYTES_PER_ELEMENT) * batchSize, gl.DYNAMIC_DRAW); | ||
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordBuffer); | ||
gl.bufferData(gl.ARRAY_BUFFER, (meshHandler.texCoordArray.length * Float32Array.BYTES_PER_ELEMENT) * batchSize, gl.DYNAMIC_DRAW); | ||
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); | ||
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, (meshHandler.indexArray.length * Uint32Array.BYTES_PER_ELEMENT) * batchSize, gl.DYNAMIC_DRAW); | ||
} | ||
|
||
/** | ||
* Source(s): | ||
* https://docs.gl/es2/ | ||
* | ||
* @param {WebGLRenderingContext} gl - WebGL Context | ||
* @param {Number} initialIndex - Index to start loading instances from | ||
* | ||
* @returns {Number} - loadedCount: Number of BatchInstance objects loaded | ||
*/ | ||
loadBatchInstances(gl, initialIndex = 0) { | ||
const instancesToLoad = Math.min(this.batchSize, this.batchInstances.length - initialIndex); | ||
|
||
let loadedCount = 0; | ||
|
||
let totalVertexOffset = 0; | ||
let totalIndexOffset = 0; | ||
let totalNormalOffset = 0; | ||
let totalTexCoordOffset = 0; | ||
|
||
for (let i = initialIndex; i < initialIndex + instancesToLoad; i++) { | ||
const batchInstance = this.batchInstances[i]; | ||
|
||
if (batchInstance === undefined) { | ||
console.log("Batch @ index " + i + " is undefined"); | ||
} | ||
|
||
batchInstance.writeInstanceToBuffer( | ||
gl, | ||
totalVertexOffset, this.positionBuffer, | ||
totalIndexOffset, this.indexBuffer, | ||
totalNormalOffset, this.normalBuffer, | ||
totalTexCoordOffset, this.textureCoordBuffer | ||
); | ||
|
||
// Increment offsets by the instance's buffer size | ||
totalVertexOffset += batchInstance.getVertexBufferSize(); | ||
totalIndexOffset += batchInstance.getIndexBufferSize(); | ||
totalNormalOffset += batchInstance.getNormalBufferSize(); | ||
totalTexCoordOffset += batchInstance.getTexCoordBufferSize(); | ||
|
||
loadedCount++; | ||
} | ||
|
||
return loadedCount; | ||
} | ||
|
||
/** | ||
* Draws the index buffer with provided other buffers | ||
* | ||
* @param {WebGLRenderingContext} gl Rendering Context | ||
*/ | ||
draw(gl) { | ||
|
||
if (this.batchInstances.length === 0) { | ||
return; | ||
} | ||
|
||
var totalLoaded = 0; | ||
do { | ||
const loaded = this.loadBatchInstances(gl, totalLoaded); | ||
totalLoaded++; | ||
gl.drawElements(gl.TRIANGLES, this.batchInstances[0].meshData.indexArray.length * loaded, gl.UNSIGNED_INT, 0); | ||
} while (totalLoaded < this.batchInstances.length); | ||
} | ||
|
||
deleteBuffers() { | ||
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, 0); | ||
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, 0); | ||
|
||
this.gl.deleteBuffer(this.batchBuffer.vertexBuffer); | ||
this.gl.deleteBuffer(this.batchBuffer.indexBuffer); | ||
this.gl.deleteBuffer(this.batchBuffer.normalBuffer); | ||
this.gl.deleteBuffer(this.batchBuffer.texCoordBuffer); | ||
this.batchBuffer.meshHandler = null; | ||
} | ||
} | ||
|
||
export default BatchGeometry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import MeshHandler from "./mesh-handler.mjs"; | ||
|
||
/** | ||
* Handle mesh handlers with associated WebGLBuffers | ||
*/ | ||
export class Geometry { | ||
|
||
/** @type {WebGLBuffer} */ | ||
positionBuffer; | ||
/** @type {WebGLBuffer} */ | ||
normalBuffer; | ||
/** @type {WebGLBuffer} */ | ||
textureCoordBuffer; | ||
/** @type {WebGLBuffer} */ | ||
indexBuffer; | ||
|
||
/** | ||
* Constructs a new Geometry object | ||
* | ||
* @param {WebGLRenderingContext} gl Rendering Context | ||
*/ | ||
constructor(gl) { | ||
this.positionBuffer = gl.createBuffer(); | ||
this.normalBuffer = gl.createBuffer(); | ||
this.textureCoordBuffer = gl.createBuffer(); | ||
this.indexBuffer = gl.createBuffer(); | ||
} | ||
|
||
/** | ||
* Draws the index buffer with provided other buffers | ||
* | ||
* @param {WebGLRenderingContext} gl Rendering Context | ||
* | ||
* @abstract | ||
*/ | ||
draw(gl) { | ||
console.error("Must be overriden"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Geometry } from "./geometry.mjs"; | ||
import { Material } from "./materials.mjs"; | ||
|
||
export class Mesh { | ||
|
||
/** @type {Geometry} */ | ||
geometry; | ||
/** @type {Material} */ | ||
material; | ||
|
||
constructor(geometry, material) { | ||
this.geometry = geometry; | ||
this.material = material; | ||
} | ||
|
||
/** | ||
* Draws the mesh to the scene | ||
* | ||
* @param {WebGLRenderingContext} gl Rendering Context | ||
*/ | ||
draw(gl) { | ||
this.material.useShaderProgram(gl); | ||
this.material.loadUniforms(gl); | ||
this.material.bindVertexPointers( | ||
gl, | ||
this.geometry.positionBuffer, | ||
this.geometry.normalBuffer, | ||
this.geometry.textureCoordBuffer, | ||
this.geometry.indexBuffer | ||
); | ||
this.geometry.draw(gl); | ||
} | ||
} |
Oops, something went wrong.