Skip to content

Commit

Permalink
V1.1, Optimizing update
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogsther committed Feb 28, 2021
1 parent f6bf9cc commit 4967b60
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/Game.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.37661862, g: 0.34757808, b: 0.32425174, a: 1}
m_IndirectSpecularColor: {r: 0.46172708, g: 0.4317774, b: 0.3634297, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down
72 changes: 51 additions & 21 deletions Assets/Scripts/ItemRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
public class ItemRenderer : MonoBehaviour
{

// Optimize update 1.1 (Item thumbnails caching)
// The majority of the items in the game do not have random colors.
// For all items that do not have random colors we can save the rendered image so that
// we do not need to render it again every time the market or inventory is loaded.
// Rendering the thumbnails is the heaviest part of the game and > 100 renders at one time will
// crash on iPhone 8.

public Dictionary<int, Texture> cachedRenders = new Dictionary<int, Texture>();

public Camera itemCamera;
public Transform container;
public RenderTexture texture;
Expand All @@ -29,6 +38,47 @@ public class ItemRenderer : MonoBehaviour

Quaternion containerDefaultRotation;

public IEnumerator RenderItem(Item item, IndexedItem origin, RawImage destination) {
WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();

InitiateRenderItem(item);

Texture2D render = new Texture2D(itemCamera.targetTexture.width, itemCamera.targetTexture.height);

RenderTexture.active = texture;

itemCamera.targetTexture = texture;
itemCamera.Render();

render.ReadPixels(new Rect(0, 0, itemCamera.targetTexture.width, itemCamera.targetTexture.height), 0, 0);
render.Apply();

destination.texture = render;

// Check if this item type has random colors. If it has we can't cache the render
// because all items of this type has diffrent colors.
if (!origin.pattern) {
cachedRenders.Add(origin.id, render);
}

yield return endOfFrame;
}

public void SetItemThumbnail(Item item, RawImage destination) {
IndexedItem origin = online.GetIndexedItem(item.item);


// Check if this item render is cached (if it has unique pattern it will never be cached so
// we don't have to check that)
if (cachedRenders.ContainsKey(origin.id)) {
destination.texture = cachedRenders[origin.id];
Debug.Log("Used cached render");
} else {
Debug.Log("Rendered new");
StartCoroutine(RenderItem(item, origin, destination));
}

}

void Start() {

Expand Down Expand Up @@ -73,27 +123,7 @@ public GameObject InitiateFinishedItem(Item item, Transform parent) {
return model;
}

public IEnumerator RenderItem(Item item, RawImage destination) {

WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();

InitiateRenderItem(item);

Texture2D render = new Texture2D(itemCamera.targetTexture.width, itemCamera.targetTexture.height);

RenderTexture.active = texture;

itemCamera.targetTexture = texture;
itemCamera.Render();

render.ReadPixels(new Rect(0, 0, itemCamera.targetTexture.width, itemCamera.targetTexture.height), 0, 0);
render.Apply();

destination.texture = render;

yield return endOfFrame;

}



}
34 changes: 12 additions & 22 deletions Assets/Scripts/MarketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class MarketManager : MonoBehaviour
public int activeItemListing = -1;


/**
* OnListings is when the user has clicked on an item on the left side of the market
* This will load all items of that type and present them on the right side of the market
* view.
*/
public void OnListings(string data) {
ItemListings listings = JsonConvert.DeserializeObject<ItemListings>(data);
IndexedItem origin = connection.GetIndexedItem(listings.items[0].item.item);
Expand All @@ -61,24 +66,15 @@ public void OnListings(string data) {

ClearTransform(listingsContent);

RawImage firstThumb = null;

for(int i = 0; i < listings.items.Length; i++) {

ItemListing listing = listings.items[i];

Transform listingObject = Instantiate(listingPrefab, listingsContent).transform;

RawImage thumbnail = listingObject.Find("Thumbnail").GetComponent<RawImage>();
if (i == 0) firstThumb = thumbnail;

if(!origin.pattern && i > 0) {
thumbnail.texture = firstThumb.texture;
} else {
StartCoroutine(renderer.RenderItem(listing.item, thumbnail));
}


renderer.SetItemThumbnail(listing.item, thumbnail);

listingObject.Find("Seller").GetComponent<Text>().text = listing.seller;
listingObject.Find("ListingPrice").GetComponent<Text>().text = listing.item.price.ToString();
Expand All @@ -88,6 +84,10 @@ public void OnListings(string data) {
}
}

/**
* Market front is the left side of the market. This is where all items of the same type
* are grouped together.
*/

public void OnMarketFront(string data) {

Expand All @@ -99,7 +99,7 @@ public void OnMarketFront(string data) {
Transform entry = Instantiate(marketFrontEntryPrefab, marketFrontScrollContent).transform;

RawImage thumbnail = entry.Find("Thumbnail").GetComponent<RawImage>();
StartCoroutine(renderer.RenderItem(frontEntry.item, thumbnail));
renderer.SetItemThumbnail(frontEntry.item, thumbnail);

entry.Find("ItemTitle").GetComponent<Text>().text = origin.name;
entry.Find("ItemBorder").GetComponent<Image>().color = origin.rarity.color;
Expand All @@ -111,6 +111,7 @@ public void OnMarketFront(string data) {
});
}

// Open the first or last opened item listings
if(itemFrontData.Length > 0) {
if (activeItemListing == -1 || !IsItemIndexListed(activeItemListing)) activeItemListing = itemFrontData[0].item.item;
OpenMarketListings(activeItemListing);
Expand Down Expand Up @@ -140,15 +141,4 @@ void ClearTransform(Transform parent) {
public void UpdateMarketFront() {
connection.Send("get_market_front");
}

void Start()
{

}

// Update is called once per frame
void Update()
{

}
}
5 changes: 3 additions & 2 deletions Assets/Scripts/OnlineConnection.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* This script manages the websocket connection
* between the client and server.
*
*/

using System.Collections;
Expand Down Expand Up @@ -67,6 +66,8 @@ public class OnlineConnection : MonoBehaviour
async void Start() {

previousGoldAmount = PlayerPrefs.GetInt("gold");


token = PlayerPrefs.GetString("token");

ws = new WebSocket("wss://dungeon.ygstr.com");
Expand Down Expand Up @@ -272,7 +273,7 @@ void LoadInventory() {
// Set color of item border
itemSlot.transform.Find("Border").GetComponent<Image>().color = origin.rarity.color;

StartCoroutine(renderer.RenderItem(inventory[i], itemSlot.GetComponentInChildren<RawImage>()));
renderer.SetItemThumbnail(inventory[i], itemSlot.GetComponentInChildren<RawImage>());
}


Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/OnlineRaidManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void OnRaidEnd(string json) {
im.Inspect(item, true);
});

StartCoroutine(renderer.RenderItem(item, earnedLootSlot.Find("Thumbnail").GetComponent<RawImage>()));
renderer.SetItemThumbnail(item, earnedLootSlot.Find("Thumbnail").GetComponent<RawImage>());
}

goldDispalyed = 0;
Expand Down

0 comments on commit 4967b60

Please sign in to comment.