WebGLLights.js 9.5 KB

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