js/OBJLoader.js

Thu, 06 Apr 2017 19:00:07 +0200

author
mdd
date
Thu, 06 Apr 2017 19:00:07 +0200
changeset 22
08f0118587d7
parent 16
800e8da193a7
permissions
-rw-r--r--

ht is not PVC....

16
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
1 /**
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
2 * @author mrdoob / http://mrdoob.com/
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
3 */
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
4
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
5 THREE.OBJLoader = function ( manager ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
6
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
7 this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
8
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
9 this.materials = null;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
10
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
11 this.regexp = {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
12 // v float float float
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
13 vertex_pattern : /^v\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
14 // vn float float float
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
15 normal_pattern : /^vn\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
16 // vt float float
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
17 uv_pattern : /^vt\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
18 // f vertex vertex vertex
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
19 face_vertex : /^f\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)(?:\s+(-?\d+))?/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
20 // f vertex/uv vertex/uv vertex/uv
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
21 face_vertex_uv : /^f\s+(-?\d+)\/(-?\d+)\s+(-?\d+)\/(-?\d+)\s+(-?\d+)\/(-?\d+)(?:\s+(-?\d+)\/(-?\d+))?/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
22 // f vertex/uv/normal vertex/uv/normal vertex/uv/normal
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
23 face_vertex_uv_normal : /^f\s+(-?\d+)\/(-?\d+)\/(-?\d+)\s+(-?\d+)\/(-?\d+)\/(-?\d+)\s+(-?\d+)\/(-?\d+)\/(-?\d+)(?:\s+(-?\d+)\/(-?\d+)\/(-?\d+))?/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
24 // f vertex//normal vertex//normal vertex//normal
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
25 face_vertex_normal : /^f\s+(-?\d+)\/\/(-?\d+)\s+(-?\d+)\/\/(-?\d+)\s+(-?\d+)\/\/(-?\d+)(?:\s+(-?\d+)\/\/(-?\d+))?/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
26 // o object_name | g group_name
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
27 object_pattern : /^[og]\s*(.+)?/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
28 // s boolean
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
29 smoothing_pattern : /^s\s+(\d+|on|off)/,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
30 // mtllib file_reference
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
31 material_library_pattern : /^mtllib /,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
32 // usemtl material_name
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
33 material_use_pattern : /^usemtl /
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
34 };
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
35
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
36 };
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
37
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
38 THREE.OBJLoader.prototype = {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
39
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
40 constructor: THREE.OBJLoader,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
41
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
42 load: function ( url, onLoad, onProgress, onError ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
43
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
44 var scope = this;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
45
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
46 var loader = new THREE.FileLoader( scope.manager );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
47 loader.setPath( this.path );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
48 loader.load( url, function ( text ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
49
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
50 onLoad( scope.parse( text ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
51
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
52 }, onProgress, onError );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
53
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
54 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
55
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
56 setPath: function ( value ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
57
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
58 this.path = value;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
59
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
60 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
61
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
62 setMaterials: function ( materials ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
63
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
64 this.materials = materials;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
65
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
66 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
67
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
68 _createParserState : function () {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
69
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
70 var state = {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
71 objects : [],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
72 object : {},
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
73
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
74 vertices : [],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
75 normals : [],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
76 uvs : [],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
77
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
78 materialLibraries : [],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
79
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
80 startObject: function ( name, fromDeclaration ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
81
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
82 // If the current object (initial from reset) is not from a g/o declaration in the parsed
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
83 // file. We need to use it for the first parsed g/o to keep things in sync.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
84 if ( this.object && this.object.fromDeclaration === false ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
85
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
86 this.object.name = name;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
87 this.object.fromDeclaration = ( fromDeclaration !== false );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
88 return;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
89
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
90 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
91
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
92 var previousMaterial = ( this.object && typeof this.object.currentMaterial === 'function' ? this.object.currentMaterial() : undefined );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
93
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
94 if ( this.object && typeof this.object._finalize === 'function' ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
95
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
96 this.object._finalize( true );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
97
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
98 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
99
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
100 this.object = {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
101 name : name || '',
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
102 fromDeclaration : ( fromDeclaration !== false ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
103
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
104 geometry : {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
105 vertices : [],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
106 normals : [],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
107 uvs : []
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
108 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
109 materials : [],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
110 smooth : true,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
111
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
112 startMaterial : function( name, libraries ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
113
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
114 var previous = this._finalize( false );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
115
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
116 // New usemtl declaration overwrites an inherited material, except if faces were declared
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
117 // after the material, then it must be preserved for proper MultiMaterial continuation.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
118 if ( previous && ( previous.inherited || previous.groupCount <= 0 ) ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
119
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
120 this.materials.splice( previous.index, 1 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
121
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
122 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
123
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
124 var material = {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
125 index : this.materials.length,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
126 name : name || '',
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
127 mtllib : ( Array.isArray( libraries ) && libraries.length > 0 ? libraries[ libraries.length - 1 ] : '' ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
128 smooth : ( previous !== undefined ? previous.smooth : this.smooth ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
129 groupStart : ( previous !== undefined ? previous.groupEnd : 0 ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
130 groupEnd : -1,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
131 groupCount : -1,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
132 inherited : false,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
133
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
134 clone : function( index ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
135 var cloned = {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
136 index : ( typeof index === 'number' ? index : this.index ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
137 name : this.name,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
138 mtllib : this.mtllib,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
139 smooth : this.smooth,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
140 groupStart : 0,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
141 groupEnd : -1,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
142 groupCount : -1,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
143 inherited : false
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
144 };
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
145 cloned.clone = this.clone.bind(cloned);
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
146 return cloned;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
147 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
148 };
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
149
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
150 this.materials.push( material );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
151
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
152 return material;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
153
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
154 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
155
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
156 currentMaterial : function() {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
157
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
158 if ( this.materials.length > 0 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
159 return this.materials[ this.materials.length - 1 ];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
160 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
161
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
162 return undefined;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
163
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
164 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
165
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
166 _finalize : function( end ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
167
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
168 var lastMultiMaterial = this.currentMaterial();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
169 if ( lastMultiMaterial && lastMultiMaterial.groupEnd === -1 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
170
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
171 lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
172 lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
173 lastMultiMaterial.inherited = false;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
174
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
175 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
176
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
177 // Ignore objects tail materials if no face declarations followed them before a new o/g started.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
178 if ( end && this.materials.length > 1 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
179
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
180 for ( var mi = this.materials.length - 1; mi >= 0; mi-- ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
181 if ( this.materials[mi].groupCount <= 0 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
182 this.materials.splice( mi, 1 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
183 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
184 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
185
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
186 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
187
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
188 // Guarantee at least one empty material, this makes the creation later more straight forward.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
189 if ( end && this.materials.length === 0 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
190
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
191 this.materials.push({
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
192 name : '',
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
193 smooth : this.smooth
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
194 });
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
195
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
196 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
197
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
198 return lastMultiMaterial;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
199
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
200 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
201 };
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
202
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
203 // Inherit previous objects material.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
204 // Spec tells us that a declared material must be set to all objects until a new material is declared.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
205 // If a usemtl declaration is encountered while this new object is being parsed, it will
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
206 // overwrite the inherited material. Exception being that there was already face declarations
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
207 // to the inherited material, then it will be preserved for proper MultiMaterial continuation.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
208
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
209 if ( previousMaterial && previousMaterial.name && typeof previousMaterial.clone === "function" ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
210
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
211 var declared = previousMaterial.clone( 0 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
212 declared.inherited = true;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
213 this.object.materials.push( declared );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
214
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
215 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
216
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
217 this.objects.push( this.object );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
218
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
219 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
220
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
221 finalize : function() {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
222
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
223 if ( this.object && typeof this.object._finalize === 'function' ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
224
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
225 this.object._finalize( true );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
226
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
227 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
228
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
229 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
230
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
231 parseVertexIndex: function ( value, len ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
232
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
233 var index = parseInt( value, 10 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
234 return ( index >= 0 ? index - 1 : index + len / 3 ) * 3;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
235
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
236 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
237
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
238 parseNormalIndex: function ( value, len ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
239
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
240 var index = parseInt( value, 10 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
241 return ( index >= 0 ? index - 1 : index + len / 3 ) * 3;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
242
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
243 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
244
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
245 parseUVIndex: function ( value, len ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
246
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
247 var index = parseInt( value, 10 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
248 return ( index >= 0 ? index - 1 : index + len / 2 ) * 2;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
249
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
250 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
251
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
252 addVertex: function ( a, b, c ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
253
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
254 var src = this.vertices;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
255 var dst = this.object.geometry.vertices;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
256
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
257 dst.push( src[ a + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
258 dst.push( src[ a + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
259 dst.push( src[ a + 2 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
260 dst.push( src[ b + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
261 dst.push( src[ b + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
262 dst.push( src[ b + 2 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
263 dst.push( src[ c + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
264 dst.push( src[ c + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
265 dst.push( src[ c + 2 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
266
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
267 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
268
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
269 addVertexLine: function ( a ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
270
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
271 var src = this.vertices;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
272 var dst = this.object.geometry.vertices;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
273
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
274 dst.push( src[ a + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
275 dst.push( src[ a + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
276 dst.push( src[ a + 2 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
277
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
278 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
279
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
280 addNormal : function ( a, b, c ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
281
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
282 var src = this.normals;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
283 var dst = this.object.geometry.normals;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
284
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
285 dst.push( src[ a + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
286 dst.push( src[ a + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
287 dst.push( src[ a + 2 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
288 dst.push( src[ b + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
289 dst.push( src[ b + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
290 dst.push( src[ b + 2 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
291 dst.push( src[ c + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
292 dst.push( src[ c + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
293 dst.push( src[ c + 2 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
294
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
295 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
296
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
297 addUV: function ( a, b, c ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
298
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
299 var src = this.uvs;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
300 var dst = this.object.geometry.uvs;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
301
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
302 dst.push( src[ a + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
303 dst.push( src[ a + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
304 dst.push( src[ b + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
305 dst.push( src[ b + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
306 dst.push( src[ c + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
307 dst.push( src[ c + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
308
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
309 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
310
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
311 addUVLine: function ( a ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
312
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
313 var src = this.uvs;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
314 var dst = this.object.geometry.uvs;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
315
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
316 dst.push( src[ a + 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
317 dst.push( src[ a + 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
318
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
319 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
320
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
321 addFace: function ( a, b, c, d, ua, ub, uc, ud, na, nb, nc, nd ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
322
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
323 var vLen = this.vertices.length;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
324
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
325 var ia = this.parseVertexIndex( a, vLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
326 var ib = this.parseVertexIndex( b, vLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
327 var ic = this.parseVertexIndex( c, vLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
328 var id;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
329
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
330 if ( d === undefined ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
331
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
332 this.addVertex( ia, ib, ic );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
333
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
334 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
335
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
336 id = this.parseVertexIndex( d, vLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
337
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
338 this.addVertex( ia, ib, id );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
339 this.addVertex( ib, ic, id );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
340
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
341 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
342
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
343 if ( ua !== undefined ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
344
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
345 var uvLen = this.uvs.length;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
346
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
347 ia = this.parseUVIndex( ua, uvLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
348 ib = this.parseUVIndex( ub, uvLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
349 ic = this.parseUVIndex( uc, uvLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
350
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
351 if ( d === undefined ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
352
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
353 this.addUV( ia, ib, ic );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
354
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
355 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
356
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
357 id = this.parseUVIndex( ud, uvLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
358
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
359 this.addUV( ia, ib, id );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
360 this.addUV( ib, ic, id );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
361
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
362 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
363
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
364 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
365
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
366 if ( na !== undefined ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
367
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
368 // Normals are many times the same. If so, skip function call and parseInt.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
369 var nLen = this.normals.length;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
370 ia = this.parseNormalIndex( na, nLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
371
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
372 ib = na === nb ? ia : this.parseNormalIndex( nb, nLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
373 ic = na === nc ? ia : this.parseNormalIndex( nc, nLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
374
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
375 if ( d === undefined ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
376
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
377 this.addNormal( ia, ib, ic );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
378
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
379 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
380
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
381 id = this.parseNormalIndex( nd, nLen );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
382
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
383 this.addNormal( ia, ib, id );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
384 this.addNormal( ib, ic, id );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
385
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
386 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
387
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
388 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
389
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
390 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
391
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
392 addLineGeometry: function ( vertices, uvs ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
393
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
394 this.object.geometry.type = 'Line';
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
395
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
396 var vLen = this.vertices.length;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
397 var uvLen = this.uvs.length;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
398
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
399 for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
400
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
401 this.addVertexLine( this.parseVertexIndex( vertices[ vi ], vLen ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
402
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
403 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
404
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
405 for ( var uvi = 0, l = uvs.length; uvi < l; uvi ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
406
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
407 this.addUVLine( this.parseUVIndex( uvs[ uvi ], uvLen ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
408
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
409 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
410
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
411 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
412
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
413 };
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
414
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
415 state.startObject( '', false );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
416
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
417 return state;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
418
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
419 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
420
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
421 parse: function ( text ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
422
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
423 console.time( 'OBJLoader' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
424
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
425 var state = this._createParserState();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
426
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
427 if ( text.indexOf( '\r\n' ) !== - 1 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
428
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
429 // This is faster than String.split with regex that splits on both
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
430 text = text.replace( /\r\n/g, '\n' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
431
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
432 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
433
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
434 if ( text.indexOf( '\\\n' ) !== - 1) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
435
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
436 // join lines separated by a line continuation character (\)
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
437 text = text.replace( /\\\n/g, '' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
438
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
439 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
440
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
441 var lines = text.split( '\n' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
442 var line = '', lineFirstChar = '', lineSecondChar = '';
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
443 var lineLength = 0;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
444 var result = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
445
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
446 // Faster to just trim left side of the line. Use if available.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
447 var trimLeft = ( typeof ''.trimLeft === 'function' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
448
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
449 for ( var i = 0, l = lines.length; i < l; i ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
450
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
451 line = lines[ i ];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
452
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
453 line = trimLeft ? line.trimLeft() : line.trim();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
454
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
455 lineLength = line.length;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
456
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
457 if ( lineLength === 0 ) continue;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
458
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
459 lineFirstChar = line.charAt( 0 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
460
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
461 // @todo invoke passed in handler if any
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
462 if ( lineFirstChar === '#' ) continue;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
463
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
464 if ( lineFirstChar === 'v' ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
465
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
466 lineSecondChar = line.charAt( 1 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
467
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
468 if ( lineSecondChar === ' ' && ( result = this.regexp.vertex_pattern.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
469
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
470 // 0 1 2 3
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
471 // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
472
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
473 state.vertices.push(
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
474 parseFloat( result[ 1 ] ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
475 parseFloat( result[ 2 ] ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
476 parseFloat( result[ 3 ] )
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
477 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
478
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
479 } else if ( lineSecondChar === 'n' && ( result = this.regexp.normal_pattern.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
480
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
481 // 0 1 2 3
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
482 // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
483
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
484 state.normals.push(
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
485 parseFloat( result[ 1 ] ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
486 parseFloat( result[ 2 ] ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
487 parseFloat( result[ 3 ] )
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
488 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
489
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
490 } else if ( lineSecondChar === 't' && ( result = this.regexp.uv_pattern.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
491
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
492 // 0 1 2
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
493 // ["vt 0.1 0.2", "0.1", "0.2"]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
494
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
495 state.uvs.push(
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
496 parseFloat( result[ 1 ] ),
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
497 parseFloat( result[ 2 ] )
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
498 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
499
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
500 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
501
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
502 throw new Error( "Unexpected vertex/normal/uv line: '" + line + "'" );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
503
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
504 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
505
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
506 } else if ( lineFirstChar === "f" ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
507
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
508 if ( ( result = this.regexp.face_vertex_uv_normal.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
509
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
510 // f vertex/uv/normal vertex/uv/normal vertex/uv/normal
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
511 // 0 1 2 3 4 5 6 7 8 9 10 11 12
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
512 // ["f 1/1/1 2/2/2 3/3/3", "1", "1", "1", "2", "2", "2", "3", "3", "3", undefined, undefined, undefined]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
513
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
514 state.addFace(
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
515 result[ 1 ], result[ 4 ], result[ 7 ], result[ 10 ],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
516 result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
517 result[ 3 ], result[ 6 ], result[ 9 ], result[ 12 ]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
518 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
519
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
520 } else if ( ( result = this.regexp.face_vertex_uv.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
521
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
522 // f vertex/uv vertex/uv vertex/uv
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
523 // 0 1 2 3 4 5 6 7 8
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
524 // ["f 1/1 2/2 3/3", "1", "1", "2", "2", "3", "3", undefined, undefined]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
525
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
526 state.addFace(
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
527 result[ 1 ], result[ 3 ], result[ 5 ], result[ 7 ],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
528 result[ 2 ], result[ 4 ], result[ 6 ], result[ 8 ]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
529 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
530
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
531 } else if ( ( result = this.regexp.face_vertex_normal.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
532
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
533 // f vertex//normal vertex//normal vertex//normal
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
534 // 0 1 2 3 4 5 6 7 8
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
535 // ["f 1//1 2//2 3//3", "1", "1", "2", "2", "3", "3", undefined, undefined]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
536
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
537 state.addFace(
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
538 result[ 1 ], result[ 3 ], result[ 5 ], result[ 7 ],
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
539 undefined, undefined, undefined, undefined,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
540 result[ 2 ], result[ 4 ], result[ 6 ], result[ 8 ]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
541 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
542
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
543 } else if ( ( result = this.regexp.face_vertex.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
544
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
545 // f vertex vertex vertex
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
546 // 0 1 2 3 4
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
547 // ["f 1 2 3", "1", "2", "3", undefined]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
548
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
549 state.addFace(
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
550 result[ 1 ], result[ 2 ], result[ 3 ], result[ 4 ]
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
551 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
552
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
553 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
554
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
555 throw new Error( "Unexpected face line: '" + line + "'" );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
556
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
557 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
558
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
559 } else if ( lineFirstChar === "l" ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
560
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
561 var lineParts = line.substring( 1 ).trim().split( " " );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
562 var lineVertices = [], lineUVs = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
563
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
564 if ( line.indexOf( "/" ) === - 1 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
565
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
566 lineVertices = lineParts;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
567
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
568 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
569
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
570 for ( var li = 0, llen = lineParts.length; li < llen; li ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
571
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
572 var parts = lineParts[ li ].split( "/" );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
573
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
574 if ( parts[ 0 ] !== "" ) lineVertices.push( parts[ 0 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
575 if ( parts[ 1 ] !== "" ) lineUVs.push( parts[ 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
576
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
577 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
578
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
579 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
580 state.addLineGeometry( lineVertices, lineUVs );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
581
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
582 } else if ( ( result = this.regexp.object_pattern.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
583
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
584 // o object_name
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
585 // or
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
586 // g group_name
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
587
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
588 // WORKAROUND: https://bugs.chromium.org/p/v8/issues/detail?id=2869
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
589 // var name = result[ 0 ].substr( 1 ).trim();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
590 var name = ( " " + result[ 0 ].substr( 1 ).trim() ).substr( 1 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
591
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
592 state.startObject( name );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
593
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
594 } else if ( this.regexp.material_use_pattern.test( line ) ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
595
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
596 // material
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
597
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
598 state.object.startMaterial( line.substring( 7 ).trim(), state.materialLibraries );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
599
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
600 } else if ( this.regexp.material_library_pattern.test( line ) ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
601
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
602 // mtl file
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
603
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
604 state.materialLibraries.push( line.substring( 7 ).trim() );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
605
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
606 } else if ( ( result = this.regexp.smoothing_pattern.exec( line ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
607
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
608 // smooth shading
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
609
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
610 // @todo Handle files that have varying smooth values for a set of faces inside one geometry,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
611 // but does not define a usemtl for each face set.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
612 // This should be detected and a dummy material created (later MultiMaterial and geometry groups).
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
613 // This requires some care to not create extra material on each smooth value for "normal" obj files.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
614 // where explicit usemtl defines geometry groups.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
615 // Example asset: examples/models/obj/cerberus/Cerberus.obj
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
616
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
617 var value = result[ 1 ].trim().toLowerCase();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
618 state.object.smooth = ( value === '1' || value === 'on' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
619
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
620 var material = state.object.currentMaterial();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
621 if ( material ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
622
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
623 material.smooth = state.object.smooth;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
624
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
625 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
626
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
627 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
628
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
629 // Handle null terminated files without exception
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
630 if ( line === '\0' ) continue;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
631
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
632 throw new Error( "Unexpected line: '" + line + "'" );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
633
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
634 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
635
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
636 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
637
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
638 state.finalize();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
639
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
640 var container = new THREE.Group();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
641 container.materialLibraries = [].concat( state.materialLibraries );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
642
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
643 for ( var i = 0, l = state.objects.length; i < l; i ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
644
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
645 var object = state.objects[ i ];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
646 var geometry = object.geometry;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
647 var materials = object.materials;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
648 var isLine = ( geometry.type === 'Line' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
649
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
650 // Skip o/g line declarations that did not follow with any faces
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
651 if ( geometry.vertices.length === 0 ) continue;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
652
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
653 var buffergeometry = new THREE.BufferGeometry();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
654
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
655 buffergeometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( geometry.vertices ), 3 ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
656
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
657 if ( geometry.normals.length > 0 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
658
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
659 buffergeometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( geometry.normals ), 3 ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
660
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
661 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
662
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
663 buffergeometry.computeVertexNormals();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
664
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
665 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
666
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
667 if ( geometry.uvs.length > 0 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
668
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
669 buffergeometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( geometry.uvs ), 2 ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
670
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
671 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
672
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
673 // Create materials
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
674
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
675 var createdMaterials = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
676
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
677 for ( var mi = 0, miLen = materials.length; mi < miLen ; mi++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
678
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
679 var sourceMaterial = materials[mi];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
680 var material = undefined;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
681
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
682 if ( this.materials !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
683
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
684 material = this.materials.create( sourceMaterial.name );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
685
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
686 // mtl etc. loaders probably can't create line materials correctly, copy properties to a line material.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
687 if ( isLine && material && ! ( material instanceof THREE.LineBasicMaterial ) ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
688
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
689 var materialLine = new THREE.LineBasicMaterial();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
690 materialLine.copy( material );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
691 material = materialLine;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
692
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
693 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
694
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
695 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
696
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
697 if ( ! material ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
698
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
699 material = ( ! isLine ? new THREE.MeshPhongMaterial() : new THREE.LineBasicMaterial() );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
700 material.name = sourceMaterial.name;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
701
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
702 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
703
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
704 material.shading = sourceMaterial.smooth ? THREE.SmoothShading : THREE.FlatShading;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
705
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
706 createdMaterials.push(material);
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
707
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
708 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
709
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
710 // Create mesh
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
711
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
712 var mesh;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
713
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
714 if ( createdMaterials.length > 1 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
715
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
716 for ( var mi = 0, miLen = materials.length; mi < miLen ; mi++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
717
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
718 var sourceMaterial = materials[mi];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
719 buffergeometry.addGroup( sourceMaterial.groupStart, sourceMaterial.groupCount, mi );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
720
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
721 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
722
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
723 var multiMaterial = new THREE.MultiMaterial( createdMaterials );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
724 mesh = ( ! isLine ? new THREE.Mesh( buffergeometry, multiMaterial ) : new THREE.LineSegments( buffergeometry, multiMaterial ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
725
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
726 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
727
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
728 mesh = ( ! isLine ? new THREE.Mesh( buffergeometry, createdMaterials[ 0 ] ) : new THREE.LineSegments( buffergeometry, createdMaterials[ 0 ] ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
729 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
730
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
731 mesh.name = object.name;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
732
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
733 container.add( mesh );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
734
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
735 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
736
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
737 console.timeEnd( 'OBJLoader' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
738
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
739 return container;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
740
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
741 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
742
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
743 };

mercurial