Skip to content

Commit

Permalink
🪟 Transparency update
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Mar 1, 2024
1 parent bb25c00 commit c06ebd0
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 6 deletions.
8 changes: 6 additions & 2 deletions dist-single/gsots3d.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist-single/gsots3d.js.map

Large diffs are not rendered by default.

Binary file added examples/_textures/tr-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gsots3d",
"version": "0.0.5-alpha.10",
"version": "0.0.5-alpha.11",
"description": "Getting S**t On The Screen in 3D. A library for doing 3D graphics in the browser.",
"author": "Ben Coleman",
"license": "MIT",
Expand Down Expand Up @@ -36,6 +36,7 @@
"lint-fix": "eslint --ext .ts src --fix && prettier --write src && prettier --write shaders",
"check": "tsc",
"build": "tsc && tsup",
"build:all": "npm run build && npm run build-single && npm run docs",
"watch": "tsc && npm run build && run-when-changed --watch 'src/**' --watch 'shaders/**' --exec 'npm run build'",
"build-single": "tsc && tsup --config tsup.config-single.js",
"watch-single": "tsc && npm run build-single && run-when-changed --watch 'src/**' --watch 'shaders/**' --exec 'npm run build-single'",
Expand Down
7 changes: 7 additions & 0 deletions shaders/phong/glsl.frag
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct Material {
sampler2D normalTex;
bool hasNormalTex;
bool unshaded;
float alphaCutoff;
};

// Inputs from vertex shader
Expand Down Expand Up @@ -169,6 +170,12 @@ void main() {
texCoord = u_flipTextureY ? vec2(v_texCoord.x, 1.0 - v_texCoord.y) : v_texCoord;
texCoord = u_flipTextureX ? vec2(1.0 - texCoord.x, texCoord.y) : texCoord;

// So parts of textures can be transparent
vec4 texel = texture(u_mat.diffuseTex, texCoord);
if (texel.a < u_mat.alphaCutoff) {
discard;
}

vec3 N = normalize(v_normal);

// Normal mapping, this is expensive so only do it if we have a normal map
Expand Down
10 changes: 10 additions & 0 deletions src/engine/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export class Material {
*/
public unshaded: boolean

/**
* Transparency threshold, pixels with alpha below this value will be discarded
* @default 0.0
*/
public alphaCutoff: number

/**
* Create a new default material with diffuse white colour, all all default properties
*/
Expand All @@ -88,6 +94,7 @@ export class Material {
this.reflectivity = 0.0

this.unshaded = false
this.alphaCutoff = 0.0

// 1 pixel white texture is used for solid colour & flat materials
// A trick to avoid having multiple shaders for textured & non-textured materials
Expand Down Expand Up @@ -257,6 +264,7 @@ export class Material {
normalTex: this.normalTex ? this.normalTex : null,
hasNormalTex: this.normalTex ? true : false,
unshaded: this.unshaded,
alphaCutoff: this.alphaCutoff,
} as UniformSet
}

Expand All @@ -275,6 +283,8 @@ export class Material {
m.diffuseTex = this.diffuseTex
m.specularTex = this.specularTex
m.normalTex = this.normalTex
m.unshaded = this.unshaded
m.alphaCutoff = this.alphaCutoff

return m
}
Expand Down

0 comments on commit c06ebd0

Please sign in to comment.