WebGLPrograms.js 7.7 KB
Newer Older
R
Rich Harris 已提交
1 2 3 4 5
import { WebGLProgram } from './WebGLProgram';
import { BackSide, DoubleSide, FlatShading, CubeUVRefractionMapping, CubeUVReflectionMapping, GammaEncoding, LinearEncoding } from '../../constants';

function WebGLPrograms ( renderer, capabilities ) {
	this.isWebGLPrograms = true;
G
gero3 已提交
6

7
	var programs = [];
G
gero3 已提交
8

G
gero3 已提交
9 10 11 12 13 14
	var shaderIDs = {
		MeshDepthMaterial: 'depth',
		MeshNormalMaterial: 'normal',
		MeshBasicMaterial: 'basic',
		MeshLambertMaterial: 'lambert',
		MeshPhongMaterial: 'phong',
15
		MeshStandardMaterial: 'physical',
W
WestLangley 已提交
16
		MeshPhysicalMaterial: 'physical',
G
gero3 已提交
17 18
		LineBasicMaterial: 'basic',
		LineDashedMaterial: 'dashed',
19
		PointsMaterial: 'points'
G
gero3 已提交
20
	};
G
gero3 已提交
21

M
Mr.doob 已提交
22
	var parameterNames = [
23 24
		"precision", "supportsVertexTextures", "map", "mapEncoding", "envMap", "envMapMode", "envMapEncoding",
		"lightMap", "aoMap", "emissiveMap", "emissiveMapEncoding", "bumpMap", "normalMap", "displacementMap", "specularMap",
25
		"roughnessMap", "metalnessMap",
M
Mr.doob 已提交
26 27 28
		"alphaMap", "combine", "vertexColors", "fog", "useFog", "fogExp",
		"flatShading", "sizeAttenuation", "logarithmicDepthBuffer", "skinning",
		"maxBones", "useVertexTexture", "morphTargets", "morphNormals",
29
		"maxMorphTargets", "maxMorphNormals", "premultipliedAlpha",
M
Mr.doob 已提交
30
		"numDirLights", "numPointLights", "numSpotLights", "numHemiLights",
31
		"shadowMapEnabled", "shadowMapType", "toneMapping", 'physicallyCorrectLights',
32
		"alphaTest", "doubleSided", "flipSided", "numClippingPlanes", "depthPacking"
M
Mr.doob 已提交
33
	];
G
gero3 已提交
34 35


G
gero3 已提交
36 37
	function allocateBones ( object ) {

G
gero3 已提交
38
		if ( capabilities.floatVertexTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
G
gero3 已提交
39 40 41 42 43 44 45 46 47 48 49 50

			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 已提交
51
			var nVertexUniforms = capabilities.maxVertexUniforms;
G
gero3 已提交
52 53 54 55
			var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );

			var maxBones = nVertexMatrices;

R
Rich Harris 已提交
56
			if ( object !== undefined && (object && object.isSkinnedMesh) ) {
G
gero3 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

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

74
	function getTextureEncodingFromMap( map, gammaOverrideLinear ) {
75 76

		var encoding;
M
Mr.doob 已提交
77 78

		if ( ! map ) {
M
Mr.doob 已提交
79

R
Rich Harris 已提交
80
			encoding = LinearEncoding;
M
Mr.doob 已提交
81

R
Rich Harris 已提交
82
		} else if ( (map && map.isTexture) ) {
M
Mr.doob 已提交
83

84
			encoding = map.encoding;
M
Mr.doob 已提交
85

R
Rich Harris 已提交
86
		} else if ( (map && map.isWebGLRenderTarget) ) {
M
Mr.doob 已提交
87

88
			console.warn( "THREE.WebGLPrograms.getTextureEncodingFromMap: don't use render targets as textures. Use their .texture property instead." );
89
			encoding = map.texture.encoding;
M
Mr.doob 已提交
90

91 92 93
		}

		// add backwards compatibility for WebGLRenderer.gammaInput/gammaOutput parameter, should probably be removed at some point.
R
Rich Harris 已提交
94
		if ( encoding === LinearEncoding && gammaOverrideLinear ) {
M
Mr.doob 已提交
95

R
Rich Harris 已提交
96
			encoding = GammaEncoding;
M
Mr.doob 已提交
97

98 99 100
		}

		return encoding;
M
Mr.doob 已提交
101

102 103
	}

T
tschw 已提交
104
	this.getParameters = function ( material, lights, fog, nClipPlanes, object ) {
M
Mr.doob 已提交
105

G
gero3 已提交
106
		var shaderID = shaderIDs[ material.type ];
M
Mr.doob 已提交
107

G
gero3 已提交
108
		// heuristics to create shader parameters according to lights in the scene
G
gero3 已提交
109 110 111
		// (not to blow over maxLights budget)

		var maxBones = allocateBones( object );
M
Mr.doob 已提交
112
		var precision = renderer.getPrecision();
G
gero3 已提交
113 114 115

		if ( material.precision !== null ) {

G
gero3 已提交
116
			precision = capabilities.getMaxPrecision( material.precision );
G
gero3 已提交
117 118 119

			if ( precision !== material.precision ) {

120
				console.warn( 'THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.' );
G
gero3 已提交
121 122 123 124

			}

		}
M
Mr.doob 已提交
125

126 127
		var currentRenderTarget = renderer.getCurrentRenderTarget();

G
gero3 已提交
128
		var parameters = {
G
gero3 已提交
129 130

			shaderID: shaderID,
G
gero3 已提交
131 132

			precision: precision,
G
gero3 已提交
133
			supportsVertexTextures: capabilities.vertexTextures,
134
			outputEncoding: getTextureEncodingFromMap( ( ! currentRenderTarget ) ? null : currentRenderTarget.texture, renderer.gammaOutput ),
G
gero3 已提交
135
			map: !! material.map,
136
			mapEncoding: getTextureEncodingFromMap( material.map, renderer.gammaInput ),
G
gero3 已提交
137 138
			envMap: !! material.envMap,
			envMapMode: material.envMap && material.envMap.mapping,
139
			envMapEncoding: getTextureEncodingFromMap( material.envMap, renderer.gammaInput ),
R
Rich Harris 已提交
140
			envMapCubeUV: ( !! material.envMap ) && ( ( material.envMap.mapping === CubeUVReflectionMapping ) || ( material.envMap.mapping === CubeUVRefractionMapping ) ),
G
gero3 已提交
141 142 143
			lightMap: !! material.lightMap,
			aoMap: !! material.aoMap,
			emissiveMap: !! material.emissiveMap,
144
			emissiveMapEncoding: getTextureEncodingFromMap( material.emissiveMap, renderer.gammaInput ),
G
gero3 已提交
145 146
			bumpMap: !! material.bumpMap,
			normalMap: !! material.normalMap,
147
			displacementMap: !! material.displacementMap,
W
WestLangley 已提交
148 149
			roughnessMap: !! material.roughnessMap,
			metalnessMap: !! material.metalnessMap,
G
gero3 已提交
150 151 152 153 154 155 156
			specularMap: !! material.specularMap,
			alphaMap: !! material.alphaMap,

			combine: material.combine,

			vertexColors: material.vertexColors,

M
Mr.doob 已提交
157
			fog: !! fog,
G
gero3 已提交
158
			useFog: material.fog,
R
Rich Harris 已提交
159
			fogExp: (fog && fog.isFogExp2),
G
gero3 已提交
160

R
Rich Harris 已提交
161
			flatShading: material.shading === FlatShading,
G
gero3 已提交
162 163

			sizeAttenuation: material.sizeAttenuation,
164
			logarithmicDepthBuffer: capabilities.logarithmicDepthBuffer,
G
gero3 已提交
165 166 167

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

			morphTargets: material.morphTargets,
			morphNormals: material.morphNormals,
M
Mr.doob 已提交
172 173
			maxMorphTargets: renderer.maxMorphTargets,
			maxMorphNormals: renderer.maxMorphNormals,
G
gero3 已提交
174

175 176 177 178
			numDirLights: lights.directional.length,
			numPointLights: lights.point.length,
			numSpotLights: lights.spot.length,
			numHemiLights: lights.hemi.length,
179

T
tschw 已提交
180 181
			numClippingPlanes: nClipPlanes,

182
			shadowMapEnabled: renderer.shadowMap.enabled && object.receiveShadow && lights.shadows.length > 0,
M
Mr.doob 已提交
183
			shadowMapType: renderer.shadowMap.type,
G
gero3 已提交
184

B
Ben Houston 已提交
185
			toneMapping: renderer.toneMapping,
186
			physicallyCorrectLights: renderer.physicallyCorrectLights,
B
Ben Houston 已提交
187

188 189
			premultipliedAlpha: material.premultipliedAlpha,

G
gero3 已提交
190
			alphaTest: material.alphaTest,
R
Rich Harris 已提交
191 192
			doubleSided: material.side === DoubleSide,
			flipSided: material.side === BackSide,
193

194
			depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false
G
gero3 已提交
195 196

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

M
Mr.doob 已提交
204
		var array = [];
G
gero3 已提交
205 206 207

		if ( parameters.shaderID ) {

M
Mr.doob 已提交
208
			array.push( parameters.shaderID );
G
gero3 已提交
209 210 211

		} else {

M
Mr.doob 已提交
212 213
			array.push( material.fragmentShader );
			array.push( material.vertexShader );
G
gero3 已提交
214 215 216 217 218 219 220

		}

		if ( material.defines !== undefined ) {

			for ( var name in material.defines ) {

M
Mr.doob 已提交
221 222
				array.push( name );
				array.push( material.defines[ name ] );
G
gero3 已提交
223 224 225 226 227

			}

		}

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

M
Mr.doob 已提交
230
			array.push( parameters[ parameterNames[ i ] ] );
G
gero3 已提交
231 232 233

		}

M
Mr.doob 已提交
234
		return array.join();
G
gero3 已提交
235 236 237

	};

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

		var program;

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

			var programInfo = programs[ p ];

			if ( programInfo.code === code ) {

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

				break;

			}

		}

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

R
Rich Harris 已提交
260
			program = new WebGLProgram( renderer, code, material, parameters );
G
gero3 已提交
261
			programs.push( program );
G
gero3 已提交
262

G
gero3 已提交
263
		}
G
gero3 已提交
264

265
		return program;
G
gero3 已提交
266

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

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

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

G
gero3 已提交
288
};
R
Rich Harris 已提交
289 290 291


export { WebGLPrograms };