소스 검색

Merge pull request #14636 from WestLangley/dev-sprite_size_attenuation

Add SpriteMaterial sizeAttenuation property
Mr.doob 7 년 전
부모
커밋
8d162f50e4
3개의 변경된 파일20개의 추가작업 그리고 8개의 파일을 삭제
  1. 4 1
      docs/api/materials/SpriteMaterial.html
  2. 6 4
      src/materials/SpriteMaterial.js
  3. 10 3
      src/renderers/shaders/ShaderLib/sprite_vert.glsl

+ 4 - 1
docs/api/materials/SpriteMaterial.html

@@ -67,8 +67,11 @@ scene.add( sprite );
 		<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>
 

+ 6 - 4
src/materials/SpriteMaterial.js

@@ -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>
  * }
  */
 
@@ -25,6 +23,8 @@ function SpriteMaterial( parameters ) {
 
 	this.rotation = 0;
 
+	this.sizeAttenuation = true;
+
 	this.lights = false;
 	this.transparent = true;
 
@@ -45,6 +45,8 @@ SpriteMaterial.prototype.copy = function ( source ) {
 
 	this.rotation = source.rotation;
 
+	this.sizeAttenuation = source.sizeAttenuation;
+
 	return this;
 
 };

+ 10 - 3
src/renderers/shaders/ShaderLib/sprite_vert.glsl

@@ -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;