Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/api/materials/SpriteMaterial.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ <h3>[property:Texture map]</h3>
<h3>[property:Radians rotation]</h3>
<p>The rotation of the sprite in radians. Default is 0.</p>

<h3>[property:Boolean sizeAttenuation]</h3>
<p>Whether the size of the sprite is attenuated by the camera depth. (Perspective camera only.) Default is *true*.</p>

<h2>Methods</h2>
<p>See the base [page:Material] class for common methods.</p>
<p>See the base [page:Material] class for common methods.</p>

<h2>Source</h2>

Expand Down
10 changes: 6 additions & 4 deletions src/materials/SpriteMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { Color } from '../math/Color.js';
*
* parameters = {
* color: <hex>,
* opacity: <float>,
* map: new THREE.Texture( <Image> ),
*
* uvOffset: new THREE.Vector2(),
* uvScale: new THREE.Vector2()
* rotation: <float>,
* sizeAttenuation: <bool>
* }
*/

Expand All @@ -25,6 +23,8 @@ function SpriteMaterial( parameters ) {

this.rotation = 0;

this.sizeAttenuation = true;

this.lights = false;
this.transparent = true;

Expand All @@ -45,6 +45,8 @@ SpriteMaterial.prototype.copy = function ( source ) {

this.rotation = source.rotation;

this.sizeAttenuation = source.sizeAttenuation;

return this;

};
Expand Down
13 changes: 10 additions & 3 deletions src/renderers/shaders/ShaderLib/sprite_vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ void main() {

#include <uv_vertex>

vec4 mvPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );

vec2 scale;
scale.x = length( vec3( modelMatrix[ 0 ].x, modelMatrix[ 0 ].y, modelMatrix[ 0 ].z ) );
scale.y = length( vec3( modelMatrix[ 1 ].x, modelMatrix[ 1 ].y, modelMatrix[ 1 ].z ) );

#ifndef USE_SIZEATTENUATION

bool isPerspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 );

if ( isPerspective ) scale *= - mvPosition.z;

#endif

vec2 alignedPosition = ( position.xy - ( center - vec2( 0.5 ) ) ) * scale;

vec2 rotatedPosition;
rotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;
rotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;

vec4 mvPosition;

mvPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );
mvPosition.xy += rotatedPosition;

gl_Position = projectionMatrix * mvPosition;
Expand Down