浏览代码

Examples: Refactor webgl_interactive_points.

Mugen87 4 年之前
父节点
当前提交
81b969ea9a
共有 1 个文件被更改,包括 21 次插入13 次删除
  1. 21 13
      examples/webgl_interactive_points.html

+ 21 - 13
examples/webgl_interactive_points.html

@@ -57,6 +57,8 @@
 
 
 			import Stats from './jsm/libs/stats.module.js';
 			import Stats from './jsm/libs/stats.module.js';
 
 
+			import { BufferGeometryUtils } from './jsm/utils/BufferGeometryUtils.js';
+
 			let renderer, scene, camera, stats;
 			let renderer, scene, camera, stats;
 
 
 			let particles;
 			let particles;
@@ -80,19 +82,25 @@
 
 
 				//
 				//
 
 
-				const vertices = new THREE.BoxGeometry( 200, 200, 200, 16, 16, 16 ).vertices;
+				let boxGeometry = new THREE.BoxBufferGeometry( 200, 200, 200, 16, 16, 16 );
 
 
-				const positions = new Float32Array( vertices.length * 3 );
-				const colors = new Float32Array( vertices.length * 3 );
-				const sizes = new Float32Array( vertices.length );
+				// if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data
 
 
-				let vertex;
-				const color = new THREE.Color();
+				boxGeometry.deleteAttribute( 'normal' );
+				boxGeometry.deleteAttribute( 'uv' );
+
+				boxGeometry = BufferGeometryUtils.mergeVertices( boxGeometry );
+
+				//
+
+				const positionAttribute = boxGeometry.getAttribute( 'position' );
 
 
-				for ( let i = 0, l = vertices.length; i < l; i ++ ) {
+				const colors = [];
+				const sizes = [];
+
+				const color = new THREE.Color();
 
 
-					vertex = vertices[ i ];
-					vertex.toArray( positions, i * 3 );
+				for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
 
 
 					color.setHSL( 0.01 + 0.1 * ( i / l ), 1.0, 0.5 );
 					color.setHSL( 0.01 + 0.1 * ( i / l ), 1.0, 0.5 );
 					color.toArray( colors, i * 3 );
 					color.toArray( colors, i * 3 );
@@ -102,9 +110,9 @@
 				}
 				}
 
 
 				const geometry = new THREE.BufferGeometry();
 				const geometry = new THREE.BufferGeometry();
-				geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
-				geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
-				geometry.setAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
+				geometry.setAttribute( 'position', positionAttribute );
+				geometry.setAttribute( 'customColor', new THREE.Float32BufferAttribute( colors, 3 ) );
+				geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
 
 
 				//
 				//
 
 
@@ -112,7 +120,7 @@
 
 
 					uniforms: {
 					uniforms: {
 						color: { value: new THREE.Color( 0xffffff ) },
 						color: { value: new THREE.Color( 0xffffff ) },
-						pointTexture: { value: new THREE.TextureLoader().load( "textures/sprites/disc.png" ) }
+						pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ) }
 					},
 					},
 					vertexShader: document.getElementById( 'vertexshader' ).textContent,
 					vertexShader: document.getElementById( 'vertexshader' ).textContent,
 					fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
 					fragmentShader: document.getElementById( 'fragmentshader' ).textContent,