forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWireframeGeometry.js
More file actions
188 lines (112 loc) · 3.83 KB
/
WireframeGeometry.js
File metadata and controls
188 lines (112 loc) · 3.83 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { BufferGeometry } from '../core/BufferGeometry';
import { BufferAttribute } from '../core/BufferAttribute';
/**
* @author mrdoob / http://mrdoob.com/
*/
function WireframeGeometry( geometry ) {
BufferGeometry.call( this );
var edge = [ 0, 0 ], hash = {};
function sortFunction( a, b ) {
return a - b;
}
var keys = [ 'a', 'b', 'c' ];
if ( (geometry && geometry.isGeometry) ) {
var vertices = geometry.vertices;
var faces = geometry.faces;
var numEdges = 0;
// allocate maximal size
var edges = new Uint32Array( 6 * faces.length );
for ( var i = 0, l = faces.length; i < l; i ++ ) {
var face = faces[ i ];
for ( var j = 0; j < 3; j ++ ) {
edge[ 0 ] = face[ keys[ j ] ];
edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ];
edge.sort( sortFunction );
var key = edge.toString();
if ( hash[ key ] === undefined ) {
edges[ 2 * numEdges ] = edge[ 0 ];
edges[ 2 * numEdges + 1 ] = edge[ 1 ];
hash[ key ] = true;
numEdges ++;
}
}
}
var coords = new Float32Array( numEdges * 2 * 3 );
for ( var i = 0, l = numEdges; i < l; i ++ ) {
for ( var j = 0; j < 2; j ++ ) {
var vertex = vertices[ edges [ 2 * i + j ] ];
var index = 6 * i + 3 * j;
coords[ index + 0 ] = vertex.x;
coords[ index + 1 ] = vertex.y;
coords[ index + 2 ] = vertex.z;
}
}
this.addAttribute( 'position', new BufferAttribute( coords, 3 ) );
} else if ( (geometry && geometry.isBufferGeometry) ) {
if ( geometry.index !== null ) {
// Indexed BufferGeometry
var indices = geometry.index.array;
var vertices = geometry.attributes.position;
var groups = geometry.groups;
var numEdges = 0;
if ( groups.length === 0 ) {
geometry.addGroup( 0, indices.length );
}
// allocate maximal size
var edges = new Uint32Array( 2 * indices.length );
for ( var o = 0, ol = groups.length; o < ol; ++ o ) {
var group = groups[ o ];
var start = group.start;
var count = group.count;
for ( var i = start, il = start + count; i < il; i += 3 ) {
for ( var j = 0; j < 3; j ++ ) {
edge[ 0 ] = indices[ i + j ];
edge[ 1 ] = indices[ i + ( j + 1 ) % 3 ];
edge.sort( sortFunction );
var key = edge.toString();
if ( hash[ key ] === undefined ) {
edges[ 2 * numEdges ] = edge[ 0 ];
edges[ 2 * numEdges + 1 ] = edge[ 1 ];
hash[ key ] = true;
numEdges ++;
}
}
}
}
var coords = new Float32Array( numEdges * 2 * 3 );
for ( var i = 0, l = numEdges; i < l; i ++ ) {
for ( var j = 0; j < 2; j ++ ) {
var index = 6 * i + 3 * j;
var index2 = edges[ 2 * i + j ];
coords[ index + 0 ] = vertices.getX( index2 );
coords[ index + 1 ] = vertices.getY( index2 );
coords[ index + 2 ] = vertices.getZ( index2 );
}
}
this.addAttribute( 'position', new BufferAttribute( coords, 3 ) );
} else {
// non-indexed BufferGeometry
var vertices = geometry.attributes.position.array;
var numEdges = vertices.length / 3;
var numTris = numEdges / 3;
var coords = new Float32Array( numEdges * 2 * 3 );
for ( var i = 0, l = numTris; i < l; i ++ ) {
for ( var j = 0; j < 3; j ++ ) {
var index = 18 * i + 6 * j;
var index1 = 9 * i + 3 * j;
coords[ index + 0 ] = vertices[ index1 ];
coords[ index + 1 ] = vertices[ index1 + 1 ];
coords[ index + 2 ] = vertices[ index1 + 2 ];
var index2 = 9 * i + 3 * ( ( j + 1 ) % 3 );
coords[ index + 3 ] = vertices[ index2 ];
coords[ index + 4 ] = vertices[ index2 + 1 ];
coords[ index + 5 ] = vertices[ index2 + 2 ];
}
}
this.addAttribute( 'position', new BufferAttribute( coords, 3 ) );
}
}
}
WireframeGeometry.prototype = Object.create( BufferGeometry.prototype );
WireframeGeometry.prototype.constructor = WireframeGeometry;
export { WireframeGeometry };