Skip to content

Commit 1e4c317

Browse files
authored
Merge pull request #17675 from WestLangley/dev_sprite_alphamap
SpriteMaterial: add alphaMap support
2 parents 8b21e90 + e2047f4 commit 1e4c317

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

docs/api/en/materials/SpriteMaterial.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ <h3>[name]( [param:Object parameters] )</h3>
5050
<h2>Properties</h2>
5151
<p>See the base [page:Material] class for common properties.</p>
5252

53+
<h3>[property:Texture alphaMap]</h3>
54+
<p>The alpha map is a grayscale texture that controls the opacity across the surface
55+
(black: fully transparent; white: fully opaque). Default is null.<br /><br />
56+
57+
Only the color of the texture is used, ignoring the alpha channel if one exists.
58+
For RGB and RGBA textures, the [page:WebGLRenderer WebGL] renderer will use the
59+
green channel when sampling this texture due to the extra bit of precision provided
60+
for green in DXT-compressed and uncompressed RGB 565 formats. Luminance-only and
61+
luminance/alpha textures will also still work as expected.
62+
</p>
63+
5364
<h3>[property:Color color]</h3>
5465
<p>[page:Color] of the material, by default set to white (0xffffff). The [page:.map] is mutiplied by the color.</p>
5566

src/materials/SpriteMaterial.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { MaterialParameters, Material } from './Material';
55
export interface SpriteMaterialParameters extends MaterialParameters {
66
color?: Color | string | number;
77
map?: Texture | null;
8+
alphaMap?: Texture | null;
89
rotation?: number;
910
sizeAttenuation?: boolean;
1011
}
@@ -15,6 +16,7 @@ export class SpriteMaterial extends Material {
1516

1617
color: Color;
1718
map: Texture | null;
19+
alphaMap: Texture | null;
1820
rotation: number;
1921
sizeAttenuation: boolean;
2022
isSpriteMaterial: true;

src/materials/SpriteMaterial.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Color } from '../math/Color.js';
77
* parameters = {
88
* color: <hex>,
99
* map: new THREE.Texture( <Image> ),
10+
* alphaMap: new THREE.Texture( <Image> ),
1011
* rotation: <float>,
1112
* sizeAttenuation: <bool>
1213
* }
@@ -19,8 +20,11 @@ function SpriteMaterial( parameters ) {
1920
this.type = 'SpriteMaterial';
2021

2122
this.color = new Color( 0xffffff );
23+
2224
this.map = null;
2325

26+
this.alphaMap = null;
27+
2428
this.rotation = 0;
2529

2630
this.sizeAttenuation = true;
@@ -40,8 +44,11 @@ SpriteMaterial.prototype.copy = function ( source ) {
4044
Material.prototype.copy.call( this, source );
4145

4246
this.color.copy( source.color );
47+
4348
this.map = source.map;
4449

50+
this.alphaMap = source.alphaMap;
51+
4552
this.rotation = source.rotation;
4653

4754
this.sizeAttenuation = source.sizeAttenuation;

src/renderers/WebGLRenderer.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,17 +2202,44 @@ function WebGLRenderer( parameters ) {
22022202
uniforms.diffuse.value.copy( material.color );
22032203
uniforms.opacity.value = material.opacity;
22042204
uniforms.rotation.value = material.rotation;
2205-
uniforms.map.value = material.map;
22062205

2207-
if ( material.map !== null ) {
2206+
if ( material.map ) {
22082207

2209-
if ( material.map.matrixAutoUpdate === true ) {
2208+
uniforms.map.value = material.map;
22102209

2211-
material.map.updateMatrix();
2210+
}
2211+
2212+
if ( material.alphaMap ) {
2213+
2214+
uniforms.alphaMap.value = material.alphaMap;
2215+
2216+
}
2217+
2218+
// uv repeat and offset setting priorities
2219+
// 1. color map
2220+
// 2. alpha map
2221+
2222+
var uvScaleMap;
2223+
2224+
if ( material.map ) {
2225+
2226+
uvScaleMap = material.map;
2227+
2228+
} else if ( material.alphaMap ) {
2229+
2230+
uvScaleMap = material.alphaMap;
2231+
2232+
}
2233+
2234+
if ( uvScaleMap !== undefined ) {
2235+
2236+
if ( uvScaleMap.matrixAutoUpdate === true ) {
2237+
2238+
uvScaleMap.updateMatrix();
22122239

22132240
}
22142241

2215-
uniforms.uvTransform.value.copy( material.map.matrix );
2242+
uniforms.uvTransform.value.copy( uvScaleMap.matrix );
22162243

22172244
}
22182245

src/renderers/shaders/ShaderLib/sprite_frag.glsl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ uniform float opacity;
55
#include <common>
66
#include <uv_pars_fragment>
77
#include <map_pars_fragment>
8+
#include <alphamap_pars_fragment>
89
#include <fog_pars_fragment>
910
#include <logdepthbuf_pars_fragment>
1011
#include <clipping_planes_pars_fragment>
@@ -18,6 +19,7 @@ void main() {
1819
1920
#include <logdepthbuf_fragment>
2021
#include <map_fragment>
22+
#include <alphamap_fragment>
2123
#include <alphatest_fragment>
2224
2325
outgoingLight = diffuseColor.rgb;

src/renderers/shaders/UniformsLib.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ var UniformsLib = {
193193
center: { value: new Vector2( 0.5, 0.5 ) },
194194
rotation: { value: 0.0 },
195195
map: { value: null },
196+
alphaMap: { value: null },
196197
uvTransform: { value: new Matrix3() }
197198

198199
}

0 commit comments

Comments
 (0)