WebGLLights.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. import { Color } from '../../math/Color.js';
  2. import { Matrix4 } from '../../math/Matrix4.js';
  3. import { Vector2 } from '../../math/Vector2.js';
  4. import { Vector3 } from '../../math/Vector3.js';
  5. import { UniformsLib } from '../shaders/UniformsLib.js';
  6. function UniformsCache() {
  7. const lights = {};
  8. return {
  9. get: function ( light ) {
  10. if ( lights[ light.id ] !== undefined ) {
  11. return lights[ light.id ];
  12. }
  13. let uniforms;
  14. switch ( light.type ) {
  15. case 'DirectionalLight':
  16. uniforms = {
  17. direction: new Vector3(),
  18. color: new Color()
  19. };
  20. break;
  21. case 'SpotLight':
  22. uniforms = {
  23. position: new Vector3(),
  24. direction: new Vector3(),
  25. color: new Color(),
  26. distance: 0,
  27. coneCos: 0,
  28. penumbraCos: 0,
  29. decay: 0
  30. };
  31. break;
  32. case 'PointLight':
  33. uniforms = {
  34. position: new Vector3(),
  35. color: new Color(),
  36. distance: 0,
  37. decay: 0
  38. };
  39. break;
  40. case 'HemisphereLight':
  41. uniforms = {
  42. direction: new Vector3(),
  43. skyColor: new Color(),
  44. groundColor: new Color()
  45. };
  46. break;
  47. case 'RectAreaLight':
  48. uniforms = {
  49. color: new Color(),
  50. position: new Vector3(),
  51. halfWidth: new Vector3(),
  52. halfHeight: new Vector3()
  53. };
  54. break;
  55. }
  56. lights[ light.id ] = uniforms;
  57. return uniforms;
  58. }
  59. };
  60. }
  61. function ShadowUniformsCache() {
  62. const lights = {};
  63. return {
  64. get: function ( light ) {
  65. if ( lights[ light.id ] !== undefined ) {
  66. return lights[ light.id ];
  67. }
  68. let uniforms;
  69. switch ( light.type ) {
  70. case 'DirectionalLight':
  71. uniforms = {
  72. shadowBias: 0,
  73. shadowNormalBias: 0,
  74. shadowRadius: 1,
  75. shadowMapSize: new Vector2()
  76. };
  77. break;
  78. case 'SpotLight':
  79. uniforms = {
  80. shadowBias: 0,
  81. shadowNormalBias: 0,
  82. shadowRadius: 1,
  83. shadowMapSize: new Vector2()
  84. };
  85. break;
  86. case 'PointLight':
  87. uniforms = {
  88. shadowBias: 0,
  89. shadowNormalBias: 0,
  90. shadowRadius: 1,
  91. shadowMapSize: new Vector2(),
  92. shadowCameraNear: 1,
  93. shadowCameraFar: 1000
  94. };
  95. break;
  96. // TODO (abelnation): set RectAreaLight shadow uniforms
  97. }
  98. lights[ light.id ] = uniforms;
  99. return uniforms;
  100. }
  101. };
  102. }
  103. let nextVersion = 0;
  104. function shadowCastingLightsFirst( lightA, lightB ) {
  105. return ( lightB.castShadow ? 1 : 0 ) - ( lightA.castShadow ? 1 : 0 );
  106. }
  107. function WebGLLights( extensions, capabilities ) {
  108. const cache = new UniformsCache();
  109. const shadowCache = ShadowUniformsCache();
  110. const state = {
  111. version: 0,
  112. hash: {
  113. directionalLength: - 1,
  114. pointLength: - 1,
  115. spotLength: - 1,
  116. rectAreaLength: - 1,
  117. hemiLength: - 1,
  118. numDirectionalShadows: - 1,
  119. numPointShadows: - 1,
  120. numSpotShadows: - 1
  121. },
  122. ambient: [ 0, 0, 0 ],
  123. probe: [],
  124. directional: [],
  125. directionalShadow: [],
  126. directionalShadowMap: [],
  127. directionalShadowMatrix: [],
  128. spot: [],
  129. spotShadow: [],
  130. spotShadowMap: [],
  131. spotShadowMatrix: [],
  132. rectArea: [],
  133. rectAreaLTC1: null,
  134. rectAreaLTC2: null,
  135. point: [],
  136. pointShadow: [],
  137. pointShadowMap: [],
  138. pointShadowMatrix: [],
  139. hemi: []
  140. };
  141. for ( let i = 0; i < 9; i ++ ) state.probe.push( new Vector3() );
  142. const vector3 = new Vector3();
  143. const matrix4 = new Matrix4();
  144. const matrix42 = new Matrix4();
  145. function setup( lights, shadows, camera ) {
  146. let r = 0, g = 0, b = 0;
  147. for ( let i = 0; i < 9; i ++ ) state.probe[ i ].set( 0, 0, 0 );
  148. let directionalLength = 0;
  149. let pointLength = 0;
  150. let spotLength = 0;
  151. let rectAreaLength = 0;
  152. let hemiLength = 0;
  153. let numDirectionalShadows = 0;
  154. let numPointShadows = 0;
  155. let numSpotShadows = 0;
  156. const viewMatrix = camera.matrixWorldInverse;
  157. lights.sort( shadowCastingLightsFirst );
  158. for ( let i = 0, l = lights.length; i < l; i ++ ) {
  159. const light = lights[ i ];
  160. const color = light.color;
  161. const intensity = light.intensity;
  162. const distance = light.distance;
  163. const shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;
  164. if ( light.isAmbientLight ) {
  165. r += color.r * intensity;
  166. g += color.g * intensity;
  167. b += color.b * intensity;
  168. } else if ( light.isLightProbe ) {
  169. for ( let j = 0; j < 9; j ++ ) {
  170. state.probe[ j ].addScaledVector( light.sh.coefficients[ j ], intensity );
  171. }
  172. } else if ( light.isDirectionalLight ) {
  173. const uniforms = cache.get( light );
  174. uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
  175. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  176. vector3.setFromMatrixPosition( light.target.matrixWorld );
  177. uniforms.direction.sub( vector3 );
  178. uniforms.direction.transformDirection( viewMatrix );
  179. if ( light.castShadow ) {
  180. const shadow = light.shadow;
  181. const shadowUniforms = shadowCache.get( light );
  182. shadowUniforms.shadowBias = shadow.bias;
  183. shadowUniforms.shadowNormalBias = shadow.normalBias;
  184. shadowUniforms.shadowRadius = shadow.radius;
  185. shadowUniforms.shadowMapSize = shadow.mapSize;
  186. state.directionalShadow[ directionalLength ] = shadowUniforms;
  187. state.directionalShadowMap[ directionalLength ] = shadowMap;
  188. state.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
  189. numDirectionalShadows ++;
  190. }
  191. state.directional[ directionalLength ] = uniforms;
  192. directionalLength ++;
  193. } else if ( light.isSpotLight ) {
  194. const uniforms = cache.get( light );
  195. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  196. uniforms.position.applyMatrix4( viewMatrix );
  197. uniforms.color.copy( color ).multiplyScalar( intensity );
  198. uniforms.distance = distance;
  199. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  200. vector3.setFromMatrixPosition( light.target.matrixWorld );
  201. uniforms.direction.sub( vector3 );
  202. uniforms.direction.transformDirection( viewMatrix );
  203. uniforms.coneCos = Math.cos( light.angle );
  204. uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
  205. uniforms.decay = light.decay;
  206. if ( light.castShadow ) {
  207. const shadow = light.shadow;
  208. const shadowUniforms = shadowCache.get( light );
  209. shadowUniforms.shadowBias = shadow.bias;
  210. shadowUniforms.shadowNormalBias = shadow.normalBias;
  211. shadowUniforms.shadowRadius = shadow.radius;
  212. shadowUniforms.shadowMapSize = shadow.mapSize;
  213. state.spotShadow[ spotLength ] = shadowUniforms;
  214. state.spotShadowMap[ spotLength ] = shadowMap;
  215. state.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
  216. numSpotShadows ++;
  217. }
  218. state.spot[ spotLength ] = uniforms;
  219. spotLength ++;
  220. } else if ( light.isRectAreaLight ) {
  221. const uniforms = cache.get( light );
  222. // (a) intensity is the total visible light emitted
  223. //uniforms.color.copy( color ).multiplyScalar( intensity / ( light.width * light.height * Math.PI ) );
  224. // (b) intensity is the brightness of the light
  225. uniforms.color.copy( color ).multiplyScalar( intensity );
  226. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  227. uniforms.position.applyMatrix4( viewMatrix );
  228. // extract local rotation of light to derive width/height half vectors
  229. matrix42.identity();
  230. matrix4.copy( light.matrixWorld );
  231. matrix4.premultiply( viewMatrix );
  232. matrix42.extractRotation( matrix4 );
  233. uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 );
  234. uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 );
  235. uniforms.halfWidth.applyMatrix4( matrix42 );
  236. uniforms.halfHeight.applyMatrix4( matrix42 );
  237. // TODO (abelnation): RectAreaLight distance?
  238. // uniforms.distance = distance;
  239. state.rectArea[ rectAreaLength ] = uniforms;
  240. rectAreaLength ++;
  241. } else if ( light.isPointLight ) {
  242. const uniforms = cache.get( light );
  243. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  244. uniforms.position.applyMatrix4( viewMatrix );
  245. uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
  246. uniforms.distance = light.distance;
  247. uniforms.decay = light.decay;
  248. if ( light.castShadow ) {
  249. const shadow = light.shadow;
  250. const shadowUniforms = shadowCache.get( light );
  251. shadowUniforms.shadowBias = shadow.bias;
  252. shadowUniforms.shadowNormalBias = shadow.normalBias;
  253. shadowUniforms.shadowRadius = shadow.radius;
  254. shadowUniforms.shadowMapSize = shadow.mapSize;
  255. shadowUniforms.shadowCameraNear = shadow.camera.near;
  256. shadowUniforms.shadowCameraFar = shadow.camera.far;
  257. state.pointShadow[ pointLength ] = shadowUniforms;
  258. state.pointShadowMap[ pointLength ] = shadowMap;
  259. state.pointShadowMatrix[ pointLength ] = light.shadow.matrix;
  260. numPointShadows ++;
  261. }
  262. state.point[ pointLength ] = uniforms;
  263. pointLength ++;
  264. } else if ( light.isHemisphereLight ) {
  265. const uniforms = cache.get( light );
  266. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  267. uniforms.direction.transformDirection( viewMatrix );
  268. uniforms.direction.normalize();
  269. uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
  270. uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
  271. state.hemi[ hemiLength ] = uniforms;
  272. hemiLength ++;
  273. }
  274. }
  275. if ( rectAreaLength > 0 ) {
  276. if ( capabilities.isWebGL2 || extensions.has( 'OES_texture_float_linear' ) ) {
  277. state.rectAreaLTC1 = UniformsLib.LTC_FLOAT_1;
  278. state.rectAreaLTC2 = UniformsLib.LTC_FLOAT_2;
  279. } else if ( extensions.has( 'OES_texture_half_float_linear' ) ) {
  280. state.rectAreaLTC1 = UniformsLib.LTC_HALF_1;
  281. state.rectAreaLTC2 = UniformsLib.LTC_HALF_2;
  282. } else {
  283. console.error( 'THREE.WebGLRenderer: Unable to use RectAreaLight. Missing WebGL extensions.' );
  284. }
  285. }
  286. state.ambient[ 0 ] = r;
  287. state.ambient[ 1 ] = g;
  288. state.ambient[ 2 ] = b;
  289. const hash = state.hash;
  290. if ( hash.directionalLength !== directionalLength ||
  291. hash.pointLength !== pointLength ||
  292. hash.spotLength !== spotLength ||
  293. hash.rectAreaLength !== rectAreaLength ||
  294. hash.hemiLength !== hemiLength ||
  295. hash.numDirectionalShadows !== numDirectionalShadows ||
  296. hash.numPointShadows !== numPointShadows ||
  297. hash.numSpotShadows !== numSpotShadows ) {
  298. state.directional.length = directionalLength;
  299. state.spot.length = spotLength;
  300. state.rectArea.length = rectAreaLength;
  301. state.point.length = pointLength;
  302. state.hemi.length = hemiLength;
  303. state.directionalShadow.length = numDirectionalShadows;
  304. state.directionalShadowMap.length = numDirectionalShadows;
  305. state.pointShadow.length = numPointShadows;
  306. state.pointShadowMap.length = numPointShadows;
  307. state.spotShadow.length = numSpotShadows;
  308. state.spotShadowMap.length = numSpotShadows;
  309. state.directionalShadowMatrix.length = numDirectionalShadows;
  310. state.pointShadowMatrix.length = numPointShadows;
  311. state.spotShadowMatrix.length = numSpotShadows;
  312. hash.directionalLength = directionalLength;
  313. hash.pointLength = pointLength;
  314. hash.spotLength = spotLength;
  315. hash.rectAreaLength = rectAreaLength;
  316. hash.hemiLength = hemiLength;
  317. hash.numDirectionalShadows = numDirectionalShadows;
  318. hash.numPointShadows = numPointShadows;
  319. hash.numSpotShadows = numSpotShadows;
  320. state.version = nextVersion ++;
  321. }
  322. }
  323. return {
  324. setup: setup,
  325. state: state
  326. };
  327. }
  328. export { WebGLLights };