From beb0abe774750af4a7fa1a2d9310785f0e044d8d Mon Sep 17 00:00:00 2001 From: dmytro lytovchenko Date: Sun, 23 Jun 2019 23:51:41 +0200 Subject: [PATCH 01/26] WIP experimental world space canvas --- .../WorldSpaceGUI/SeeThroughZ.shader | 92 ++++++++++++ TLM/TLM/TLM.csproj | 2 + TLM/TLM/UI/CanvasGUI/WorldSpaceUI.cs | 132 ++++++++++++++++++ TLM/TLM/UI/SubTools/LaneArrowTool.cs | 46 +++++- 4 files changed, 265 insertions(+), 7 deletions(-) create mode 100644 TLM/TLM/Resources/WorldSpaceGUI/SeeThroughZ.shader create mode 100644 TLM/TLM/UI/CanvasGUI/WorldSpaceUI.cs diff --git a/TLM/TLM/Resources/WorldSpaceGUI/SeeThroughZ.shader b/TLM/TLM/Resources/WorldSpaceGUI/SeeThroughZ.shader new file mode 100644 index 000000000..724b3a2a0 --- /dev/null +++ b/TLM/TLM/Resources/WorldSpaceGUI/SeeThroughZ.shader @@ -0,0 +1,92 @@ +// This shader renders UI through the world geometry +// +Shader "Custom/UI Overlay SH" +{ + Properties + { + [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {} + _Color("Tint", Color) = (1,1,1,1) + + _StencilComp("Stencil Comparison", Float) = 8 + _Stencil("Stencil ID", Float) = 0 + _StencilOp("Stencil Operation", Float) = 0 + _StencilWriteMask("Stencil Write Mask", Float) = 255 + _StencilReadMask("Stencil Read Mask", Float) = 255 + + _ColorMask("Color Mask", Float) = 15 + } + + SubShader + { + Tags + { + "Queue" = "Overlay" + "IgnoreProjector" = "True" + "RenderType" = "Transparent" + "PreviewType" = "Plane" + "CanUseSpriteAtlas" = "True" + } + + Stencil + { + Ref[_Stencil] + Comp[_StencilComp] + Pass[_StencilOp] + ReadMask[_StencilReadMask] + WriteMask[_StencilWriteMask] + } + + Cull Off + Lighting Off + ZWrite Off + ZTest Off + Blend SrcAlpha OneMinusSrcAlpha + ColorMask[_ColorMask] + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + #include "UnityCG.cginc" + + struct appdata_t + { + float4 vertex : POSITION; + float4 color : COLOR; + float2 texcoord : TEXCOORD0; + }; + + struct v2f + { + float4 vertex : SV_POSITION; + fixed4 color : COLOR; + half2 texcoord : TEXCOORD0; + }; + + fixed4 _Color; + + v2f vert(appdata_t IN) + { + v2f OUT; + OUT.vertex = UnityObjectToClipPos(IN.vertex); + OUT.texcoord = IN.texcoord; +#ifdef UNITY_HALF_TEXEL_OFFSET + OUT.vertex.xy += (_ScreenParams.zw - 1.0)*float2(-1,1); +#endif + OUT.color = IN.color * _Color; + return OUT; + } + + sampler2D _MainTex; + + fixed4 frag(v2f IN) : SV_Target + { + half4 color = tex2D(_MainTex, IN.texcoord) * IN.color; + clip(color.a - 0.01); + return color; + } + ENDCG + } + } +} diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index f5799538a..2d9122b6b 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -229,6 +229,7 @@ + @@ -454,6 +455,7 @@ + diff --git a/TLM/TLM/UI/CanvasGUI/WorldSpaceUI.cs b/TLM/TLM/UI/CanvasGUI/WorldSpaceUI.cs new file mode 100644 index 000000000..3cb88f88a --- /dev/null +++ b/TLM/TLM/UI/CanvasGUI/WorldSpaceUI.cs @@ -0,0 +1,132 @@ +using System; +using System.Reflection; +using CSUtil.Commons; +using UnityEngine; +using UnityEngine.UI; +using Object = System.Object; + +namespace TrafficManager.UI.CanvasGUI { + public class WorldSpaceUI + { + private GameObject canvasGameObj_; + private ulong counter_; + private Shader seeThroughShader_; + + public WorldSpaceUI(Vector3 pos, Quaternion rot) { + // seeThroughShader_ = Resources.Load("WorldSpaceGUI.SeeThroughZ"); + + canvasGameObj_ = new GameObject(); + canvasGameObj_.name = "WorldSpaceUI Canvas " + (counter_++); + + var canvasComponent = canvasGameObj_.AddComponent(); + canvasComponent.renderMode = RenderMode.WorldSpace; + + var scalerComponent = canvasGameObj_.AddComponent(); + canvasGameObj_.AddComponent(); + + // var rtComponent = canvasGameObj_.GetComponent(); + // rtComponent.rotation = rot; + // rtComponent.position = pos; + // rtComponent.localScale = new Vector3(1f, 1f, 1f); + + scalerComponent.dynamicPixelsPerUnit = 2f; + scalerComponent.referencePixelsPerUnit = 1f; + + canvasComponent.worldCamera = Camera.main; + canvasGameObj_.transform.SetPositionAndRotation(pos, rot); + canvasGameObj_.transform.localScale = new Vector3(1f, 1f, 1f); + } + + public void DestroyCanvas() { + // canvasGO_.transform.SetParent(null); + UnityEngine.Object.Destroy(canvasGameObj_); + canvasGameObj_ = null; + } + + private RectTransform SetupRectTransform(Transform c, Vector3 pos, Vector2 size) { + var rectTransform = c.GetComponent(); + rectTransform.localPosition = pos; + rectTransform.sizeDelta = size; + + c.localScale = new Vector3(1f, 1f, 1f); + c.rotation = Quaternion.identity; + return rectTransform; + } + + public GameObject AddText(Vector3 pos, Vector2 size, string str, GameObject parent = null) { + // Text + var textGameObj = new GameObject(); + textGameObj.transform.SetParent(parent == null ? canvasGameObj_.transform : parent.transform); + textGameObj.name = "Text " + (counter_++); + + var textComponent = textGameObj.AddComponent(); + // C:S fonts: OpenSans-Regular, OpenSans-Semibold. NanumGothic and ArchitectsDaughter + textComponent.font = Resources.GetBuiltinResource("OpenSans-Regular"); + textComponent.text = str; + textComponent.fontSize = 10; + textComponent.color = Color.black; + SetupMaterial(textComponent.material); + + // Text position + SetupRectTransform(textComponent.GetComponent(), pos, size); + return textGameObj; + } + + private void SetupMaterial(Material material) { + // material.shader = seeThroughShader_; + } + + public GameObject AddButton(Vector3 pos, Vector2 size, + string str = "", + GameObject parent = null) { + // Add the button object + var buttonGO = new GameObject(); + buttonGO.transform.SetParent(parent == null ? canvasGameObj_.transform : parent.transform); + buttonGO.name = "Button " + (counter_++); + + //----------------- + + buttonGO.AddComponent(); + + // var imageComponent = buttonGO.AddComponent(); + + var imageComponent = buttonGO.AddComponent(); + var buttonComponent = buttonGO.AddComponent