WebGLProgramCache.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. THREE.WebGLProgramCache = function (renderer1,gl, extensions) {
  2. var programs = [];
  3. var supportsVertexTextures = gl.getParameter( gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) > 0;
  4. var supportsBoneTextures = gl.getParameter( gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) > 0 && extensions.get( 'OES_texture_float' );
  5. var shaderIDs = {
  6. MeshDepthMaterial: 'depth',
  7. MeshNormalMaterial: 'normal',
  8. MeshBasicMaterial: 'basic',
  9. MeshLambertMaterial: 'lambert',
  10. MeshPhongMaterial: 'phong',
  11. LineBasicMaterial: 'basic',
  12. LineDashedMaterial: 'dashed',
  13. PointCloudMaterial: 'particle_basic'
  14. };
  15. var parameterNames = [" precision","supportsVertexTextures","map","envMap","envMapMode","lightMap","aoMap","emissiveMap","bumpMap","normalMap","specularMap","alphaMap","combine",
  16. "vertexColors","fog","useFog","fogExp","flatShading","sizeAttenuation","logarithmicDepthBuffer","skinning","maxBones","useVertexTexture","morphTargets","morphNormals",
  17. "maxMorphTargets","maxMorphNormals","maxDirLights","maxPointLights","maxSpotLights","maxHemiLights","maxShadows","shadowMapEnabled","shadowMapType","shadowMapDebug",
  18. "alphaTest","metal","doubleSided","flipSided"];
  19. function allocateBones ( object ) {
  20. if ( supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
  21. return 1024;
  22. } else {
  23. // default for when object is not specified
  24. // ( for example when prebuilding shader to be used with multiple objects )
  25. //
  26. // - leave some extra space for other uniforms
  27. // - limit here is ANGLE's 254 max uniform vectors
  28. // (up to 54 should be safe)
  29. var nVertexUniforms = gl.getParameter( gl.MAX_VERTEX_UNIFORM_VECTORS );
  30. var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
  31. var maxBones = nVertexMatrices;
  32. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  33. maxBones = Math.min( object.skeleton.bones.length, maxBones );
  34. if ( maxBones < object.skeleton.bones.length ) {
  35. console.warn( 'WebGLRenderer: too many bones - ' + object.skeleton.bones.length + ', this GPU supports just ' + maxBones + ' (try OpenGL instead of ANGLE)' );
  36. }
  37. }
  38. return maxBones;
  39. }
  40. }
  41. function allocateLights( lights ) {
  42. var dirLights = 0;
  43. var pointLights = 0;
  44. var spotLights = 0;
  45. var hemiLights = 0;
  46. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  47. var light = lights[ l ];
  48. if ( light.onlyShadow || light.visible === false ) continue;
  49. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  50. if ( light instanceof THREE.PointLight ) pointLights ++;
  51. if ( light instanceof THREE.SpotLight ) spotLights ++;
  52. if ( light instanceof THREE.HemisphereLight ) hemiLights ++;
  53. }
  54. return { 'directional': dirLights, 'point': pointLights, 'spot': spotLights, 'hemi': hemiLights };
  55. }
  56. function allocateShadows( lights ) {
  57. var maxShadows = 0;
  58. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  59. var light = lights[ l ];
  60. if ( ! light.castShadow ) continue;
  61. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  62. if ( light instanceof THREE.DirectionalLight ) maxShadows ++;
  63. }
  64. return maxShadows;
  65. };
  66. this.getParameters = function(material, lights, fog, object){
  67. var shaderID = shaderIDs[ material.type ];
  68. // heuristics to create shader parameters according to lights in the scene
  69. // (not to blow over maxLights budget)
  70. var maxLightCount = allocateLights( lights );
  71. var maxShadows = allocateShadows( lights );
  72. var maxBones = allocateBones( object );
  73. var precision = renderer1.getPrecision();
  74. if ( material.precision !== null ) {
  75. precision = renderer1.state.getMaxPrecision( material.precision );
  76. if ( precision !== material.precision ) {
  77. console.warn( 'THREE.WebGLRenderer.initMaterial:', material.precision, 'not supported, using', precision, 'instead.' );
  78. }
  79. }
  80. var parameters = {
  81. shaderID: shaderID,
  82. precision: precision,
  83. supportsVertexTextures: supportsVertexTextures,
  84. map: !! material.map,
  85. envMap: !! material.envMap,
  86. envMapMode: material.envMap && material.envMap.mapping,
  87. lightMap: !! material.lightMap,
  88. aoMap: !! material.aoMap,
  89. emissiveMap: !! material.emissiveMap,
  90. bumpMap: !! material.bumpMap,
  91. normalMap: !! material.normalMap,
  92. specularMap: !! material.specularMap,
  93. alphaMap: !! material.alphaMap,
  94. combine: material.combine,
  95. vertexColors: material.vertexColors,
  96. fog: fog,
  97. useFog: material.fog,
  98. fogExp: fog instanceof THREE.FogExp2,
  99. flatShading: material.shading === THREE.FlatShading,
  100. sizeAttenuation: material.sizeAttenuation,
  101. logarithmicDepthBuffer: renderer1.logarithmicDepthBuffer,
  102. skinning: material.skinning,
  103. maxBones: maxBones,
  104. useVertexTexture: supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture,
  105. morphTargets: material.morphTargets,
  106. morphNormals: material.morphNormals,
  107. maxMorphTargets: renderer1.maxMorphTargets,
  108. maxMorphNormals: renderer1.maxMorphNormals,
  109. maxDirLights: maxLightCount.directional,
  110. maxPointLights: maxLightCount.point,
  111. maxSpotLights: maxLightCount.spot,
  112. maxHemiLights: maxLightCount.hemi,
  113. maxShadows: maxShadows,
  114. shadowMapEnabled: renderer1.shadowMap.enabled && object.receiveShadow && maxShadows > 0,
  115. shadowMapType: renderer1.shadowMap.type,
  116. shadowMapDebug: renderer1.shadowMap.debug,
  117. alphaTest: material.alphaTest,
  118. metal: material.metal,
  119. doubleSided: material.side === THREE.DoubleSide,
  120. flipSided: material.side === THREE.BackSide
  121. };
  122. return parameters;
  123. };
  124. this.getProgramCode = function(material, parameters){
  125. var chunks = [];
  126. if ( parameters.shaderID ) {
  127. chunks.push( parameters.shaderID );
  128. } else {
  129. chunks.push( material.fragmentShader );
  130. chunks.push( material.vertexShader );
  131. }
  132. if ( material.defines !== undefined ) {
  133. for ( var name in material.defines ) {
  134. chunks.push( name );
  135. chunks.push( material.defines[ name ] );
  136. }
  137. }
  138. for ( var i = 0; i < parameterNames.length; i++ ) {
  139. var parameterName = parameterNames[i];
  140. chunks.push( parameterName );
  141. chunks.push( parameters[ parameterName ] );
  142. }
  143. return chunks.join();
  144. };
  145. this.getProgram = function(material,parameters,code){
  146. var program;
  147. // Check if code has been already compiled
  148. for ( var p = 0, pl = programs.length; p < pl; p ++ ) {
  149. var programInfo = programs[ p ];
  150. if ( programInfo.code === code ) {
  151. program = programInfo;
  152. break;
  153. }
  154. }
  155. if ( program === undefined ) {
  156. program = new THREE.WebGLProgram( renderer1, code, material, parameters );
  157. programs.push( program );
  158. }
  159. return program ;
  160. }
  161. };