浏览代码

Fix problems with non floating type in InterleavedBufferAttribute. (#9190)

* Add 'normalized' bool and accessor to BufferAttribute array in InterleavedBufferAttribute

* Add webgl_buffergeometry_points_interleaved example

featuring InterleavedBuffer with different numercial type (float32 and
uint8)

* Change to match BufferAttribute syntax
Dvad 9 年之前
父节点
当前提交
97ffef18d5
共有 2 个文件被更改,包括 181 次插入1 次删除
  1. 172 0
      examples/webgl_buffergeometry_points_interleaved.html
  2. 9 1
      src/core/InterleavedBufferAttribute.js

+ 172 - 0
examples/webgl_buffergeometry_points_interleaved.html

@@ -0,0 +1,172 @@
+<!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">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 mesh;
+
+			init();
+			animate();
+
+			function init() {
+
+				container = document.getElementById( 'container' );
+
+				camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 5, 3500 );
+				camera.position.z = 2750;
+
+				scene = new THREE.Scene();
+				scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
+
+				//
+
+				var particles = 500000;
+
+				var geometry = new THREE.BufferGeometry();
+
+				var interleaved_buffer_float32 = new Float32Array( particles * 4 );
+				interleaved_buffer_uint8 = new Uint8Array(interleaved_buffer_float32.buffer)
+
+				var color = new THREE.Color();
+
+				var n = 1000, n2 = n / 2; // particles spread in the cube
+
+				for ( var i = 0; i < interleaved_buffer_float32.length; i += 4 ) {
+
+					// positions
+
+					var x = Math.random() * n - n2;
+					var y = Math.random() * n - n2;
+					var z = Math.random() * n - n2;
+
+					interleaved_buffer_float32[ i ]     = x;
+					interleaved_buffer_float32[ i + 1 ] = y;
+					interleaved_buffer_float32[ i + 2 ] = z;
+
+					// colors
+
+					var vx = ( x / n ) + 0.5;
+					var vy = ( y / n ) + 0.5;
+					var vz = ( z / n ) + 0.5;
+
+					color.setRGB( vx, vy, vz );
+
+					interleaved_buffer_uint8[ 4*(i + 3)+0] = color.r*255;
+					interleaved_buffer_uint8[ 4*(i + 3)+1] = color.g*255;
+					interleaved_buffer_uint8[ 4*(i + 3)+2] = color.b*255;
+					interleaved_buffer_uint8[ 4*(i + 3)+3] = 0;
+
+				}
+
+				ibp = new THREE.InterleavedBuffer(interleaved_buffer_float32, 4);
+				ibc = new THREE.InterleavedBuffer(interleaved_buffer_uint8, 16);
+				
+				geometry.addAttribute( 'position', new THREE.InterleavedBufferAttribute(ibp, 3, 0, false ) );
+				geometry.addAttribute( 'color', new THREE.InterleavedBufferAttribute(ibc, 3, 12, true ) );
+
+				geometry.computeBoundingSphere();
+
+				//
+
+				var material = new THREE.PointsMaterial( { size: 15, vertexColors: THREE.VertexColors } );
+
+				particleSystem = new THREE.Points( geometry, material );
+				scene.add( particleSystem );
+
+				//
+
+				renderer = new THREE.WebGLRenderer( { antialias: false } );
+				renderer.setClearColor( scene.fog.color );
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+				container.appendChild( renderer.domElement );
+
+				//
+
+				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;
+
+				particleSystem.rotation.x = time * 0.25;
+				particleSystem.rotation.y = time * 0.5;
+
+				renderer.render( scene, camera );
+
+			}
+
+		</script>
+
+	</body>
+</html>

+ 9 - 1
src/core/InterleavedBufferAttribute.js

@@ -2,7 +2,7 @@
  * @author benaadams / https://twitter.com/ben_a_adams
  */
 
-THREE.InterleavedBufferAttribute = function ( interleavedBuffer, itemSize, offset ) {
+THREE.InterleavedBufferAttribute = function ( interleavedBuffer, itemSize, offset, normalized ) {
 
 	this.uuid = THREE.Math.generateUUID();
 
@@ -10,6 +10,8 @@ THREE.InterleavedBufferAttribute = function ( interleavedBuffer, itemSize, offse
 	this.itemSize = itemSize;
 	this.offset = offset;
 
+	this.normalized = normalized === true;
+
 };
 
 
@@ -30,6 +32,12 @@ THREE.InterleavedBufferAttribute.prototype = {
 
 	},
 
+	get array() {
+
+		return this.data.array;
+
+	},
+
 	setX: function ( index, x ) {
 
 		this.data.array[ index * this.data.stride + this.offset ] = x;