WebGLPrograms.js 6.4 KB

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