提交 3e94be06 编写于 作者: M Mugen87

Added XYZLoader.

上级 45b0103e
......@@ -129,6 +129,7 @@
"webgl_loader_vrml",
"webgl_loader_vtk",
"webgl_loader_x",
"webgl_loader_xyz",
"webgl_lod",
"webgl_marchingcubes",
"webgl_materials",
......
import {
BufferGeometry,
Loader,
LoadingManager
} from '../../../src/Three';
export class XYZLoader extends Loader {
constructor( manager?: LoadingManager );
load( url: string, onLoad: ( geometry: BufferGeometry ) => void, onProgress?: ( event: ProgressEvent ) => void, onError?: ( event: ErrorEvent ) => void ): void;
parse( data: string, onLoad: ( geometry: BufferGeometry ) => void ): object;
}
import {
BufferGeometry,
FileLoader,
Float32BufferAttribute,
Loader
} from '../../../build/three.module.js';
class XYZLoader extends Loader {
load( url, onLoad, onProgress, onError ) {
const scope = this;
const loader = new FileLoader( this.manager );
loader.setPath( this.path );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( this.withCredentials );
loader.load( url, function ( text ) {
try {
onLoad( scope.parse( text ) );
} catch ( e ) {
if ( onError ) {
onError( e );
} else {
console.error( e );
}
scope.manager.itemError( url );
}
}, onProgress, onError );
}
parse( text ) {
const lines = text.split( '\n' );
const vertices = [];
const colors = [];
for ( let line of lines ) {
line = line.trim();
if ( line.charAt( 0 ) === '#' ) continue; // skip comments
const lineValues = line.split( /\s+/ );
if ( lineValues.length === 3 ) {
// XYZ
vertices.push( parseFloat( lineValues[ 0 ] ) );
vertices.push( parseFloat( lineValues[ 1 ] ) );
vertices.push( parseFloat( lineValues[ 2 ] ) );
}
if ( lineValues.length === 6 ) {
// XYZRGB
vertices.push( parseFloat( lineValues[ 0 ] ) );
vertices.push( parseFloat( lineValues[ 1 ] ) );
vertices.push( parseFloat( lineValues[ 2 ] ) );
colors.push( parseFloat( lineValues[ 3 ] ) / 255 );
colors.push( parseFloat( lineValues[ 4 ] ) / 255 );
colors.push( parseFloat( lineValues[ 5 ] ) / 255 );
}
}
const geometry = new BufferGeometry();
geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
if ( colors.length > 0 ) {
geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
}
return geometry;
}
}
export { XYZLoader };
# helix_201.xyz
#
0.3517846 -0.7869986 -2.873479
0.5057634 -0.7079139 -2.871073
0.6422613 -0.5988617 -2.868600
0.7554799 -0.4636500 -2.866056
0.8404377 -0.3072074 -2.863440
0.8931900 -0.1353982 -2.860748
0.9110007 4.5183171E-02 -2.857978
0.8924776 0.2274645 -2.855126
0.8376509 0.4041732 -2.852190
0.7480025 0.5681100 -2.849165
0.6264315 0.7124491 -2.846050
0.4771712 0.8310061 -2.842839
0.3056472 0.9185020 -2.839530
0.1182791 0.9707931 -2.836119
-7.7742465E-02 0.9850534 -2.832601
-0.2747397 0.9599173 -2.828971
-0.4648297 0.8955631 -2.825226
-0.6402391 0.7937393 -2.821360
-0.7936199 0.6577247 -2.817368
-0.9183395 0.4922375 -2.813246
-1.008770 0.3032688 -2.808988
-1.060525 9.7884014E-02 -2.804586
-1.070667 -0.1160435 -2.800037
-1.037852 -0.3301237 -2.795332
-0.9624209 -0.5357682 -2.790466
-0.8464230 -0.7245325 -2.785430
-0.6935734 -0.8884509 -2.780218
-0.5091438 -1.020364 -2.774821
-0.2997888 -1.114221 -2.769231
-7.3318549E-02 -1.165346 -2.763439
0.1615903 -1.170658 -2.757435
0.3957134 -1.128828 -2.751210
0.6196325 -1.040377 -2.744753
0.8241052 -0.9076963 -2.738054
1.000432 -0.7350051 -2.731100
1.140819 -0.5282233 -2.723878
1.238709 -0.2947820 -2.716377
1.289071 -4.3367516E-02 -2.708581
1.288640 0.2163926 -2.700478
1.236092 0.4742811 -2.692050
1.132149 0.7198882 -2.683281
0.9796010 0.9430137 -2.674155
0.7832562 1.134079 -2.664653
0.5498004 1.284522 -2.654754
0.2875895 1.387166 -2.644440
6.3577644E-03 1.436542 -2.633687
-0.2831245 1.429154 -2.622472
-0.5694501 1.363673 -2.610770
-0.8410097 1.241050 -2.598557
-1.086441 1.064551 -2.585803
-1.295096 0.8396897 -2.572479
-1.457477 0.5740758 -2.558554
-1.565650 0.2771796 -2.543995
-1.613611 -3.9990790E-02 -2.528766
-1.597577 -0.3652730 -2.512832
-1.516212 -0.6858066 -2.496151
-1.370756 -0.9885159 -2.478682
-1.165054 -1.260625 -2.460381
-0.9054906 -1.490177 -2.441200
-0.6008227 -1.666533 -2.421091
-0.2619001 -1.780845 -2.400000
9.8691247E-02 -1.826468 -2.377872
0.4670753 -1.799298 -2.354648
0.8285937 -1.698024 -2.330267
1.168371 -1.524281 -2.304664
1.471899 -1.282684 -2.277770
1.725636 -0.9807486 -2.249514
1.917587 -0.6286960 -2.219820
2.037840 -0.2391386 -2.188612
2.079050 0.1733468 -2.155806
2.036829 0.5927297 -2.121320
1.910035 1.002130 -2.085066
1.700944 1.384473 -2.046955
1.415291 1.723174 -2.006894
1.062158 2.002851 -1.964792
0.6537460 2.209998 -1.920553
0.2049761 2.333622 -1.874085
-0.2670151 2.365798 -1.825295
-0.7435041 2.302130 -1.774091
-1.204912 2.142068 -1.720387
-1.631593 1.889093 -1.664101
-2.004674 1.550727 -1.605156
-2.306892 1.138374 -1.543487
-2.523408 0.6669796 -1.479038
-2.642545 0.1545199 -1.411765
-2.656429 -0.3786647 -1.341641
-2.561478 -0.9106821 -1.268656
-2.358729 -1.419004 -1.192822
-2.053959 -1.881455 -1.114172
-1.657589 -2.277234 -1.032764
-1.184375 -2.587906 -0.9486833
-0.6528603 -2.798331 -0.8620437
-8.4640868E-02 -2.897469 -0.7729880
0.4965631 -2.879015 -0.6816889
1.065963 -2.741819 -0.5883484
1.598853 -2.490066 -0.4931970
2.071785 -2.133190 -0.3964912
2.463719 -1.685521 -0.2985111
2.757063 -1.165668 -0.1995571
2.938568 -0.5956771 -9.9944495E-02
3.000000 0.0000000E+00 0.0000000E+00
2.938568 0.5956771 9.9944495E-02
2.757063 1.165668 0.1995571
2.463719 1.685521 0.2985111
2.071785 2.133190 0.3964912
1.598853 2.490066 0.4931970
1.065963 2.741819 0.5883484
0.4965631 2.879015 0.6816889
-8.4640868E-02 2.897469 0.7729880
-0.6528603 2.798331 0.8620437
-1.184375 2.587906 0.9486833
-1.657589 2.277234 1.032764
-2.053959 1.881455 1.114172
-2.358729 1.419004 1.192822
-2.561478 0.9106821 1.268656
-2.656429 0.3786647 1.341641
-2.642545 -0.1545199 1.411765
-2.523408 -0.6669796 1.479038
-2.306892 -1.138374 1.543487
-2.004674 -1.550727 1.605156
-1.631593 -1.889093 1.664101
-1.204912 -2.142068 1.720387
-0.7435041 -2.302130 1.774091
-0.2670151 -2.365798 1.825295
0.2049761 -2.333622 1.874085
0.6537460 -2.209998 1.920553
1.062158 -2.002851 1.964792
1.415291 -1.723174 2.006894
1.700944 -1.384473 2.046955
1.910035 -1.002130 2.085066
2.036829 -0.5927297 2.121320
2.079050 -0.1733468 2.155806
2.037840 0.2391386 2.188612
1.917587 0.6286960 2.219820
1.725636 0.9807486 2.249514
1.471899 1.282684 2.277770
1.168371 1.524281 2.304664
0.8285937 1.698024 2.330267
0.4670753 1.799298 2.354648
9.8691247E-02 1.826468 2.377872
-0.2619001 1.780845 2.400000
-0.6008227 1.666533 2.421091
-0.9054906 1.490177 2.441200
-1.165054 1.260625 2.460381
-1.370756 0.9885159 2.478682
-1.516212 0.6858066 2.496151
-1.597577 0.3652730 2.512832
-1.613611 3.9990790E-02 2.528766
-1.565650 -0.2771796 2.543995
-1.457477 -0.5740758 2.558554
-1.295096 -0.8396897 2.572479
-1.086441 -1.064551 2.585803
-0.8410097 -1.241050 2.598557
-0.5694501 -1.363673 2.610770
-0.2831245 -1.429154 2.622472
6.3577644E-03 -1.436542 2.633687
0.2875895 -1.387166 2.644440
0.5498004 -1.284522 2.654754
0.7832562 -1.134079 2.664653
0.9796010 -0.9430137 2.674155
1.132149 -0.7198882 2.683281
1.236092 -0.4742811 2.692050
1.288640 -0.2163926 2.700478
1.289071 4.3367516E-02 2.708581
1.238709 0.2947820 2.716377
1.140819 0.5282233 2.723878
1.000432 0.7350051 2.731100
0.8241052 0.9076963 2.738054
0.6196325 1.040377 2.744753
0.3957134 1.128828 2.751210
0.1615903 1.170658 2.757435
-7.3318549E-02 1.165346 2.763439
-0.2997888 1.114221 2.769231
-0.5091438 1.020364 2.774821
-0.6935734 0.8884509 2.780218
-0.8464230 0.7245325 2.785430
-0.9624209 0.5357682 2.790466
-1.037852 0.3301237 2.795332
-1.070667 0.1160435 2.800037
-1.060525 -9.7884014E-02 2.804586
-1.008770 -0.3032688 2.808988
-0.9183395 -0.4922375 2.813246
-0.7936199 -0.6577247 2.817368
-0.6402391 -0.7937393 2.821360
-0.4648297 -0.8955631 2.825226
-0.2747397 -0.9599173 2.828971
-7.7742465E-02 -0.9850534 2.832601
0.1182791 -0.9707931 2.836119
0.3056472 -0.9185020 2.839530
0.4771712 -0.8310061 2.842839
0.6264315 -0.7124491 2.846050
0.7480025 -0.5681100 2.849165
0.8376509 -0.4041732 2.852190
0.8924776 -0.2274645 2.855126
0.9110007 -4.5183171E-02 2.857978
0.8931900 0.1353982 2.860748
0.8404377 0.3072074 2.863440
0.7554799 0.4636500 2.866056
0.6422613 0.5988617 2.868600
0.5057634 0.7079139 2.871073
0.3517846 0.7869986 2.873479
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - loaders - XYZ</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="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - XYZ loader<br/>
asset from <a href="https://people.math.sc.edu/Burkardt/data/xyz/xyz.html" target="_blank" rel="noopener">people.math.sc.edu</a> via GNU LGPL
</div>
<script type="module">
import * as THREE from '../build/three.module.js';
import { XYZLoader } from './jsm/loaders/XYZLoader.js';
let camera, scene, renderer, clock;
let points;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.set( 10, 7, 10 );
scene = new THREE.Scene();
scene.add( camera );
camera.lookAt( scene.position );
clock = new THREE.Clock();
const loader = new XYZLoader();
loader.load( 'models/xyz/helix_201.xyz', function ( geometry ) {
geometry.center();
const material = new THREE.PointsMaterial( { size: 0.1 } );
points = new THREE.Points( geometry, material );
scene.add( points );
} );
//
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
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 );
const delta = clock.getDelta();
if ( points ) {
points.rotation.x += delta * 0.2;
points.rotation.y += delta * 0.5;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册