1
1
using System ;
2
2
using System . IO ;
3
3
using Unity . Collections ;
4
+ using Unity . Collections . LowLevel . Unsafe ;
4
5
using UnityEditor ;
5
6
#if UNITY_2020_2_OR_NEWER
6
7
using UnityEditor . AssetImporters ;
@@ -118,7 +119,7 @@ public string GetPhotometricType()
118
119
int width = 2 * textureSize ;
119
120
int height = 2 * textureSize ;
120
121
121
- NativeArray < Color32 > colorBuffer ;
122
+ NativeArray < Color > colorBuffer ;
122
123
123
124
switch ( m_iesReader . PhotometricType )
124
125
{
@@ -148,7 +149,7 @@ public string GetPhotometricType()
148
149
/// <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>
149
150
public ( string , Texture ) Generate2DCookie ( TextureImporterCompression compression , float coneAngle , int textureSize , bool applyLightAttenuation )
150
151
{
151
- NativeArray < Color32 > colorBuffer ;
152
+ NativeArray < Color > colorBuffer ;
152
153
153
154
switch ( m_iesReader . PhotometricType )
154
155
{
@@ -171,7 +172,7 @@ public string GetPhotometricType()
171
172
int width = 2 * textureSize ;
172
173
int height = textureSize ;
173
174
174
- NativeArray < Color32 > colorBuffer ;
175
+ NativeArray < Color > colorBuffer ;
175
176
176
177
switch ( m_iesReader . PhotometricType )
177
178
{
@@ -189,7 +190,7 @@ public string GetPhotometricType()
189
190
return GenerateTexture ( TextureImporterType . Default , TextureImporterShape . Texture2D , compression , width , height , colorBuffer ) ;
190
191
}
191
192
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 )
193
194
{
194
195
// Default values set by the TextureGenerationSettings constructor can be found in this file on GitHub:
195
196
// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/AssetPipeline/TextureGenerator.bindings.cs
@@ -218,6 +219,7 @@ public string GetPhotometricType()
218
219
platformSettings . maxTextureSize = 2048 ;
219
220
platformSettings . resizeAlgorithm = TextureResizeAlgorithm . Bilinear ;
220
221
platformSettings . textureCompression = compression ;
222
+ platformSettings . format = TextureImporterFormat . RGB9E5 ;
221
223
222
224
TextureGenerationOutput output = TextureGenerator . GenerateTexture ( settings , colorBuffer ) ;
223
225
@@ -229,21 +231,22 @@ public string GetPhotometricType()
229
231
return ( output . importInspectorWarnings , output . output ) ;
230
232
}
231
233
232
- private static byte PackIESValue ( float value )
234
+ Color ComputePixelColor ( float horizontalAnglePosition , float verticalAnglePosition , float attenuation = 1.0f )
233
235
{
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 ) ;
235
238
}
236
239
237
- NativeArray < Color32 > BuildTypeACylindricalTexture ( int width , int height )
240
+ NativeArray < Color > BuildTypeACylindricalTexture ( int width , int height )
238
241
{
239
242
float stepU = 360f / ( width - 1 ) ;
240
243
float stepV = 180f / ( height - 1 ) ;
241
244
242
- var textureBuffer = new NativeArray < Color32 > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
245
+ var textureBuffer = new NativeArray < Color > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
243
246
244
247
for ( int y = 0 ; y < height ; y ++ )
245
248
{
246
- var slice = new NativeSlice < Color32 > ( textureBuffer , y * width , width ) ;
249
+ var slice = new NativeSlice < Color > ( textureBuffer , y * width , width ) ;
247
250
248
251
float latitude = y * stepV - 90f ; // in range [-90..+90] degrees
249
252
@@ -255,24 +258,23 @@ NativeArray<Color32> BuildTypeACylindricalTexture(int width, int height)
255
258
256
259
float horizontalAnglePosition = m_iesReader . ComputeTypeAorBHorizontalAnglePosition ( longitude ) ;
257
260
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 ) ;
260
262
}
261
263
}
262
264
263
265
return textureBuffer ;
264
266
}
265
267
266
- NativeArray < Color32 > BuildTypeBCylindricalTexture ( int width , int height )
268
+ NativeArray < Color > BuildTypeBCylindricalTexture ( int width , int height )
267
269
{
268
270
float stepU = k_TwoPi / ( width - 1 ) ;
269
271
float stepV = Mathf . PI / ( height - 1 ) ;
270
272
271
- var textureBuffer = new NativeArray < Color32 > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
273
+ var textureBuffer = new NativeArray < Color > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
272
274
273
275
for ( int y = 0 ; y < height ; y ++ )
274
276
{
275
- var slice = new NativeSlice < Color32 > ( textureBuffer , y * width , width ) ;
277
+ var slice = new NativeSlice < Color > ( textureBuffer , y * width , width ) ;
276
278
277
279
float v = y * stepV - k_HalfPi ; // in range [-90..+90] degrees
278
280
@@ -293,24 +295,23 @@ NativeArray<Color32> BuildTypeBCylindricalTexture(int width, int height)
293
295
float horizontalAnglePosition = m_iesReader . ComputeTypeAorBHorizontalAnglePosition ( longitude ) ;
294
296
float verticalAnglePosition = m_iesReader . ComputeVerticalAnglePosition ( latitude ) ;
295
297
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 ) ;
298
299
}
299
300
}
300
301
301
302
return textureBuffer ;
302
303
}
303
304
304
- NativeArray < Color32 > BuildTypeCCylindricalTexture ( int width , int height )
305
+ NativeArray < Color > BuildTypeCCylindricalTexture ( int width , int height )
305
306
{
306
307
float stepU = k_TwoPi / ( width - 1 ) ;
307
308
float stepV = Mathf . PI / ( height - 1 ) ;
308
309
309
- var textureBuffer = new NativeArray < Color32 > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
310
+ var textureBuffer = new NativeArray < Color > ( width * height , Allocator . Temp , NativeArrayOptions . UninitializedMemory ) ;
310
311
311
312
for ( int y = 0 ; y < height ; y ++ )
312
313
{
313
- var slice = new NativeSlice < Color32 > ( textureBuffer , y * width , width ) ;
314
+ var slice = new NativeSlice < Color > ( textureBuffer , y * width , width ) ;
314
315
315
316
float v = y * stepV - k_HalfPi ; // in range [-90..+90] degrees
316
317
@@ -331,25 +332,24 @@ NativeArray<Color32> BuildTypeCCylindricalTexture(int width, int height)
331
332
float horizontalAnglePosition = m_iesReader . ComputeTypeCHorizontalAnglePosition ( longitude ) ;
332
333
float verticalAnglePosition = m_iesReader . ComputeVerticalAnglePosition ( latitude ) ;
333
334
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 ) ;
336
336
}
337
337
}
338
338
339
339
return textureBuffer ;
340
340
}
341
341
342
- NativeArray < Color32 > BuildTypeAGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
342
+ NativeArray < Color > BuildTypeAGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
343
343
{
344
344
float limitUV = Mathf . Tan ( 0.5f * coneAngle * Mathf . Deg2Rad ) ;
345
345
float stepUV = ( 2 * limitUV ) / ( size - 3 ) ;
346
346
347
- var textureBuffer = new NativeArray < Color32 > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
347
+ var textureBuffer = new NativeArray < Color > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
348
348
349
349
// Leave a one-pixel black border around the texture to avoid cookie spilling.
350
350
for ( int y = 1 ; y < size - 1 ; y ++ )
351
351
{
352
- var slice = new NativeSlice < Color32 > ( textureBuffer , y * size , size ) ;
352
+ var slice = new NativeSlice < Color > ( textureBuffer , y * size , size ) ;
353
353
354
354
float v = ( y - 1 ) * stepUV - limitUV ;
355
355
@@ -368,25 +368,24 @@ NativeArray<Color32> BuildTypeAGnomonicTexture(float coneAngle, int size, bool a
368
368
// Factor in the light attenuation further from the texture center.
369
369
float lightAttenuation = applyLightAttenuation ? rayLengthSquared : 1f ;
370
370
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 ) ;
373
372
}
374
373
}
375
374
376
375
return textureBuffer ;
377
376
}
378
377
379
- NativeArray < Color32 > BuildTypeBGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
378
+ NativeArray < Color > BuildTypeBGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
380
379
{
381
380
float limitUV = Mathf . Tan ( 0.5f * coneAngle * Mathf . Deg2Rad ) ;
382
381
float stepUV = ( 2 * limitUV ) / ( size - 3 ) ;
383
382
384
- var textureBuffer = new NativeArray < Color32 > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
383
+ var textureBuffer = new NativeArray < Color > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
385
384
386
385
// Leave a one-pixel black border around the texture to avoid cookie spilling.
387
386
for ( int y = 1 ; y < size - 1 ; y ++ )
388
387
{
389
- var slice = new NativeSlice < Color32 > ( textureBuffer , y * size , size ) ;
388
+ var slice = new NativeSlice < Color > ( textureBuffer , y * size , size ) ;
390
389
391
390
float v = ( y - 1 ) * stepUV - limitUV ;
392
391
@@ -406,25 +405,24 @@ NativeArray<Color32> BuildTypeBGnomonicTexture(float coneAngle, int size, bool a
406
405
// Factor in the light attenuation further from the texture center.
407
406
float lightAttenuation = applyLightAttenuation ? rayLengthSquared : 1f ;
408
407
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 ) ;
411
409
}
412
410
}
413
411
414
412
return textureBuffer ;
415
413
}
416
414
417
- NativeArray < Color32 > BuildTypeCGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
415
+ NativeArray < Color > BuildTypeCGnomonicTexture ( float coneAngle , int size , bool applyLightAttenuation )
418
416
{
419
417
float limitUV = Mathf . Tan ( 0.5f * coneAngle * Mathf . Deg2Rad ) ;
420
418
float stepUV = ( 2 * limitUV ) / ( size - 3 ) ;
421
419
422
- var textureBuffer = new NativeArray < Color32 > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
420
+ var textureBuffer = new NativeArray < Color > ( size * size , Allocator . Temp , NativeArrayOptions . ClearMemory ) ;
423
421
424
422
// Leave a one-pixel black border around the texture to avoid cookie spilling.
425
423
for ( int y = 1 ; y < size - 1 ; y ++ )
426
424
{
427
- var slice = new NativeSlice < Color32 > ( textureBuffer , y * size , size ) ;
425
+ var slice = new NativeSlice < Color > ( textureBuffer , y * size , size ) ;
428
426
429
427
float v = ( y - 1 ) * stepUV - limitUV ;
430
428
@@ -443,8 +441,7 @@ NativeArray<Color32> BuildTypeCGnomonicTexture(float coneAngle, int size, bool a
443
441
// Factor in the light attenuation further from the texture center.
444
442
float lightAttenuation = applyLightAttenuation ? ( uvLength * uvLength + 1 ) : 1f ;
445
443
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 ) ;
448
445
}
449
446
}
450
447
0 commit comments