WebGLLights.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 nextVersion = 0;
  79. function shadowCastingLightsFirst( lightA, lightB ) {
  80. return ( lightB.castShadow ? 1 : 0 ) - ( lightA.castShadow ? 1 : 0 );
  81. }
  82. function WebGLLights() {
  83. var cache = new UniformsCache();
  84. var state = {
  85. version: 0,
  86. hash: {
  87. directionalLength: - 1,
  88. pointLength: - 1,
  89. spotLength: - 1,
  90. rectAreaLength: - 1,
  91. hemiLength: - 1,
  92. numDirectionalShadows: - 1,
  93. numPointShadows: - 1,
  94. numSpotShadows: - 1,
  95. },
  96. ambient: [ 0, 0, 0 ],
  97. probe: [],
  98. directional: [],
  99. directionalShadowMap: [],
  100. directionalShadowMatrix: [],
  101. spot: [],
  102. spotShadowMap: [],
  103. spotShadowMatrix: [],
  104. rectArea: [],
  105. point: [],
  106. pointShadowMap: [],
  107. pointShadowMatrix: [],
  108. hemi: [],
  109. };
  110. for ( var i = 0; i < 9; i ++ ) state.probe.push( new Vector3() );
  111. var vector3 = new Vector3();
  112. var matrix4 = new Matrix4();
  113. var matrix42 = new Matrix4();
  114. function setup( lights, shadows, camera ) {
  115. var r = 0, g = 0, b = 0;
  116. for ( var i = 0; i < 9; i ++ ) state.probe[ i ].set( 0, 0, 0 );
  117. var directionalLength = 0;
  118. var pointLength = 0;
  119. var spotLength = 0;
  120. var rectAreaLength = 0;
  121. var hemiLength = 0;
  122. var numDirectionalShadows = 0;
  123. var numPointShadows = 0;
  124. var numSpotShadows = 0;
  125. var viewMatrix = camera.matrixWorldInverse;
  126. lights.sort( shadowCastingLightsFirst );
  127. for ( var i = 0, l = lights.length; i < l; i ++ ) {
  128. var light = lights[ i ];
  129. var color = light.color;
  130. var intensity = light.intensity;
  131. var distance = light.distance;
  132. var shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;
  133. if ( light.isAmbientLight ) {
  134. r += color.r * intensity;
  135. g += color.g * intensity;
  136. b += color.b * intensity;
  137. } else if ( light.isLightProbe ) {
  138. for ( var j = 0; j < 9; j ++ ) {
  139. state.probe[ j ].addScaledVector( light.sh.coefficients[ j ], intensity );
  140. }
  141. } else if ( light.isDirectionalLight ) {
  142. var uniforms = cache.get( light );
  143. uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
  144. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  145. vector3.setFromMatrixPosition( light.target.matrixWorld );
  146. uniforms.direction.sub( vector3 );
  147. uniforms.direction.transformDirection( viewMatrix );
  148. uniforms.shadow = light.castShadow;
  149. if ( light.castShadow ) {
  150. var shadow = light.shadow;
  151. uniforms.shadowBias = shadow.bias;
  152. uniforms.shadowRadius = shadow.radius;
  153. uniforms.shadowMapSize = shadow.mapSize;
  154. state.directionalShadowMap[ directionalLength ] = shadowMap;
  155. state.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
  156. numDirectionalShadows ++;
  157. }
  158. state.directional[ directionalLength ] = uniforms;
  159. directionalLength ++;
  160. } else if ( light.isSpotLight ) {
  161. var uniforms = cache.get( light );
  162. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  163. uniforms.position.applyMatrix4( viewMatrix );
  164. uniforms.color.copy( color ).multiplyScalar( intensity );
  165. uniforms.distance = distance;
  166. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  167. vector3.setFromMatrixPosition( light.target.matrixWorld );
  168. uniforms.direction.sub( vector3 );
  169. uniforms.direction.transformDirection( viewMatrix );
  170. uniforms.coneCos = Math.cos( light.angle );
  171. uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
  172. uniforms.decay = light.decay;
  173. uniforms.shadow = light.castShadow;
  174. if ( light.castShadow ) {
  175. var shadow = light.shadow;
  176. uniforms.shadowBias = shadow.bias;
  177. uniforms.shadowRadius = shadow.radius;
  178. uniforms.shadowMapSize = shadow.mapSize;
  179. state.spotShadowMap[ spotLength ] = shadowMap;
  180. state.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
  181. numSpotShadows ++;
  182. }
  183. state.spot[ spotLength ] = uniforms;
  184. spotLength ++;
  185. } else if ( light.isRectAreaLight ) {
  186. var uniforms = cache.get( light );
  187. // (a) intensity is the total visible light emitted
  188. //uniforms.color.copy( color ).multiplyScalar( intensity / ( light.width * light.height * Math.PI ) );
  189. // (b) intensity is the brightness of the light
  190. uniforms.color.copy( color ).multiplyScalar( intensity );
  191. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  192. uniforms.position.applyMatrix4( viewMatrix );
  193. // extract local rotation of light to derive width/height half vectors
  194. matrix42.identity();
  195. matrix4.copy( light.matrixWorld );
  196. matrix4.premultiply( viewMatrix );
  197. matrix42.extractRotation( matrix4 );
  198. uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 );
  199. uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 );
  200. uniforms.halfWidth.applyMatrix4( matrix42 );
  201. uniforms.halfHeight.applyMatrix4( matrix42 );
  202. // TODO (abelnation): RectAreaLight distance?
  203. // uniforms.distance = distance;
  204. state.rectArea[ rectAreaLength ] = uniforms;
  205. rectAreaLength ++;
  206. } else if ( light.isPointLight ) {
  207. var uniforms = cache.get( light );
  208. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  209. uniforms.position.applyMatrix4( viewMatrix );
  210. uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
  211. uniforms.distance = light.distance;
  212. uniforms.decay = light.decay;
  213. uniforms.shadow = light.castShadow;
  214. if ( light.castShadow ) {
  215. var shadow = light.shadow;
  216. uniforms.shadowBias = shadow.bias;
  217. uniforms.shadowRadius = shadow.radius;
  218. uniforms.shadowMapSize = shadow.mapSize;
  219. uniforms.shadowCameraNear = shadow.camera.near;
  220. uniforms.shadowCameraFar = shadow.camera.far;
  221. state.pointShadowMap[ pointLength ] = shadowMap;
  222. state.pointShadowMatrix[ pointLength ] = light.shadow.matrix;
  223. numPointShadows ++;
  224. }
  225. state.point[ pointLength ] = uniforms;
  226. pointLength ++;
  227. } else if ( light.isHemisphereLight ) {
  228. var uniforms = cache.get( light );
  229. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  230. uniforms.direction.transformDirection( viewMatrix );
  231. uniforms.direction.normalize();
  232. uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
  233. uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
  234. state.hemi[ hemiLength ] = uniforms;
  235. hemiLength ++;
  236. }
  237. }
  238. state.ambient[ 0 ] = r;
  239. state.ambient[ 1 ] = g;
  240. state.ambient[ 2 ] = b;
  241. var hash = state.hash;
  242. if ( hash.directionalLength !== directionalLength ||
  243. hash.pointLength !== pointLength ||
  244. hash.spotLength !== spotLength ||
  245. hash.rectAreaLength !== rectAreaLength ||
  246. hash.hemiLength !== hemiLength ||
  247. hash.numDirectionalShadows !== numDirectionalShadows ||
  248. hash.numPointShadows !== numPointShadows ||
  249. hash.numSpotShadows !== numSpotShadows ) {
  250. state.directional.length = directionalLength;
  251. state.spot.length = spotLength;
  252. state.rectArea.length = rectAreaLength;
  253. state.point.length = pointLength;
  254. state.hemi.length = hemiLength;
  255. state.directionalShadowMap.length = numDirectionalShadows;
  256. state.pointShadowMap.length = numPointShadows;
  257. state.spotShadowMap.length = numSpotShadows;
  258. state.directionalShadowMatrix.length = numDirectionalShadows;
  259. state.pointShadowMatrix.length = numPointShadows;
  260. state.spotShadowMatrix.length = numSpotShadows;
  261. hash.directionalLength = directionalLength;
  262. hash.pointLength = pointLength;
  263. hash.spotLength = spotLength;
  264. hash.rectAreaLength = rectAreaLength;
  265. hash.hemiLength = hemiLength;
  266. hash.numDirectionalShadows = numDirectionalShadows;
  267. hash.numPointShadows = numPointShadows;
  268. hash.numSpotShadows = numSpotShadows;
  269. state.version = nextVersion ++;
  270. }
  271. }
  272. return {
  273. setup: setup,
  274. state: state
  275. };
  276. }
  277. export { WebGLLights };