WebGLLights.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { Color } from '../../math/Color.js';
  5. import { Matrix4 } from '../../math/Matrix4.js';
  6. import { Vector2 } from '../../math/Vector2.js';
  7. import { Vector3 } from '../../math/Vector3.js';
  8. function UniformsCache() {
  9. var lights = {};
  10. return {
  11. get: function ( light ) {
  12. if ( lights[ light.id ] !== undefined ) {
  13. return lights[ light.id ];
  14. }
  15. var uniforms;
  16. switch ( light.type ) {
  17. case 'DirectionalLight':
  18. uniforms = {
  19. direction: new Vector3(),
  20. color: new Color(),
  21. shadow: false,
  22. shadowBias: 0,
  23. shadowRadius: 1,
  24. shadowMapSize: new Vector2()
  25. };
  26. break;
  27. case 'SpotLight':
  28. uniforms = {
  29. position: new Vector3(),
  30. direction: new Vector3(),
  31. color: new Color(),
  32. distance: 0,
  33. coneCos: 0,
  34. penumbraCos: 0,
  35. decay: 0,
  36. shadow: false,
  37. shadowBias: 0,
  38. shadowRadius: 1,
  39. shadowMapSize: new Vector2()
  40. };
  41. break;
  42. case 'PointLight':
  43. uniforms = {
  44. position: new Vector3(),
  45. color: new Color(),
  46. distance: 0,
  47. decay: 0,
  48. shadow: false,
  49. shadowBias: 0,
  50. shadowRadius: 1,
  51. shadowMapSize: new Vector2(),
  52. shadowCameraNear: 1,
  53. shadowCameraFar: 1000
  54. };
  55. break;
  56. case 'HemisphereLight':
  57. uniforms = {
  58. direction: new Vector3(),
  59. skyColor: new Color(),
  60. groundColor: new Color()
  61. };
  62. break;
  63. case 'RectAreaLight':
  64. uniforms = {
  65. color: new Color(),
  66. position: new Vector3(),
  67. halfWidth: new Vector3(),
  68. halfHeight: new Vector3()
  69. // TODO (abelnation): set RectAreaLight shadow uniforms
  70. };
  71. break;
  72. }
  73. lights[ light.id ] = uniforms;
  74. return uniforms;
  75. }
  76. };
  77. }
  78. var count = 0;
  79. function WebGLLights() {
  80. var cache = new UniformsCache();
  81. var state = {
  82. id: count ++,
  83. hash: {
  84. stateID: - 1,
  85. directionalLength: - 1,
  86. pointLength: - 1,
  87. spotLength: - 1,
  88. rectAreaLength: - 1,
  89. hemiLength: - 1,
  90. shadowsLength: - 1,
  91. value: 0
  92. },
  93. ambient: [ 0, 0, 0 ],
  94. probe: [],
  95. directional: [],
  96. directionalShadowMap: [],
  97. directionalShadowMatrix: [],
  98. spot: [],
  99. spotShadowMap: [],
  100. spotShadowMatrix: [],
  101. rectArea: [],
  102. point: [],
  103. pointShadowMap: [],
  104. pointShadowMatrix: [],
  105. hemi: []
  106. };
  107. for ( var i = 0; i < 9; i ++ ) state.probe.push( new Vector3() );
  108. var vector3 = new Vector3();
  109. var matrix4 = new Matrix4();
  110. var matrix42 = new Matrix4();
  111. function setup( lights, shadows, camera ) {
  112. var r = 0, g = 0, b = 0;
  113. for ( var i = 0; i < 9; i ++ ) state.probe[ i ].set( 0, 0, 0 );
  114. var directionalLength = 0;
  115. var pointLength = 0;
  116. var spotLength = 0;
  117. var rectAreaLength = 0;
  118. var hemiLength = 0;
  119. var viewMatrix = camera.matrixWorldInverse;
  120. for ( var i = 0, l = lights.length; i < l; i ++ ) {
  121. var light = lights[ i ];
  122. var color = light.color;
  123. var intensity = light.intensity;
  124. var distance = light.distance;
  125. var shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;
  126. if ( light.isAmbientLight ) {
  127. r += color.r * intensity;
  128. g += color.g * intensity;
  129. b += color.b * intensity;
  130. } else if ( light.isLightProbe ) {
  131. for ( var j = 0; j < 9; j ++ ) {
  132. state.probe[ j ].addScaledVector( light.sh.coefficients[ j ], intensity );
  133. }
  134. } else if ( light.isDirectionalLight ) {
  135. var uniforms = cache.get( light );
  136. uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
  137. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  138. vector3.setFromMatrixPosition( light.target.matrixWorld );
  139. uniforms.direction.sub( vector3 );
  140. uniforms.direction.transformDirection( viewMatrix );
  141. uniforms.shadow = light.castShadow;
  142. if ( light.castShadow ) {
  143. var shadow = light.shadow;
  144. uniforms.shadowBias = shadow.bias;
  145. uniforms.shadowRadius = shadow.radius;
  146. uniforms.shadowMapSize = shadow.mapSize;
  147. }
  148. state.directionalShadowMap[ directionalLength ] = shadowMap;
  149. state.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
  150. state.directional[ directionalLength ] = uniforms;
  151. directionalLength ++;
  152. } else if ( light.isSpotLight ) {
  153. var uniforms = cache.get( light );
  154. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  155. uniforms.position.applyMatrix4( viewMatrix );
  156. uniforms.color.copy( color ).multiplyScalar( intensity );
  157. uniforms.distance = distance;
  158. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  159. vector3.setFromMatrixPosition( light.target.matrixWorld );
  160. uniforms.direction.sub( vector3 );
  161. uniforms.direction.transformDirection( viewMatrix );
  162. uniforms.coneCos = Math.cos( light.angle );
  163. uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
  164. uniforms.decay = light.decay;
  165. uniforms.shadow = light.castShadow;
  166. if ( light.castShadow ) {
  167. var shadow = light.shadow;
  168. uniforms.shadowBias = shadow.bias;
  169. uniforms.shadowRadius = shadow.radius;
  170. uniforms.shadowMapSize = shadow.mapSize;
  171. }
  172. state.spotShadowMap[ spotLength ] = shadowMap;
  173. state.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
  174. state.spot[ spotLength ] = uniforms;
  175. spotLength ++;
  176. } else if ( light.isRectAreaLight ) {
  177. var uniforms = cache.get( light );
  178. // (a) intensity is the total visible light emitted
  179. //uniforms.color.copy( color ).multiplyScalar( intensity / ( light.width * light.height * Math.PI ) );
  180. // (b) intensity is the brightness of the light
  181. uniforms.color.copy( color ).multiplyScalar( intensity );
  182. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  183. uniforms.position.applyMatrix4( viewMatrix );
  184. // extract local rotation of light to derive width/height half vectors
  185. matrix42.identity();
  186. matrix4.copy( light.matrixWorld );
  187. matrix4.premultiply( viewMatrix );
  188. matrix42.extractRotation( matrix4 );
  189. uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 );
  190. uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 );
  191. uniforms.halfWidth.applyMatrix4( matrix42 );
  192. uniforms.halfHeight.applyMatrix4( matrix42 );
  193. // TODO (abelnation): RectAreaLight distance?
  194. // uniforms.distance = distance;
  195. state.rectArea[ rectAreaLength ] = uniforms;
  196. rectAreaLength ++;
  197. } else if ( light.isPointLight ) {
  198. var uniforms = cache.get( light );
  199. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  200. uniforms.position.applyMatrix4( viewMatrix );
  201. uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
  202. uniforms.distance = light.distance;
  203. uniforms.decay = light.decay;
  204. uniforms.shadow = light.castShadow;
  205. if ( light.castShadow ) {
  206. var shadow = light.shadow;
  207. uniforms.shadowBias = shadow.bias;
  208. uniforms.shadowRadius = shadow.radius;
  209. uniforms.shadowMapSize = shadow.mapSize;
  210. uniforms.shadowCameraNear = shadow.camera.near;
  211. uniforms.shadowCameraFar = shadow.camera.far;
  212. }
  213. state.pointShadowMap[ pointLength ] = shadowMap;
  214. state.pointShadowMatrix[ pointLength ] = light.shadow.matrix;
  215. state.point[ pointLength ] = uniforms;
  216. pointLength ++;
  217. } else if ( light.isHemisphereLight ) {
  218. var uniforms = cache.get( light );
  219. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  220. uniforms.direction.transformDirection( viewMatrix );
  221. uniforms.direction.normalize();
  222. uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
  223. uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
  224. state.hemi[ hemiLength ] = uniforms;
  225. hemiLength ++;
  226. }
  227. }
  228. state.ambient[ 0 ] = r;
  229. state.ambient[ 1 ] = g;
  230. state.ambient[ 2 ] = b;
  231. var hash = state.hash;
  232. if ( hash.directionalLength !== directionalLength ||
  233. hash.pointLength !== pointLength ||
  234. hash.spotLength !== spotLength ||
  235. hash.rectAreaLength !== rectAreaLength ||
  236. hash.hemiLength !== hemiLength ||
  237. hash.shadowsLength !== shadows.length ) {
  238. state.directional.length = directionalLength;
  239. state.spot.length = spotLength;
  240. state.rectArea.length = rectAreaLength;
  241. state.point.length = pointLength;
  242. state.hemi.length = hemiLength;
  243. hash.stateID = state.id;
  244. hash.directionalLength = directionalLength;
  245. hash.pointLength = pointLength;
  246. hash.spotLength = spotLength;
  247. hash.rectAreaLength = rectAreaLength;
  248. hash.hemiLength = hemiLength;
  249. hash.shadowsLength = shadows.length;
  250. hash.value ++;
  251. }
  252. }
  253. return {
  254. setup: setup,
  255. state: state
  256. };
  257. }
  258. export { WebGLLights };
粤ICP备19079148号