IESLoader.js 8.1 KB

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