Skip to content

ShaderChunk: Turn alphaTest into a uniform #22110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions examples/jsm/utils/GeometryCompressionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ class PackedPhongMaterial extends MeshPhongMaterial {
ShaderChunk.uv2_pars_fragment,
ShaderChunk.map_pars_fragment,
ShaderChunk.alphamap_pars_fragment,
ShaderChunk.alphatest_pars_fragment,
ShaderChunk.aomap_pars_fragment,
ShaderChunk.lightmap_pars_fragment,
ShaderChunk.emissivemap_pars_fragment,
Expand Down
10 changes: 5 additions & 5 deletions examples/webgl_interactive_points.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

uniform vec3 color;
uniform sampler2D pointTexture;
uniform float alphaTest;

varying vec3 vColor;

Expand All @@ -45,7 +46,7 @@

gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );

if ( gl_FragColor.a < ALPHATEST ) discard;
if ( gl_FragColor.a < alphaTest ) discard;

}

Expand Down Expand Up @@ -120,12 +121,11 @@

uniforms: {
color: { value: new THREE.Color( 0xffffff ) },
pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ) }
pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ) },
alphaTest: { value: 0.9 }
},
vertexShader: document.getElementById( 'vertexshader' ).textContent,
fragmentShader: document.getElementById( 'fragmentshader' ).textContent,

alphaTest: 0.9
fragmentShader: document.getElementById( 'fragmentshader' ).textContent

} );

Expand Down
2 changes: 2 additions & 0 deletions src/renderers/shaders/ShaderChunk.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import alphamap_fragment from './ShaderChunk/alphamap_fragment.glsl.js';
import alphamap_pars_fragment from './ShaderChunk/alphamap_pars_fragment.glsl.js';
import alphatest_fragment from './ShaderChunk/alphatest_fragment.glsl.js';
import alphatest_pars_fragment from './ShaderChunk/alphatest_pars_fragment.glsl.js';
import aomap_fragment from './ShaderChunk/aomap_fragment.glsl.js';
import aomap_pars_fragment from './ShaderChunk/aomap_pars_fragment.glsl.js';
import begin_vertex from './ShaderChunk/begin_vertex.glsl.js';
Expand Down Expand Up @@ -133,6 +134,7 @@ export const ShaderChunk = {
alphamap_fragment: alphamap_fragment,
alphamap_pars_fragment: alphamap_pars_fragment,
alphatest_fragment: alphatest_fragment,
alphatest_pars_fragment: alphatest_pars_fragment,
aomap_fragment: aomap_fragment,
aomap_pars_fragment: aomap_pars_fragment,
begin_vertex: begin_vertex,
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/shaders/ShaderChunk/alphatest_fragment.glsl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default /* glsl */`
#ifdef ALPHATEST
#ifdef USE_ALPHATEST

if ( diffuseColor.a < ALPHATEST ) discard;
if ( diffuseColor.a < alphaTest ) discard;

#endif
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default /* glsl */`
#ifdef USE_ALPHATEST

uniform float alphaTest;

#endif
`;
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib/depth_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default /* glsl */`
#include <uv_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>

Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib/distanceRGBA_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ varying vec3 vWorldPosition;
#include <uv_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <clipping_planes_pars_fragment>

void main () {
Expand Down
3 changes: 2 additions & 1 deletion src/renderers/shaders/ShaderLib/meshbasic_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ uniform float opacity;
#include <uv2_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <envmap_common_pars_fragment>
Expand Down Expand Up @@ -42,7 +43,7 @@ void main() {

// accumulation (baked indirect lighting only)
#ifdef USE_LIGHTMAP

vec4 lightMapTexel= texture2D( lightMap, vUv2 );
reflectedLight.indirectDiffuse += lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;

Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib/meshlambert_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ varying vec3 vIndirectFront;
#include <uv2_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <emissivemap_pars_fragment>
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shaders/ShaderLib/meshmatcap_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ varying vec3 vViewPosition;
#include <uv_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>

#include <alphatest_pars_fragment>
#include <fog_pars_fragment>
#include <bumpmap_pars_fragment>
#include <normalmap_pars_fragment>
Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib/meshphong_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ uniform float opacity;
#include <uv2_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <emissivemap_pars_fragment>
Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib/meshphysical_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ varying vec3 vViewPosition;
#include <uv2_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <emissivemap_pars_fragment>
Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib/meshtoon_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ uniform float opacity;
#include <uv2_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <emissivemap_pars_fragment>
Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib/points_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ uniform float opacity;
#include <common>
#include <color_pars_fragment>
#include <map_particle_pars_fragment>
#include <alphatest_pars_fragment>
#include <fog_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>
Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib/sprite_frag.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ uniform float opacity;
#include <uv_pars_fragment>
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <fog_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>
Expand Down
3 changes: 3 additions & 0 deletions src/renderers/shaders/UniformsLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const UniformsLib = {
uv2Transform: { value: new Matrix3() },

alphaMap: { value: null },
alphaTest: { value: null }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, shouldn't this be 0 (similar to opacity)?


},

Expand Down Expand Up @@ -193,6 +194,7 @@ const UniformsLib = {
scale: { value: 1.0 },
map: { value: null },
alphaMap: { value: null },
alphaTest: { value: null },
uvTransform: { value: new Matrix3() }

},
Expand All @@ -205,6 +207,7 @@ const UniformsLib = {
rotation: { value: 0.0 },
map: { value: null },
alphaMap: { value: null },
alphaTest: { value: null },
uvTransform: { value: new Matrix3() }

}
Expand Down
18 changes: 18 additions & 0 deletions src/renderers/webgl/WebGLMaterials.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ function WebGLMaterials( properties ) {

}

if ( material.alphaTest > 0 ) {

uniforms.alphaTest.value = material.alphaTest;

}

if ( material.specularMap ) {

uniforms.specularMap.value = material.specularMap;
Expand Down Expand Up @@ -330,6 +336,12 @@ function WebGLMaterials( properties ) {

}

if ( material.alphaTest > 0 ) {

uniforms.alphaTest.value = material.alphaTest;

}

// uv repeat and offset setting priorities
// 1. color map
// 2. alpha map
Expand Down Expand Up @@ -378,6 +390,12 @@ function WebGLMaterials( properties ) {

}

if ( material.alphaTest > 0 ) {

uniforms.alphaTest.value = material.alphaTest;

}

// uv repeat and offset setting priorities
// 1. color map
// 2. alpha map
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,6 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) {

customDefines,

parameters.alphaTest ? '#define ALPHATEST ' + parameters.alphaTest + ( parameters.alphaTest % 1 ? '' : '.0' ) : '', // add '.0' if integer

'#define GAMMA_FACTOR ' + gammaFactorDefine,

( parameters.useFog && parameters.fog ) ? '#define USE_FOG' : '',
Expand All @@ -609,6 +607,7 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) {
parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '',
parameters.metalnessMap ? '#define USE_METALNESSMAP' : '',
parameters.alphaMap ? '#define USE_ALPHAMAP' : '',
parameters.alphaTest ? '#define USE_ALPHATEST' : '',

parameters.sheen ? '#define USE_SHEEN' : '',
parameters.transmission ? '#define USE_TRANSMISSION' : '',
Expand Down
6 changes: 3 additions & 3 deletions src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingSta
'map', 'mapEncoding', 'matcap', 'matcapEncoding', 'envMap', 'envMapMode', 'envMapEncoding', 'envMapCubeUV',
'lightMap', 'lightMapEncoding', 'aoMap', 'emissiveMap', 'emissiveMapEncoding', 'bumpMap', 'normalMap', 'objectSpaceNormalMap', 'tangentSpaceNormalMap', 'clearcoatMap', 'clearcoatRoughnessMap', 'clearcoatNormalMap', 'displacementMap', 'specularMap',
'roughnessMap', 'metalnessMap', 'gradientMap',
'alphaMap', 'combine', 'vertexColors', 'vertexAlphas', 'vertexTangents', 'vertexUvs', 'uvsVertexOnly', 'fog', 'useFog', 'fogExp2',
'alphaMap', 'alphaTest', 'combine', 'vertexColors', 'vertexAlphas', 'vertexTangents', 'vertexUvs', 'uvsVertexOnly', 'fog', 'useFog', 'fogExp2',
'flatShading', 'sizeAttenuation', 'logarithmicDepthBuffer', 'skinning',
'maxBones', 'useVertexTexture', 'morphTargets', 'morphNormals', 'premultipliedAlpha',
'numDirLights', 'numPointLights', 'numSpotLights', 'numHemiLights', 'numRectAreaLights',
'numDirLightShadows', 'numPointLightShadows', 'numSpotLightShadows',
'shadowMapEnabled', 'shadowMapType', 'toneMapping', 'physicallyCorrectLights',
'alphaTest', 'doubleSided', 'flipSided', 'numClippingPlanes', 'numClipIntersection', 'depthPacking', 'dithering',
'doubleSided', 'flipSided', 'numClippingPlanes', 'numClipIntersection', 'depthPacking', 'dithering',
'sheen', 'transmission', 'transmissionMap', 'thicknessMap'
];

Expand Down Expand Up @@ -197,6 +197,7 @@ function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingSta
metalnessMap: !! material.metalnessMap,
specularMap: !! material.specularMap,
alphaMap: !! material.alphaMap,
alphaTest: material.alphaTest > 0,

gradientMap: !! material.gradientMap,

Expand Down Expand Up @@ -253,7 +254,6 @@ function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingSta

premultipliedAlpha: material.premultipliedAlpha,

alphaTest: material.alphaTest,
doubleSided: material.side === DoubleSide,
flipSided: material.side === BackSide,

Expand Down