IESLoader.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import {
  2. DataTexture,
  3. FileLoader,
  4. FloatType,
  5. RedFormat,
  6. MathUtils,
  7. Loader,
  8. UnsignedByteType,
  9. LinearFilter,
  10. HalfFloatType,
  11. DataUtils
  12. } from 'three';
  13. /**
  14. * A loader for the IES format.
  15. *
  16. * The loaded texture should be assigned to {@link IESSpotLight#map}.
  17. *
  18. * ```js
  19. * const loader = new IESLoader();
  20. * const texture = await loader.loadAsync( 'ies/007cfb11e343e2f42e3b476be4ab684e.ies' );
  21. *
  22. * const spotLight = new THREE.IESSpotLight( 0xff0000, 500 );
  23. * spotLight.iesMap = texture;
  24. * ```
  25. *
  26. * @augments Loader
  27. * @three_import import { IESLoader } from 'three/addons/loaders/IESLoader.js';
  28. */
  29. class IESLoader extends Loader {
  30. /**
  31. * Constructs a new IES loader.
  32. *
  33. * @param {LoadingManager} [manager] - The loading manager.
  34. */
  35. constructor( manager ) {
  36. super( manager );
  37. /**
  38. * The texture type.
  39. *
  40. * @type {(HalfFloatType|FloatType)}
  41. * @default HalfFloatType
  42. */
  43. this.type = HalfFloatType;
  44. }
  45. _getIESValues( iesLamp, type ) {
  46. const width = 360;
  47. const height = 180;
  48. const size = width * height;
  49. const data = new Array( size );
  50. function interpolateCandelaValues( phi, theta ) {
  51. let phiIndex = 0, thetaIndex = 0;
  52. let startTheta = 0, endTheta = 0, startPhi = 0, endPhi = 0;
  53. for ( let i = 0; i < iesLamp.numHorAngles - 1; ++ i ) { // numHorAngles = horAngles.length-1 because of extra padding, so this wont cause an out of bounds error
  54. if ( theta < iesLamp.horAngles[ i + 1 ] || i == iesLamp.numHorAngles - 2 ) {
  55. thetaIndex = i;
  56. startTheta = iesLamp.horAngles[ i ];
  57. endTheta = iesLamp.horAngles[ i + 1 ];
  58. break;
  59. }
  60. }
  61. for ( let i = 0; i < iesLamp.numVerAngles - 1; ++ i ) {
  62. if ( phi < iesLamp.verAngles[ i + 1 ] || i == iesLamp.numVerAngles - 2 ) {
  63. phiIndex = i;
  64. startPhi = iesLamp.verAngles[ i ];
  65. endPhi = iesLamp.verAngles[ i + 1 ];
  66. break;
  67. }
  68. }
  69. const deltaTheta = endTheta - startTheta;
  70. const deltaPhi = endPhi - startPhi;
  71. if ( deltaPhi === 0 ) // Outside range
  72. return 0;
  73. const t1 = deltaTheta === 0 ? 0 : ( theta - startTheta ) / deltaTheta;
  74. const t2 = ( phi - startPhi ) / deltaPhi;
  75. const nextThetaIndex = deltaTheta === 0 ? thetaIndex : thetaIndex + 1;
  76. const v1 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex ], t1 );
  77. const v2 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex + 1 ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex + 1 ], t1 );
  78. const v = MathUtils.lerp( v1, v2, t2 );
  79. return v;
  80. }
  81. const startTheta = iesLamp.horAngles[ 0 ], endTheta = iesLamp.horAngles[ iesLamp.numHorAngles - 1 ];
  82. for ( let i = 0; i < size; ++ i ) {
  83. let theta = i % width;
  84. const phi = Math.floor( i / width );
  85. if ( endTheta - startTheta !== 0 && ( theta < startTheta || theta >= endTheta ) ) { // Handle symmetry for hor angles
  86. theta %= endTheta * 2;
  87. if ( theta > endTheta )
  88. theta = endTheta * 2 - theta;
  89. }
  90. data[ phi + theta * height ] = interpolateCandelaValues( phi, theta );
  91. }
  92. let result = null;
  93. if ( type === UnsignedByteType ) result = Uint8Array.from( data.map( v => Math.min( v * 0xFF, 0xFF ) ) );
  94. else if ( type === HalfFloatType ) result = Uint16Array.from( data.map( v => DataUtils.toHalfFloat( v ) ) );
  95. else if ( type === FloatType ) result = Float32Array.from( data );
  96. else console.error( 'IESLoader: Unsupported type:', type );
  97. return result;
  98. }
  99. /**
  100. * Starts loading from the given URL and passes the loaded IES texture
  101. * to the `onLoad()` callback.
  102. *
  103. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  104. * @param {function(DataTexture)} onLoad - Executed when the loading process has been finished.
  105. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  106. * @param {onErrorCallback} onError - Executed when errors occur.
  107. */
  108. load( url, onLoad, onProgress, onError ) {
  109. const loader = new FileLoader( this.manager );
  110. loader.setResponseType( 'text' );
  111. loader.setCrossOrigin( this.crossOrigin );
  112. loader.setWithCredentials( this.withCredentials );
  113. loader.setPath( this.path );
  114. loader.setRequestHeader( this.requestHeader );
  115. loader.load( url, text => {
  116. onLoad( this.parse( text ) );
  117. }, onProgress, onError );
  118. }
  119. /**
  120. * Parses the given IES data.
  121. *
  122. * @param {string} text - The raw IES data.
  123. * @return {DataTexture} THE IES data as a texture.
  124. */
  125. parse( text ) {
  126. const type = this.type;
  127. const iesLamp = new IESLamp( text );
  128. const data = this._getIESValues( iesLamp, type );
  129. const texture = new DataTexture( data, 180, 1, RedFormat, type );
  130. texture.minFilter = LinearFilter;
  131. texture.magFilter = LinearFilter;
  132. texture.needsUpdate = true;
  133. return texture;
  134. }
  135. }
  136. function IESLamp( text ) {
  137. const _self = this;
  138. const textArray = text.split( '\n' );
  139. let lineNumber = 0;
  140. let line;
  141. _self.verAngles = [ ];
  142. _self.horAngles = [ ];
  143. _self.candelaValues = [ ];
  144. _self.tiltData = { };
  145. _self.tiltData.angles = [ ];
  146. _self.tiltData.mulFactors = [ ];
  147. function textToArray( text ) {
  148. text = text.replace( /^\s+|\s+$/g, '' ); // remove leading or trailing spaces
  149. text = text.replace( /,/g, ' ' ); // replace commas with spaces
  150. text = text.replace( /\s\s+/g, ' ' ); // replace white space/tabs etc by single whitespace
  151. const array = text.split( ' ' );
  152. return array;
  153. }
  154. function readArray( count, array ) {
  155. while ( true ) {
  156. const line = textArray[ lineNumber ++ ];
  157. const lineData = textToArray( line );
  158. for ( let i = 0; i < lineData.length; ++ i ) {
  159. array.push( Number( lineData[ i ] ) );
  160. }
  161. if ( array.length === count )
  162. break;
  163. }
  164. }
  165. function readTilt() {
  166. let line = textArray[ lineNumber ++ ];
  167. let lineData = textToArray( line );
  168. _self.tiltData.lampToLumGeometry = Number( lineData[ 0 ] );
  169. line = textArray[ lineNumber ++ ];
  170. lineData = textToArray( line );
  171. _self.tiltData.numAngles = Number( lineData[ 0 ] );
  172. readArray( _self.tiltData.numAngles, _self.tiltData.angles );
  173. readArray( _self.tiltData.numAngles, _self.tiltData.mulFactors );
  174. }
  175. function readLampValues() {
  176. const values = [ ];
  177. readArray( 10, values );
  178. _self.count = Number( values[ 0 ] );
  179. _self.lumens = Number( values[ 1 ] );
  180. _self.multiplier = Number( values[ 2 ] );
  181. _self.numVerAngles = Number( values[ 3 ] );
  182. _self.numHorAngles = Number( values[ 4 ] );
  183. _self.gonioType = Number( values[ 5 ] );
  184. _self.units = Number( values[ 6 ] );
  185. _self.width = Number( values[ 7 ] );
  186. _self.length = Number( values[ 8 ] );
  187. _self.height = Number( values[ 9 ] );
  188. }
  189. function readLampFactors() {
  190. const values = [ ];
  191. readArray( 3, values );
  192. _self.ballFactor = Number( values[ 0 ] );
  193. _self.blpFactor = Number( values[ 1 ] );
  194. _self.inputWatts = Number( values[ 2 ] );
  195. }
  196. while ( true ) {
  197. line = textArray[ lineNumber ++ ];
  198. if ( line.includes( 'TILT' ) ) {
  199. break;
  200. }
  201. }
  202. if ( ! line.includes( 'NONE' ) ) {
  203. if ( line.includes( 'INCLUDE' ) ) {
  204. readTilt();
  205. } else {
  206. // TODO:: Read tilt data from a file
  207. }
  208. }
  209. readLampValues();
  210. readLampFactors();
  211. // Initialize candela value array
  212. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  213. _self.candelaValues.push( [ ] );
  214. }
  215. // Parse Angles
  216. readArray( _self.numVerAngles, _self.verAngles );
  217. readArray( _self.numHorAngles, _self.horAngles );
  218. // Parse Candela values
  219. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  220. readArray( _self.numVerAngles, _self.candelaValues[ i ] );
  221. }
  222. // Calculate actual candela values, and normalize.
  223. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  224. for ( let j = 0; j < _self.numVerAngles; ++ j ) {
  225. _self.candelaValues[ i ][ j ] *= _self.candelaValues[ i ][ j ] * _self.multiplier
  226. * _self.ballFactor * _self.blpFactor;
  227. }
  228. }
  229. let maxVal = - 1;
  230. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  231. for ( let j = 0; j < _self.numVerAngles; ++ j ) {
  232. const value = _self.candelaValues[ i ][ j ];
  233. maxVal = maxVal < value ? value : maxVal;
  234. }
  235. }
  236. const bNormalize = true;
  237. if ( bNormalize && maxVal > 0 ) {
  238. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  239. for ( let j = 0; j < _self.numVerAngles; ++ j ) {
  240. _self.candelaValues[ i ][ j ] /= maxVal;
  241. }
  242. }
  243. }
  244. }
  245. export { IESLoader };