11using System ;
22using System . IO ;
33using Unity . Collections ;
4+ using Unity . Collections . LowLevel . Unsafe ;
45using UnityEditor ;
56#if UNITY_2020_2_OR_NEWER
67using UnityEditor . AssetImporters ;
@@ -118,7 +119,7 @@ public string GetPhotometricType()
118119 int width = 2 * textureSize ;
119120 int height = 2 * textureSize ;
120121
121- NativeArray < Color32 > colorBuffer ;
122+ NativeArray < Color > colorBuffer ;
122123
123124 switch ( m_iesReader . PhotometricType )
124125 {
@@ -148,7 +149,7 @@ public string GetPhotometricType()
148149 /// <returns>A Generated 2D texture doing the projection of the IES using the Gnomonic projection of the bottom half hemisphere with the given 'cone angle'</returns>
149150 public ( string , Texture ) Generate2DCookie ( TextureImporterCompression compression , float coneAngle , int textureSize , bool applyLightAttenuation )
150151 {
151- NativeArray < Color32 > colorBuffer ;
152+ NativeArray < Color > colorBuffer ;
152153
153154 switch ( m_iesReader . PhotometricType )
154155 {
@@ -171,7 +172,7 @@ public string GetPhotometricType()
171172 int width = 2 * textureSize ;
172173 int height = textureSize ;
173174
174- NativeArray < Color32 > colorBuffer ;
175+ NativeArray < Color > colorBuffer ;
175176
176177 switch ( m_iesReader . PhotometricType )
177178 {
@@ -189,7 +190,7 @@ public string GetPhotometricType()
189190 return GenerateTexture ( TextureImporterType . Default , TextureImporterShape . Texture2D , compression , width , height , colorBuffer ) ;
190191 }
191192
192- ( string , Texture ) GenerateTexture ( TextureImporterType type , TextureImporterShape shape , TextureImporterCompression compression , int width , int height , NativeArray < Color32 > colorBuffer )
193+ ( string , Texture ) GenerateTexture ( TextureImporterType type , TextureImporterShape shape , TextureImporterCompression compression , int width , int height , NativeArray < Color > colorBuffer )
193194 {
194195 // Default values set by the TextureGenerationSettings constructor can be found in this file on GitHub:
195196 // https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/AssetPipeline/TextureGenerator.bindings.cs
@@ -218,6 +219,7 @@ public string GetPhotometricType()
218219 platformSettings . maxTextureSize = 2048 ;
219220 platformSettings . resizeAlgorithm = TextureResizeAlgorithm . Bilinear ;
220221 platformSettings . textureCompression = compression ;
222+ platformSettings . format = TextureImporterFormat . RGB9E5 ;
221223
222224 TextureGenerationOutput output = TextureGenerator . GenerateTexture ( settings , colorBuffer ) ;
223225
@@ -229,21 +231,22 @@ public string GetPhotometricType()
229231 return ( output . importInspectorWarnings , output . output ) ;
230232 }
231233
232- private static byte PackIESValue ( float value )
234+ Color ComputePixelColor ( float horizontalAnglePosition , float verticalAnglePosition , float attenuation = 1.0f )
233235 {
234- return ( byte ) Math . Clamp ( value * 255 , 0 , 255 ) ;
236+ float value = m_iesReader . InterpolateBilinear ( horizontalAnglePosition , verticalAnglePosition ) / ( m_iesReader . MaxCandelas * attenuation ) ;
237+ return new Color ( value , value , value , value ) ;
235238 }
236239
237- NativeArray < Color32 > BuildTypeACylindricalTexture ( int width , int height )
240+ NativeArray < Color > BuildTypeACylindricalTexture ( int width , int height )
238241 {
239242 float stepU = 360f / ( width - 1 ) ;
240243 float stepV = 180f / ( height - 1 ) ;
241244
242- var textureBuffer = new NativeArray < Color32 > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
245+ var textureBuffer = new NativeArray < Color > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
243246
244247 for ( int y = 0 ; y < height ; y ++ )
245248 {
246- var slice = new NativeSlice < Color32 > ( textureBuffer , y * width , width ) ;
249+ var slice = new NativeSlice < Color > ( textureBuffer , y * width , width ) ;
247250
248251 float latitude = y * stepV - 90f ; // in range [-90..+90] degrees
249252
@@ -255,24 +258,23 @@ NativeArray<Color32> BuildTypeACylindricalTexture(int width, int height)
255258
256259 float horizontalAnglePosition = m_iesReader . ComputeTypeAorBHorizontalAnglePosition ( longitude ) ;
257260
258- byte value = PackIESValue ( m_iesReader . InterpolateBilinear ( horizontalAnglePosition , verticalAnglePosition ) / m_iesReader . MaxCandelas ) ;
259- slice [ x ] = new Color32 ( value , value , value , value ) ;
261+ slice [ x ] = ComputePixelColor ( horizontalAnglePosition , verticalAnglePosition ) ;
260262 }
261263 }
262264
263265 return textureBuffer ;
264266 }
265267
266- NativeArray < Color32 > BuildTypeBCylindricalTexture ( int width , int height )
268+ NativeArray < Color > BuildTypeBCylindricalTexture ( int width , int height )
267269 {
268270 float stepU = k_TwoPi / ( width - 1 ) ;
269271 float stepV = Mathf . PI / ( height - 1 ) ;
270272
271- var textureBuffer = new NativeArray < Color32 > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
273+ var textureBuffer = new NativeArray < Color > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
272274
273275 for ( int y = 0 ; y < height ; y ++ )
274276 {
275- var slice = new NativeSlice < Color32 > ( textureBuffer , y * width , width ) ;
277+ var slice = new NativeSlice < Color > ( textureBuffer , y * width , width ) ;
276278
277279 float v = y * stepV - k_HalfPi ; // in range [-90..+90] degrees
278280
@@ -293,24 +295,23 @@ NativeArray<Color32> BuildTypeBCylindricalTexture(int width, int height)
293295 float horizontalAnglePosition = m_iesReader . ComputeTypeAorBHorizontalAnglePosition ( longitude ) ;
294296 float verticalAnglePosition = m_iesReader . ComputeVerticalAnglePosition ( latitude ) ;
295297
296- byte value = PackIESValue ( m_iesReader . InterpolateBilinear ( horizontalAnglePosition , verticalAnglePosition ) / m_iesReader . MaxCandelas ) ;
297- slice [ x ] = new Color32 ( value , value , value , value ) ;
298+ slice [ x ] = ComputePixelColor ( horizontalAnglePosition , verticalAnglePosition ) ;
298299 }
299300 }
300301
301302 return textureBuffer ;
302303 }
303304
304- NativeArray < Color32 > BuildTypeCCylindricalTexture ( int width , int height )
305+ NativeArray < Color > BuildTypeCCylindricalTexture ( int width , int height )
305306 {
306307 float stepU = k_TwoPi / ( width - 1 ) ;
307308 float stepV = Mathf . PI / ( height - 1 ) ;
308309
309- var textureBuffer = new NativeArray < Color32 > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
310+ var textureBuffer = new NativeArray < Color > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
310311
311312 for ( int y = 0 ; y < height ; y ++ )
312313 {
313- var slice = new NativeSlice < Color32 > ( textureBuffer , y * width , width ) ;
314+ var slice = new NativeSlice < Color > ( textureBuffer , y * width , width ) ;
314315
315316 float v = y * stepV - k_HalfPi ; // in range [-90..+90] degrees
316317
@@ -331,25 +332,24 @@ NativeArray<Color32> BuildTypeCCylindricalTexture(int width, int height)
331332 float horizontalAnglePosition = m_iesReader . ComputeTypeCHorizontalAnglePosition ( longitude ) ;
332333 float verticalAnglePosition = m_iesReader . ComputeVerticalAnglePosition ( latitude ) ;
333334
334- byte value = PackIESValue ( m_iesReader . InterpolateBilinear ( horizontalAnglePosition , verticalAnglePosition ) / m_iesReader . MaxCandelas ) ;
335- slice [ x ] = new Color32 ( value , value , value , value ) ;
335+ slice [ x ] = ComputePixelColor ( horizontalAnglePosition , verticalAnglePosition ) ;
336336 }
337337 }
338338
339339 return textureBuffer ;
340340 }
341341
342- NativeArray < Color32 > BuildTypeAGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
342+ NativeArray < Color > BuildTypeAGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
343343 {
344344 float limitUV = Mathf . Tan ( 0.5f * coneAngle * Mathf . Deg2Rad ) ;
345345 float stepUV = ( 2 * limitUV ) / ( size - 3 ) ;
346346
347- var textureBuffer = new NativeArray < Color32 > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
347+ var textureBuffer = new NativeArray < Color > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
348348
349349 // Leave a one-pixel black border around the texture to avoid cookie spilling.
350350 for ( int y = 1 ; y < size - 1 ; y ++ )
351351 {
352- var slice = new NativeSlice < Color32 > ( textureBuffer , y * size , size ) ;
352+ var slice = new NativeSlice < Color > ( textureBuffer , y * size , size ) ;
353353
354354 float v = ( y - 1 ) * stepUV - limitUV ;
355355
@@ -368,25 +368,24 @@ NativeArray<Color32> BuildTypeAGnomonicTexture(float coneAngle, int size, bool a
368368 // Factor in the light attenuation further from the texture center.
369369 float lightAttenuation = applyLightAttenuation ? rayLengthSquared : 1f ;
370370
371- byte value = PackIESValue ( m_iesReader . InterpolateBilinear ( horizontalAnglePosition , verticalAnglePosition ) / ( m_iesReader . MaxCandelas * lightAttenuation ) ) ;
372- slice [ x ] = new Color32 ( value , value , value , value ) ;
371+ slice [ x ] = ComputePixelColor ( horizontalAnglePosition , verticalAnglePosition , lightAttenuation ) ;
373372 }
374373 }
375374
376375 return textureBuffer ;
377376 }
378377
379- NativeArray < Color32 > BuildTypeBGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
378+ NativeArray < Color > BuildTypeBGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
380379 {
381380 float limitUV = Mathf . Tan ( 0.5f * coneAngle * Mathf . Deg2Rad ) ;
382381 float stepUV = ( 2 * limitUV ) / ( size - 3 ) ;
383382
384- var textureBuffer = new NativeArray < Color32 > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
383+ var textureBuffer = new NativeArray < Color > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
385384
386385 // Leave a one-pixel black border around the texture to avoid cookie spilling.
387386 for ( int y = 1 ; y < size - 1 ; y ++ )
388387 {
389- var slice = new NativeSlice < Color32 > ( textureBuffer , y * size , size ) ;
388+ var slice = new NativeSlice < Color > ( textureBuffer , y * size , size ) ;
390389
391390 float v = ( y - 1 ) * stepUV - limitUV ;
392391
@@ -406,25 +405,24 @@ NativeArray<Color32> BuildTypeBGnomonicTexture(float coneAngle, int size, bool a
406405 // Factor in the light attenuation further from the texture center.
407406 float lightAttenuation = applyLightAttenuation ? rayLengthSquared : 1f ;
408407
409- byte value = PackIESValue ( m_iesReader . InterpolateBilinear ( horizontalAnglePosition , verticalAnglePosition ) / ( m_iesReader . MaxCandelas * lightAttenuation ) ) ;
410- slice [ x ] = new Color32 ( value , value , value , value ) ;
408+ slice [ x ] = ComputePixelColor ( horizontalAnglePosition , verticalAnglePosition , lightAttenuation ) ;
411409 }
412410 }
413411
414412 return textureBuffer ;
415413 }
416414
417- NativeArray < Color32 > BuildTypeCGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
415+ NativeArray < Color > BuildTypeCGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
418416 {
419417 float limitUV = Mathf . Tan ( 0.5f * coneAngle * Mathf . Deg2Rad ) ;
420418 float stepUV = ( 2 * limitUV ) / ( size - 3 ) ;
421419
422- var textureBuffer = new NativeArray < Color32 > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
420+ var textureBuffer = new NativeArray < Color > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
423421
424422 // Leave a one-pixel black border around the texture to avoid cookie spilling.
425423 for ( int y = 1 ; y < size - 1 ; y ++ )
426424 {
427- var slice = new NativeSlice < Color32 > ( textureBuffer , y * size , size ) ;
425+ var slice = new NativeSlice < Color > ( textureBuffer , y * size , size ) ;
428426
429427 float v = ( y - 1 ) * stepUV - limitUV ;
430428
@@ -443,8 +441,7 @@ NativeArray<Color32> BuildTypeCGnomonicTexture(float coneAngle, int size, bool a
443441 // Factor in the light attenuation further from the texture center.
444442 float lightAttenuation = applyLightAttenuation ? ( uvLength * uvLength + 1 ) : 1f ;
445443
446- byte value = PackIESValue ( m_iesReader . InterpolateBilinear ( horizontalAnglePosition , verticalAnglePosition ) / ( m_iesReader . MaxCandelas * lightAttenuation ) ) ;
447- slice [ x ] = new Color32 ( value , value , value , value ) ;
444+ slice [ x ] = ComputePixelColor ( horizontalAnglePosition , verticalAnglePosition , lightAttenuation ) ;
448445 }
449446 }
450447
0 commit comments