Browse Source

Added new buffer attribute class

Luis Blanco 7 years ago
parent
commit
1911f1c106

+ 189 - 0
examples/webgl_buffergeometry_points_dynamicbufferattribute.html

@@ -0,0 +1,189 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgl - buffergeometry [particles]</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<style>
+			body {
+				color: #cccccc;
+				font-family:Monospace;
+				font-size:13px;
+				text-align:center;
+
+				background-color: #050505;
+				margin: 0px;
+				overflow: hidden;
+			}
+
+			#info {
+				position: absolute;
+				top: 0px; width: 100%;
+				padding: 5px;
+			}
+
+			a {
+				color: #0080ff;
+			}
+		</style>
+	</head>
+	<body>
+
+		<div id="container"></div>
+		<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - particles</div>
+
+		<script src="../build/three.js"></script>
+
+		<script src="js/Detector.js"></script>
+		<script src="js/libs/stats.min.js"></script>
+
+		<script>
+
+			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
+
+			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.context;
+
+				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.DynamicBufferAttribute( gl, pos, gl.FLOAT, 3, particles );
+				geometry.addAttribute( 'position', posAttr );
+				
+				setInterval(function () {
+					posAttr.buffer = (posAttr.buffer === pos) ? pos2 : pos;
+					posAttr.needsUpdate = true;
+				}, 2000);
+				
+				geometry.addAttribute( 'color', new THREE.DynamicBufferAttribute( gl, rgb, gl.FLOAT, 3, particles ) );
+
+				geometry.computeBoundingSphere();
+
+				//
+
+				var material = new THREE.PointsMaterial( { size: 15, vertexColors: THREE.VertexColors } );
+
+				points = new THREE.Points( geometry, material );
+				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 - 0
src/Three.js

@@ -90,6 +90,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 { DynamicBufferAttribute } from './core/DynamicBufferAttribute.js';
 export * from './core/BufferAttribute.js';
 export { Face3 } from './core/Face3.js';
 export { Object3D } from './core/Object3D.js';

+ 20 - 1
src/core/BufferGeometry.js

@@ -68,7 +68,7 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 
 	addAttribute: function ( name, attribute ) {
 
-		if ( ! ( attribute && attribute.isBufferAttribute ) && ! ( attribute && attribute.isInterleavedBufferAttribute ) ) {
+		if ( ! (attribute && (attribute.isBufferAttribute || attribute.isInterleavedBufferAttribute || attribute.isDynamicBufferAttribute) ) ) {
 
 			console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' );
 
@@ -598,6 +598,17 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 
 		var position = this.attributes.position;
 
+		if ( position && position.isDynamicBufferAttribute ) {
+
+			this.boundingBox.set(
+				new Vector3( - Infinity, - Infinity, - Infinity ),
+				new Vector3( + Infinity, + Infinity, + Infinity )
+			)
+
+			return;
+
+		}
+
 		if ( position !== undefined ) {
 
 			this.boundingBox.setFromBufferAttribute( position );
@@ -631,6 +642,14 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 
 			var position = this.attributes.position;
 
+			if ( position && position.isDynamicBufferAttribute ) {
+
+				this.boundingSphere.set(new Vector3(), Infinity)
+
+				return;
+
+			}
+
 			if ( position ) {
 
 				var center = this.boundingSphere.center;

+ 93 - 0
src/core/DynamicBufferAttribute.js

@@ -0,0 +1,93 @@
+import { _Math } from '../math/Math.js';
+
+/**
+ * @author raub / https://github.com/raub
+ */
+
+
+function DynamicBufferAttribute( gl, buffer, type, itemSize, count ) {
+
+	this.sizes = [
+		[gl.FLOAT, 4],
+		[gl.UNSIGNED_SHORT, 2],
+		[gl.SHORT, 2],
+		[gl.UNSIGNED_INT, 4],
+		[gl.INT, 4],
+		[gl.BYTE, 1],
+		[gl.UNSIGNED_BYTE, 1],
+	].reduce(function (accum, current) {
+		accum[current[0]] = current[1];
+		return accum;
+	}, {});
+
+	if ( ! this.sizes[type] ) {
+		throw new TypeError( 'THREE.DynamicBufferAttribute: unsupported GL data type.' );
+	}
+
+	this.uuid = _Math.generateUUID();
+
+	this.buffer = buffer;
+	this.type = type;
+	this.itemSize = itemSize;
+	this.elementSize = this.sizes[type];
+	this.count = count;
+
+	this.version = 0;
+
+}
+
+Object.defineProperty( DynamicBufferAttribute.prototype, 'needsUpdate', {
+
+	set: function ( value ) {
+
+		if ( value === true ) this.version ++;
+
+	}
+
+} );
+
+Object.assign( DynamicBufferAttribute.prototype, {
+
+	isDynamicBufferAttribute: true,
+
+	setBuffer: function ( buffer ) {
+
+		this.buffer = buffer;
+
+		return this;
+
+	},
+
+	setType: function ( type ) {
+
+		if ( ! sizes[type] ) {
+			throw new TypeError( 'THREE.DynamicBufferAttribute: unsupported GL data type.' );
+		}
+
+		this.type = type;
+		this.elementSize = this.sizes[type];
+
+		return this;
+
+	},
+
+	setItemSize: function ( itemSize ) {
+
+		this.itemSize = itemSize;
+
+		return this;
+
+	},
+
+	setCount: function ( count ) {
+
+		this.count = count;
+
+		return this;
+
+	},
+
+} );
+
+
+export { DynamicBufferAttribute };

+ 13 - 0
src/renderers/webgl/WebGLAttributes.js

@@ -123,6 +123,19 @@ function WebGLAttributes( gl ) {
 
 	function update( attribute, bufferType ) {
 
+		if (attribute.isDynamicBufferAttribute) {
+
+			buffers[ attribute.uuid ] = {
+				buffer: attribute.buffer,
+				type: attribute.type,
+				bytesPerElement: attribute.elementSize,
+				version: attribute.version
+			};
+
+			return;
+
+		}
+
 		if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
 
 		var data = buffers[ attribute.uuid ];