Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
背景リピート(Target依存)
Shader "MBL/Repeat"
{
Properties
{
[NoScaleOffset]
_MainTex("Texture", 2D) = "white" {}
_OffsetX("OffsetX", Float) = 0.0
_OffsetY("OffsetY", Float) = 0.0
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _OffsetX;
float _OffsetY;
v2f vert(appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
//オフセット + UV値を(0~1)でRepeat
float2 uv = float2(abs(fmod(i.uv.x + _OffsetX, 1.0)), abs(fmod(i.uv.y + _OffsetY, 1.0)));
fixed4 col = tex2D(_MainTex, uv);
return col;
}
ENDCG
}
}
}
using System.Collections;
using UnityEngine;
public class RepeatWithTargetPosition : MonoBehaviour
{
[SerializeField]
private Material mat;
[SerializeField]
private Transform target;
[SerializeField]
private bool XRepeat = true;
[SerializeField]
private bool YRepeat;
[SerializeField, Range(0.001f, 1f)]
private float repeatSpeed = 0.01f;
private void Update()
{
if(target == null)
return;
if(XRepeat)
mat.SetFloat("_OffsetX", target.position.x * repeatSpeed);
if(YRepeat)
mat.SetFloat("_OffsetY", target.position.y * repeatSpeed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment