Skip to content

Commit 026cde0

Browse files
committed
ENG-8627 | Fix .3ds files not rendering in MFR viewer
1 parent 6498822 commit 026cde0

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

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

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,12 @@ JSC3D.Autodesk3DSLoader.prototype.readAmount = function(reader,end) {
199199
len = reader.readUInt32();
200200

201201
switch (cid) {
202-
case 0x0030: // Floats
202+
case 0x0030:
203203
amount = reader.readUInt16();
204204
break;
205+
case 0x0031:
206+
amount = reader.readFloat32() * 100.0;
207+
break;
205208
default:
206209
break;
207210
}
@@ -489,8 +492,8 @@ JSC3D.Autodesk3DSLoader.prototype.parseSmoothingGroups = function (reader) {
489492
JSC3D.Autodesk3DSLoader.prototype.finalizeCurrentMaterial = function () {
490493
var mat = new JSC3D.Material;
491494

492-
if (this._cur_mat.colorMap) {
493-
mat.textureFileName = this._cur_mat.colorMap.texture;
495+
if (this._cur_mat.colorMap && this._cur_mat.colorMap.url) {
496+
mat.textureFileName = this._cur_mat.colorMap.url;
494497
}
495498
else {
496499
mat.diffuseColor = this._cur_mat.diffuseColor;
@@ -572,25 +575,59 @@ JSC3D.Autodesk3DSLoader.prototype.parse3DS = function(scene, data) {
572575
// mesh.texCoordIndexBuffer = mesh.indexBuffer;
573576
mesh.material = new JSC3D.Material;
574577

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) {
578+
// Defaults: opaque & double-sided (helps open meshes)
579+
mesh.material.transparency = 0;
580+
mesh.isDoubleSided = true;
581+
mesh.material.bothSides = true;
582+
583+
var materialFaces = this._cur_obj.materialFaces || {};
584+
var sawMaterial = false;
585+
for (var materialName in materialFaces) {
586+
if (!Object.prototype.hasOwnProperty.call(materialFaces, materialName))
587+
continue;
588+
sawMaterial = true;
589+
590+
var currentMaterial = this._materials[materialName];
591+
if (!currentMaterial) {
592+
if (JSC3D.console) JSC3D.console.logWarning('3DS: missing material "' + materialName + '", using defaults.');
593+
continue;
594+
}
595+
596+
if (currentMaterial.colorMap && currentMaterial.colorMap.url) {
580597
if(JSC3D.console)
581-
JSC3D.console.logInfo('set texture: '+currentMaterial.colorMap.url);
598+
JSC3D.console.logInfo('set texture: ' + currentMaterial.colorMap.url);
582599
this.setupTexture([mesh], currentMaterial.colorMap.url);
583600
}
584-
else {
601+
else if (currentMaterial.diffuseColor != null) {
585602
mesh.material.diffuseColor = currentMaterial.diffuseColor;
586603
}
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;
604+
605+
// Transparency: normalize (0..1), snap tiny values to 0 to avoid z-sort artifacts
606+
var t = currentMaterial.transparency;
607+
if (typeof t === 'number') {
608+
var alpha = (t > 1) ? (t / 100.0) : t;
609+
alpha = Math.max(0, Math.min(1, alpha));
610+
if (alpha < 0.01) alpha = 0;
611+
mesh.material.transparency = alpha;
612+
} else {
613+
mesh.material.transparency = 0;
614+
}
615+
616+
var two = (currentMaterial.twoSided !== undefined) ? !!currentMaterial.twoSided : true;
617+
mesh.isDoubleSided = two;
618+
mesh.material.bothSides = two;
591619
}
592620

621+
if (!sawMaterial) {
622+
mesh.isDoubleSided = true;
623+
mesh.material.bothSides = true;
624+
mesh.material.transparency = 0;
625+
}
593626

627+
if (!mesh.texCoordBuffer || !mesh.texCoordIndexBuffer) {
628+
mesh.texCoordBuffer = null;
629+
mesh.texCoordIndexBuffer = null;
630+
}
594631

595632
//mesh.faceNormalBuffer = [];
596633
// mesh.init();
@@ -672,7 +709,7 @@ JSC3D.Autodesk3DSLoader.prototype.parse3DS = function(scene, data) {
672709
this._cur_obj = {};
673710
this._cur_obj.name = this.readNulTermString(reader);
674711
this._cur_obj.materials = [];
675-
this._cur_obj.materialFaces = [];
712+
this._cur_obj.materialFaces = {};
676713
break;
677714

678715
case 0x4100: // OBJ_TRIMESH

0 commit comments

Comments
 (0)