1 /**
2 * @author alteredq / http://alteredqualia.com/
3 * @author mrdoob / http://mrdoob.com/
4 */
5
6 /**@namespace*/
7 THREE.ImageUtils = {
8
9 crossOrigin: 'anonymous',
10
11 loadTexture: function ( url, mapping, onLoad, onError ) {
12
13 var image = new Image();
14 var texture = new THREE.Texture( image, mapping );
15
16 var loader = new THREE.ImageLoader();
17
18 loader.addEventListener( 'load', function ( event ) {
19
20 texture.image = event.content;
21 texture.needsUpdate = true;
22
23 if ( onLoad ) onLoad( texture );
24
25 } );
26
27 loader.addEventListener( 'error', function ( event ) {
28
29 if ( onError ) onError( event.message );
30
31 } );
32
33 loader.crossOrigin = this.crossOrigin;
34 loader.load( url, image );
35
36 texture.sourceFile = url;
37
38 return texture;
39
40 },
41
42 loadCompressedTexture: function ( url, mapping, onLoad, onError ) {
43
44 var texture = new THREE.CompressedTexture();
45 texture.mapping = mapping;
46
47 var request = new XMLHttpRequest();
48
49 request.onload = function () {
50
51 var buffer = request.response;
52 var dds = THREE.ImageUtils.parseDDS( buffer, true );
53
54 texture.format = dds.format;
55
56 texture.mipmaps = dds.mipmaps;
57 texture.image.width = dds.width;
58 texture.image.height = dds.height;
59
60 // gl.generateMipmap fails for compressed textures
61 // mipmaps must be embedded in the DDS file
62 // or texture filters must not use mipmapping
63
64 texture.generateMipmaps = false;
65
66 texture.needsUpdate = true;
67
68 if ( onLoad ) onLoad( texture );
69
70 }
71
72 request.onerror = onError;
73
74 request.open( 'GET', url, true );
75 request.responseType = "arraybuffer";
76 request.send( null );
77
78 return texture;
79
80 },
81
82 loadTextureCube: function ( array, mapping, onLoad, onError ) {
83
84 var images = [];
85 images.loadCount = 0;
86
87 var texture = new THREE.Texture();
88 texture.image = images;
89 if ( mapping !== undefined ) texture.mapping = mapping;
90
91 // no flipping needed for cube textures
92
93 texture.flipY = false;
94
95 for ( var i = 0, il = array.length; i < il; ++ i ) {
96
97 var cubeImage = new Image();
98 images[ i ] = cubeImage;
99
100 cubeImage.onload = function () {
101
102 images.loadCount += 1;
103
104 if ( images.loadCount === 6 ) {
105
106 texture.needsUpdate = true;
107 if ( onLoad ) onLoad();
108
109 }
110
111 };
112
113 cubeImage.onerror = onError;
114
115 cubeImage.crossOrigin = this.crossOrigin;
116 cubeImage.src = array[ i ];
117
118 }
119
120 return texture;
121
122 },
123
124 loadCompressedTextureCube: function ( array, mapping, onLoad, onError ) {
125
126 var images = [];
127 images.loadCount = 0;
128
129 var texture = new THREE.CompressedTexture();
130 texture.image = images;
131 if ( mapping !== undefined ) texture.mapping = mapping;
132
133 // no flipping for cube textures
134 // (also flipping doesn't work for compressed textures )
135
136 texture.flipY = false;
137
138 // can't generate mipmaps for compressed textures
139 // mips must be embedded in DDS files
140
141 texture.generateMipmaps = false;
142
143 var generateCubeFaceCallback = function ( rq, img ) {
144
145 return function () {
146
147 var buffer = rq.response;
148 var dds = THREE.ImageUtils.parseDDS( buffer, true );
149
150 img.format = dds.format;
151
152 img.mipmaps = dds.mipmaps;
153 img.width = dds.width;
154 img.height = dds.height;
155
156 images.loadCount += 1;
157
158 if ( images.loadCount === 6 ) {
159
160 texture.format = dds.format;
161 texture.needsUpdate = true;
162 if ( onLoad ) onLoad();
163
164 }
165
166 }
167
168 }
169
170 for ( var i = 0, il = array.length; i < il; ++ i ) {
171
172 var cubeImage = {};
173 images[ i ] = cubeImage;
174
175 var request = new XMLHttpRequest();
176
177 request.onload = generateCubeFaceCallback( request, cubeImage );
178 request.onerror = onError;
179
180 var url = array[ i ];
181
182 request.open( 'GET', url, true );
183 request.responseType = "arraybuffer";
184 request.send( null );
185
186 }
187
188 return texture;
189
190 },
191
192 parseDDS: function ( buffer, loadMipmaps ) {
193
194 var dds = { mipmaps: [], width: 0, height: 0, format: null, mipmapCount: 1 };
195
196 // Adapted from @toji's DDS utils
197 // https://github.com/toji/webgl-texture-utils/blob/master/texture-util/dds.js
198
199 // All values and structures referenced from:
200 // http://msdn.microsoft.com/en-us/library/bb943991.aspx/
201
202 var DDS_MAGIC = 0x20534444;
203
204 var DDSD_CAPS = 0x1,
205 DDSD_HEIGHT = 0x2,
206 DDSD_WIDTH = 0x4,
207 DDSD_PITCH = 0x8,
208 DDSD_PIXELFORMAT = 0x1000,
209 DDSD_MIPMAPCOUNT = 0x20000,
210 DDSD_LINEARSIZE = 0x80000,
211 DDSD_DEPTH = 0x800000;
212
213 var DDSCAPS_COMPLEX = 0x8,
214 DDSCAPS_MIPMAP = 0x400000,
215 DDSCAPS_TEXTURE = 0x1000;
216
217 var DDSCAPS2_CUBEMAP = 0x200,
218 DDSCAPS2_CUBEMAP_POSITIVEX = 0x400,
219 DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800,
220 DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000,
221 DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000,
222 DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000,
223 DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000,
224 DDSCAPS2_VOLUME = 0x200000;
225
226 var DDPF_ALPHAPIXELS = 0x1,
227 DDPF_ALPHA = 0x2,
228 DDPF_FOURCC = 0x4,
229 DDPF_RGB = 0x40,
230 DDPF_YUV = 0x200,
231 DDPF_LUMINANCE = 0x20000;
232
233 function fourCCToInt32( value ) {
234
235 return value.charCodeAt(0) +
236 (value.charCodeAt(1) << 8) +
237 (value.charCodeAt(2) << 16) +
238 (value.charCodeAt(3) << 24);
239
240 }
241
242 function int32ToFourCC( value ) {
243
244 return String.fromCharCode(
245 value & 0xff,
246 (value >> 8) & 0xff,
247 (value >> 16) & 0xff,
248 (value >> 24) & 0xff
249 );
250 }
251
252 var FOURCC_DXT1 = fourCCToInt32("DXT1");
253 var FOURCC_DXT3 = fourCCToInt32("DXT3");
254 var FOURCC_DXT5 = fourCCToInt32("DXT5");
255
256 var headerLengthInt = 31; // The header length in 32 bit ints
257
258 // Offsets into the header array
259
260 var off_magic = 0;
261
262 var off_size = 1;
263 var off_flags = 2;
264 var off_height = 3;
265 var off_width = 4;
266
267 var off_mipmapCount = 7;
268
269 var off_pfFlags = 20;
270 var off_pfFourCC = 21;
271
272 // Parse header
273
274 var header = new Int32Array( buffer, 0, headerLengthInt );
275
276 if ( header[ off_magic ] !== DDS_MAGIC ) {
277
278 console.error( "ImageUtils.parseDDS(): Invalid magic number in DDS header" );
279 return dds;
280
281 }
282
283 if ( ! header[ off_pfFlags ] & DDPF_FOURCC ) {
284
285 console.error( "ImageUtils.parseDDS(): Unsupported format, must contain a FourCC code" );
286 return dds;
287
288 }
289
290 var blockBytes;
291
292 var fourCC = header[ off_pfFourCC ];
293
294 switch ( fourCC ) {
295
296 case FOURCC_DXT1:
297
298 blockBytes = 8;
299 dds.format = THREE.RGB_S3TC_DXT1_Format;
300 break;
301
302 case FOURCC_DXT3:
303
304 blockBytes = 16;
305 dds.format = THREE.RGBA_S3TC_DXT3_Format;
306 break;
307
308 case FOURCC_DXT5:
309
310 blockBytes = 16;
311 dds.format = THREE.RGBA_S3TC_DXT5_Format;
312 break;
313
314 default:
315
316 console.error( "ImageUtils.parseDDS(): Unsupported FourCC code: ", int32ToFourCC( fourCC ) );
317 return dds;
318
319 }
320
321 dds.mipmapCount = 1;
322
323 if ( header[ off_flags ] & DDSD_MIPMAPCOUNT && loadMipmaps !== false ) {
324
325 dds.mipmapCount = Math.max( 1, header[ off_mipmapCount ] );
326
327 }
328
329 dds.width = header[ off_width ];
330 dds.height = header[ off_height ];
331
332 var dataOffset = header[ off_size ] + 4;
333
334 // Extract mipmaps buffers
335
336 var width = dds.width;
337 var height = dds.height;
338
339 for ( var i = 0; i < dds.mipmapCount; i ++ ) {
340
341 var dataLength = Math.max( 4, width ) / 4 * Math.max( 4, height ) / 4 * blockBytes;
342 var byteArray = new Uint8Array( buffer, dataOffset, dataLength );
343
344 var mipmap = { "data": byteArray, "width": width, "height": height };
345 dds.mipmaps.push( mipmap );
346
347 dataOffset += dataLength;
348
349 width = Math.max( width * 0.5, 1 );
350 height = Math.max( height * 0.5, 1 );
351
352 }
353
354 return dds;
355
356 },
357
358 getNormalMap: function ( image, depth ) {
359
360 // Adapted from http://www.paulbrunt.co.uk/lab/heightnormal/
361
362 var cross = function ( a, b ) {
363
364 return [ a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ], a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ], a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ] ];
365
366 }
367
368 var subtract = function ( a, b ) {
369
370 return [ a[ 0 ] - b[ 0 ], a[ 1 ] - b[ 1 ], a[ 2 ] - b[ 2 ] ];
371
372 }
373
374 var normalize = function ( a ) {
375
376 var l = Math.sqrt( a[ 0 ] * a[ 0 ] + a[ 1 ] * a[ 1 ] + a[ 2 ] * a[ 2 ] );
377 return [ a[ 0 ] / l, a[ 1 ] / l, a[ 2 ] / l ];
378
379 }
380
381 depth = depth | 1;
382
383 var width = image.width;
384 var height = image.height;
385
386 var canvas = document.createElement( 'canvas' );
387 canvas.width = width;
388 canvas.height = height;
389
390 var context = canvas.getContext( '2d' );
391 context.drawImage( image, 0, 0 );
392
393 var data = context.getImageData( 0, 0, width, height ).data;
394 var imageData = context.createImageData( width, height );
395 var output = imageData.data;
396
397 for ( var x = 0; x < width; x ++ ) {
398
399 for ( var y = 0; y < height; y ++ ) {
400
401 var ly = y - 1 < 0 ? 0 : y - 1;
402 var uy = y + 1 > height - 1 ? height - 1 : y + 1;
403 var lx = x - 1 < 0 ? 0 : x - 1;
404 var ux = x + 1 > width - 1 ? width - 1 : x + 1;
405
406 var points = [];
407 var origin = [ 0, 0, data[ ( y * width + x ) * 4 ] / 255 * depth ];
408 points.push( [ - 1, 0, data[ ( y * width + lx ) * 4 ] / 255 * depth ] );
409 points.push( [ - 1, - 1, data[ ( ly * width + lx ) * 4 ] / 255 * depth ] );
410 points.push( [ 0, - 1, data[ ( ly * width + x ) * 4 ] / 255 * depth ] );
411 points.push( [ 1, - 1, data[ ( ly * width + ux ) * 4 ] / 255 * depth ] );
412 points.push( [ 1, 0, data[ ( y * width + ux ) * 4 ] / 255 * depth ] );
413 points.push( [ 1, 1, data[ ( uy * width + ux ) * 4 ] / 255 * depth ] );
414 points.push( [ 0, 1, data[ ( uy * width + x ) * 4 ] / 255 * depth ] );
415 points.push( [ - 1, 1, data[ ( uy * width + lx ) * 4 ] / 255 * depth ] );
416
417 var normals = [];
418 var num_points = points.length;
419
420 for ( var i = 0; i < num_points; i ++ ) {
421
422 var v1 = points[ i ];
423 var v2 = points[ ( i + 1 ) % num_points ];
424 v1 = subtract( v1, origin );
425 v2 = subtract( v2, origin );
426 normals.push( normalize( cross( v1, v2 ) ) );
427
428 }
429
430 var normal = [ 0, 0, 0 ];
431
432 for ( var i = 0; i < normals.length; i ++ ) {
433
434 normal[ 0 ] += normals[ i ][ 0 ];
435 normal[ 1 ] += normals[ i ][ 1 ];
436 normal[ 2 ] += normals[ i ][ 2 ];
437
438 }
439
440 normal[ 0 ] /= normals.length;
441 normal[ 1 ] /= normals.length;
442 normal[ 2 ] /= normals.length;
443
444 var idx = ( y * width + x ) * 4;
445
446 output[ idx ] = ( ( normal[ 0 ] + 1.0 ) / 2.0 * 255 ) | 0;
447 output[ idx + 1 ] = ( ( normal[ 1 ] + 1.0 ) / 2.0 * 255 ) | 0;
448 output[ idx + 2 ] = ( normal[ 2 ] * 255 ) | 0;
449 output[ idx + 3 ] = 255;
450
451 }
452
453 }
454
455 context.putImageData( imageData, 0, 0 );
456
457 return canvas;
458
459 },
460
461 generateDataTexture: function ( width, height, color ) {
462
463 var size = width * height;
464 var data = new Uint8Array( 3 * size );
465
466 var r = Math.floor( color.r * 255 );
467 var g = Math.floor( color.g * 255 );
468 var b = Math.floor( color.b * 255 );
469
470 for ( var i = 0; i < size; i ++ ) {
471
472 data[ i * 3 ] = r;
473 data[ i * 3 + 1 ] = g;
474 data[ i * 3 + 2 ] = b;
475
476 }
477
478 var texture = new THREE.DataTexture( data, width, height, THREE.RGBFormat );
479 texture.needsUpdate = true;
480
481 return texture;
482
483 }
484
485 };
486
nike free rn
new balance hombre baratas
cinturones gucci
ugg rebajas
cinturon gucci
ray ban baratas
nike cortez
peuterey mujer
christian louboutin madrid
mbt zapatos
gafas ray ban baratas
mbt ofertas
air max blancas
mbt barcelona
nike air max 90
woolrich barcelona
nike mujer
botas ugg
gafas de sol carrera aratas
air max 2016 baratas
oakley baratas
nike air max 2016