소스 검색

Simplified SpritePlugin a bit more.

Mr.doob 11 년 전
부모
커밋
b2ab717f22
4개의 변경된 파일101개의 추가작업 그리고 119개의 파일을 삭제
  1. 99 17
      src/extras/renderers/plugins/SpritePlugin.js
  2. 0 98
      src/extras/shaders/ShaderSprite.js
  3. 1 2
      utils/build/includes/extras.json
  4. 1 2
      utils/build/includes/webgl.json

+ 99 - 17
src/extras/renderers/plugins/SpritePlugin.js

@@ -5,7 +5,7 @@
 
 THREE.SpritePlugin = function () {
 
-	var _gl, _renderer, _precision;
+	var _gl, _renderer;
 
 	var vertices, faces, vertexBuffer, elementBuffer;
 	var program, attributes, uniforms;
@@ -15,8 +15,6 @@ THREE.SpritePlugin = function () {
 		_gl = renderer.context;
 		_renderer = renderer;
 
-		_precision = renderer.getPrecision();
-
 		vertices = new Float32Array( [
 			- 0.5, - 0.5, 0, 0, 
 			  0.5, - 0.5, 1, 0,
@@ -38,7 +36,7 @@ THREE.SpritePlugin = function () {
 		_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, elementBuffer );
 		_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faces, _gl.STATIC_DRAW );
 
-		program = createProgram( THREE.ShaderSprite[ 'sprite' ], _precision );
+		program = createProgram();
 
 		attributes = {
 			position:			_gl.getAttribLocation ( program, 'position' ),
@@ -60,13 +58,13 @@ THREE.SpritePlugin = function () {
 			modelViewMatrix: 	_gl.getUniformLocation( program, 'modelViewMatrix' ),
 			projectionMatrix:	_gl.getUniformLocation( program, 'projectionMatrix' ),
 
-			fogType: 			_gl.getUniformLocation( program, 'fogType' ),
-			fogDensity: 		_gl.getUniformLocation( program, 'fogDensity' ),
-			fogNear: 			_gl.getUniformLocation( program, 'fogNear' ),
-			fogFar: 			_gl.getUniformLocation( program, 'fogFar' ),
-			fogColor: 			_gl.getUniformLocation( program, 'fogColor' ),
+			fogType:			_gl.getUniformLocation( program, 'fogType' ),
+			fogDensity:			_gl.getUniformLocation( program, 'fogDensity' ),
+			fogNear:			_gl.getUniformLocation( program, 'fogNear' ),
+			fogFar:				_gl.getUniformLocation( program, 'fogFar' ),
+			fogColor:			_gl.getUniformLocation( program, 'fogColor' ),
 
-			alphaTest: 			_gl.getUniformLocation( program, 'alphaTest' )
+			alphaTest:			_gl.getUniformLocation( program, 'alphaTest' )
 		};
 
 	};
@@ -217,23 +215,107 @@ THREE.SpritePlugin = function () {
 
 	};
 
-	function createProgram ( shader, precision ) {
+	function createProgram () {
 
 		var program = _gl.createProgram();
 
-		var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
 		var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
+		var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
 
-		var prefix = 'precision ' + precision + ' float;\n';
+		_gl.shaderSource( vertexShader, [
+			
+			'precision ' + _renderer.getPrecision() + ' float;',
 
-		_gl.shaderSource( fragmentShader, prefix + shader.fragmentShader );
-		_gl.shaderSource( vertexShader, prefix + shader.vertexShader );
+			'uniform mat4 modelViewMatrix;',
+			'uniform mat4 projectionMatrix;',
+			'uniform float rotation;',
+			'uniform vec2 scale;',
+			'uniform vec2 uvOffset;',
+			'uniform vec2 uvScale;',
+			'uniform vec2 halfViewport;',
+
+			'attribute vec2 position;',
+			'attribute vec2 uv;',
+
+			'varying vec2 vUV;',
+
+			'void main() {',
+
+				'vUV = uvOffset + uv * uvScale;',
+
+				'vec2 alignedPosition = position * scale;',
+
+				'vec2 rotatedPosition;',
+				'rotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;',
+				'rotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;',
+
+				'vec4 finalPosition;',
+
+				'finalPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );',
+				'finalPosition.xy += rotatedPosition;',
+				'finalPosition = projectionMatrix * finalPosition;',
+
+				'gl_Position = finalPosition;',
+
+			'}'
+
+		].join( '\n' ) );
+
+		_gl.shaderSource( fragmentShader, [
+
+			'precision ' + _renderer.getPrecision() + ' float;',
+
+			'uniform vec3 color;',
+			'uniform sampler2D map;',
+			'uniform float opacity;',
+
+			'uniform int fogType;',
+			'uniform vec3 fogColor;',
+			'uniform float fogDensity;',
+			'uniform float fogNear;',
+			'uniform float fogFar;',
+			'uniform float alphaTest;',
+
+			'varying vec2 vUV;',
+
+			'void main() {',
+
+				'vec4 texture = texture2D( map, vUV );',
+
+				'if ( texture.a < alphaTest ) discard;',
+
+				'gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );',
+
+				'if ( fogType > 0 ) {',
+
+					'float depth = gl_FragCoord.z / gl_FragCoord.w;',
+					'float fogFactor = 0.0;',
+
+					'if ( fogType == 1 ) {',
+
+						'fogFactor = smoothstep( fogNear, fogFar, depth );',
+
+					'} else {',
+
+						'const float LOG2 = 1.442695;',
+						'float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );',
+						'fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );',
+
+					'}',
+
+					'gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );',
+
+				'}',
+
+			'}'
+
+		].join( '\n' ) );
 
-		_gl.compileShader( fragmentShader );
 		_gl.compileShader( vertexShader );
+		_gl.compileShader( fragmentShader );
 
-		_gl.attachShader( program, fragmentShader );
 		_gl.attachShader( program, vertexShader );
+		_gl.attachShader( program, fragmentShader );
 
 		_gl.linkProgram( program );
 

+ 0 - 98
src/extras/shaders/ShaderSprite.js

@@ -1,98 +0,0 @@
-/**
- * @author mikael emtinger / http://gomo.se/
- * @author alteredq / http://alteredqualia.com/
- *
- */
-
-THREE.ShaderSprite = {
-
-	'sprite': {
-
-		vertexShader: [
-
-			"uniform mat4 modelViewMatrix;",
-			"uniform mat4 projectionMatrix;",
-			"uniform float rotation;",
-			"uniform vec2 scale;",
-			"uniform vec2 uvOffset;",
-			"uniform vec2 uvScale;",
-			"uniform vec2 halfViewport;",
-
-			"attribute vec2 position;",
-			"attribute vec2 uv;",
-
-			"varying vec2 vUV;",
-
-			"void main() {",
-
-				"vUV = uvOffset + uv * uvScale;",
-
-				"vec2 alignedPosition = position * scale;",
-
-				"vec2 rotatedPosition;",
-				"rotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;",
-				"rotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;",
-
-				"vec4 finalPosition;",
-
-				"finalPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
-				"finalPosition.xy += rotatedPosition;",
-				"finalPosition = projectionMatrix * finalPosition;",
-
-				"gl_Position = finalPosition;",
-
-			"}"
-
-		].join( "\n" ),
-
-		fragmentShader: [
-
-			"uniform vec3 color;",
-			"uniform sampler2D map;",
-			"uniform float opacity;",
-
-			"uniform int fogType;",
-			"uniform vec3 fogColor;",
-			"uniform float fogDensity;",
-			"uniform float fogNear;",
-			"uniform float fogFar;",
-			"uniform float alphaTest;",
-
-			"varying vec2 vUV;",
-
-			"void main() {",
-
-				"vec4 texture = texture2D( map, vUV );",
-
-				"if ( texture.a < alphaTest ) discard;",
-
-				"gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );",
-
-				"if ( fogType > 0 ) {",
-
-					"float depth = gl_FragCoord.z / gl_FragCoord.w;",
-					"float fogFactor = 0.0;",
-
-					"if ( fogType == 1 ) {",
-
-						"fogFactor = smoothstep( fogNear, fogFar, depth );",
-
-					"} else {",
-
-						"const float LOG2 = 1.442695;",
-						"float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
-						"fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
-
-					"}",
-
-					"gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
-
-				"}",
-
-			"}"
-
-		].join( "\n" )
-
-	}
-
-};

+ 1 - 2
utils/build/includes/extras.json

@@ -63,6 +63,5 @@
 	"src/extras/renderers/plugins/ShadowMapPlugin.js",
 	"src/extras/renderers/plugins/SpritePlugin.js",
 	"src/extras/renderers/plugins/DepthPassPlugin.js",
-	"src/extras/shaders/ShaderFlares.js",
-	"src/extras/shaders/ShaderSprite.js"
+	"src/extras/shaders/ShaderFlares.js"
 ]

+ 1 - 2
utils/build/includes/webgl.json

@@ -89,6 +89,5 @@
 	"src/extras/renderers/plugins/LensFlarePlugin.js",
 	"src/extras/renderers/plugins/ShadowMapPlugin.js",
 	"src/extras/renderers/plugins/SpritePlugin.js",
-	"src/extras/shaders/ShaderFlares.js",
-	"src/extras/shaders/ShaderSprite.js"
+	"src/extras/shaders/ShaderFlares.js"
 ]