Skip to content

Commit 5643cc4

Browse files
committed
Upgrade modules to use command buffers when rendering appearances
1 parent 409ec38 commit 5643cc4

6 files changed

Lines changed: 53 additions & 35 deletions

File tree

OpenTibia/Assets/Scripts/Core/Appearances/AppearanceInstance.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ public CachedSprite GetSprite(int layer, int patternX, int patternY, int pattern
157157

158158
return cachedRequest.CachedSprite;
159159
}
160-
161-
public void Draw(Vector2Int screenPosition, Vector2 zoom, int patternX, int patternY, int patternZ, bool highlighted = false, float highlightOpacity = 0) {
162-
Draw(null, screenPosition, zoom, patternX, patternY, patternZ, highlighted, highlightOpacity);
163-
}
164160

165161
public virtual void Draw(CommandBuffer commandBuffer, Vector2Int screenPosition, Vector2 zoom,
166162
int patternX, int patternY, int patternZ, bool highlighted = false, float highlightOpacity = 0) {

OpenTibia/Assets/Scripts/Core/Components/SplitStackWindow.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using UnityEngine.Events;
44
using UnityEngine.UI;
55

6+
using CommandBuffer = UnityEngine.Rendering.CommandBuffer;
7+
68
namespace OpenTibiaUnity.Core.Components
79
{
810
public class SplitStackWindow : Base.Window
@@ -112,17 +114,20 @@ protected void OnGUI() {
112114
_itemImage.texture = _renderTexture;
113115
}
114116

115-
RenderTexture.active = _renderTexture;
116-
Utils.GraphicsUtility.ClearWithTransparency();
117+
var commandBuffer = new CommandBuffer();
118+
commandBuffer.SetRenderTarget(_renderTexture);
119+
commandBuffer.ClearRenderTarget(false, true, Core.Utils.GraphicsUtility.TransparentColor);
120+
117121
if (!!_objectType) {
118122
if (_objectInstance == null || _objectInstance.Id != _objectType.Id)
119123
_objectInstance = OpenTibiaUnity.AppearanceStorage.CreateObjectInstance(_objectType.Id, _objectAmount);
120124

121125
var zoom = new Vector2(Screen.width / (float)_renderTexture.width, Screen.height / (float)_renderTexture.height);
122-
_objectInstance.Draw(Vector2Int.zero, zoom, 0, 0, 0);
126+
_objectInstance.Draw(commandBuffer, Vector2Int.zero, zoom, 0, 0, 0);
123127
}
124128

125-
RenderTexture.active = null;
129+
Graphics.ExecuteCommandBuffer(commandBuffer);
130+
commandBuffer.Dispose();
126131
}
127132
}
128133
}

OpenTibia/Assets/Scripts/Modules/Container/ContainerWindow.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using UnityEngine;
77
using UnityEngine.UI;
88

9+
using CommandBuffer = UnityEngine.Rendering.CommandBuffer;
10+
911
namespace OpenTibiaUnity.Modules.Container
1012
{
1113
public class ContainerWindow : Core.Components.Base.MiniWindow, IUseWidget, IMoveWidget, IWidgetContainerWidget
@@ -44,10 +46,11 @@ protected void OnGUI() {
4446
if (!_slotsRenderTexture)
4547
return;
4648

47-
Vector2 zoom = new Vector2(Screen.width / (float)_slotsRenderTexture.width, Screen.height / (float)_slotsRenderTexture.height);
49+
var zoom = new Vector2(Screen.width / (float)_slotsRenderTexture.width, Screen.height / (float)_slotsRenderTexture.height);
4850

49-
RenderTexture.active = _slotsRenderTexture;
50-
Core.Utils.GraphicsUtility.ClearWithTransparency();
51+
var commandBuffer = new CommandBuffer();
52+
commandBuffer.SetRenderTarget(_slotsRenderTexture);
53+
commandBuffer.ClearRenderTarget(false, true, Core.Utils.GraphicsUtility.TransparentColor);
5154
for (int i = 0; i < 4; i++) {
5255
for (int j = 0; j < _rows; j++) {
5356
int index = j * 4 + i;
@@ -57,7 +60,7 @@ protected void OnGUI() {
5760
if (!@object.ClampeToFieldSize)
5861
@object.ClampeToFieldSize = true;
5962
@object.Animate(OpenTibiaUnity.TicksMillis);
60-
@object.Draw(new Vector2Int(Constants.FieldSize * i, Constants.FieldSize * j), zoom, 0, 0, 0);
63+
@object.Draw(commandBuffer, new Vector2Int(Constants.FieldSize * i, Constants.FieldSize * j), zoom, 0, 0, 0);
6164
}
6265
}
6366
}
@@ -67,9 +70,10 @@ protected void OnGUI() {
6770
int iconRow = _numberOfSlots / 4;
6871
if (!_containerView.Icon.ClampeToFieldSize)
6972
_containerView.Icon.ClampeToFieldSize = true;
70-
_containerView.Icon.Draw(new Vector2Int(Constants.FieldSize * iconColumn, Constants.FieldSize * iconRow), zoom, 0, 0, 0);
73+
_containerView.Icon.Draw(commandBuffer, new Vector2Int(Constants.FieldSize * iconColumn, Constants.FieldSize * iconRow), zoom, 0, 0, 0);
7174

72-
RenderTexture.active = null;
75+
Graphics.ExecuteCommandBuffer(commandBuffer);
76+
commandBuffer.Dispose();
7377
}
7478

7579
protected override void OnDestroy() {

OpenTibia/Assets/Scripts/Modules/Hotkeys/HotkeysWindow.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using UnityEngine;
77
using UnityEngine.UI;
88

9+
using CommandBuffer = UnityEngine.Rendering.CommandBuffer;
10+
911
namespace OpenTibiaUnity.Modules.Hotkeys
1012
{
1113
public class HotkeysWindow : Core.Components.Base.Window {
@@ -18,7 +20,7 @@ public class HotkeysWindow : Core.Components.Base.Window {
1820
private const KeyCode StartKeyCode = KeyCode.F1;
1921
private const KeyCode EndKeyCode = KeyCode.F12;
2022

21-
private static RenderTexture s_RenderTexture;
23+
private static RenderTexture s_renderTexture;
2224

2325
public const string TextUseOnYourself = "<color=#AFFEAF>{0}: (use object on yourself)</color>";
2426
public const string TextUseOnTarget = "<color=#FEAFAF>{0}: (use object on target)</color>";
@@ -126,18 +128,21 @@ protected void OnGUI() {
126128
return;
127129
}
128130

129-
if (s_RenderTexture == null) {
130-
s_RenderTexture = new RenderTexture(Constants.FieldSize, Constants.FieldSize, 0, RenderTextureFormat.ARGB32);
131-
s_RenderTexture.filterMode = FilterMode.Point;
131+
if (s_renderTexture == null) {
132+
s_renderTexture = new RenderTexture(Constants.FieldSize, Constants.FieldSize, 0, RenderTextureFormat.ARGB32);
133+
s_renderTexture.filterMode = FilterMode.Point;
132134

133-
_objectImage.texture = s_RenderTexture;
135+
_objectImage.texture = s_renderTexture;
134136
}
135137

136-
RenderTexture.active = s_RenderTexture;
137-
Core.Utils.GraphicsUtility.ClearWithTransparency();
138-
var zoom = new Vector2(Screen.width / (float)s_RenderTexture.width, Screen.height / (float)s_RenderTexture.height);
139-
_objectInstance.Draw(new Vector2Int(0, 0), zoom, 0, 0, 0);
140-
RenderTexture.active = null;
138+
var commandBuffer = new CommandBuffer();
139+
commandBuffer.SetRenderTarget(s_renderTexture);
140+
commandBuffer.ClearRenderTarget(false, true, Core.Utils.GraphicsUtility.TransparentColor);
141+
142+
var zoom = new Vector2(Screen.width / (float)s_renderTexture.width, Screen.height / (float)s_renderTexture.height);
143+
_objectInstance.Draw(commandBuffer, new Vector2Int(0, 0), zoom, 0, 0, 0);
144+
Graphics.ExecuteCommandBuffer(commandBuffer);
145+
commandBuffer.Dispose();
141146

142147
if (!_objectImage.enabled)
143148
_objectImage.enabled = true;

OpenTibia/Assets/Scripts/Modules/Inventory/InventoryWindow.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using UnityEngine.EventSystems;
77
using UnityEngine.UI;
88

9+
using CommandBuffer = UnityEngine.Rendering.CommandBuffer;
10+
911
namespace OpenTibiaUnity.Modules.Inventory
1012
{
1113
public class InventoryWindow : Core.Components.Base.MiniWindow, IUseWidget, IMoveWidget, IWidgetContainerWidget
@@ -185,19 +187,21 @@ private void OnGUI() {
185187

186188
Vector2 zoom = new Vector2(Screen.width / (float)_slotsRenderTexture.width, Screen.height / (float)_slotsRenderTexture.height);
187189

188-
RenderTexture.active = _slotsRenderTexture;
189-
Core.Utils.GraphicsUtility.ClearWithTransparency();
190+
var commandBuffer = new CommandBuffer();
191+
commandBuffer.SetRenderTarget(_slotsRenderTexture);
192+
commandBuffer.ClearRenderTarget(false, true, Core.Utils.GraphicsUtility.TransparentColor);
190193
for (int i = 0; i < (int)ClothSlots.Hip; i++) {
191194
var @object = BodyContainerView.Objects[i];
192195
if (@object) {
193196
if (!@object.ClampeToFieldSize)
194197
@object.ClampeToFieldSize = true;
195198
@object.Animate(OpenTibiaUnity.TicksMillis);
196-
@object.Draw(new Vector2Int(Constants.FieldSize * i, 0), zoom, 0, 0, 0);
199+
@object.Draw(commandBuffer, new Vector2Int(Constants.FieldSize * i, 0), zoom, 0, 0, 0);
197200
}
198201
}
199202

200-
RenderTexture.active = null;
203+
Graphics.ExecuteCommandBuffer(commandBuffer);
204+
commandBuffer.Dispose();
201205
}
202206

203207
protected override void OnDestroy() {

OpenTibia/Assets/Scripts/Modules/Outfit/OutfitWindow.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using UnityEngine;
77
using UnityEngine.UI;
88

9+
using CommandBuffer = UnityEngine.Rendering.CommandBuffer;
10+
911
namespace OpenTibiaUnity.Modules.Outfit
1012
{
1113
public class OutfitWindow : Core.Components.Base.Window
@@ -120,8 +122,9 @@ protected void OnGUI() {
120122
_rawImageMount.texture = _renderTexture;
121123
}
122124

123-
RenderTexture.active = _renderTexture;
124-
Core.Utils.GraphicsUtility.ClearWithTransparency();
125+
var commandBuffer = new CommandBuffer();
126+
commandBuffer.SetRenderTarget(_renderTexture);
127+
commandBuffer.ClearRenderTarget(false, true, Core.Utils.GraphicsUtility.TransparentColor);
125128

126129
if (!!_currentOutfit) {
127130
var screenPosition = new Vector2Int(Constants.FieldSize, Constants.FieldSize);
@@ -133,9 +136,9 @@ protected void OnGUI() {
133136
}
134137

135138
if (_currentOutfit is OutfitInstance)
136-
_currentOutfit.Draw(screenPosition, zoom, (int)_currentDirection, 0, 0);
139+
_currentOutfit.Draw(commandBuffer, screenPosition, zoom, (int)_currentDirection, 0, 0);
137140
else
138-
_currentOutfit.Draw(screenPosition, zoom, 0, 0, 0);
141+
_currentOutfit.Draw(commandBuffer, screenPosition, zoom, 0, 0, 0);
139142

140143
if (!_rawImageOutfit.enabled)
141144
_rawImageOutfit.enabled = true;
@@ -149,15 +152,16 @@ protected void OnGUI() {
149152
screenPosition += new Vector2Int(Constants.FieldSize * 2, 0);
150153

151154
if (_currentMount is OutfitInstance)
152-
_currentMount.Draw(screenPosition, zoom, (int)_currentDirection, 0, 0);
155+
_currentMount.Draw(commandBuffer, screenPosition, zoom, (int)_currentDirection, 0, 0);
153156
else
154-
_currentMount.Draw(screenPosition, zoom, 0, 0, 0);
157+
_currentMount.Draw(commandBuffer, screenPosition, zoom, 0, 0, 0);
155158

156159
if (!_rawImageMount.enabled)
157160
_rawImageMount.enabled = true;
158161
}
159162

160-
RenderTexture.active = null;
163+
Graphics.ExecuteCommandBuffer(commandBuffer);
164+
commandBuffer.Dispose();
161165
}
162166

163167
protected override void OnEnable() {

0 commit comments

Comments
 (0)