WebGLPrograms.js 6.8 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 18 19 20 21 22 23 24 25 26
	var parameterNames = [
		"precision", "supportsVertexTextures", "map", "envMap", "envMapMode",
		"lightMap", "aoMap", "emissiveMap", "bumpMap", "normalMap", "specularMap",
		"alphaMap", "combine", "vertexColors", "fog", "useFog", "fogExp",
		"flatShading", "sizeAttenuation", "logarithmicDepthBuffer", "skinning",
		"maxBones", "useVertexTexture", "morphTargets", "morphNormals",
		"maxMorphTargets", "maxMorphNormals", "maxDirLights", "maxPointLights",
		"maxSpotLights", "maxHemiLights", "maxShadows", "shadowMapEnabled",
		"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 78 79 80 81 82 83 84 85 86 87 88 89
	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 ];

			if ( light.onlyShadow || light.visible === false ) continue;

			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 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	function allocateShadows( lights ) {

		var maxShadows = 0;

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

			var light = lights[ l ];

			if ( ! light.castShadow ) continue;

			if ( light instanceof THREE.SpotLight ) maxShadows ++;
			if ( light instanceof THREE.DirectionalLight ) maxShadows ++;

		}

		return maxShadows;

G
gero3 已提交
108
	}
G
gero3 已提交
109

M
Mr.doob 已提交
110
	this.getParameters = function ( material, lights, fog, object ) {
G
gero3 已提交
111 112 113

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

G
gero3 已提交
116
		var maxLightCount = allocateLights( lights );
G
gero3 已提交
117 118
		var maxShadows = allocateShadows( lights );
		var maxBones = allocateBones( object );
M
Mr.doob 已提交
119
		var precision = renderer.getPrecision();
G
gero3 已提交
120 121 122

		if ( material.precision !== null ) {

G
gero3 已提交
123
			precision = capabilities.getMaxPrecision( material.precision );
G
gero3 已提交
124 125 126 127 128 129 130 131 132 133

			if ( precision !== material.precision ) {

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

			}

		}

		var parameters = {
G
gero3 已提交
134 135

			shaderID: shaderID,
G
gero3 已提交
136 137

			precision: precision,
G
gero3 已提交
138
			supportsVertexTextures: capabilities.vertexTextures,
G
gero3 已提交
139 140 141 142 143 144 145 146 147

			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,
148
			displacementMap: !! material.displacementMap,
G
gero3 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162
			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,
M
Mr.doob 已提交
163
			logarithmicDepthBuffer: renderer.logarithmicDepthBuffer,
G
gero3 已提交
164 165 166

			skinning: material.skinning,
			maxBones: maxBones,
G
gero3 已提交
167
			useVertexTexture: capabilities.floatVertexTextures && object && object.skeleton && object.skeleton.useVertexTexture,
G
gero3 已提交
168 169 170

			morphTargets: material.morphTargets,
			morphNormals: material.morphNormals,
M
Mr.doob 已提交
171 172
			maxMorphTargets: renderer.maxMorphTargets,
			maxMorphNormals: renderer.maxMorphNormals,
G
gero3 已提交
173 174 175 176 177 178 179

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

			maxShadows: maxShadows,
M
Mr.doob 已提交
180 181 182
			shadowMapEnabled: renderer.shadowMap.enabled && object.receiveShadow && maxShadows > 0,
			shadowMapType: renderer.shadowMap.type,
			shadowMapDebug: renderer.shadowMap.debug,
G
gero3 已提交
183 184 185 186 187 188 189

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

		};
G
gero3 已提交
190

G
gero3 已提交
191
		return parameters;
G
gero3 已提交
192

G
gero3 已提交
193
	};
G
gero3 已提交
194

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

G
gero3 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		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 已提交
221 222 223
		for ( var i = 0; i < parameterNames.length; i ++ ) {

			var parameterName = parameterNames[ i ];
G
gero3 已提交
224 225 226 227 228 229
			chunks.push( parameterName );
			chunks.push( parameters[ parameterName ] );

		}

		return chunks.join();
G
gero3 已提交
230 231 232

	};

233
	this.acquireProgram = function ( material, parameters, code ) {
G
gero3 已提交
234 235 236 237

		var program;

		// Check if code has been already compiled
G
gero3 已提交
238 239 240 241 242 243 244
		for ( var p = 0, pl = programs.length; p < pl; p ++ ) {

			var programInfo = programs[ p ];

			if ( programInfo.code === code ) {

				program = programInfo;
245
				++ program.usedTimes;
G
gero3 已提交
246 247 248 249 250 251 252 253

				break;

			}

		}

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

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

G
gero3 已提交
258
		}
G
gero3 已提交
259

260
		return program;
G
gero3 已提交
261

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	};

	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 已提交
279

280 281 282
	// Exposed for resource monitoring & error feedback via renderer.info:
	this.programs = programs;

G
gero3 已提交
283
};