Load alpha value from .mtl material

This commit is contained in:
Lucas Dower 2022-07-12 19:26:19 +01:00
parent 7c71f60817
commit f9d2e37a07
4 changed files with 32 additions and 3 deletions

View File

@ -4,6 +4,7 @@ uniform sampler2D u_texture;
uniform sampler2D u_alpha;
uniform bool u_useAlphaMap;
uniform bool u_useAlphaChannel;
uniform float u_alphaValue;
varying float v_lighting;
varying vec2 v_texcoord;
@ -106,6 +107,8 @@ void main() {
alpha = u_useAlphaChannel ? texture2D(u_alpha, tex).a : texture2D(u_alpha, tex).r;
}
alpha *= u_alphaValue;
alpha = dither8x8(gl_FragCoord.xy, alpha);
if (alpha < 0.5)
{

View File

@ -1,6 +1,6 @@
import { MaterialType, Mesh, SolidMaterial, TexturedMaterial, Tri } from '../mesh';
import { Vector3 } from '../vector';
import { UV, ASSERT, AppError, REGEX_NUMBER, RegExpBuilder, REGEX_NZ_ANY, LOG_ERROR } from '../util';
import { UV, ASSERT, AppError, REGEX_NUMBER, RegExpBuilder, REGEX_NZ_ANY, LOG_ERROR, LOG } from '../util';
import { checkFractional, checkNaN } from '../math';
import fs from 'fs';
@ -189,6 +189,7 @@ export class ObjImporter extends IImporter {
];
private _currentColour: RGBA = RGBAColours.BLACK;
private _currentAlpha: number = 1.0;
private _currentTexture: string = '';
private _currentTransparencyTexture: string = '';
private _materialReady: boolean = false;
@ -220,7 +221,7 @@ export class ObjImporter extends IImporter {
const b = parseFloat(match.b);
checkNaN(r, g, b);
checkFractional(r, g, b);
this._currentColour = { r: r, g: g, b: b, a: 1.0 };
this._currentColour = { r: r, g: g, b: b, a: this._currentAlpha };
this._materialReady = true;
},
},
@ -251,6 +252,21 @@ export class ObjImporter extends IImporter {
this._materialReady = true;
},
},
{
// Transparency value
// e.g. 'd 0.7500'
regex: new RegExpBuilder()
.add(/^d/)
.addNonzeroWhitespace()
.add(REGEX_NUMBER, 'alpha')
.toRegExp(),
delegate: (match: { [key: string]: string }) => {
const alpha = parseFloat(match.alpha);
checkNaN(alpha);
checkFractional(alpha);
this._currentAlpha = alpha;
},
},
];
override parseFile(filePath: string) {
@ -272,6 +288,7 @@ export class ObjImporter extends IImporter {
}
this._parseMTL();
LOG('Materials', this._materials);
}
override toMesh(): Mesh {
@ -372,6 +389,7 @@ export class ObjImporter extends IImporter {
type: MaterialType.textured,
path: this._currentTexture,
alphaPath: this._currentTransparencyTexture === '' ? undefined : this._currentTransparencyTexture,
alphaValue: this._currentAlpha,
};
this._currentTransparencyTexture = '';
} else {
@ -380,6 +398,7 @@ export class ObjImporter extends IImporter {
colour: this._currentColour,
};
}
this._currentAlpha = 1.0;
}
}
}

View File

@ -29,6 +29,7 @@ export interface TexturedMaterial {
path: string;
type: MaterialType.textured;
alphaPath?: string;
alphaValue: number;
}
export type MaterialMap = {[key: string]: (SolidMaterial | TexturedMaterial)};
@ -312,7 +313,9 @@ export class Mesh {
return material.colour;
} else {
ASSERT(materialName in this._loadedTextures, 'Sampling texture that is not loaded');
return this._loadedTextures[materialName].getRGBA(uv, textureFiltering);
const colour = this._loadedTextures[materialName].getRGBA(uv, textureFiltering);
colour.a *= material.alphaValue;
return colour;
}
}
@ -384,6 +387,8 @@ export class Mesh {
} else {
materials[materialName] = {
type: MaterialType.textured,
alphaValue: material.alphaValue,
alphaPath: material.alphaPath,
path: material.path,
};
};

View File

@ -184,6 +184,7 @@ export class Renderer {
src: material.path,
mag: this._gl.LINEAR,
}),
alphaValue: material.alphaValue,
alpha: material.alphaPath ? twgl.createTexture(this._gl, {
src: material.alphaPath,
mag: this._gl.LINEAR,
@ -294,6 +295,7 @@ export class Renderer {
u_alpha: materialBuffer.material.alpha,
u_useAlphaMap: materialBuffer.material.alpha !== undefined,
u_useAlphaChannel: materialBuffer.material.useAlphaChannel,
u_alphaValue: materialBuffer.material.alphaValue,
});
} else {
this._drawRegister(materialBuffer.buffer, ShaderManager.Get.solidTriProgram, {