WebGLPrograms.js 6.8 KB

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