Unity Shader Experiment – Sprite Sheets –
シェーダーの実験。
クリックすると、別ウィンドウが開きます。
※ブラウザはSafariを使用してください。
cube のテクスチャーに、スプライトシートによるアニメーションを投影する自作シェーダー。
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
Shader "SpriteSheets" { Properties { _MainTex("Base (RGB)", 2D) = "white" {} // texture _TexWidth("Sheet Width", float) = 0.0 // the width of the texture _CellAmount("Cell Amount", float) = 0.0 // the number of cells _Speed("Speed", Range(0.01, 32)) = 12 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert sampler2D _MainTex; float _TexWidth; float _CellAmount; float _Speed; struct Input { float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutput o) { // Store UVs in a separate variable float2 spriteUV = IN.uv_MainTex; // Calculate the width of a single cell in sprite sheet and get a UV percentage that each cell takes up float cellPixelWidth = _TexWidth/_CellAmount; // the width of a cell float cellUVPercentage = cellPixelWidth/_TexWidth; // the percentage of a cell // Get a stair step value out of time to increment the uv offset float timeVal = fmod(_Time.y * _Speed, _CellAmount); // returns the remainder of x/y with the same sign as x timeVal = ceil(timeVal); // ceiling // Animate the uvs forward by the width percentage of each cell float xValue = spriteUV.x; // current x position of the texture xValue += cellUVPercentage * timeVal * _CellAmount; // xValue *= cellUVPercentage; // new position spriteUV = float2(xValue, spriteUV.y); // x = new position, y = static position half4 c = tex2D(_MainTex, spriteUV); o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" } |