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

M
Mr.doob 已提交
4
function WebGLPrograms( renderer, capabilities ) {
G
gero3 已提交
5

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

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

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


M
Mr.doob 已提交
35
	function allocateBones( object ) {
G
gero3 已提交
36

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

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

			var maxBones = nVertexMatrices;

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

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

73
	function getTextureEncodingFromMap( map, gammaOverrideLinear ) {
74 75

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

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

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

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

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

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

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

90 91 92
		}

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

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

97 98 99
		}

		return encoding;
M
Mr.doob 已提交
100

101 102
	}

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

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

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

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

		if ( material.precision !== null ) {

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

			if ( precision !== material.precision ) {

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

			}

		}
M
Mr.doob 已提交
124

125 126
		var currentRenderTarget = renderer.getCurrentRenderTarget();

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

			shaderID: shaderID,
G
gero3 已提交
130 131

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

			combine: material.combine,

			vertexColors: material.vertexColors,

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

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

			sizeAttenuation: material.sizeAttenuation,
163
			logarithmicDepthBuffer: capabilities.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
			numDirLights: lights.directional.length,
			numPointLights: lights.point.length,
			numSpotLights: lights.spot.length,
			numHemiLights: lights.hemi.length,
178

T
tschw 已提交
179 180
			numClippingPlanes: nClipPlanes,

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

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

187 188
			premultipliedAlpha: material.premultipliedAlpha,

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

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

		};
196

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

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

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

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

		if ( parameters.shaderID ) {

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

		} else {

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

		}

		if ( material.defines !== undefined ) {

			for ( var name in material.defines ) {

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

			}

		}

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

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

		}

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

	};

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

		var program;

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

			var programInfo = programs[ p ];

			if ( programInfo.code === code ) {

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

				break;

			}

		}

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

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

G
gero3 已提交
262
		}
G
gero3 已提交
263

264
		return program;
G
gero3 已提交
265

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

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

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

M
Mr.doob 已提交
287
}
R
Rich Harris 已提交
288 289


290
export { WebGLPrograms };