From 1c134fb2722100cddaae1ef6d63ff3eefbe182a4 Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Wed, 28 Oct 2015 20:43:01 +0000 Subject: [PATCH] Added UIImage Crop shader style --HG-- branch : develop_4.6 --- Materials.meta | 5 ++ Materials/UIImageCropMaterial.mat | Bin 0 -> 4264 bytes Materials/UIImageCropMaterial.mat.meta | 4 ++ Scripts/Primitives.meta | 5 ++ Scripts/UIImageCrop.cs | 63 +++++++++++++++++++++++ Scripts/UIImageCrop.cs.meta | 8 +++ Shaders.meta | 5 ++ Shaders/UIImageCrop.shader | 67 +++++++++++++++++++++++++ Shaders/UIImageCrop.shader.meta | 5 ++ 9 files changed, 162 insertions(+) create mode 100644 Materials.meta create mode 100644 Materials/UIImageCropMaterial.mat create mode 100644 Materials/UIImageCropMaterial.mat.meta create mode 100644 Scripts/Primitives.meta create mode 100644 Scripts/UIImageCrop.cs create mode 100644 Scripts/UIImageCrop.cs.meta create mode 100644 Shaders.meta create mode 100644 Shaders/UIImageCrop.shader create mode 100644 Shaders/UIImageCrop.shader.meta diff --git a/Materials.meta b/Materials.meta new file mode 100644 index 0000000..d0db19e --- /dev/null +++ b/Materials.meta @@ -0,0 +1,5 @@ +fileFormatVersion: 2 +guid: 9c13e9266c2d4ed4d9dfe8d643b19d09 +folderAsset: yes +DefaultImporter: + userData: diff --git a/Materials/UIImageCropMaterial.mat b/Materials/UIImageCropMaterial.mat new file mode 100644 index 0000000000000000000000000000000000000000..c1fa6e75f98d673efb940b41b4c9811703216bf4 GIT binary patch literal 4264 zcmeHKOOqQl5Y_@AVG~G5cs~u{{a8pg>bLU+0w|%Iw$`C zs`4|Ed#;?|%8dj6fjd`Bf8C>5c~%K$s@zpG(^7xxZhdNLW|W#=QtH?}rPLGjq=!;x z7S1l5@19csOeT{#YClWYb?uBzbYHFL+$b_kfP_4X+gPbX^cbdj(o0RJmZVM%+BY_S zG@I_rq+=R=-OJTu1c5^co?dUYoV~QxNnB>vcAX(7Sz4#@Eo-`Zqou7$o%rG?kFW#p zVosqg0UJr*G_P_TPOFR~kp$GzZmR$xA&4X+AWlXi@8!@;Y5FNmAG18bWA;Wx<3C;j zp}8;q!3ZdKHc5Ll`>*vtm_|$pXE~PVvbB1o#(i5d45lpo>UL%YrJGxh151jDys6D-AwFrXOKv-0{ z+04?80*`M5P^e&>w-rI%&>HNkLgkJ1W(Dtg9khef?PLJ*8SXn>-im;}nRaTI&yII0b~MF4V*9%hdl2YGx0_Qz;uw3cg2d#)NbgtdXoitK zh}flk8^~9x_rnl0mbI?dz(<4uaZDazJj>+)@i7@8o?YnE)w_T$lF-FNs`nE?mv9WL zR_~_~Of~aUMM#W~R`2P+iO+tzdKV)IVY#SVycf>|ph7K9iq)*2S)ux~5HuDpstKPZ z3?~aLVfImd4h9mREc9ur&!dZk5##?nC@wH~4~}6~sxL+`)yzL9jL$qu^`*dx&wiTf z%Mk?gck_#tWq(ZIZB-9|O4WE@$(mT!uLSlvvO~n)WBU^K_J68i?*mmg@?Y+s4k z(S>Q%4%eWndvxJ#g$w=a_xAkHcmDis?e64{zkWHpDs46P68p8`Ikgmk3_?_#JM44|hr}F5a(m W`v+;?`StHQd~qMY95K%OkmtXFvGPm+ literal 0 HcmV?d00001 diff --git a/Materials/UIImageCropMaterial.mat.meta b/Materials/UIImageCropMaterial.mat.meta new file mode 100644 index 0000000..649017b --- /dev/null +++ b/Materials/UIImageCropMaterial.mat.meta @@ -0,0 +1,4 @@ +fileFormatVersion: 2 +guid: 7919197bf9b9d854fa3b0b0c116c2152 +NativeFormatImporter: + userData: diff --git a/Scripts/Primitives.meta b/Scripts/Primitives.meta new file mode 100644 index 0000000..a2295eb --- /dev/null +++ b/Scripts/Primitives.meta @@ -0,0 +1,5 @@ +fileFormatVersion: 2 +guid: f657b7093c5ea7b42897453c086b7cef +folderAsset: yes +DefaultImporter: + userData: diff --git a/Scripts/UIImageCrop.cs b/Scripts/UIImageCrop.cs new file mode 100644 index 0000000..22538ae --- /dev/null +++ b/Scripts/UIImageCrop.cs @@ -0,0 +1,63 @@ +/// Credit 00christian00 +/// Sourced from - http://forum.unity3d.com/threads/any-way-to-show-part-of-an-image-without-using-mask.360085/#post-2332030 + + +namespace UnityEngine.UI.Extensions +{ + [AddComponentMenu("UI/Extensions/UIImageCrop")] + [ExecuteInEditMode] + [RequireComponent(typeof(RectTransform))] + public class UIImageCrop : MonoBehaviour + { + MaskableGraphic mGraphic; + Material mat; + int XCropProperty, YCropProperty; + public float XCrop = 0f; + public float YCrop = 0f; + + + // Use this for initialization + void Start() + { + + SetMaterial(); + } + public void SetMaterial() + { + mGraphic = this.GetComponent(); + XCropProperty = Shader.PropertyToID("_XCrop"); + YCropProperty = Shader.PropertyToID("_YCrop"); + if (mGraphic != null) + { + mat = mGraphic.material; + + } + else Debug.LogError("Please attach a UI component"); + } + public void OnValidate() + { + SetMaterial(); + SetXCrop(XCrop); + SetYCrop(YCrop); + } + /// + /// Set the x crop factor, with x being a normalized value 0-1f. + /// + /// + public void SetXCrop(float xcrop) + { + XCrop = Mathf.Clamp01(xcrop); + mat.SetFloat(XCropProperty, XCrop); + } + + /// + /// Set the y crop factor, with y being a normalized value 0-1f. + /// + /// + public void SetYCrop(float ycrop) + { + YCrop = Mathf.Clamp01(ycrop); + mat.SetFloat(YCropProperty, YCrop); + } + } +} \ No newline at end of file diff --git a/Scripts/UIImageCrop.cs.meta b/Scripts/UIImageCrop.cs.meta new file mode 100644 index 0000000..ac959b0 --- /dev/null +++ b/Scripts/UIImageCrop.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 022c3983095dce44c9504739a8ed7324 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Shaders.meta b/Shaders.meta new file mode 100644 index 0000000..d4a4df4 --- /dev/null +++ b/Shaders.meta @@ -0,0 +1,5 @@ +fileFormatVersion: 2 +guid: 0873351fdb3a65f43901b3ec088375b0 +folderAsset: yes +DefaultImporter: + userData: diff --git a/Shaders/UIImageCrop.shader b/Shaders/UIImageCrop.shader new file mode 100644 index 0000000..c225403 --- /dev/null +++ b/Shaders/UIImageCrop.shader @@ -0,0 +1,67 @@ +/// Credit 00christian00 +/// Sourced from - http://forum.unity3d.com/threads/any-way-to-show-part-of-an-image-without-using-mask.360085/#post-2332030 + +Shader "UI Extensions/UI Image Crop" { + Properties + { + _MainTex ("Base (RGB)", 2D) = "white" {} + + _XCrop ("X Crop", Range(0.0,1.0)) = 1 + _YCrop ("Y Crop", Range(0.0,1.0)) = 1 + } + + SubShader { + + ZWrite Off + Tags + { + "Queue" = "Transparent" + "RenderType" = "Transparent" + "IgnoreProjector" = "True" + } + Blend SrcAlpha OneMinusSrcAlpha + + Pass { + + CGPROGRAM + + #pragma vertex vert + #pragma fragment frag + #include "UnityCG.cginc" + + struct v2f { + float4 pos : POSITION; + fixed4 color : COLOR; + float2 uv : TEXCOORD0; //UV1 coord + }; + + uniform sampler2D _MainTex; + float4 _MainTex_ST; + uniform float _XCrop; + uniform float _YCrop; + + v2f vert (v2f v) + { + + v2f o; + o.color=v.color; + o.color.a=0.1; + o.pos = mul (UNITY_MATRIX_MVP, v.pos); + + o.uv=TRANSFORM_TEX(v.uv, _MainTex); + + return o; + } + fixed4 frag (v2f i) : COLOR + { + + //return fixed4(0.25,0,0,1); + i.color.a=step(i.uv.x,_XCrop); + //I don't like the bottom up ordering,so I reverse it + i.color.a=i.color.a*step(1-i.uv.y,_YCrop); + return i.color * tex2D (_MainTex, i.uv); + } + ENDCG + } + } +} diff --git a/Shaders/UIImageCrop.shader.meta b/Shaders/UIImageCrop.shader.meta new file mode 100644 index 0000000..583a0f6 --- /dev/null +++ b/Shaders/UIImageCrop.shader.meta @@ -0,0 +1,5 @@ +fileFormatVersion: 2 +guid: ccf5a0c8f87d3c547aff3daecb3164a4 +ShaderImporter: + defaultTextures: [] + userData: