KITASENJU DESIGN BLOG

memo, html, javascript, unity

スプライトのためにgrid上に分割したときの座標

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class SpriteUV {

    public static Vector4 GetUv(int idx,int numX, int numY){

        int splitX = numX;
        int splitY = numY;

        int xx = idx % splitX;
        int yy = Mathf.FloorToInt( idx / splitX );

        return _GetUv(xx,yy,splitX,splitY);

    }

    private static Vector4 _GetUv(int idxX, int idxY, float splitX, float splitY){

        float xx = idxX * 1f/splitX;
        float yy = ((splitY-1f)-idxY) * 1f/splitY;
        return new Vector4(xx,yy,1/splitX,1/splitY);

    }

    public static int IndexToX(int idx,int numX){
        return idx % numX;
    }
    public static int IndexToY(int idx,int numX){
        return Mathf.FloorToInt( idx / numX );
    }

}
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture

                float2 uv = i.uv;

                uv.x = _Offset.x + _Offset.z * uv.x;
                uv.y = _Offset.y + _Offset.w * uv.y;

                fixed4 col = tex2D(_MainTex, uv);
                
                return col;
            }
"FOOTER"