From 3842e1afcf7ffa419b710adba3155e8b64e14322 Mon Sep 17 00:00:00 2001 From: Viktor Kovacs Date: Tue, 11 May 2021 20:43:57 +0200 Subject: [PATCH] PNG Transparency #21 Treat materials with png texture automatically as transparent. --- source/external/rhino.importer.js | 2 +- source/import/importer3ds.js | 3 ++- source/import/importerobj.js | 5 +++-- source/import/importerply.js | 2 +- source/import/importerutils.js | 13 +++++++++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/source/external/rhino.importer.js b/source/external/rhino.importer.js index 0b4d854..2346d12 100644 --- a/source/external/rhino.importer.js +++ b/source/external/rhino.importer.js @@ -280,7 +280,7 @@ OV.Importer3dm = class extends OV.ImporterBase SetColor (material.diffuse, rhinoMaterial.diffuseColor); SetColor (material.specular, rhinoMaterial.specularColor); material.opacity = 1.0 - rhinoMaterial.transparency; - material.transparent = OV.IsLower (material.opacity, 1.0); + OV.UpdateMaterialTransparency (material); // material.shininess = rhinoMaterial.shine / 255.0; if (IsBlack (material.diffuse) && !IsWhite (rhinoMaterial.reflectionColor)) { SetColor (material.diffuse, rhinoMaterial.reflectionColor); diff --git a/source/import/importer3ds.js b/source/import/importer3ds.js index aa9ffae..36b5eb8 100644 --- a/source/import/importer3ds.js +++ b/source/import/importer3ds.js @@ -158,9 +158,10 @@ OV.Importer3ds = class extends OV.ImporterBase shininessStrength = obj.ReadPercentageChunk (reader, chunkLength); } else if (chunkId === OV.CHUNK3DS.MAT_TRANSPARENCY) { material.opacity = 1.0 - obj.ReadPercentageChunk (reader, chunkLength); - material.transparent = OV.IsLower (material.opacity, 1.0); + OV.UpdateMaterialTransparency (material); } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP) { material.diffuseMap = obj.ReadTextureMapChunk (reader, chunkLength); + OV.UpdateMaterialTransparency (material); } else { obj.SkipChunk (reader, chunkLength); } diff --git a/source/import/importerobj.js b/source/import/importerobj.js index c0ab107..cedcac1 100644 --- a/source/import/importerobj.js +++ b/source/import/importerobj.js @@ -226,6 +226,7 @@ OV.ImporterObj = class extends OV.ImporterBase return true; } this.currentMaterial.diffuseMap = CreateTexture (keyword, line, this.callbacks); + OV.UpdateMaterialTransparency (this.currentMaterial); return true; } else if (keyword === 'map_ks') { if (this.currentMaterial === null || parameters.length === 0) { @@ -268,14 +269,14 @@ OV.ImporterObj = class extends OV.ImporterBase return true; } this.currentMaterial.opacity = 1.0 - parseFloat (parameters[0]); - this.currentMaterial.transparent = OV.IsLower (this.currentMaterial.opacity, 1.0); + OV.UpdateMaterialTransparency (this.currentMaterial); return true; } else if (keyword === 'd') { if (this.currentMaterial === null || parameters.length < 1) { return true; } this.currentMaterial.opacity = parseFloat (parameters[0]); - this.currentMaterial.transparent = OV.IsLower (this.currentMaterial.opacity, 1.0); + OV.UpdateMaterialTransparency (this.currentMaterial); return true; } diff --git a/source/import/importerply.js b/source/import/importerply.js index 4de747c..9cf77d8 100644 --- a/source/import/importerply.js +++ b/source/import/importerply.js @@ -290,7 +290,7 @@ OV.ImporterPly = class extends OV.ImporterBase material.name = materialName; material.diffuse = new OV.Color (color[0], color[1], color[2]); material.opacity = color[3] / 255.0; - material.transparent = OV.IsLower (material.opacity, 1.0); + OV.UpdateMaterialTransparency (material); materialIndex = obj.model.AddMaterial (material); colorToMaterial[materialName] = materialIndex; } diff --git a/source/import/importerutils.js b/source/import/importerutils.js index 6cbb1a3..580bdda 100644 --- a/source/import/importerutils.js +++ b/source/import/importerutils.js @@ -70,3 +70,16 @@ OV.ResizeImageToPowerOfTwoSides = function (image) context.drawImage (image, 0, 0, width, height); return context.getImageData (0, 0, width, height); }; + +OV.UpdateMaterialTransparency = function (material) +{ + material.transparent = false; + if (OV.IsLower (material.opacity, 1.0)) { + material.transparent = true; + } else if (material.diffuseMap !== null) { + let extension = OV.GetFileExtension (material.diffuseMap.name); + if (extension === 'png') { + material.transparent = true; + } + } +};