WebGLPrograms.js 7.3 KB

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