PNG Transparency #21

Treat materials with png texture automatically as transparent.
This commit is contained in:
Viktor Kovacs 2021-05-11 20:43:57 +02:00
parent 59ebed95bc
commit 3842e1afcf
5 changed files with 20 additions and 5 deletions

View File

@ -280,7 +280,7 @@ OV.Importer3dm = class extends OV.ImporterBase
SetColor (material.diffuse, rhinoMaterial.diffuseColor); SetColor (material.diffuse, rhinoMaterial.diffuseColor);
SetColor (material.specular, rhinoMaterial.specularColor); SetColor (material.specular, rhinoMaterial.specularColor);
material.opacity = 1.0 - rhinoMaterial.transparency; material.opacity = 1.0 - rhinoMaterial.transparency;
material.transparent = OV.IsLower (material.opacity, 1.0); OV.UpdateMaterialTransparency (material);
// material.shininess = rhinoMaterial.shine / 255.0; // material.shininess = rhinoMaterial.shine / 255.0;
if (IsBlack (material.diffuse) && !IsWhite (rhinoMaterial.reflectionColor)) { if (IsBlack (material.diffuse) && !IsWhite (rhinoMaterial.reflectionColor)) {
SetColor (material.diffuse, rhinoMaterial.reflectionColor); SetColor (material.diffuse, rhinoMaterial.reflectionColor);

View File

@ -158,9 +158,10 @@ OV.Importer3ds = class extends OV.ImporterBase
shininessStrength = obj.ReadPercentageChunk (reader, chunkLength); shininessStrength = obj.ReadPercentageChunk (reader, chunkLength);
} else if (chunkId === OV.CHUNK3DS.MAT_TRANSPARENCY) { } else if (chunkId === OV.CHUNK3DS.MAT_TRANSPARENCY) {
material.opacity = 1.0 - obj.ReadPercentageChunk (reader, chunkLength); 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) { } else if (chunkId === OV.CHUNK3DS.MAT_TEXMAP) {
material.diffuseMap = obj.ReadTextureMapChunk (reader, chunkLength); material.diffuseMap = obj.ReadTextureMapChunk (reader, chunkLength);
OV.UpdateMaterialTransparency (material);
} else { } else {
obj.SkipChunk (reader, chunkLength); obj.SkipChunk (reader, chunkLength);
} }

View File

@ -226,6 +226,7 @@ OV.ImporterObj = class extends OV.ImporterBase
return true; return true;
} }
this.currentMaterial.diffuseMap = CreateTexture (keyword, line, this.callbacks); this.currentMaterial.diffuseMap = CreateTexture (keyword, line, this.callbacks);
OV.UpdateMaterialTransparency (this.currentMaterial);
return true; return true;
} else if (keyword === 'map_ks') { } else if (keyword === 'map_ks') {
if (this.currentMaterial === null || parameters.length === 0) { if (this.currentMaterial === null || parameters.length === 0) {
@ -268,14 +269,14 @@ OV.ImporterObj = class extends OV.ImporterBase
return true; return true;
} }
this.currentMaterial.opacity = 1.0 - parseFloat (parameters[0]); this.currentMaterial.opacity = 1.0 - parseFloat (parameters[0]);
this.currentMaterial.transparent = OV.IsLower (this.currentMaterial.opacity, 1.0); OV.UpdateMaterialTransparency (this.currentMaterial);
return true; return true;
} else if (keyword === 'd') { } else if (keyword === 'd') {
if (this.currentMaterial === null || parameters.length < 1) { if (this.currentMaterial === null || parameters.length < 1) {
return true; return true;
} }
this.currentMaterial.opacity = parseFloat (parameters[0]); this.currentMaterial.opacity = parseFloat (parameters[0]);
this.currentMaterial.transparent = OV.IsLower (this.currentMaterial.opacity, 1.0); OV.UpdateMaterialTransparency (this.currentMaterial);
return true; return true;
} }

View File

@ -290,7 +290,7 @@ OV.ImporterPly = class extends OV.ImporterBase
material.name = materialName; material.name = materialName;
material.diffuse = new OV.Color (color[0], color[1], color[2]); material.diffuse = new OV.Color (color[0], color[1], color[2]);
material.opacity = color[3] / 255.0; material.opacity = color[3] / 255.0;
material.transparent = OV.IsLower (material.opacity, 1.0); OV.UpdateMaterialTransparency (material);
materialIndex = obj.model.AddMaterial (material); materialIndex = obj.model.AddMaterial (material);
colorToMaterial[materialName] = materialIndex; colorToMaterial[materialName] = materialIndex;
} }

View File

@ -70,3 +70,16 @@ OV.ResizeImageToPowerOfTwoSides = function (image)
context.drawImage (image, 0, 0, width, height); context.drawImage (image, 0, 0, width, height);
return context.getImageData (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;
}
}
};