├── DisolveController.cs ├── DisolveShader.shader ├── LICENSE └── README.md /DisolveController.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class DisolveController : MonoBehaviour { 5 | 6 | public float disolveFreq = 1f; //The noise frequency 7 | public float disolveSpeed = 1f; //Disolve effect speed 8 | public Vector3 disolvePoint; //The point to disolve around 9 | float disolveAmount = 0; 10 | public bool disolved = false; //true = Materials in radius disolve; false = reverses the effect 11 | public float maxDist = 25f; //The maximum distance of the disolve effect; 12 | public float distLimitFac = 1f; //Also tweaks the maximum distance in a more flexible way 13 | 14 | public GameObject tracker; //Used as a test to set disolvePoint via mouse movement 15 | 16 | 17 | // Use this for initialization 18 | void Start () { 19 | //Applys values to shaders 20 | Shader.SetGlobalFloat("_DisolveFreq", disolveFreq); 21 | Shader.SetGlobalFloat("_IsoVal", disolveAmount); 22 | Shader.SetGlobalFloat("_MaxDist", maxDist); 23 | Shader.SetGlobalFloat("_DistanceLimitFac", distLimitFac); 24 | } 25 | 26 | // Update is called once per frame 27 | void Update () { 28 | //Toggles Disolve Effect 29 | if(Input.GetKeyDown(KeyCode.Alpha1)) 30 | { 31 | disolved = false; 32 | } 33 | if (Input.GetKeyDown(KeyCode.Alpha2)) 34 | { 35 | disolved = true; 36 | } 37 | 38 | //Lerps the disolveAmount and applys it as IsoValue to the Shader 39 | if(disolved == false) 40 | { 41 | if(disolveAmount > 0) 42 | { 43 | disolveAmount = Mathf.Lerp(disolveAmount, 0f, Time.fixedDeltaTime * disolveSpeed * 0.3f); 44 | Shader.SetGlobalFloat("_IsoVal", disolveAmount); 45 | } 46 | } 47 | else 48 | { 49 | if (disolveAmount < 2) 50 | { 51 | disolveAmount = Mathf.Lerp(disolveAmount, 2f, Time.fixedDeltaTime * disolveSpeed *0.3f); 52 | Shader.SetGlobalFloat("_IsoVal", disolveAmount); 53 | } 54 | } 55 | 56 | 57 | //Sends out a ray and returns the hit point as the DisolvePoint 58 | Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 59 | RaycastHit hit; 60 | 61 | if (Physics.Raycast(ray, out hit, 100)) 62 | { 63 | tracker.transform.position = hit.point; 64 | disolvePoint = new Vector3(hit.point.x*0.2f, hit.point.y * 0.2f, hit.point.z * 0.2f); 65 | Shader.SetGlobalVector("_DisolvePoint", disolvePoint); 66 | } 67 | } 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /DisolveShader.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/DisolveShader" { 2 | Properties { 3 | _Color ("Color", Color) = (1,1,1,1) 4 | _MainTex("Albedo (RGB) Alpha (A)", 2D) = "white" {} 5 | _Glossiness ("Smoothness", Range(0,1)) = 0.5 6 | _Metallic ("Metallic", Range(0,1)) = 0.0 7 | _BumpMap("Bumpmap", 2D) = "bump" {} 8 | [Toggle] _Inverse("inversed", Float) = 0 9 | _CutOut("Alpha Cutout", Float) = 0.5 10 | } 11 | SubShader { 12 | //Tags { "RenderType"="Opaque" } 13 | 14 | Tags{ "Queue" = "AlphaTest" "RenderType" = "TransparentCutout" } //Rendertype to check material for alpha masking 15 | LOD 200 16 | 17 | //Use this to make the inner faces of your model visible trough transparent faces 18 | Cull Off 19 | 20 | 21 | CGPROGRAM 22 | // Physically based Standard lighting model, and enable shadows on all light types 23 | 24 | //#pragma surface surf Standard vertex:vert alphatest:_Cutoff addshadow //Unity Cutout mehtod 25 | 26 | #pragma surface surf Standard fullforwardshadows vertex:vert addshadow 27 | 28 | 29 | // Use shader model 3.0 target, to get nicer looking lighting 30 | #pragma target 3.0 31 | 32 | #include "noiseSimplex.cginc" 33 | 34 | 35 | 36 | 37 | sampler2D _MainTex; 38 | 39 | struct vertexInput { 40 | float4 vertex : POSITION; 41 | }; 42 | 43 | struct Input { 44 | float2 uv_MainTex; 45 | float2 uv_BumpMap; 46 | float4 pos : SV_POSITION; 47 | float3 srcPos : TEXCOORD0; 48 | }; 49 | 50 | half _Glossiness; 51 | half _Metallic; 52 | fixed4 _Color; 53 | sampler2D _BumpMap; 54 | 55 | //The uniform values get controlled via the DisolveCOntroller Script via Shader.SetGlobalFloat 56 | uniform float _DisolveFreq; 57 | uniform float _IsoVal; 58 | uniform float3 _DisolvePoint; 59 | uniform float _MaxDist; 60 | uniform float _DistanceLimitFac; 61 | 62 | 63 | float _Inverse; 64 | float _CutOut; 65 | 66 | Input vert(inout appdata_full v, out Input o) 67 | { 68 | UNITY_INITIALIZE_OUTPUT(Input, o); 69 | 70 | 71 | //Inputs the position of the vertex and mutliplies it by the size of the noise. (_DisolveFreq) 72 | o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 73 | 74 | o.srcPos = mul(unity_ObjectToWorld, v.vertex).xyz; 75 | 76 | o.srcPos *= _DisolveFreq; 77 | 78 | return o; 79 | } 80 | 81 | void surf (Input IN, inout SurfaceOutputStandard o) { 82 | // Albedo comes from a texture tinted by color 83 | fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; 84 | 85 | //Samples the noise from noiseSimplex.cginc 86 | float ns = snoise(IN.srcPos) / 2 + 0.5f; 87 | 88 | //Calculate distance to the disolve point 89 | float dist = distance(IN.srcPos, _DisolvePoint); 90 | 91 | //The maximum value corresponds to the "if (disolveAmount < 2)" line in the controller. Change either one of these, so the maximum here is bigger than the value in the script, 92 | //to stop the effect forming a solid border around the radius. 93 | float q = clamp(dist / _MaxDist, 0, 2); 94 | 95 | //Compute the Iso Value 96 | float compareIso = _IsoVal-(q*_DistanceLimitFac); 97 | 98 | if (_Inverse == 1) { 99 | compareIso = 1.0 - compareIso; 100 | } 101 | 102 | //c.a gives you the ability to use the texture alpha channel as transparency 103 | if (c.a > _CutOut) { 104 | //if noise Value is bigger than iso: Normal Shading 105 | if (ns > compareIso) { 106 | o.Albedo = c.rgb; 107 | o.Emission = float3(0, 0, 0); 108 | 109 | } 110 | //if noise Value is slightly smaller than iso: Glowing Edge 111 | else if (ns + 0.05 > compareIso) { 112 | o.Albedo = float4(0.0, 0.8, 1.0, c.a); 113 | o.Emission = float3(0, 0.5, 0.9); 114 | } 115 | //else discard the values to make a transparent face. 116 | else { 117 | discard; 118 | 119 | } 120 | } 121 | else { 122 | discard; 123 | } 124 | 125 | // Metallic and smoothness come from slider variables 126 | 127 | o.Metallic = _Metallic; 128 | o.Smoothness = _Glossiness; 129 | o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); 130 | } 131 | ENDCG 132 | } 133 | Fallback "Transparent/Cutout/VertexLit" 134 | } 135 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Voleuro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # unity-experiments 2 | This is an implementation of a disolve effect I made for my own game. It's inspired by the article by Glowfish interactive https://madewith.unity.com/stories/dissolving-the-world-part-2 3 | 4 | IMPORTANT NOTICE: 5 | This programm uses the noise implementation Lex-DRL that you can find here: https://forum.unity3d.com/threads/2d-3d-4d-optimised-perlin-noise-cg-hlsl-library-cginc.218372/ It will not work without it. 6 | 7 | You should further notice that I am not a professional programmer and made this in my sparetime. So I can not guarantee you that this code is the most efficient or bug proof it can be. It's only ment for demonstrational purposes. 8 | --------------------------------------------------------------------------------