1 |
|
2 |
|
3 |
|
4 |
|
5 | import { Vector2 } from '../math/Vector2.js';
|
6 |
|
7 | function DirectGeometry() {
|
8 |
|
9 | this.vertices = [];
|
10 | this.normals = [];
|
11 | this.colors = [];
|
12 | this.uvs = [];
|
13 | this.uvs2 = [];
|
14 |
|
15 | this.groups = [];
|
16 |
|
17 | this.morphTargets = {};
|
18 |
|
19 | this.skinWeights = [];
|
20 | this.skinIndices = [];
|
21 |
|
22 |
|
23 |
|
24 | this.boundingBox = null;
|
25 | this.boundingSphere = null;
|
26 |
|
27 |
|
28 |
|
29 | this.verticesNeedUpdate = false;
|
30 | this.normalsNeedUpdate = false;
|
31 | this.colorsNeedUpdate = false;
|
32 | this.uvsNeedUpdate = false;
|
33 | this.groupsNeedUpdate = false;
|
34 |
|
35 | }
|
36 |
|
37 | Object.assign( DirectGeometry.prototype, {
|
38 |
|
39 | computeGroups: function ( geometry ) {
|
40 |
|
41 | var group;
|
42 | var groups = [];
|
43 | var materialIndex = undefined;
|
44 |
|
45 | var faces = geometry.faces;
|
46 |
|
47 | for ( var i = 0; i < faces.length; i ++ ) {
|
48 |
|
49 | var face = faces[ i ];
|
50 |
|
51 |
|
52 |
|
53 | if ( face.materialIndex !== materialIndex ) {
|
54 |
|
55 | materialIndex = face.materialIndex;
|
56 |
|
57 | if ( group !== undefined ) {
|
58 |
|
59 | group.count = ( i * 3 ) - group.start;
|
60 | groups.push( group );
|
61 |
|
62 | }
|
63 |
|
64 | group = {
|
65 | start: i * 3,
|
66 | materialIndex: materialIndex
|
67 | };
|
68 |
|
69 | }
|
70 |
|
71 | }
|
72 |
|
73 | if ( group !== undefined ) {
|
74 |
|
75 | group.count = ( i * 3 ) - group.start;
|
76 | groups.push( group );
|
77 |
|
78 | }
|
79 |
|
80 | this.groups = groups;
|
81 |
|
82 | },
|
83 |
|
84 | fromGeometry: function ( geometry ) {
|
85 |
|
86 | var faces = geometry.faces;
|
87 | var vertices = geometry.vertices;
|
88 | var faceVertexUvs = geometry.faceVertexUvs;
|
89 |
|
90 | var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
|
91 | var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
|
92 |
|
93 |
|
94 |
|
95 | var morphTargets = geometry.morphTargets;
|
96 | var morphTargetsLength = morphTargets.length;
|
97 |
|
98 | var morphTargetsPosition;
|
99 |
|
100 | if ( morphTargetsLength > 0 ) {
|
101 |
|
102 | morphTargetsPosition = [];
|
103 |
|
104 | for ( var i = 0; i < morphTargetsLength; i ++ ) {
|
105 |
|
106 | morphTargetsPosition[ i ] = [];
|
107 |
|
108 | }
|
109 |
|
110 | this.morphTargets.position = morphTargetsPosition;
|
111 |
|
112 | }
|
113 |
|
114 | var morphNormals = geometry.morphNormals;
|
115 | var morphNormalsLength = morphNormals.length;
|
116 |
|
117 | var morphTargetsNormal;
|
118 |
|
119 | if ( morphNormalsLength > 0 ) {
|
120 |
|
121 | morphTargetsNormal = [];
|
122 |
|
123 | for ( var i = 0; i < morphNormalsLength; i ++ ) {
|
124 |
|
125 | morphTargetsNormal[ i ] = [];
|
126 |
|
127 | }
|
128 |
|
129 | this.morphTargets.normal = morphTargetsNormal;
|
130 |
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 | var skinIndices = geometry.skinIndices;
|
136 | var skinWeights = geometry.skinWeights;
|
137 |
|
138 | var hasSkinIndices = skinIndices.length === vertices.length;
|
139 | var hasSkinWeights = skinWeights.length === vertices.length;
|
140 |
|
141 |
|
142 |
|
143 | for ( var i = 0; i < faces.length; i ++ ) {
|
144 |
|
145 | var face = faces[ i ];
|
146 |
|
147 | this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
|
148 |
|
149 | var vertexNormals = face.vertexNormals;
|
150 |
|
151 | if ( vertexNormals.length === 3 ) {
|
152 |
|
153 | this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
|
154 |
|
155 | } else {
|
156 |
|
157 | var normal = face.normal;
|
158 |
|
159 | this.normals.push( normal, normal, normal );
|
160 |
|
161 | }
|
162 |
|
163 | var vertexColors = face.vertexColors;
|
164 |
|
165 | if ( vertexColors.length === 3 ) {
|
166 |
|
167 | this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
|
168 |
|
169 | } else {
|
170 |
|
171 | var color = face.color;
|
172 |
|
173 | this.colors.push( color, color, color );
|
174 |
|
175 | }
|
176 |
|
177 | if ( hasFaceVertexUv === true ) {
|
178 |
|
179 | var vertexUvs = faceVertexUvs[ 0 ][ i ];
|
180 |
|
181 | if ( vertexUvs !== undefined ) {
|
182 |
|
183 | this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
|
184 |
|
185 | } else {
|
186 |
|
187 | console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
|
188 |
|
189 | this.uvs.push( new Vector2(), new Vector2(), new Vector2() );
|
190 |
|
191 | }
|
192 |
|
193 | }
|
194 |
|
195 | if ( hasFaceVertexUv2 === true ) {
|
196 |
|
197 | var vertexUvs = faceVertexUvs[ 1 ][ i ];
|
198 |
|
199 | if ( vertexUvs !== undefined ) {
|
200 |
|
201 | this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
|
202 |
|
203 | } else {
|
204 |
|
205 | console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
|
206 |
|
207 | this.uvs2.push( new Vector2(), new Vector2(), new Vector2() );
|
208 |
|
209 | }
|
210 |
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 | for ( var j = 0; j < morphTargetsLength; j ++ ) {
|
216 |
|
217 | var morphTarget = morphTargets[ j ].vertices;
|
218 |
|
219 | morphTargetsPosition[ j ].push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
|
220 |
|
221 | }
|
222 |
|
223 | for ( var j = 0; j < morphNormalsLength; j ++ ) {
|
224 |
|
225 | var morphNormal = morphNormals[ j ].vertexNormals[ i ];
|
226 |
|
227 | morphTargetsNormal[ j ].push( morphNormal.a, morphNormal.b, morphNormal.c );
|
228 |
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 | if ( hasSkinIndices ) {
|
234 |
|
235 | this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
|
236 |
|
237 | }
|
238 |
|
239 | if ( hasSkinWeights ) {
|
240 |
|
241 | this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
|
242 |
|
243 | }
|
244 |
|
245 | }
|
246 |
|
247 | this.computeGroups( geometry );
|
248 |
|
249 | this.verticesNeedUpdate = geometry.verticesNeedUpdate;
|
250 | this.normalsNeedUpdate = geometry.normalsNeedUpdate;
|
251 | this.colorsNeedUpdate = geometry.colorsNeedUpdate;
|
252 | this.uvsNeedUpdate = geometry.uvsNeedUpdate;
|
253 | this.groupsNeedUpdate = geometry.groupsNeedUpdate;
|
254 |
|
255 | return this;
|
256 |
|
257 | }
|
258 |
|
259 | } );
|
260 |
|
261 |
|
262 | export { DirectGeometry };
|