Skip to content

Commit

Permalink
Generate tilemap editor icons properly (#1219)
Browse files Browse the repository at this point in the history
* Replace AssetPreview with RuntimePreviewGenerator, update RuntimePreviewGenerator

* Implement trojans

* implement computer explosion

* ObjectIcon shader
  • Loading branch information
iamteapot422 authored Jun 30, 2023
1 parent 798ef26 commit fb80904
Show file tree
Hide file tree
Showing 5 changed files with 734 additions and 441 deletions.
79 changes: 79 additions & 0 deletions Assets/Content/Resources/Simple Toon/Shaders/ObjectIcon.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Shader "Unlit/ObjectIcon"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_EmissionMap ("Emission Map", 2D) = "black" {}
[HDR]_EmissionColor ("Emission Color", Color) = (0,0,0,0)

}

SubShader
{
Pass
{
Tags{ "RenderType" = "Opaque" "Queue" = "Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile_fog

#include "UnityCG.cginc"

struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal : NORMAL;
};

struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
half3 worldNormal : TEXCOORD1;
float4 worldPos : TEXCOORD2;
float3 viewDir : TEXCOORD3;
};

sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _EmissionMap;
float4 _EmissionColor;

v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.viewDir = WorldSpaceViewDir(v.vertex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
float3 viewDir = normalize(i.viewDir);
fixed4 texColor = tex2D(_MainTex, i.texcoord);
fixed4 emission = tex2D(_EmissionMap, i.texcoord) * _EmissionColor;

float3 lightVector = (float3(-0.258, 0.22, 0.966));

float NdotL = dot(i.worldNormal, lightVector);
float light = (smoothstep(0, 0.75, NdotL) * 0.525 + 0.475);
float4 shadow = float4(0,0,0.04f,0) * (1 - light);


// float4 rimDot = (1 - dot(viewDir, i.worldNormal));
// rimDot = (rimDot * rimDot * rimDot) * 0.15;

fixed3 color = texColor.rgb * light + shadow + emission;
fixed4 outcolor;
outcolor.rgb = color.rgb;
outcolor.a = texColor.a;
return outcolor;
}
ENDCG
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions Assets/Scripts/SS3D/Systems/Inventory/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,12 @@ public void SetVisibility(bool visible)
{
// TODO: Make this handle multiple renderers, with different states
Renderer[] renderers = GetComponentsInChildren<Renderer>();

foreach (Renderer childRenderer in renderers)
{
childRenderer.enabled = visible;
}
}

public bool IsVisible()
{
// TODO: Make this handle multiple renderers
Expand Down Expand Up @@ -305,15 +304,15 @@ public Sprite GenerateIcon()
{
storedItem.parent = null;
}

Transform previewObject = Instantiate(transform, null, false);
previewObject.gameObject.hideFlags = HideFlags.HideAndDontSave;

previewObject.GetComponent<Item>().SetVisibility(true);
Sprite icon;
try
{
Texture2D texture = RuntimePreviewGenerator.GenerateModelPreviewWithShader(previewObject,
Shader.Find("Legacy Shaders/Diffuse"), null, 128, 128, false, true);
Shader.Find("Legacy Shaders/Diffuse"), null, 128, 128);
icon = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f), 100);
icon.name = transform.name;
Expand Down
33 changes: 10 additions & 23 deletions Assets/Scripts/SS3D/Systems/Tile/TileResourceLoader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using SS3D.Core.Behaviours;
using SS3D.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace SS3D.Systems.Tile
Expand All @@ -31,43 +28,33 @@ private void LoadAssets()

GenericObjectSo[] tempAssets = Resources.LoadAll<GenericObjectSo>("");
StartCoroutine(LoadAssetsWithIcon(tempAssets));
//LoadAssetsWithIcon(tempAssets);
}

private IEnumerator LoadAssetsWithIcon(GenericObjectSo[] assets)
{
List<Texture2D> tempIcons = new List<Texture2D>();

#if UNITY_EDITOR
RuntimePreviewGenerator.OrthographicMode = true;
foreach (var asset in assets)
{
Texture2D texture = AssetPreview.GetAssetPreview(asset.prefab);
yield return new WaitUntil(() => AssetPreview.IsLoadingAssetPreview(asset.GetInstanceID()) == false);


if (texture == null)
{
// Unity is dumb, so we need to reload generated textures...
texture = AssetPreview.GetAssetPreview(asset.prefab);
}
Texture2D texture = RuntimePreviewGenerator.GenerateModelPreviewWithShader(asset.prefab.transform,
Shader.Find("Unlit/ObjectIcon"), null, 128, 128, true, true);

tempIcons.Add(texture);
}
#endif


for (int i = 0; i < assets.Length; i++)
{
#if UNITY_EDITOR

// If we reach this point... Give up and load a default texture instead
if (tempIcons[i] != null)
{
assets[i].icon = Sprite.Create(tempIcons[i], new Rect(0, 0, tempIcons[i].width, tempIcons[i].height), new Vector2(0.5f, 0.5f));
#endif
if (assets[i].icon == null)
}
else
{
assets[i].icon = _missingIcon;

}
_assets.Add(assets[i]);
}

IsInitialized = true;
yield return null;
}
Expand Down
Loading

0 comments on commit fb80904

Please sign in to comment.