WebGLLights.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 shadowCastingAndTexturingLightsFirst( lightA, lightB ) {
  105. return ( lightB.castShadow ? 2 : 0 ) - ( lightA.castShadow ? 2 : 0 ) + ( lightB.map ? 1 : 0 ) - ( lightA.map ? 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. numSpotMaps: - 1,
  122. numLightProbes: - 1
  123. },
  124. ambient: [ 0, 0, 0 ],
  125. probe: [],
  126. directional: [],
  127. directionalShadow: [],
  128. directionalShadowMap: [],
  129. directionalShadowMatrix: [],
  130. spot: [],
  131. spotLightMap: [],
  132. spotShadow: [],
  133. spotShadowMap: [],
  134. spotLightMatrix: [],
  135. rectArea: [],
  136. rectAreaLTC1: null,
  137. rectAreaLTC2: null,
  138. point: [],
  139. pointShadow: [],
  140. pointShadowMap: [],
  141. pointShadowMatrix: [],
  142. hemi: [],
  143. numSpotLightShadowsWithMaps: 0,
  144. numLightProbes: 0
  145. };
  146. for ( let i = 0; i < 9; i ++ ) state.probe.push( new Vector3() );
  147. const vector3 = new Vector3();
  148. const matrix4 = new Matrix4();
  149. const matrix42 = new Matrix4();
  150. function setup( lights, useLegacyLights ) {
  151. let r = 0, g = 0, b = 0;
  152. for ( let i = 0; i < 9; i ++ ) state.probe[ i ].set( 0, 0, 0 );
  153. let directionalLength = 0;
  154. let pointLength = 0;
  155. let spotLength = 0;
  156. let rectAreaLength = 0;
  157. let hemiLength = 0;
  158. let numDirectionalShadows = 0;
  159. let numPointShadows = 0;
  160. let numSpotShadows = 0;
  161. let numSpotMaps = 0;
  162. let numSpotShadowsWithMaps = 0;
  163. let numLightProbes = 0;
  164. // ordering : [shadow casting + map texturing, map texturing, shadow casting, none ]
  165. lights.sort( shadowCastingAndTexturingLightsFirst );
  166. // artist-friendly light intensity scaling factor
  167. const scaleFactor = ( useLegacyLights === true ) ? Math.PI : 1;
  168. for ( let i = 0, l = lights.length; i < l; i ++ ) {
  169. const light = lights[ i ];
  170. const color = light.color;
  171. const intensity = light.intensity;
  172. const distance = light.distance;
  173. const shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;
  174. if ( light.isAmbientLight ) {
  175. r += color.r * intensity * scaleFactor;
  176. g += color.g * intensity * scaleFactor;
  177. b += color.b * intensity * scaleFactor;
  178. } else if ( light.isLightProbe ) {
  179. for ( let j = 0; j < 9; j ++ ) {
  180. state.probe[ j ].addScaledVector( light.sh.coefficients[ j ], intensity );
  181. }
  182. numLightProbes ++;
  183. } else if ( light.isDirectionalLight ) {
  184. const uniforms = cache.get( light );
  185. uniforms.color.copy( light.color ).multiplyScalar( light.intensity * scaleFactor );
  186. if ( light.castShadow ) {
  187. const shadow = light.shadow;
  188. const shadowUniforms = shadowCache.get( light );
  189. shadowUniforms.shadowBias = shadow.bias;
  190. shadowUniforms.shadowNormalBias = shadow.normalBias;
  191. shadowUniforms.shadowRadius = shadow.radius;
  192. shadowUniforms.shadowMapSize = shadow.mapSize;
  193. state.directionalShadow[ directionalLength ] = shadowUniforms;
  194. state.directionalShadowMap[ directionalLength ] = shadowMap;
  195. state.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
  196. numDirectionalShadows ++;
  197. }
  198. state.directional[ directionalLength ] = uniforms;
  199. directionalLength ++;
  200. } else if ( light.isSpotLight ) {
  201. const uniforms = cache.get( light );
  202. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  203. uniforms.color.copy( color ).multiplyScalar( intensity * scaleFactor );
  204. uniforms.distance = distance;
  205. uniforms.coneCos = Math.cos( light.angle );
  206. uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
  207. uniforms.decay = light.decay;
  208. state.spot[ spotLength ] = uniforms;
  209. const shadow = light.shadow;
  210. if ( light.map ) {
  211. state.spotLightMap[ numSpotMaps ] = light.map;
  212. numSpotMaps ++;
  213. // make sure the lightMatrix is up to date
  214. // TODO : do it if required only
  215. shadow.updateMatrices( light );
  216. if ( light.castShadow ) numSpotShadowsWithMaps ++;
  217. }
  218. state.spotLightMatrix[ spotLength ] = shadow.matrix;
  219. if ( light.castShadow ) {
  220. const shadowUniforms = shadowCache.get( light );
  221. shadowUniforms.shadowBias = shadow.bias;
  222. shadowUniforms.shadowNormalBias = shadow.normalBias;
  223. shadowUniforms.shadowRadius = shadow.radius;
  224. shadowUniforms.shadowMapSize = shadow.mapSize;
  225. state.spotShadow[ spotLength ] = shadowUniforms;
  226. state.spotShadowMap[ spotLength ] = shadowMap;
  227. numSpotShadows ++;
  228. }
  229. spotLength ++;
  230. } else if ( light.isRectAreaLight ) {
  231. const uniforms = cache.get( light );
  232. uniforms.color.copy( color ).multiplyScalar( intensity );
  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. state.rectArea[ rectAreaLength ] = uniforms;
  236. rectAreaLength ++;
  237. } else if ( light.isPointLight ) {
  238. const uniforms = cache.get( light );
  239. uniforms.color.copy( light.color ).multiplyScalar( light.intensity * scaleFactor );
  240. uniforms.distance = light.distance;
  241. uniforms.decay = light.decay;
  242. if ( light.castShadow ) {
  243. const shadow = light.shadow;
  244. const shadowUniforms = shadowCache.get( light );
  245. shadowUniforms.shadowBias = shadow.bias;
  246. shadowUniforms.shadowNormalBias = shadow.normalBias;
  247. shadowUniforms.shadowRadius = shadow.radius;
  248. shadowUniforms.shadowMapSize = shadow.mapSize;
  249. shadowUniforms.shadowCameraNear = shadow.camera.near;
  250. shadowUniforms.shadowCameraFar = shadow.camera.far;
  251. state.pointShadow[ pointLength ] = shadowUniforms;
  252. state.pointShadowMap[ pointLength ] = shadowMap;
  253. state.pointShadowMatrix[ pointLength ] = light.shadow.matrix;
  254. numPointShadows ++;
  255. }
  256. state.point[ pointLength ] = uniforms;
  257. pointLength ++;
  258. } else if ( light.isHemisphereLight ) {
  259. const uniforms = cache.get( light );
  260. uniforms.skyColor.copy( light.color ).multiplyScalar( intensity * scaleFactor );
  261. uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity * scaleFactor );
  262. state.hemi[ hemiLength ] = uniforms;
  263. hemiLength ++;
  264. }
  265. }
  266. if ( rectAreaLength > 0 ) {
  267. if ( capabilities.isWebGL2 ) {
  268. // WebGL 2
  269. if ( extensions.has( 'OES_texture_float_linear' ) === true ) {
  270. state.rectAreaLTC1 = UniformsLib.LTC_FLOAT_1;
  271. state.rectAreaLTC2 = UniformsLib.LTC_FLOAT_2;
  272. } else {
  273. state.rectAreaLTC1 = UniformsLib.LTC_HALF_1;
  274. state.rectAreaLTC2 = UniformsLib.LTC_HALF_2;
  275. }
  276. } else {
  277. // WebGL 1
  278. if ( extensions.has( 'OES_texture_float_linear' ) === true ) {
  279. state.rectAreaLTC1 = UniformsLib.LTC_FLOAT_1;
  280. state.rectAreaLTC2 = UniformsLib.LTC_FLOAT_2;
  281. } else if ( extensions.has( 'OES_texture_half_float_linear' ) === true ) {
  282. state.rectAreaLTC1 = UniformsLib.LTC_HALF_1;
  283. state.rectAreaLTC2 = UniformsLib.LTC_HALF_2;
  284. } else {
  285. console.error( 'THREE.WebGLRenderer: Unable to use RectAreaLight. Missing WebGL extensions.' );
  286. }
  287. }
  288. }
  289. state.ambient[ 0 ] = r;
  290. state.ambient[ 1 ] = g;
  291. state.ambient[ 2 ] = b;
  292. const hash = state.hash;
  293. if ( hash.directionalLength !== directionalLength ||
  294. hash.pointLength !== pointLength ||
  295. hash.spotLength !== spotLength ||
  296. hash.rectAreaLength !== rectAreaLength ||
  297. hash.hemiLength !== hemiLength ||
  298. hash.numDirectionalShadows !== numDirectionalShadows ||
  299. hash.numPointShadows !== numPointShadows ||
  300. hash.numSpotShadows !== numSpotShadows ||
  301. hash.numSpotMaps !== numSpotMaps ||
  302. hash.numLightProbes !== numLightProbes ) {
  303. state.directional.length = directionalLength;
  304. state.spot.length = spotLength;
  305. state.rectArea.length = rectAreaLength;
  306. state.point.length = pointLength;
  307. state.hemi.length = hemiLength;
  308. state.directionalShadow.length = numDirectionalShadows;
  309. state.directionalShadowMap.length = numDirectionalShadows;
  310. state.pointShadow.length = numPointShadows;
  311. state.pointShadowMap.length = numPointShadows;
  312. state.spotShadow.length = numSpotShadows;
  313. state.spotShadowMap.length = numSpotShadows;
  314. state.directionalShadowMatrix.length = numDirectionalShadows;
  315. state.pointShadowMatrix.length = numPointShadows;
  316. state.spotLightMatrix.length = numSpotShadows + numSpotMaps - numSpotShadowsWithMaps;
  317. state.spotLightMap.length = numSpotMaps;
  318. state.numSpotLightShadowsWithMaps = numSpotShadowsWithMaps;
  319. state.numLightProbes = numLightProbes;
  320. hash.directionalLength = directionalLength;
  321. hash.pointLength = pointLength;
  322. hash.spotLength = spotLength;
  323. hash.rectAreaLength = rectAreaLength;
  324. hash.hemiLength = hemiLength;
  325. hash.numDirectionalShadows = numDirectionalShadows;
  326. hash.numPointShadows = numPointShadows;
  327. hash.numSpotShadows = numSpotShadows;
  328. hash.numSpotMaps = numSpotMaps;
  329. hash.numLightProbes = numLightProbes;
  330. state.version = nextVersion ++;
  331. }
  332. }
  333. function setupView( lights, camera ) {
  334. let directionalLength = 0;
  335. let pointLength = 0;
  336. let spotLength = 0;
  337. let rectAreaLength = 0;
  338. let hemiLength = 0;
  339. const viewMatrix = camera.matrixWorldInverse;
  340. for ( let i = 0, l = lights.length; i < l; i ++ ) {
  341. const light = lights[ i ];
  342. if ( light.isDirectionalLight ) {
  343. const uniforms = state.directional[ directionalLength ];
  344. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  345. vector3.setFromMatrixPosition( light.target.matrixWorld );
  346. uniforms.direction.sub( vector3 );
  347. uniforms.direction.transformDirection( viewMatrix );
  348. directionalLength ++;
  349. } else if ( light.isSpotLight ) {
  350. const uniforms = state.spot[ spotLength ];
  351. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  352. uniforms.position.applyMatrix4( viewMatrix );
  353. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  354. vector3.setFromMatrixPosition( light.target.matrixWorld );
  355. uniforms.direction.sub( vector3 );
  356. uniforms.direction.transformDirection( viewMatrix );
  357. spotLength ++;
  358. } else if ( light.isRectAreaLight ) {
  359. const uniforms = state.rectArea[ rectAreaLength ];
  360. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  361. uniforms.position.applyMatrix4( viewMatrix );
  362. // extract local rotation of light to derive width/height half vectors
  363. matrix42.identity();
  364. matrix4.copy( light.matrixWorld );
  365. matrix4.premultiply( viewMatrix );
  366. matrix42.extractRotation( matrix4 );
  367. uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 );
  368. uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 );
  369. uniforms.halfWidth.applyMatrix4( matrix42 );
  370. uniforms.halfHeight.applyMatrix4( matrix42 );
  371. rectAreaLength ++;
  372. } else if ( light.isPointLight ) {
  373. const uniforms = state.point[ pointLength ];
  374. uniforms.position.setFromMatrixPosition( light.matrixWorld );
  375. uniforms.position.applyMatrix4( viewMatrix );
  376. pointLength ++;
  377. } else if ( light.isHemisphereLight ) {
  378. const uniforms = state.hemi[ hemiLength ];
  379. uniforms.direction.setFromMatrixPosition( light.matrixWorld );
  380. uniforms.direction.transformDirection( viewMatrix );
  381. hemiLength ++;
  382. }
  383. }
  384. }
  385. return {
  386. setup: setup,
  387. setupView: setupView,
  388. state: state
  389. };
  390. }
  391. export { WebGLLights };