-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
/
XYZLoader.js
107 lines (64 loc) · 1.88 KB
/
XYZLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {
BufferGeometry,
Color,
FileLoader,
Float32BufferAttribute,
Loader,
SRGBColorSpace
} from 'three';
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 = [];
const color = new Color();
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 ] ) );
const r = parseFloat( lineValues[ 3 ] ) / 255;
const g = parseFloat( lineValues[ 4 ] ) / 255;
const b = parseFloat( lineValues[ 5 ] ) / 255;
color.setRGB( r, g, b, SRGBColorSpace );
colors.push( color.r, color.g, color.b );
}
}
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 };