WebGLPrograms.js 7.1 KB
Newer Older
M
Mr.doob 已提交
1
THREE.WebGLPrograms = function ( renderer, capabilities ) {
G
gero3 已提交
2

3
	var programs = [];
G
gero3 已提交
4

G
gero3 已提交
5 6 7 8 9 10 11 12
	var shaderIDs = {
		MeshDepthMaterial: 'depth',
		MeshNormalMaterial: 'normal',
		MeshBasicMaterial: 'basic',
		MeshLambertMaterial: 'lambert',
		MeshPhongMaterial: 'phong',
		LineBasicMaterial: 'basic',
		LineDashedMaterial: 'dashed',
13
		PointsMaterial: 'points'
G
gero3 已提交
14
	};
G
gero3 已提交
15

M
Mr.doob 已提交
16 17
	var parameterNames = [
		"precision", "supportsVertexTextures", "map", "envMap", "envMapMode",
M
Mr.doob 已提交
18
		"lightMap", "aoMap", "emissiveMap", "bumpMap", "normalMap", "displacementMap", "specularMap",
M
Mr.doob 已提交
19 20 21 22
		"alphaMap", "combine", "vertexColors", "fog", "useFog", "fogExp",
		"flatShading", "sizeAttenuation", "logarithmicDepthBuffer", "skinning",
		"maxBones", "useVertexTexture", "morphTargets", "morphNormals",
		"maxMorphTargets", "maxMorphNormals", "maxDirLights", "maxPointLights",
M
Mr.doob 已提交
23
		"maxSpotLights", "maxHemiLights", "maxShadows", "shadowMapEnabled", "pointLightShadows",
M
Mr.doob 已提交
24 25 26
		"shadowMapType", "shadowMapDebug", "alphaTest", "metal", "doubleSided",
		"flipSided"
	];
G
gero3 已提交
27 28


G
gero3 已提交
29 30
	function allocateBones ( object ) {

G
gero3 已提交
31
		if ( capabilities.floatVertexTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
G
gero3 已提交
32 33 34 35 36 37 38 39 40 41 42 43

			return 1024;

		} else {

			// default for when object is not specified
			// ( for example when prebuilding shader to be used with multiple objects )
			//
			//  - leave some extra space for other uniforms
			//  - limit here is ANGLE's 254 max uniform vectors
			//    (up to 54 should be safe)

G
gero3 已提交
44
			var nVertexUniforms = capabilities.maxVertexUniforms;
G
gero3 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
			var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );

			var maxBones = nVertexMatrices;

			if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {

				maxBones = Math.min( object.skeleton.bones.length, maxBones );

				if ( maxBones < object.skeleton.bones.length ) {

					console.warn( 'WebGLRenderer: too many bones - ' + object.skeleton.bones.length + ', this GPU supports just ' + maxBones + ' (try OpenGL instead of ANGLE)' );

				}

			}

			return maxBones;

		}

	}
G
gero3 已提交
66

G
gero3 已提交
67 68 69 70 71 72 73 74 75 76 77
	function allocateLights( lights ) {

		var dirLights = 0;
		var pointLights = 0;
		var spotLights = 0;
		var hemiLights = 0;

		for ( var l = 0, ll = lights.length; l < ll; l ++ ) {

			var light = lights[ l ];

M
Mr.doob 已提交
78
			if ( light.visible === false ) continue;
G
gero3 已提交
79 80 81 82 83 84 85 86 87 88 89

			if ( light instanceof THREE.DirectionalLight ) dirLights ++;
			if ( light instanceof THREE.PointLight ) pointLights ++;
			if ( light instanceof THREE.SpotLight ) spotLights ++;
			if ( light instanceof THREE.HemisphereLight ) hemiLights ++;

		}

		return { 'directional': dirLights, 'point': pointLights, 'spot': spotLights, 'hemi': hemiLights };

	}
G
gero3 已提交
90

G
gero3 已提交
91 92 93
	function allocateShadows( lights ) {

		var maxShadows = 0;
94
		var pointLightShadows = 0;
G
gero3 已提交
95 96 97 98 99 100 101

		for ( var l = 0, ll = lights.length; l < ll; l ++ ) {

			var light = lights[ l ];

			if ( ! light.castShadow ) continue;

102 103 104 105 106 107
			if ( light instanceof THREE.SpotLight || light instanceof THREE.DirectionalLight ) maxShadows ++;
			if ( light instanceof THREE.PointLight ) {

				maxShadows ++;
				pointLightShadows ++;

M
Mr.doob 已提交
108
			}
G
gero3 已提交
109 110 111

		}

112
		return { 'maxShadows': maxShadows, 'pointLightShadows': pointLightShadows };
G
gero3 已提交
113

G
gero3 已提交
114
	}
G
gero3 已提交
115

M
Mr.doob 已提交
116
	this.getParameters = function ( material, lights, fog, object ) {
G
gero3 已提交
117 118 119

		var shaderID = shaderIDs[ material.type ];
		// heuristics to create shader parameters according to lights in the scene
G
gero3 已提交
120 121
		// (not to blow over maxLights budget)

G
gero3 已提交
122
		var maxLightCount = allocateLights( lights );
123
		var allocatedShadows = allocateShadows( lights );
G
gero3 已提交
124
		var maxBones = allocateBones( object );
M
Mr.doob 已提交
125
		var precision = renderer.getPrecision();
G
gero3 已提交
126 127 128

		if ( material.precision !== null ) {

G
gero3 已提交
129
			precision = capabilities.getMaxPrecision( material.precision );
G
gero3 已提交
130 131 132 133 134 135 136 137 138 139

			if ( precision !== material.precision ) {

				console.warn( 'THREE.WebGLRenderer.initMaterial:', material.precision, 'not supported, using', precision, 'instead.' );

			}

		}

		var parameters = {
G
gero3 已提交
140 141

			shaderID: shaderID,
G
gero3 已提交
142 143

			precision: precision,
G
gero3 已提交
144
			supportsVertexTextures: capabilities.vertexTextures,
G
gero3 已提交
145 146 147 148 149 150 151 152 153

			map: !! material.map,
			envMap: !! material.envMap,
			envMapMode: material.envMap && material.envMap.mapping,
			lightMap: !! material.lightMap,
			aoMap: !! material.aoMap,
			emissiveMap: !! material.emissiveMap,
			bumpMap: !! material.bumpMap,
			normalMap: !! material.normalMap,
154
			displacementMap: !! material.displacementMap,
G
gero3 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168
			specularMap: !! material.specularMap,
			alphaMap: !! material.alphaMap,

			combine: material.combine,

			vertexColors: material.vertexColors,

			fog: fog,
			useFog: material.fog,
			fogExp: fog instanceof THREE.FogExp2,

			flatShading: material.shading === THREE.FlatShading,

			sizeAttenuation: material.sizeAttenuation,
169
			logarithmicDepthBuffer: capabilities.logarithmicDepthBuffer,
G
gero3 已提交
170 171 172

			skinning: material.skinning,
			maxBones: maxBones,
G
gero3 已提交
173
			useVertexTexture: capabilities.floatVertexTextures && object && object.skeleton && object.skeleton.useVertexTexture,
G
gero3 已提交
174 175 176

			morphTargets: material.morphTargets,
			morphNormals: material.morphNormals,
M
Mr.doob 已提交
177 178
			maxMorphTargets: renderer.maxMorphTargets,
			maxMorphNormals: renderer.maxMorphNormals,
G
gero3 已提交
179 180 181 182 183 184

			maxDirLights: maxLightCount.directional,
			maxPointLights: maxLightCount.point,
			maxSpotLights: maxLightCount.spot,
			maxHemiLights: maxLightCount.hemi,

185 186 187
			maxShadows: allocatedShadows.maxShadows,
			pointLightShadows: allocatedShadows.pointLightShadows,
			shadowMapEnabled: renderer.shadowMap.enabled && object.receiveShadow && allocatedShadows.maxShadows > 0,
M
Mr.doob 已提交
188 189
			shadowMapType: renderer.shadowMap.type,
			shadowMapDebug: renderer.shadowMap.debug,
G
gero3 已提交
190 191 192 193 194 195 196

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

		};
G
gero3 已提交
197

G
gero3 已提交
198
		return parameters;
G
gero3 已提交
199

G
gero3 已提交
200
	};
G
gero3 已提交
201

M
Mr.doob 已提交
202
	this.getProgramCode = function ( material, parameters ) {
G
gero3 已提交
203

G
gero3 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		var chunks = [];

		if ( parameters.shaderID ) {

			chunks.push( parameters.shaderID );

		} else {

			chunks.push( material.fragmentShader );
			chunks.push( material.vertexShader );

		}

		if ( material.defines !== undefined ) {

			for ( var name in material.defines ) {

				chunks.push( name );
				chunks.push( material.defines[ name ] );

			}

		}

G
gero3 已提交
228 229 230
		for ( var i = 0; i < parameterNames.length; i ++ ) {

			var parameterName = parameterNames[ i ];
G
gero3 已提交
231 232 233 234 235 236
			chunks.push( parameterName );
			chunks.push( parameters[ parameterName ] );

		}

		return chunks.join();
G
gero3 已提交
237 238 239

	};

240
	this.acquireProgram = function ( material, parameters, code ) {
G
gero3 已提交
241 242 243 244

		var program;

		// Check if code has been already compiled
G
gero3 已提交
245 246 247 248 249 250 251
		for ( var p = 0, pl = programs.length; p < pl; p ++ ) {

			var programInfo = programs[ p ];

			if ( programInfo.code === code ) {

				program = programInfo;
252
				++ program.usedTimes;
G
gero3 已提交
253 254 255 256 257 258 259 260

				break;

			}

		}

		if ( program === undefined ) {
G
gero3 已提交
261

M
Mr.doob 已提交
262
			program = new THREE.WebGLProgram( renderer, code, material, parameters );
G
gero3 已提交
263
			programs.push( program );
G
gero3 已提交
264

G
gero3 已提交
265
		}
G
gero3 已提交
266

267
		return program;
G
gero3 已提交
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	};

	this.releaseProgram = function( program ) {

		if ( -- program.usedTimes === 0 ) {

			// Remove from unordered set
			var i = programs.indexOf( program );
			programs[ i ] = programs[ programs.length - 1 ];
			programs.pop();

			// Free WebGL resources
			program.destroy();

		}

	};
G
gero3 已提交
286

287 288 289
	// Exposed for resource monitoring & error feedback via renderer.info:
	this.programs = programs;

G
gero3 已提交
290
};