Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GLBufferAttribute #13196

Merged
merged 41 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
152c85f
Added onCreateCallback for WebGLAttributes #11883
raub Jan 29, 2018
7aed6c9
Revert "Added onCreateCallback for WebGLAttributes #11882"
raub Feb 10, 2018
db0fab0
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Feb 10, 2018
1911f1c
Added new buffer attribute class
raub Feb 10, 2018
adc861f
dynamic buffer attr : fixed significant update overhead
raub Feb 13, 2018
ae6dd4f
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Feb 13, 2018
36425d2
rename to ExternalBufferAttribute
raub Mar 16, 2018
2aa32bc
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Apr 18, 2018
6e26560
renamed to GLBufferAttribute, fixed lint errors
raub Apr 18, 2018
00deed9
WebGLAttributes use get/set method for GLBufferAttibute
raub Apr 18, 2018
79c66a2
fix lint error
raub Apr 18, 2018
bf283b5
added GLBufferAttribute to docs
raub Apr 18, 2018
bbc5638
changed GLBufferAttribute example title
raub Apr 18, 2018
b5bcd51
fixed missing space in GLBufferAttribute example
raub Apr 18, 2018
9359e2f
fixed info title in GLBufferAttribute example
raub Apr 18, 2018
920caf3
Merge branch 'master' of https://github.com/mrdoob/three.js into dev
raub Aug 24, 2018
368d1fd
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Aug 24, 2018
f8f0b41
Add culling warnings for GLBufferAttribute, update the example
raub Aug 27, 2018
ddf9406
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Aug 27, 2018
4b5c8ee
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Nov 25, 2018
54c6ef8
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Dec 5, 2018
461f4f6
Fix GLBufferAttribute example
raub Dec 5, 2018
15a8615
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Jan 23, 2019
0c95418
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Feb 20, 2019
70aee41
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Aug 9, 2019
9ba4d01
Add parameter to the constructor
raub Aug 9, 2019
01356f3
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Nov 15, 2019
1d73a66
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Nov 24, 2019
a0f4661
Update GLBufferAttribute
raub Apr 26, 2020
35d9f1e
Merge branch 'master' of https://github.com/mrdoob/three.js into dev
raub Apr 26, 2020
8df321f
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Apr 26, 2020
3bc4a2a
Add example screenshot
raub Apr 26, 2020
0064c2b
Fix e2e test
raub Apr 26, 2020
419e9cb
Fix docs list entry
raub Apr 27, 2020
93a7c58
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Apr 27, 2020
4aed9dc
Add zh doc entry
raub Apr 27, 2020
8282023
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Aug 2, 2020
c16eb64
Update GLBufferAttribute
raub Aug 2, 2020
727e89b
Update screenshot for GLBufferAttribute example
raub Aug 2, 2020
7860f32
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Aug 2, 2020
c73e993
Merge branch 'dev' of https://github.com/mrdoob/three.js into dev
raub Aug 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions docs/api/en/core/GLBufferAttribute.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<base href="../../" />
<script src="list.js"></script>
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>

<h1>[name]</h1>

<p class="desc">
This buffer attribute class does not construct a VBO. Instead, it uses
whatever VBO is passed in constructor and can later be altered via the
*buffer* property.<br /><br />
It is required to pass additional params alongside the VBO. Those are:
the GL context, the GL data type, the number of components per vertex
and the number of vertices.<br /><br />
The most common use case for this class is when some kind of GPGPU
calculation interferes or even produces the VBOs in question.
</p>

<h2>Constructor</h2>
<h3>[name]( [param:WebGLRenderingContext gl], [param:WebGLBuffer buffer], [param:GLenum type], [param:Integer itemSize], [param:Integer count], [param:Boolean normalized] )</h3>
<p>
gl — Must be a [link:https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext].
<br />
buffer — Must be a [link:https://developer.mozilla.org/en-US/docs/Web/API/WebGLBuffer].
<br />
type — One of [link:https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants#Data_types].
<br />
itemSize — The number of values of the array that should be associated with
a particular vertex. For instance, if this
attribute is storing a 3-component vector (such as a position, normal, or color), then itemSize should be 3.
<br />
count — The expected number of vertices in VBO.
<br /><br />

normalized — (optional) Applies to integer data only. Indicates how the underlying data
in the buffer maps to the values in the GLSL code. For instance, if [page:TypedArray array] is an instance
of UInt16Array, and [page:Boolean normalized] is true, the values 0 - +65535 in the array
data will be mapped to 0.0f - +1.0f in the GLSL attribute. An Int16Array (signed) would map
from -32767 - +32767 to -1.0f - +1.0f. If [page:Boolean normalized] is false, the values
will be converted to floats unmodified, i.e. 32767 becomes 32767.0f.
</p>

<h2>Properties</h2>

<h3>[property:WebGLBuffer buffer]</h3>
<p>
The current <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLBuffer" target="_blank">WebGLBuffer</a> instance.
</p>

<h3>[property:Integer count]</h3>
<p>
The expected number of vertices in VBO.
</p>

<h3>[property:Boolean normalized]</h3>
<p>
Indicates how the underlying data in the buffer maps to the values in the GLSL shader code.
See the constructor above for details.
</p>

<h3>[property:Integer itemSize]</h3>
<p>
How many values make up each item (vertex).
</p>

<h3>[property:Integer elementSize]</h3>
<p>
Stores the corresponding size in bytes for the current *type* property value.
</p>
<p>
<b>Don't touch</b>.
</p>

<h3>[property:GLenum type]</h3>
<p>
A <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants#Standard_WebGL_1_constants" target="_blank">WebGL Data Type</a> describing the underlying VBO contents.
</p>
<p>
Setting this is only allowed with *setType*.
</p>

<h3>[property:Boolean isGLBufferAttribute]</h3>
<p>
Should be *true*.
</p>

<h2>Methods</h2>

<h3>[method:null setBuffer]( buffer ) </h3>
<p>Sets the *buffer* property.</p>

<h3>[method:null setType]( type ) </h3>
<p>Sets the *type* property.</p>

<h3>[method:null setItemSize]( itemSize ) </h3>
<p>Sets the *itemSize* property.</p>

<h3>[method:null setCount]( count ) </h3>
<p>Sets the *count* property.</p>



<h2>Source</h2>

[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
</body>
</html>
1 change: 1 addition & 0 deletions docs/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var list = {
"EventDispatcher": "api/en/core/EventDispatcher",
"Face3": "api/en/core/Face3",
"Geometry": "api/en/core/Geometry",
"GLBufferAttribute": "api/en/core/GLBufferAttribute",
"InstancedBufferAttribute": "api/en/core/InstancedBufferAttribute",
"InstancedBufferGeometry": "api/en/core/InstancedBufferGeometry",
"InstancedInterleavedBuffer": "api/en/core/InstancedInterleavedBuffer",
Expand Down
1 change: 1 addition & 0 deletions examples/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ var files = {
"webgl_buffergeometry_lines_indexed",
"webgl_buffergeometry_morphtargets",
"webgl_buffergeometry_points",
"webgl_buffergeometry_points_glbufferattribute",
"webgl_buffergeometry_points_interleaved",
"webgl_buffergeometry_rawshader",
"webgl_buffergeometry_selective_draw",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
178 changes: 178 additions & 0 deletions examples/webgl_buffergeometry_points_glbufferattribute.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - buffergeometry - custom VBOs</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>

<div id="container"></div>
<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - custom VBOs</div>

<script type="module">

import * as THREE from '../build/three.module.js';

import Stats from './jsm/libs/stats.module.js';

var container, stats;

var camera, scene, renderer;

var points;

init();
animate();

function init() {

container = document.getElementById( 'container' );

//

renderer = new THREE.WebGLRenderer( { antialias: false } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );

container.appendChild( renderer.domElement );

//

camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 5, 3500 );
camera.position.z = 2750;

scene = new THREE.Scene();
scene.background = new THREE.Color( 0x050505 );
scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );

//

var particles = 500000;

var geometry = new THREE.BufferGeometry();

var positions = [];
var positions2 = [];
var colors = [];

var color = new THREE.Color();

var n = 1000, n2 = n / 2; // particles spread in the cube

for ( var i = 0; i < particles; i ++ ) {

// positions

var x = Math.random() * n - n2;
var y = Math.random() * n - n2;
var z = Math.random() * n - n2;

positions.push( x, y, z );
positions2.push( z, x, y );

// colors

var vx = ( x / n ) + 0.5;
var vy = ( y / n ) + 0.5;
var vz = ( z / n ) + 0.5;

color.setRGB( vx, vy, vz );

colors.push( color.r, color.g, color.b );

}

var gl = renderer.getContext();

var pos = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, pos );
gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( positions ), gl.STATIC_DRAW );

var pos2 = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, pos2 );
gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( positions2 ), gl.STATIC_DRAW );

var rgb = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, rgb );
gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( colors ), gl.STATIC_DRAW );

var posAttr = new THREE.GLBufferAttribute( gl, pos, gl.FLOAT, 3, particles );
geometry.setAttribute( 'position', posAttr );

setInterval( function () {

posAttr.buffer = ( posAttr.buffer === pos ) ? pos2 : pos;
posAttr.needsUpdate = true;

}, 2000 );

var drawCount = 0;
setInterval( function () {

drawCount = 1 + ( drawCount + Math.floor( 1000 * Math.random() ) ) % particles;
geometry.setDrawRange( 0, drawCount );

}, 20 );

geometry.setAttribute( 'color', new THREE.GLBufferAttribute( gl, rgb, gl.FLOAT, 3, particles ) );

//

var material = new THREE.PointsMaterial( { size: 15, vertexColors: THREE.VertexColors } );

points = new THREE.Points( geometry, material );

// Choose one:
// geometry.boundingSphere = ( new THREE.Sphere() ).set( new THREE.Vector3(), Infinity );
points.frustumCulled = false;

scene.add( points );

//

stats = new Stats();
container.appendChild( stats.dom );

//

window.addEventListener( 'resize', onWindowResize, false );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

//

function animate() {

requestAnimationFrame( animate );

render();
stats.update();

}

function render() {

var time = Date.now() * 0.001;

points.rotation.x = time * 0.25;
points.rotation.y = time * 0.5;

renderer.render( scene, camera );

}

</script>

</body>
</html>
1 change: 1 addition & 0 deletions src/Three.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export { InterleavedBufferAttribute } from './core/InterleavedBufferAttribute.js
export { InstancedInterleavedBuffer } from './core/InstancedInterleavedBuffer.js';
export { InterleavedBuffer } from './core/InterleavedBuffer.js';
export { InstancedBufferAttribute } from './core/InstancedBufferAttribute.js';
export { GLBufferAttribute } from './core/GLBufferAttribute.js';
export * from './core/BufferAttribute.js';
export { Face3 } from './core/Face3.js';
export { Object3D } from './core/Object3D.js';
Expand Down
25 changes: 24 additions & 1 deletion src/core/BufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,19 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
var position = this.attributes.position;
var morphAttributesPosition = this.morphAttributes.position;

if ( position && position.isGLBufferAttribute ) {
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

console.error( 'THREE.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box. Alternatively set "mesh.frustumCulled" to "false".', this );

this.boundingBox.set(
new Vector3( - Infinity, - Infinity, - Infinity ),
new Vector3( + Infinity, + Infinity, + Infinity )
);

return;

}

if ( position !== undefined ) {

this.boundingBox.setFromBufferAttribute( position );
Expand Down Expand Up @@ -601,7 +614,7 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy

if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {

console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
console.error( 'THREE.BufferGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );

}

Expand All @@ -618,6 +631,16 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
var position = this.attributes.position;
var morphAttributesPosition = this.morphAttributes.position;

if ( position && position.isGLBufferAttribute ) {

console.error( 'THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere. Alternatively set "mesh.frustumCulled" to "false".', this );

this.boundingSphere.set( new Vector3(), Infinity );

return;

}

if ( position ) {

// first, find the center of the bounding sphere
Expand Down
Loading