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

	var programs = [];

G
gero3 已提交
5 6 7 8 9 10 11 12 13 14
	var shaderIDs = {
		MeshDepthMaterial: 'depth',
		MeshNormalMaterial: 'normal',
		MeshBasicMaterial: 'basic',
		MeshLambertMaterial: 'lambert',
		MeshPhongMaterial: 'phong',
		LineBasicMaterial: 'basic',
		LineDashedMaterial: 'dashed',
		PointCloudMaterial: 'particle_basic'
	};
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161

			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,
			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 已提交
162
			logarithmicDepthBuffer: renderer.logarithmicDepthBuffer,
G
gero3 已提交
163 164 165

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

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

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

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

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

		};
G
gero3 已提交
189

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

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

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

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

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

		}

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

	};

M
Mr.doob 已提交
232
	this.getProgram = function ( material, parameters, code ) {
G
gero3 已提交
233 234 235 236

		var program;

		// Check if code has been already compiled
G
gero3 已提交
237 238 239 240 241 242 243 244 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;

				break;

			}

		}

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

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

G
gero3 已提交
256
		}
G
gero3 已提交
257

G
gero3 已提交
258
		return program ;
G
gero3 已提交
259 260 261

	}

G
gero3 已提交
262
};