1
0

IESLoader.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. class IESLoader extends Loader {
  14. constructor( manager ) {
  15. super( manager );
  16. this.type = HalfFloatType;
  17. }
  18. _getIESValues( iesLamp, type ) {
  19. const width = 360;
  20. const height = 180;
  21. const size = width * height;
  22. const data = new Array( size );
  23. function interpolateCandelaValues( phi, theta ) {
  24. let phiIndex = 0, thetaIndex = 0;
  25. let startTheta = 0, endTheta = 0, startPhi = 0, endPhi = 0;
  26. 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
  27. if ( theta < iesLamp.horAngles[ i + 1 ] || i == iesLamp.numHorAngles - 2 ) {
  28. thetaIndex = i;
  29. startTheta = iesLamp.horAngles[ i ];
  30. endTheta = iesLamp.horAngles[ i + 1 ];
  31. break;
  32. }
  33. }
  34. for ( let i = 0; i < iesLamp.numVerAngles - 1; ++ i ) {
  35. if ( phi < iesLamp.verAngles[ i + 1 ] || i == iesLamp.numVerAngles - 2 ) {
  36. phiIndex = i;
  37. startPhi = iesLamp.verAngles[ i ];
  38. endPhi = iesLamp.verAngles[ i + 1 ];
  39. break;
  40. }
  41. }
  42. const deltaTheta = endTheta - startTheta;
  43. const deltaPhi = endPhi - startPhi;
  44. if ( deltaPhi === 0 ) // Outside range
  45. return 0;
  46. const t1 = deltaTheta === 0 ? 0 : ( theta - startTheta ) / deltaTheta;
  47. const t2 = ( phi - startPhi ) / deltaPhi;
  48. const nextThetaIndex = deltaTheta === 0 ? thetaIndex : thetaIndex + 1;
  49. const v1 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex ], t1 );
  50. const v2 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex + 1 ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex + 1 ], t1 );
  51. const v = MathUtils.lerp( v1, v2, t2 );
  52. return v;
  53. }
  54. const startTheta = iesLamp.horAngles[ 0 ], endTheta = iesLamp.horAngles[ iesLamp.numHorAngles - 1 ];
  55. for ( let i = 0; i < size; ++ i ) {
  56. let theta = i % width;
  57. const phi = Math.floor( i / width );
  58. if ( endTheta - startTheta !== 0 && ( theta < startTheta || theta >= endTheta ) ) { // Handle symmetry for hor angles
  59. theta %= endTheta * 2;
  60. if ( theta > endTheta )
  61. theta = endTheta * 2 - theta;
  62. }
  63. data[ phi + theta * height ] = interpolateCandelaValues( phi, theta );
  64. }
  65. let result = null;
  66. if ( type === UnsignedByteType ) result = Uint8Array.from( data.map( v => Math.min( v * 0xFF, 0xFF ) ) );
  67. else if ( type === HalfFloatType ) result = Uint16Array.from( data.map( v => DataUtils.toHalfFloat( v ) ) );
  68. else if ( type === FloatType ) result = Float32Array.from( data );
  69. else console.error( 'IESLoader: Unsupported type:', type );
  70. return result;
  71. }
  72. load( url, onLoad, onProgress, onError ) {
  73. const loader = new FileLoader( this.manager );
  74. loader.setResponseType( 'text' );
  75. loader.setCrossOrigin( this.crossOrigin );
  76. loader.setWithCredentials( this.withCredentials );
  77. loader.setPath( this.path );
  78. loader.setRequestHeader( this.requestHeader );
  79. loader.load( url, text => {
  80. onLoad( this.parse( text ) );
  81. }, onProgress, onError );
  82. }
  83. parse( text ) {
  84. const type = this.type;
  85. const iesLamp = new IESLamp( text );
  86. const data = this._getIESValues( iesLamp, type );
  87. const texture = new DataTexture( data, 180, 1, RedFormat, type );
  88. texture.minFilter = LinearFilter;
  89. texture.magFilter = LinearFilter;
  90. texture.needsUpdate = true;
  91. return texture;
  92. }
  93. }
  94. function IESLamp( text ) {
  95. const _self = this;
  96. const textArray = text.split( '\n' );
  97. let lineNumber = 0;
  98. let line;
  99. _self.verAngles = [ ];
  100. _self.horAngles = [ ];
  101. _self.candelaValues = [ ];
  102. _self.tiltData = { };
  103. _self.tiltData.angles = [ ];
  104. _self.tiltData.mulFactors = [ ];
  105. function textToArray( text ) {
  106. text = text.replace( /^\s+|\s+$/g, '' ); // remove leading or trailing spaces
  107. text = text.replace( /,/g, ' ' ); // replace commas with spaces
  108. text = text.replace( /\s\s+/g, ' ' ); // replace white space/tabs etc by single whitespace
  109. const array = text.split( ' ' );
  110. return array;
  111. }
  112. function readArray( count, array ) {
  113. while ( true ) {
  114. const line = textArray[ lineNumber ++ ];
  115. const lineData = textToArray( line );
  116. for ( let i = 0; i < lineData.length; ++ i ) {
  117. array.push( Number( lineData[ i ] ) );
  118. }
  119. if ( array.length === count )
  120. break;
  121. }
  122. }
  123. function readTilt() {
  124. let line = textArray[ lineNumber ++ ];
  125. let lineData = textToArray( line );
  126. _self.tiltData.lampToLumGeometry = Number( lineData[ 0 ] );
  127. line = textArray[ lineNumber ++ ];
  128. lineData = textToArray( line );
  129. _self.tiltData.numAngles = Number( lineData[ 0 ] );
  130. readArray( _self.tiltData.numAngles, _self.tiltData.angles );
  131. readArray( _self.tiltData.numAngles, _self.tiltData.mulFactors );
  132. }
  133. function readLampValues() {
  134. const values = [ ];
  135. readArray( 10, values );
  136. _self.count = Number( values[ 0 ] );
  137. _self.lumens = Number( values[ 1 ] );
  138. _self.multiplier = Number( values[ 2 ] );
  139. _self.numVerAngles = Number( values[ 3 ] );
  140. _self.numHorAngles = Number( values[ 4 ] );
  141. _self.gonioType = Number( values[ 5 ] );
  142. _self.units = Number( values[ 6 ] );
  143. _self.width = Number( values[ 7 ] );
  144. _self.length = Number( values[ 8 ] );
  145. _self.height = Number( values[ 9 ] );
  146. }
  147. function readLampFactors() {
  148. const values = [ ];
  149. readArray( 3, values );
  150. _self.ballFactor = Number( values[ 0 ] );
  151. _self.blpFactor = Number( values[ 1 ] );
  152. _self.inputWatts = Number( values[ 2 ] );
  153. }
  154. while ( true ) {
  155. line = textArray[ lineNumber ++ ];
  156. if ( line.includes( 'TILT' ) ) {
  157. break;
  158. }
  159. }
  160. if ( ! line.includes( 'NONE' ) ) {
  161. if ( line.includes( 'INCLUDE' ) ) {
  162. readTilt();
  163. } else {
  164. // TODO:: Read tilt data from a file
  165. }
  166. }
  167. readLampValues();
  168. readLampFactors();
  169. // Initialize candela value array
  170. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  171. _self.candelaValues.push( [ ] );
  172. }
  173. // Parse Angles
  174. readArray( _self.numVerAngles, _self.verAngles );
  175. readArray( _self.numHorAngles, _self.horAngles );
  176. // Parse Candela values
  177. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  178. readArray( _self.numVerAngles, _self.candelaValues[ i ] );
  179. }
  180. // Calculate actual candela values, and normalize.
  181. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  182. for ( let j = 0; j < _self.numVerAngles; ++ j ) {
  183. _self.candelaValues[ i ][ j ] *= _self.candelaValues[ i ][ j ] * _self.multiplier
  184. * _self.ballFactor * _self.blpFactor;
  185. }
  186. }
  187. let maxVal = - 1;
  188. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  189. for ( let j = 0; j < _self.numVerAngles; ++ j ) {
  190. const value = _self.candelaValues[ i ][ j ];
  191. maxVal = maxVal < value ? value : maxVal;
  192. }
  193. }
  194. const bNormalize = true;
  195. if ( bNormalize && maxVal > 0 ) {
  196. for ( let i = 0; i < _self.numHorAngles; ++ i ) {
  197. for ( let j = 0; j < _self.numVerAngles; ++ j ) {
  198. _self.candelaValues[ i ][ j ] /= maxVal;
  199. }
  200. }
  201. }
  202. }
  203. export { IESLoader };