11using System . Collections ;
2- using System . Collections . Generic ;
32using ActionUnity ;
43using NUnit . Framework ;
54using Unity . Mathematics ;
65using UnityEngine ;
76using UnityEngine . TestTools ;
87
9- public class InteropTests
8+ public class InteropTests
109{
1110 private GameObject _gameObjectWithInteropHandler ;
1211 private InteropHandlerSample _interopHandlerSample ;
12+
1313 [ SetUp ]
1414 public void SetUp ( )
1515 {
@@ -26,99 +26,83 @@ public void TearDown()
2626 }
2727
2828 [ UnityTest ]
29- public IEnumerator TestInteropHandler ( )
29+ public IEnumerator TestTextureInteropHandler ( )
3030 {
3131 // Wait for a few seconds to allow the simulation to run
32- float simulationTime = 0.5f ;
32+ float simulationTime = 0.5f ;
3333 yield return new WaitForSeconds ( simulationTime ) ;
3434
3535 // Now that the simulation has run, run your tests
3636 // Yield one more frame to ensure everything is updated
37- yield return null ;
38- // Perform your tests as previously described
37+ yield return null ;
38+ // Perform your tests as previously described
3939 TextureContainsExpectedValues ( ) ;
40- TextureArrayContainsExpectedValues ( ) ;
41- // ComputeBufferContainsExpectedValues();
4240 }
4341
44- public void TextureContainsExpectedValues ( )
45- {
46- Texture2D originalTexture = _interopHandlerSample . Texture ;
47-
48- // Create a temporary RenderTexture with the same dimensions as the original texture
49- RenderTexture tempRenderTexture = new RenderTexture ( originalTexture . width , originalTexture . height , 0 ) ;
50- RenderTexture . active = tempRenderTexture ;
51-
52- // Copy the content of the original Texture2D to the RenderTexture
53- Graphics . Blit ( originalTexture , tempRenderTexture ) ;
42+ [ UnityTest ]
43+ public IEnumerator TestTextureArrayInteropHandler ( )
44+ {
45+ // Wait for a few seconds to allow the simulation to run
46+ float simulationTime = 0.5f ;
47+ yield return new WaitForSeconds ( simulationTime ) ;
5448
55- // Create a new Texture2D to read the pixels from the RenderTexture
56- Texture2D copiedTexture = new Texture2D ( originalTexture . width , originalTexture . height ) ;
57- copiedTexture . ReadPixels ( new Rect ( 0 , 0 , originalTexture . width , originalTexture . height ) , 0 , 0 ) ;
58- copiedTexture . Apply ( ) ;
49+ // Now that the simulation has run, run your tests
50+ // Yield one more frame to ensure everything is updated
51+ yield return null ;
52+ TextureArrayContainsExpectedValues ( ) ;
53+ }
5954
60- // Loop through each pixel and check the value
61- Color [ ] pixels = copiedTexture . GetPixels ( ) ;
62- foreach ( Color pixel in pixels )
55+ [ UnityTest ]
56+ public IEnumerator TestBufferInteropHandler ( )
6357 {
64- // Implement your pixel value verification logic here
65- float expectedValue = math . abs ( math . cos ( Time . time ) ) ;
66- Assert . IsTrue ( math . abs ( expectedValue - pixel . g ) < 1e-2f ) ;
67- }
58+ // Wait for a few seconds to allow the simulation to run
59+ float simulationTime = 0.5f ;
60+ yield return new WaitForSeconds ( simulationTime ) ;
6861
69- // Clean up resources
70- RenderTexture . active = null ;
71- Object . Destroy ( copiedTexture ) ;
72- Object . Destroy ( tempRenderTexture ) ;
73- }
62+ // Now that the simulation has run, run your tests
63+ // Yield one more frame to ensure everything is updated
64+ yield return null ;
65+ ComputeBufferContainsExpectedValues ( ) ;
66+ ;
67+ }
7468
75- public void TextureArrayContainsExpectedValues ( )
69+ public void TextureContainsExpectedValues ( )
7670 {
77- Texture2DArray textureArray = _interopHandlerSample . TextureArray ;
78-
79- // Create a RenderTexture to copy the Texture2DArray.
80- RenderTexture renderTexture = new RenderTexture ( textureArray . width , textureArray . height , 0 , RenderTextureFormat . ARGB32 ) ;
81- renderTexture . enableRandomWrite = true ;
82- renderTexture . Create ( ) ;
71+ Texture2D originalTexture = _interopHandlerSample . Texture ;
8372
84- // Set up a temporary camera to render the Texture2DArray to the RenderTexture.
85- // Create a temporary camera GameObject and set its target texture.
86- GameObject tempCameraObject = new GameObject ( "TempCamera" ) ;
87- Camera tempCamera = tempCameraObject . AddComponent < Camera > ( ) ;
88- tempCamera . targetTexture = renderTexture ;
89- tempCamera . RenderWithShader ( Shader . Find ( "Unlit/Texture" ) , "RenderType" ) ;
73+ // Create a temporary RenderTexture with the same dimensions as the original texture
74+ RenderTexture tempRenderTexture = new ( originalTexture . width , originalTexture . height , 0 ) ;
75+ RenderTexture . active = tempRenderTexture ;
9076
91- // Create a temporary texture to read the pixels from the RenderTexture.
92- Texture2D tempTexture = new Texture2D ( textureArray . width , textureArray . height , TextureFormat . ARGB32 , false ) ;
77+ // Copy the content of the original Texture2D to the RenderTexture
78+ Graphics . Blit ( originalTexture , tempRenderTexture ) ;
9379
94- // Read the pixels from the RenderTexture into the temporary texture.
95- RenderTexture . active = renderTexture ;
96- tempTexture . ReadPixels ( new Rect ( 0 , 0 , textureArray . width , textureArray . height ) , 0 , 0 ) ;
97- tempTexture . Apply ( ) ;
80+ // Create a new Texture2D to read the pixels from the RenderTexture
81+ Texture2D copiedTexture = new ( originalTexture . width , originalTexture . height ) ;
82+ copiedTexture . ReadPixels ( new Rect ( 0 , 0 , originalTexture . width , originalTexture . height ) , 0 , 0 ) ;
83+ copiedTexture . Apply ( ) ;
9884
99- // Loop through the slices of the Texture2DArray and compare pixel values.
100- for ( int z = 0 ; z < textureArray . depth ; z ++ )
85+ // Loop through each pixel and check the value
86+ Color [ ] pixels = copiedTexture . GetPixels ( ) ;
87+ foreach ( Color pixel in pixels )
10188 {
102- for ( int x = 0 ; x < textureArray . width ; x ++ )
103- {
104- for ( int y = 0 ; y < textureArray . height ; y ++ )
105- {
106- Color expectedColor = new Color ( z % 2 , math . abs ( ( z + 1 ) * math . cos ( Time . time ) ) , 0 , 1.0f ) ;
107- Color actualColor = tempTexture . GetPixel ( x , y ) ;
108- Debug . Log ( expectedColor + " " + actualColor ) ;
109- // Compare the colors with a tolerance.
110- Assert . IsTrue ( ( Vector4 . Distance ( expectedColor , actualColor ) < 1e-2 ) ) ;
111- }
112- }
89+ // Implement your pixel value verification logic here
90+ float expectedValue = math . abs ( math . cos ( Time . time ) ) ;
91+ Assert . IsTrue ( math . abs ( expectedValue - pixel . g ) < 1e-2f ) ;
11392 }
11493
115- // Clean up temporary objects.
116- Object . Destroy ( tempCamera . gameObject ) ;
117- Object . Destroy ( renderTexture ) ;
118- Object . Destroy ( tempTexture ) ;
119-
94+ // Clean up resources
95+ RenderTexture . active = null ;
96+ Object . Destroy ( copiedTexture ) ;
97+ Object . Destroy ( tempRenderTexture ) ;
12098 }
12199
100+ public void TextureArrayContainsExpectedValues ( )
101+ {
102+ Texture2DArray textureArray = _interopHandlerSample . TextureArray ;
103+ Debug . LogWarning ( "TODO implement this function" ) ;
104+ // TODO to implement
105+ }
122106
123107 public void ComputeBufferContainsExpectedValues ( )
124108 {
@@ -128,22 +112,20 @@ public void ComputeBufferContainsExpectedValues()
128112 Assert . IsNotNull ( computeBuffer ) ;
129113
130114 // Set the buffer data to an array to access the values
131- float4 [ ] bufferData = new float4 [ computeBuffer . count ] ;
115+ var bufferData = new float4 [ computeBuffer . count ] ;
132116 computeBuffer . GetData ( bufferData ) ;
133117
134118 // Loop through the buffer data and verify values at each index
135119 for ( int x = 0 ; x < bufferData . Length ; x ++ )
136120 {
137- float4 expectedValue = new float4
138- (
139- math . cos ( 2 * math . PI * Time . time / x ) ,
140- math . sin ( 2 * math . PI * Time . time / x ) ,
121+ float4 expectedValue = new (
122+ math . cos ( 2 * math . PI * Time . time / ( math . abs ( x ) + 1.0f ) ) ,
123+ math . sin ( 2 * math . PI * Time . time / ( math . abs ( x ) + 1.0f ) ) ,
141124 0.0f ,
142125 1.0f
143126 ) ;
144127
145- // Implement your verification logic for the ComputeBuffer data here
146- Assert . AreEqual ( expectedValue , bufferData [ x ] ) ;
128+ Assert . IsTrue ( math . length ( expectedValue - bufferData [ x ] ) < 1e-2f ) ;
147129 }
148130 }
149131}
0 commit comments