@@ -25,8 +25,8 @@ public sealed class PluginTextureRenderer : IDisposable
2525 private IntPtr _textureBufferPtr ;
2626 private bool _updated ;
2727
28- private readonly int _textureWidth ;
29- private readonly int _textureHeight ;
28+ private int _textureWidth ;
29+ private int _textureHeight ;
3030 private readonly int _bytesPerPixel = 4 ; // RGBA32. 1 byte (8 bits) per channel.
3131
3232 private readonly Thread _pluginRenderThread ;
@@ -48,60 +48,50 @@ public sealed class PluginTextureRenderer : IDisposable
4848 private CustomSampler _textureUpdateLoopSampler ;
4949#endif
5050
51- public PluginTextureRenderer ( RawTextureDataUpdateCallback callback , Texture2D targetTexture , bool autoDispose = true )
51+ public PluginTextureRenderer ( RawTextureDataUpdateCallback callback , int textureWidth , int textureHeight ,
52+ int targetFrameRateOfPluginRenderThread = 60 , bool autoDispose = true )
5253 {
5354#if DEVELOPMENT_BUILD || UNITY_EDITOR
5455 _textureUpdateLoopSampler = CustomSampler . Create ( "RawTextureDataUpdateFunction" ) ;
5556#endif
5657
5758 _loopAction = RawTextureDataUpdate ;
5859 _rawTextureDataUpdateCallback = callback ;
60+ DebugLog ( $ "[{ nameof ( PluginTextureRenderer ) } ] The RawTextureDataUpdateCallback is \n '{ _rawTextureDataUpdateCallback . Target } .{ _rawTextureDataUpdateCallback . Method . Name } '.") ;
5961
60- _textureBuffer = new uint [ targetTexture . width * targetTexture . height ] ;
61- _textureBufferHandle = GCHandle . Alloc ( _textureBuffer , GCHandleType . Pinned ) ;
62- _textureBufferPtr = _textureBufferHandle . AddrOfPinnedObject ( ) ;
63-
64- if ( targetTexture . format != TextureFormat . RGBA32 )
65- {
66- _disposed = true ;
67- DebugLogError ( $ "[{ nameof ( PluginTextureRenderer ) } ] Unsupported texture format: { targetTexture . format } ") ;
68- return ;
69- }
62+ CreateTextureBuffer ( textureWidth , textureHeight ) ;
7063
7164 if ( autoDispose ) { UnityEngine . Application . quitting += Dispose ; }
7265
73- _targetTexture = targetTexture ;
74- _textureWidth = targetTexture . width ;
75- _textureHeight = targetTexture . height ;
66+ _targetFrameTimeMilliseconds = ( int ) ( 1000.0f / targetFrameRateOfPluginRenderThread ) ;
67+ DebugLog ( $ "[ { nameof ( PluginTextureRenderer ) } ] Target frame rate: { targetFrameRateOfPluginRenderThread } " ) ;
68+ DebugLog ( $ "[ { nameof ( PluginTextureRenderer ) } ] Target frame time milliseconds: { _targetFrameTimeMilliseconds } " ) ;
7669
7770 _cts = new CancellationTokenSource ( ) ;
7871 _pluginRenderThread = new Thread ( PluginRenderThread ) ;
7972 _pluginRenderThread . Start ( ) ;
8073 }
8174
82- public PluginTextureRenderer ( IssuePluginCustomTextureUpdateCallback callback , Texture2D targetTexture , bool autoDispose = true )
75+ public PluginTextureRenderer ( IssuePluginCustomTextureUpdateCallback callback , int textureWidth , int textureHeight ,
76+ int targetFrameRateOfPluginRenderThread = 60 , bool autoDispose = true )
8377 {
8478#if DEVELOPMENT_BUILD || UNITY_EDITOR
8579 _textureUpdateLoopSampler = CustomSampler . Create ( "CustomTextureUpdateFunction" ) ;
8680#endif
8781
8882 _loopAction = IssuePluginCustomTextureUpdate ;
8983 _customTextureUpdateCallback = callback ;
84+ DebugLog ( $ "[{ nameof ( PluginTextureRenderer ) } ] The CustomTextureUpdateCallback is \n '{ _customTextureUpdateCallback . Target } .{ _customTextureUpdateCallback . Method . Name } '.") ;
9085
9186 _textureUpdateParamsPtr = Marshal . AllocHGlobal ( Marshal . SizeOf ( _textureUpdateParams ) ) ;
9287
93- if ( targetTexture . format != TextureFormat . RGBA32 )
94- {
95- _disposed = true ;
96- DebugLogError ( $ "[{ nameof ( PluginTextureRenderer ) } ] Unsupported texture format: { targetTexture . format } ") ;
97- return ;
98- }
88+ CreateTextureBuffer ( textureWidth , textureHeight ) ;
9989
10090 if ( autoDispose ) { UnityEngine . Application . quitting += Dispose ; }
10191
102- _targetTexture = targetTexture ;
103- _textureWidth = targetTexture . width ;
104- _textureHeight = targetTexture . height ;
92+ _targetFrameTimeMilliseconds = ( int ) ( 1000.0f / targetFrameRateOfPluginRenderThread ) ;
93+ DebugLog ( $ "[ { nameof ( PluginTextureRenderer ) } ] Target frame rate: { targetFrameRateOfPluginRenderThread } " ) ;
94+ DebugLog ( $ "[ { nameof ( PluginTextureRenderer ) } ] Target frame time milliseconds: { _targetFrameTimeMilliseconds } " ) ;
10595
10696 _cts = new CancellationTokenSource ( ) ;
10797 _pluginRenderThread = new Thread ( PluginRenderThread ) ;
@@ -136,9 +126,46 @@ public void Dispose()
136126 DebugLog ( $ "[{ nameof ( PluginTextureRenderer ) } ] Disposed") ;
137127 }
138128
139- public void SetUserData ( uint userData )
129+ /// <summary>
130+ /// Runs on Unity main thread.
131+ /// </summary>
132+ /// <param name="width"></param>
133+ /// <param name="height"></param>
134+ /// <returns></returns>
135+ public IntPtr CreateTextureBuffer ( int width , int height )
140136 {
141- _userData = userData ;
137+ _updated = false ;
138+
139+ _textureBufferPtr = IntPtr . Zero ;
140+ Marshal . FreeHGlobal ( _textureUpdateParamsPtr ) ;
141+
142+ if ( _textureBuffer != null )
143+ {
144+ _textureBuffer = null ;
145+ _textureBufferHandle . Free ( ) ;
146+ }
147+
148+ UnityEngine . Object . Destroy ( _targetTexture ) ;
149+
150+ _targetTexture = new Texture2D ( width , height , TextureFormat . RGBA32 , false ) ;
151+ _textureWidth = width ;
152+ _textureHeight = height ;
153+
154+ if ( _rawTextureDataUpdateCallback != null )
155+ {
156+ _textureBuffer = new uint [ _targetTexture . width * _targetTexture . height ] ;
157+ _textureBufferHandle = GCHandle . Alloc ( _textureBuffer , GCHandleType . Pinned ) ;
158+ _textureBufferPtr = _textureBufferHandle . AddrOfPinnedObject ( ) ;
159+ }
160+
161+ if ( _customTextureUpdateCallback != null )
162+ {
163+ _textureUpdateParamsPtr = Marshal . AllocHGlobal ( Marshal . SizeOf ( _textureUpdateParams ) ) ;
164+ }
165+
166+ DebugLog ( $ "[{ nameof ( PluginTextureRenderer ) } ] Create texture: { _textureWidth } x{ _textureHeight } [pixels]") ;
167+
168+ return _textureBufferPtr ;
142169 }
143170
144171 public IntPtr GetTextureBufferPtr ( )
@@ -154,6 +181,11 @@ public IntPtr GetTextureBufferPtr()
154181 }
155182 }
156183
184+ public void SetUserData ( uint userData )
185+ {
186+ _userData = userData ;
187+ }
188+
157189 /// <summary>
158190 /// Runs on another thread.
159191 /// </summary>
0 commit comments