Skip to content

Commit 0368a7a

Browse files
author
aardgoose
committed
support InstancedMesh and updateAttribute()
1 parent 6ac9f4c commit 0368a7a

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

examples/jsm/renderers/webgl/WebGLBackend.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,16 @@ class WebGLBackend extends Backend {
190190

191191
//
192192

193+
const instanceCount = this.getInstanceCount( renderObject );
193194

194195
if ( index !== null ) {
195196

196197
const indexData = this.get( index );
197-
198198
const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
199199

200-
if ( geometry.isInstancedBufferGeometry ) {
200+
if ( instanceCount > 1 ) {
201201

202-
gl.drawElementsInstanced( mode, index.count, indexData.type, firstVertex, geometry.instanceCount );
202+
gl.drawElementsInstanced( mode, index.count, indexData.type, firstVertex, instanceCount );
203203

204204
} else {
205205

@@ -215,9 +215,9 @@ class WebGLBackend extends Backend {
215215
const positionAttribute = geometry.attributes.position;
216216
const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
217217

218-
if ( geometry.isInstancedBufferGeometry ) {
218+
if ( instanceCount > 1 ) {
219219

220-
gl.drawArraysInstanced( mode, 0, vertexCount, geometry.instanceCount );
220+
gl.drawArraysInstanced( mode, 0, vertexCount, instanceCount );
221221

222222
} else {
223223

@@ -517,7 +517,7 @@ class WebGLBackend extends Backend {
517517

518518
gl.vertexAttribPointer( i, attribute.itemSize, attributeData.type, false, stride, offset );
519519

520-
if ( attribute.isInstancedBufferAttribute ) {
520+
if ( attribute.isInstancedBufferAttribute && ! attribute.isInterleavedBufferAttribute ) {
521521

522522
gl.vertexAttribDivisor( i, attribute.meshPerAttribute );
523523

@@ -632,9 +632,9 @@ class WebGLBackend extends Backend {
632632

633633
}
634634

635-
updateAttribute( /*attribute*/ ) {
635+
updateAttribute( attribute ) {
636636

637-
console.warn( 'Abstract class.' );
637+
this.attributeUtils.updateAttribute( attribute );
638638

639639
}
640640

examples/jsm/renderers/webgl/nodes/GLSLNodeBuilder.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const precisionLib = {
1313
high: 'highp'
1414
};
1515

16+
const supports = {
17+
instance: true
18+
};
19+
1620
class GLSLNodeBuilder extends NodeBuilder {
1721

1822
constructor( object, renderer, scene = null ) {
@@ -139,7 +143,7 @@ class GLSLNodeBuilder extends NodeBuilder {
139143

140144
if ( shaderStage === 'vertex' ) {
141145

142-
const attributes = this.attributes;
146+
const attributes = this.getAttributesArray();
143147

144148
let location = 0;
145149

@@ -205,6 +209,13 @@ class GLSLNodeBuilder extends NodeBuilder {
205209

206210
}
207211

212+
isAvailable( name ) {
213+
214+
return supports[ name ] === true;
215+
216+
}
217+
218+
208219
isFlipY() {
209220

210221
return true;

examples/jsm/renderers/webgl/utils/WebGLAttributeUtils.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ class WebGLAttributeUtils {
33
constructor( backend ) {
44

55
this.backend = backend;
6-
this.buffers = new WeakMap();
76

87
}
98

@@ -15,7 +14,10 @@ class WebGLAttributeUtils {
1514
const array = attribute.array;
1615
const usage = attribute.usage || gl.STATIC_DRAW;
1716

18-
let bufferGPU = this.buffers.get( array );
17+
const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
18+
const bufferData = backend.get( bufferAttribute );
19+
20+
let bufferGPU = bufferData.bufferGPU;
1921

2022
if ( bufferGPU === undefined ) {
2123

@@ -25,7 +27,9 @@ class WebGLAttributeUtils {
2527
gl.bufferData( bufferType, array, usage );
2628
gl.bindBuffer( bufferType, null );
2729

28-
this.buffers.set( array, bufferGPU );
30+
bufferData.bufferGPU = bufferGPU;
31+
bufferData.bufferType = bufferType;
32+
bufferData.version = bufferAttribute.version;
2933

3034
}
3135

@@ -82,12 +86,29 @@ class WebGLAttributeUtils {
8286
backend.set( attribute, {
8387
bufferGPU,
8488
type,
85-
bytesPerElement: array.BYTES_PER_ELEMENT,
86-
version: attribute.version
89+
bytesPerElement: array.BYTES_PER_ELEMENT
8790
} );
8891

8992
}
9093

94+
updateAttribute( attribute ) {
95+
96+
const backend = this.backend;
97+
const { gl } = backend;
98+
99+
const array = attribute.array;
100+
const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
101+
const bufferData = backend.get( bufferAttribute );
102+
const bufferType = bufferData.bufferType;
103+
104+
gl.bindBuffer( bufferType, bufferData.bufferGPU );
105+
gl.bufferSubData( bufferType, 0, array );
106+
gl.bindBuffer( bufferType, null );
107+
108+
bufferData.version = bufferAttribute.version++;
109+
110+
}
111+
91112
}
92113

93114
export default WebGLAttributeUtils;

0 commit comments

Comments
 (0)