浏览代码

Fixed bug in compressed textures loader.

This was making mess for non-square textures.
alteredq 13 年之前
父节点
当前提交
89bc466c55

+ 4 - 4
build/three.min.js

@@ -510,10 +510,10 @@ THREE.ImageUtils={crossOrigin:"anonymous",loadTexture:function(a,b,c,d){var f=ne
 f.loadCount+1;if(f.loadCount===6){e.needsUpdate=true;c&&c()}};f[b].crossOrigin=this.crossOrigin;f[b].src=a[b]}return e},loadCompressedTexture:function(a,b,c,d){var f=new THREE.CompressedTexture;f.mapping=b;var e=new XMLHttpRequest;e.onload=function(){var a=THREE.ImageUtils.parseDDS(e.response,true);f.format=a.format;f.mipmaps=a.mipmaps;f.image.width=a.width;f.image.height=a.height;f.generateMipmaps=false;f.needsUpdate=true;c&&c(f)};e.onerror=d;e.open("GET",a,true);e.responseType="arraybuffer";e.send(null);
 return f},parseDDS:function(a,b){function c(a){return a.charCodeAt(0)+(a.charCodeAt(1)<<8)+(a.charCodeAt(2)<<16)+(a.charCodeAt(3)<<24)}var d={mipmaps:[],width:0,height:0,format:null,mipmapCount:1},f=c("DXT1"),e=c("DXT3"),g=c("DXT5"),h=new Int32Array(a,0,31);if(h[0]!==542327876){console.error("ImageUtils.parseDDS(): Invalid magic number in DDS header");return d}if(!h[20]&4){console.error("ImageUtils.parseDDS(): Unsupported format, must contain a FourCC code");return d}var i=h[21];switch(i){case f:f=
 8;d.format=THREE.RGB_S3TC_DXT1_Format;break;case e:f=16;d.format=THREE.RGBA_S3TC_DXT3_Format;break;case g:f=16;d.format=THREE.RGBA_S3TC_DXT5_Format;break;default:console.error("ImageUtils.parseDDS(): Unsupported FourCC code: ",String.fromCharCode(i&255,i>>8&255,i>>16&255,i>>24&255));return d}d.mipmapCount=1;if(h[2]&131072&&b!==false)d.mipmapCount=Math.max(1,h[7]);d.width=h[4];d.height=h[3];h=h[1]+4;e=d.width;g=d.height;for(i=0;i<d.mipmapCount;i++){var l=Math.max(4,e)/4*Math.max(4,g)/4*f,j={data:new Uint8Array(a,
-h,l),width:e,height:g};d.mipmaps.push(j);h=h+l;e=e*0.5;g=g*0.5}return d},getNormalMap:function(a,b){var c=function(a){var b=Math.sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);return[a[0]/b,a[1]/b,a[2]/b]},b=b|1,d=a.width,f=a.height,e=document.createElement("canvas");e.width=d;e.height=f;var g=e.getContext("2d");g.drawImage(a,0,0);for(var h=g.getImageData(0,0,d,f).data,i=g.createImageData(d,f),l=i.data,j=0;j<d;j++)for(var n=0;n<f;n++){var m=n-1<0?0:n-1,q=n+1>f-1?f-1:n+1,p=j-1<0?0:j-1,o=j+1>d-1?d-1:j+1,r=[],
-t=[0,0,h[(n*d+j)*4]/255*b];r.push([-1,0,h[(n*d+p)*4]/255*b]);r.push([-1,-1,h[(m*d+p)*4]/255*b]);r.push([0,-1,h[(m*d+j)*4]/255*b]);r.push([1,-1,h[(m*d+o)*4]/255*b]);r.push([1,0,h[(n*d+o)*4]/255*b]);r.push([1,1,h[(q*d+o)*4]/255*b]);r.push([0,1,h[(q*d+j)*4]/255*b]);r.push([-1,1,h[(q*d+p)*4]/255*b]);m=[];p=r.length;for(q=0;q<p;q++){var o=r[q],u=r[(q+1)%p],o=[o[0]-t[0],o[1]-t[1],o[2]-t[2]],u=[u[0]-t[0],u[1]-t[1],u[2]-t[2]];m.push(c([o[1]*u[2]-o[2]*u[1],o[2]*u[0]-o[0]*u[2],o[0]*u[1]-o[1]*u[0]]))}r=[0,0,
-0];for(q=0;q<m.length;q++){r[0]=r[0]+m[q][0];r[1]=r[1]+m[q][1];r[2]=r[2]+m[q][2]}r[0]=r[0]/m.length;r[1]=r[1]/m.length;r[2]=r[2]/m.length;t=(n*d+j)*4;l[t]=(r[0]+1)/2*255|0;l[t+1]=(r[1]+1)/2*255|0;l[t+2]=r[2]*255|0;l[t+3]=255}g.putImageData(i,0,0);return e},generateDataTexture:function(a,b,c){for(var d=a*b,f=new Uint8Array(3*d),e=Math.floor(c.r*255),g=Math.floor(c.g*255),c=Math.floor(c.b*255),h=0;h<d;h++){f[h*3]=e;f[h*3+1]=g;f[h*3+2]=c}a=new THREE.DataTexture(f,a,b,THREE.RGBFormat);a.needsUpdate=true;
-return a}};
+h,l),width:e,height:g};d.mipmaps.push(j);h=h+l;e=Math.max(e*0.5,1);g=Math.max(g*0.5,1)}return d},getNormalMap:function(a,b){var c=function(a){var b=Math.sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);return[a[0]/b,a[1]/b,a[2]/b]},b=b|1,d=a.width,f=a.height,e=document.createElement("canvas");e.width=d;e.height=f;var g=e.getContext("2d");g.drawImage(a,0,0);for(var h=g.getImageData(0,0,d,f).data,i=g.createImageData(d,f),l=i.data,j=0;j<d;j++)for(var n=0;n<f;n++){var m=n-1<0?0:n-1,q=n+1>f-1?f-1:n+1,p=j-1<0?0:j-1,
+o=j+1>d-1?d-1:j+1,r=[],t=[0,0,h[(n*d+j)*4]/255*b];r.push([-1,0,h[(n*d+p)*4]/255*b]);r.push([-1,-1,h[(m*d+p)*4]/255*b]);r.push([0,-1,h[(m*d+j)*4]/255*b]);r.push([1,-1,h[(m*d+o)*4]/255*b]);r.push([1,0,h[(n*d+o)*4]/255*b]);r.push([1,1,h[(q*d+o)*4]/255*b]);r.push([0,1,h[(q*d+j)*4]/255*b]);r.push([-1,1,h[(q*d+p)*4]/255*b]);m=[];p=r.length;for(q=0;q<p;q++){var o=r[q],u=r[(q+1)%p],o=[o[0]-t[0],o[1]-t[1],o[2]-t[2]],u=[u[0]-t[0],u[1]-t[1],u[2]-t[2]];m.push(c([o[1]*u[2]-o[2]*u[1],o[2]*u[0]-o[0]*u[2],o[0]*u[1]-
+o[1]*u[0]]))}r=[0,0,0];for(q=0;q<m.length;q++){r[0]=r[0]+m[q][0];r[1]=r[1]+m[q][1];r[2]=r[2]+m[q][2]}r[0]=r[0]/m.length;r[1]=r[1]/m.length;r[2]=r[2]/m.length;t=(n*d+j)*4;l[t]=(r[0]+1)/2*255|0;l[t+1]=(r[1]+1)/2*255|0;l[t+2]=r[2]*255|0;l[t+3]=255}g.putImageData(i,0,0);return e},generateDataTexture:function(a,b,c){for(var d=a*b,f=new Uint8Array(3*d),e=Math.floor(c.r*255),g=Math.floor(c.g*255),c=Math.floor(c.b*255),h=0;h<d;h++){f[h*3]=e;f[h*3+1]=g;f[h*3+2]=c}a=new THREE.DataTexture(f,a,b,THREE.RGBFormat);
+a.needsUpdate=true;return a}};
 THREE.SceneUtils={showHierarchy:function(a,b){THREE.SceneUtils.traverseHierarchy(a,function(a){a.visible=b})},traverseHierarchy:function(a,b){var c,d,f=a.children.length;for(d=0;d<f;d++){c=a.children[d];b(c);THREE.SceneUtils.traverseHierarchy(c,b)}},createMultiMaterialObject:function(a,b){var c,d=b.length,f=new THREE.Object3D;for(c=0;c<d;c++){var e=new THREE.Mesh(a,b[c]);f.add(e)}return f},cloneObject:function(a){var b;if(a instanceof THREE.MorphAnimMesh){b=new THREE.MorphAnimMesh(a.geometry,a.material);
 b.duration=a.duration;b.mirroredLoop=a.mirroredLoop;b.time=a.time;b.lastKeyframe=a.lastKeyframe;b.currentKeyframe=a.currentKeyframe;b.direction=a.direction;b.directionBackwards=a.directionBackwards}else if(a instanceof THREE.SkinnedMesh)b=new THREE.SkinnedMesh(a.geometry,a.material,a.useVertexTexture);else if(a instanceof THREE.Mesh)b=new THREE.Mesh(a.geometry,a.material);else if(a instanceof THREE.Line)b=new THREE.Line(a.geometry,a.material,a.type);else if(a instanceof THREE.Ribbon)b=new THREE.Ribbon(a.geometry,
 a.material);else if(a instanceof THREE.ParticleSystem){b=new THREE.ParticleSystem(a.geometry,a.material);b.sortParticles=a.sortParticles}else if(a instanceof THREE.Particle)b=new THREE.Particle(a.material);else if(a instanceof THREE.Sprite){b=new THREE.Sprite({});b.color.copy(a.color);b.map=a.map;b.blending=a.blending;b.useScreenCoordinates=a.useScreenCoordinates;b.mergeWith3D=a.mergeWith3D;b.affectedByDistance=a.affectedByDistance;b.scaleByViewport=a.scaleByViewport;b.alignment=a.alignment;b.rotation3d.copy(a.rotation3d);

+ 107 - 0
examples/models/utf8/ben_dds.js

@@ -0,0 +1,107 @@
+{
+  "materials": {
+    "gums": { "map_Kd": "dds/James_Mouth_Gum_Lores.dds" },
+    "tongue": { "map_Kd": "dds/James_Tongue_Lores.dds" },
+    "teethbottom": { "Kd": [251, 248, 248] },
+    "teethtop": { "Kd": [251, 248, 248] },
+    "topeyelashes": { "Kd": [66, 52, 42], "map_Kd": "dds/James_EyeLashTopTran.dds", "d": 0.999 },
+    "bottomeyelashes": { "Kd": [66, 52, 42], "map_Kd": "dds/James_EyeLashBotTran.dds", "d": 0.999 },
+    "head": { "map_Kd": "dds/James_Face_Color_Hair_Lores.dds", "Ks": [25,25,25], "Ns": 70 },
+    "eyetrans": { "Kd": [0, 0, 0] },
+    "pupil": { "Kd": [1, 1, 1] },
+    "iris": { "map_Kd": "dds/James_Eye_Inner_Green.dds" },
+    "eyeball": { "map_Kd": "dds/James_Eye_Green.dds" },
+    "pants": { "map_Kd": "dds/MJeans1TEX_Lores.dds", "Ks": [30,30,30], "Ns": 20 },
+    "tshirt3": { "map_Kd": "dds/MTshirt3TEX_Lores.dds", "Ks": [30,30,30], "Ns": 20 },
+    "skinbody": { "map_Kd": "dds/James_Body_Lores.dds", "Ks": [25,25,25], "Ns": 70 },
+    "fingernails": { "map_Kd": "dds/Nail_Hand_01_Lores.dds" },
+    "soleshoe": { "map_Kd": "dds/MCasShoe1TEX_Lores.dds" },
+    "sole": { "map_Kd": "dds/MCasShoe1TEX_Lores.dds" },
+    "laces": { "map_Kd": "dds/MCasShoe1TEX_Lores.dds" },
+    "bow": { "map_Kd": "dds/MCasShoe1TEX_Lores.dds" }
+  },
+  "decodeParams": {
+    "decodeOffsets": [-2533,-149,-6225,0,0,-511,-511,-511],
+    "decodeScales": [0.000043,0.000043,0.000043,0.000978,0.000978,0.001957,0.001957,0.001957]
+  },
+  "urls": {
+    "ben.utf8": [
+      { "material": "bottomeyelashes",
+        "attribRange": [0, 130],
+        "codeRange": [1040, 388, 192]
+      },
+      { "material": "bow",
+        "attribRange": [1428, 1848],
+        "codeRange": [16212, 6457, 3224]
+      },
+      { "material": "eyeball",
+        "attribRange": [22669, 3834],
+        "codeRange": [53341, 14697, 7284]
+      },
+      { "material": "eyetrans",
+        "attribRange": [68038, 5248],
+        "codeRange": [110022, 20180, 9964]
+      },
+      { "material": "fingernails",
+        "attribRange": [130202, 1023],
+        "codeRange": [138386, 2669, 1228]
+      },
+      { "material": "gums",
+        "attribRange": [141055, 1446],
+        "codeRange": [152623, 5270, 2624]
+      },
+      { "material": "head",
+        "attribRange": [157893, 2219],
+        "codeRange": [175645, 8353, 4168]
+      },
+      { "material": "iris",
+        "attribRange": [183998, 902],
+        "codeRange": [191214, 3332, 1664]
+      },
+      { "material": "laces",
+        "attribRange": [194546, 1016],
+        "codeRange": [202674, 3590, 1792]
+      },
+      { "material": "pants",
+        "attribRange": [206264, 8200],
+        "codeRange": [271864, 30625, 15293]
+      },
+      { "material": "pupil",
+        "attribRange": [302489, 148],
+        "codeRange": [303673, 581, 288]
+      },
+      { "material": "skinbody",
+        "attribRange": [304254, 4990],
+        "codeRange": [344174, 15770, 7830]
+      },
+      { "material": "sole",
+        "attribRange": [359944, 2588],
+        "codeRange": [380648, 9345, 4668]
+      },
+      { "material": "soleshoe",
+        "attribRange": [389993, 3164],
+        "codeRange": [415305, 10721, 5352]
+      },
+      { "material": "teethbottom",
+        "attribRange": [426026, 1235],
+        "codeRange": [435906, 3513, 1656]
+      },
+      { "material": "teethtop",
+        "attribRange": [439419, 1666],
+        "codeRange": [452747, 3937, 1816]
+      },
+      { "material": "tongue",
+        "attribRange": [456684, 845],
+        "codeRange": [463444, 3162, 1578]
+      },
+      { "material": "topeyelashes",
+        "attribRange": [466606, 130],
+        "codeRange": [467646, 388, 192]
+      },
+      { "material": "tshirt3",
+        "attribRange": [468034, 4283],
+        "codeRange": [502298, 14470, 7216]
+      }
+    ]
+  }
+}

二进制
examples/models/utf8/dds/James_Body_Lores.dds


二进制
examples/models/utf8/dds/James_EyeLashBotTran.dds


二进制
examples/models/utf8/dds/James_EyeLashTopTran.dds


二进制
examples/models/utf8/dds/James_Eye_Green.dds


二进制
examples/models/utf8/dds/James_Eye_Inner_Green.dds


二进制
examples/models/utf8/dds/James_Face_Color_Hair_Lores.dds


二进制
examples/models/utf8/dds/James_Mouth_Gum_Lores.dds


二进制
examples/models/utf8/dds/James_Tongue_Lores.dds


二进制
examples/models/utf8/dds/MCasShoe1TEX_Lores.dds


二进制
examples/models/utf8/dds/MJeans1TEX_Lores.dds


二进制
examples/models/utf8/dds/MTshirt3TEX_Lores.dds


二进制
examples/models/utf8/dds/Nail_Hand_01_Lores.dds


+ 1 - 1
examples/webgl_loader_utf8.html

@@ -168,7 +168,7 @@
 
 				}, { normalizeRGB: true } );
 
-				loader.load( "models/utf8/ben.js", function ( object ) {
+				loader.load( "models/utf8/ben_dds.js", function ( object ) {
 
 					var end = Date.now();
 					console.log( "ben", end - start, "ms" );

+ 3 - 2
src/extras/ImageUtils.js

@@ -266,8 +266,9 @@ THREE.ImageUtils = {
 			dds.mipmaps.push( mipmap );
 
 			dataOffset += dataLength;
-			width *= 0.5;
-			height *= 0.5;
+
+			width = Math.max( width * 0.5, 1 );
+			height = Math.max( height * 0.5, 1 );
 
 		}