Skip to content

Commit aea8068

Browse files
committed
Merge branch 'feature/fix-3ds' into feature/buff-worms
Closes #404 [ENGR-8627]
2 parents 0f0b4e8 + e9f9e06 commit aea8068

File tree

1 file changed

+73
-24
lines changed

1 file changed

+73
-24
lines changed

mfr/extensions/jsc3d/static/js/jsc3d.3ds.js

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ JSC3D.Autodesk3DSLoader.prototype.loadFromUrl = function(urlName) {
7777
xhr.onreadystatechange = function() {
7878
if(this.readyState == 4) {
7979
if(this.status == 200 || this.status == 0) {
80-
if(JSC3D.console)
80+
if(JSC3D.console) {
8181
JSC3D.console.logInfo('Finished loading 3DS file "' + urlName + '".');
82+
}
8283
if(self.onload) {
83-
if(self.onprogress)
84+
if(self.onprogress) {
8485
self.onprogress('Loading 3DS file ...', 1);
86+
}
8587
// if(JSC3D.PlatformInfo.browser == 'ie' && JSC3D.PlatformInfo.version >= '10') {
8688
// // asynchronously decode blob to binary string
8789
// var blobReader = new FileReader;
@@ -107,10 +109,12 @@ JSC3D.Autodesk3DSLoader.prototype.loadFromUrl = function(urlName) {
107109
}
108110
}
109111
else {
110-
if(JSC3D.console)
112+
if(JSC3D.console) {
111113
JSC3D.console.logError('Failed to load 3DS file "' + urlName + '".');
112-
if(self.onerror)
114+
}
115+
if(self.onerror) {
113116
self.onerror('Failed to load 3DS file "' + urlName + '".');
117+
}
114118
}
115119
self.request = null;
116120
}
@@ -199,9 +203,12 @@ JSC3D.Autodesk3DSLoader.prototype.readAmount = function(reader,end) {
199203
len = reader.readUInt32();
200204

201205
switch (cid) {
202-
case 0x0030: // Floats
206+
case 0x0030:
203207
amount = reader.readUInt16();
204208
break;
209+
case 0x0031:
210+
amount = reader.readFloat32() * 100.0;
211+
break;
205212
default:
206213
break;
207214
}
@@ -461,14 +468,16 @@ JSC3D.Autodesk3DSLoader.prototype.parseObjectAnimation = function (reader,end) {
461468
if (name != '$$$DUMMY' && this._unfinalized_objects.hasOwnProperty(name)) {
462469
vo = this._unfinalized_objects[name];
463470
// todo vasea
464-
if(JSC3D.console)
471+
if(JSC3D.console) {
465472
JSC3D.console.logInfo('Construct object '+vo.name);
473+
}
466474
// this._cur_obj = null;
467475
obj = null;//this.constructObject(vo, pivot);
468476

469477
if (obj) {
470-
if(JSC3D.console)
478+
if(JSC3D.console) {
471479
JSC3D.console.logInfo('finished loading the object '+vo.name);
480+
}
472481
}
473482

474483
delete this._unfinalized_objects[name];
@@ -489,8 +498,8 @@ JSC3D.Autodesk3DSLoader.prototype.parseSmoothingGroups = function (reader) {
489498
JSC3D.Autodesk3DSLoader.prototype.finalizeCurrentMaterial = function () {
490499
var mat = new JSC3D.Material;
491500

492-
if (this._cur_mat.colorMap) {
493-
mat.textureFileName = this._cur_mat.colorMap.texture;
501+
if (this._cur_mat.colorMap && this._cur_mat.colorMap.url) {
502+
mat.textureFileName = this._cur_mat.colorMap.url;
494503
}
495504
else {
496505
mat.diffuseColor = this._cur_mat.diffuseColor;
@@ -517,8 +526,9 @@ JSC3D.Autodesk3DSLoader.prototype.setupTexture = function(meshList, textureUrlNa
517526
texture.onready = function() {
518527
for(var i=0; i<meshList.length; i++)
519528
meshList[i].setTexture(this);
520-
if(self.onresource)
529+
if(self.onresource) {
521530
self.onresource(this);
531+
}
522532
};
523533

524534
texture.createFromUrl(textureUrlName);
@@ -555,8 +565,9 @@ JSC3D.Autodesk3DSLoader.prototype.parse3DS = function(scene, data) {
555565
// Can't finalize at this point, because we have to wait until the full
556566
// animation section has been parsed for any potential pivot definitions
557567

558-
if(JSC3D.console)
568+
if(JSC3D.console) {
559569
JSC3D.console.logInfo('Optimize the mesh and add it to the scene. ');
570+
}
560571

561572
//have to parse all the materials and create a mesh fdor each material
562573

@@ -572,25 +583,63 @@ JSC3D.Autodesk3DSLoader.prototype.parse3DS = function(scene, data) {
572583
// mesh.texCoordIndexBuffer = mesh.indexBuffer;
573584
mesh.material = new JSC3D.Material;
574585

575-
var materialFaces = this._cur_obj.materialFaces;
576-
var currentMaterial = null;
577-
for(var materialName in materialFaces){
578-
currentMaterial = this._materials[materialName];
579-
if (currentMaterial.colorMap) {
580-
if(JSC3D.console)
581-
JSC3D.console.logInfo('set texture: '+currentMaterial.colorMap.url);
586+
// Defaults: opaque & double-sided (helps open meshes)
587+
mesh.material.transparency = 0;
588+
mesh.isDoubleSided = true;
589+
mesh.material.bothSides = true;
590+
591+
let materialFaces = this._cur_obj.materialFaces || {};
592+
let sawMaterial = false;
593+
for (let materialName in materialFaces) {
594+
if (!Object.prototype.hasOwnProperty.call(materialFaces, materialName)) {
595+
continue;
596+
}
597+
sawMaterial = true;
598+
599+
var currentMaterial = this._materials[materialName];
600+
if (!currentMaterial) {
601+
if (JSC3D.console) {
602+
JSC3D.console.logWarning('3DS: missing material "' + materialName + '", using defaults.');
603+
}
604+
continue;
605+
}
606+
607+
if (currentMaterial.colorMap && currentMaterial.colorMap.url) {
608+
if(JSC3D.console) {
609+
JSC3D.console.logInfo('set texture: ' + currentMaterial.colorMap.url);
610+
}
582611
this.setupTexture([mesh], currentMaterial.colorMap.url);
583612
}
584-
else {
613+
else if (currentMaterial.diffuseColor != null) {
585614
mesh.material.diffuseColor = currentMaterial.diffuseColor;
586615
}
587-
// mesh.material.ambientColor = currentMaterial.ambientColor;
588-
// mesh.material.simulateSpecular = (currentMaterial.specularColor>0);
589-
mesh.isDoubleSided = true;//currentMaterial.twoSided;
590-
mesh.material.transparency = currentMaterial.transparency>0?(currentMaterial.transparency)/100:0;
616+
617+
// Transparency: normalize (0..1), snap tiny values to 0 to avoid z-sort artifacts
618+
var t = currentMaterial.transparency;
619+
if (typeof t === 'number') {
620+
var alpha = (t > 1) ? (t / 100.0) : t;
621+
alpha = Math.max(0, Math.min(1, alpha));
622+
if (alpha < 0.01) alpha = 0;
623+
mesh.material.transparency = alpha;
624+
} else {
625+
mesh.material.transparency = 0;
626+
}
627+
628+
var two = (currentMaterial.twoSided !== undefined) ? !!currentMaterial.twoSided : true;
629+
mesh.isDoubleSided = two;
630+
mesh.material.bothSides = two;
591631
}
592632

633+
if (!sawMaterial) {
634+
mesh.isDoubleSided = true;
635+
mesh.material.bothSides = true;
636+
mesh.material.transparency = 0;
637+
}
593638

639+
if (!mesh.texCoordBuffer || !mesh.texCoordIndexBuffer) {
640+
mesh.texCoordBuffer = null;
641+
mesh.texCoordIndexBuffer = null;
642+
}
594643

595644
//mesh.faceNormalBuffer = [];
596645
// mesh.init();
@@ -672,7 +721,7 @@ JSC3D.Autodesk3DSLoader.prototype.parse3DS = function(scene, data) {
672721
this._cur_obj = {};
673722
this._cur_obj.name = this.readNulTermString(reader);
674723
this._cur_obj.materials = [];
675-
this._cur_obj.materialFaces = [];
724+
this._cur_obj.materialFaces = {};
676725
break;
677726

678727
case 0x4100: // OBJ_TRIMESH

0 commit comments

Comments
 (0)