123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- THREE.WebGLPrograms = function ( renderer, capabilities ) {
- var programs = [];
- var shaderIDs = {
- MeshDepthMaterial: 'depth',
- MeshNormalMaterial: 'normal',
- MeshBasicMaterial: 'basic',
- MeshLambertMaterial: 'lambert',
- MeshPhongMaterial: 'phong',
- LineBasicMaterial: 'basic',
- LineDashedMaterial: 'dashed',
- PointsMaterial: 'points'
- };
- 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"
- ];
- function allocateBones ( object ) {
- if ( capabilities.floatVertexTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
- 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)
- var nVertexUniforms = capabilities.maxVertexUniforms;
- 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;
- }
- }
- 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 };
- }
- 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;
- }
- this.getParameters = function ( material, lights, fog, object ) {
- var shaderID = shaderIDs[ material.type ];
- // heuristics to create shader parameters according to lights in the scene
- // (not to blow over maxLights budget)
- var maxLightCount = allocateLights( lights );
- var maxShadows = allocateShadows( lights );
- var maxBones = allocateBones( object );
- var precision = renderer.getPrecision();
- if ( material.precision !== null ) {
- precision = capabilities.getMaxPrecision( material.precision );
- if ( precision !== material.precision ) {
- console.warn( 'THREE.WebGLRenderer.initMaterial:', material.precision, 'not supported, using', precision, 'instead.' );
- }
- }
- var parameters = {
- shaderID: shaderID,
- precision: precision,
- supportsVertexTextures: capabilities.vertexTextures,
- 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,
- displacementMap: !! material.displacementMap,
- 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,
- logarithmicDepthBuffer: capabilities.logarithmicDepthBuffer,
- skinning: material.skinning,
- maxBones: maxBones,
- useVertexTexture: capabilities.floatVertexTextures && object && object.skeleton && object.skeleton.useVertexTexture,
- morphTargets: material.morphTargets,
- morphNormals: material.morphNormals,
- maxMorphTargets: renderer.maxMorphTargets,
- maxMorphNormals: renderer.maxMorphNormals,
- maxDirLights: maxLightCount.directional,
- maxPointLights: maxLightCount.point,
- maxSpotLights: maxLightCount.spot,
- maxHemiLights: maxLightCount.hemi,
- maxShadows: maxShadows,
- shadowMapEnabled: renderer.shadowMap.enabled && object.receiveShadow && maxShadows > 0,
- shadowMapType: renderer.shadowMap.type,
- shadowMapDebug: renderer.shadowMap.debug,
- alphaTest: material.alphaTest,
- metal: material.metal,
- doubleSided: material.side === THREE.DoubleSide,
- flipSided: material.side === THREE.BackSide
- };
- return parameters;
- };
- this.getProgramCode = function ( material, parameters ) {
- 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 ] );
- }
- }
- for ( var i = 0; i < parameterNames.length; i ++ ) {
- var parameterName = parameterNames[ i ];
- chunks.push( parameterName );
- chunks.push( parameters[ parameterName ] );
- }
- return chunks.join();
- };
- this.acquireProgram = function ( material, parameters, code ) {
- var program;
- // Check if code has been already compiled
- for ( var p = 0, pl = programs.length; p < pl; p ++ ) {
- var programInfo = programs[ p ];
- if ( programInfo.code === code ) {
- program = programInfo;
- ++ program.usedTimes;
- break;
- }
- }
- if ( program === undefined ) {
- program = new THREE.WebGLProgram( renderer, code, material, parameters );
- programs.push( program );
- }
- return program;
- };
- 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();
- }
- };
- // Exposed for resource monitoring & error feedback via renderer.info:
- this.programs = programs;
- };
|