├── SunburstOpaque.shader ├── SunburstTransparent.shader ├── README.md └── SunburstEffects.cs /SunburstOpaque.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/SunburstOpaque" { 2 | Properties { 3 | _Color ("Color", Color) = (1, 1, 1, 1) 4 | } 5 | SubShader { 6 | Tags { "RenderType"="Opaque" } 7 | 8 | Cull off 9 | 10 | CGPROGRAM 11 | #pragma surface surf Lambert 12 | 13 | float4 _Color; 14 | 15 | struct Input { 16 | half dummy; 17 | }; 18 | 19 | void surf (Input IN, inout SurfaceOutput o) { 20 | o.Albedo = _Color.rgb; 21 | } 22 | ENDCG 23 | } 24 | FallBack "Diffuse" 25 | } 26 | -------------------------------------------------------------------------------- /SunburstTransparent.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/SunburstTransparent" { 2 | Properties { 3 | _Color ("Color", Color) = (1, 1, 1, 1) 4 | } 5 | SubShader { 6 | Tags { "RenderType"="Transparent" "Queue"="Transparent" } 7 | 8 | Cull off 9 | 10 | CGPROGRAM 11 | #pragma surface surf Lambert alpha 12 | 13 | float4 _Color; 14 | 15 | struct Input { 16 | half dummy; 17 | }; 18 | 19 | void surf (Input IN, inout SurfaceOutput o) { 20 | o.Albedo = _Color.rgb; 21 | o.Alpha = _Color.a; 22 | } 23 | ENDCG 24 | } 25 | FallBack "Diffuse" 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | unity-sunburst-effects 2 | ====================== 3 | 4 | Sunburst effects with mesh animation (Unity, C#) 5 | 6 | ![Screenshot](http://keijiro.github.io/unity-sunburst-effects/Screenshot.png) 7 | 8 | [Demo (Vimeo)](https://vimeo.com/76949095) 9 | 10 | Usage 11 | ----- 12 | 13 | There are four parameters. 14 | 15 | - Beam Count - The number of beams (rays). 16 | - Beam Width - The width of beam. 17 | - Speed - The speed of animation. 18 | - Scale Power - The power (exponent) factor of the scaling function. 19 | 20 | For a detailed example, see [the test branch](https://github.com/keijiro/unity-sunburst-effects/tree/test) 21 | 22 | License 23 | ------- 24 | 25 | Copyright (C) 2013 Keijiro Takahashi 26 | 27 | Permission is hereby granted, free of charge, to any person obtaining a copy of 28 | this software and associated documentation files (the "Software"), to deal in 29 | the Software without restriction, including without limitation the rights to 30 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 31 | the Software, and to permit persons to whom the Software is furnished to do so, 32 | subject to the following conditions: 33 | 34 | The above copyright notice and this permission notice shall be included in all 35 | copies or substantial portions of the Software. 36 | 37 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 38 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 39 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 40 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 41 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 42 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 43 | -------------------------------------------------------------------------------- /SunburstEffects.cs: -------------------------------------------------------------------------------- 1 | // Sunburst effects. 2 | // By Keijiro Takahashi, 2013 3 | // https://github.com/keijiro/unity-sunburst-mesh-fx 4 | using UnityEngine; 5 | using System.Collections; 6 | 7 | [RequireComponent(typeof(MeshFilter)), RequireComponent(typeof(MeshRenderer))] 8 | public class SunburstEffects : MonoBehaviour 9 | { 10 | #region Public variables 11 | public int beamCount = 100; 12 | [Range(0.01f, 0.5f)] 13 | public float beamWidth = 0.1f; 14 | [Range(0.1f, 10.0f)] 15 | public float speed = 0.4f; 16 | [Range(1.0f, 10.0f)] 17 | public float scalePower = 1.0f; 18 | #endregion 19 | 20 | #region Beam vectors 21 | Vector3[] beamDir; 22 | Vector3[] beamExt; 23 | #endregion 24 | 25 | #region Mesh data 26 | Mesh mesh; 27 | Vector3[] vertices; 28 | #endregion 29 | 30 | #region Animation parameters 31 | const float indexToNoise = 0.77f; 32 | float time; 33 | #endregion 34 | 35 | #region Private functions 36 | void ResetBeams () 37 | { 38 | // Allocate arrays. 39 | beamDir = new Vector3[beamCount]; 40 | beamExt = new Vector3[beamCount]; 41 | vertices = new Vector3[beamCount * 3]; 42 | var normals = new Vector3[beamCount * 3]; 43 | 44 | // Initialize the beam vectors. 45 | var normalIndex = 0; 46 | for (var i = 0; i < beamCount; i++) { 47 | // Make a beam in a completely random way. 48 | var dir = Random.onUnitSphere; 49 | var ext = Random.onUnitSphere; 50 | beamDir [i] = dir; 51 | beamExt [i] = ext; 52 | 53 | // Use a slightly modified vector on the first vertex to make a gradation. 54 | var normal = Vector3.Cross (dir, ext).normalized; 55 | normals [normalIndex++] = Vector3.Lerp (dir, normal, 0.5f).normalized; 56 | normals [normalIndex++] = normal; 57 | normals [normalIndex++] = normal; 58 | } 59 | 60 | // Initialize the triangle set. 61 | var indices = new int[beamCount * 3]; 62 | for (var i = 0; i < indices.Length; i++) { 63 | indices [i] = i; 64 | } 65 | 66 | // Initialize the mesh. 67 | mesh.Clear (); 68 | mesh.vertices = vertices; 69 | mesh.normals = normals; 70 | mesh.triangles = indices; 71 | } 72 | 73 | void UpdateVertices () 74 | { 75 | var vertexIndex = 0; 76 | for (var i = 0; i < beamCount; i++) { 77 | // Use 2D Perlin noise to animate the beam. 78 | var scale = Mathf.Pow (Mathf.PerlinNoise (time, i * indexToNoise), scalePower); 79 | 80 | // Never modify the first vertex. 81 | vertexIndex++; 82 | 83 | // Update the 2nd and 3rd vertices. 84 | var tip = beamDir [i] * scale; 85 | var ext = beamExt [i] * beamWidth * scale; 86 | vertices [vertexIndex++] = tip - ext; 87 | vertices [vertexIndex++] = tip + ext; 88 | } 89 | } 90 | #endregion 91 | 92 | #region Monobehaviour functions 93 | void Awake () 94 | { 95 | // Initialize the mesh instance. 96 | mesh = new Mesh (); 97 | mesh.MarkDynamic (); 98 | GetComponent ().sharedMesh = mesh; 99 | 100 | // Initialize the beam array. 101 | ResetBeams (); 102 | } 103 | 104 | void Update () 105 | { 106 | // Reset the beam array if the number was changed. 107 | if (beamCount != beamDir.Length) { 108 | ResetBeams (); 109 | } 110 | 111 | // Do animation. 112 | UpdateVertices (); 113 | 114 | // Update the vertex array. 115 | mesh.vertices = vertices; 116 | 117 | // Advance the time count. 118 | time += Time.deltaTime * speed; 119 | } 120 | #endregion 121 | } --------------------------------------------------------------------------------