Fragment shader---2D纹理采样2

1、Tiling:x:在x上的平铺数   y:在y方向的平铺数

Offset: x:在x方向上的偏移量   y:在y方向的偏移量

使用自定义的着色器,并没有进行平铺



改为系统默认的着色器,立方体进行了平铺



2、平铺texture

Shader "Custom/s_texture_two" {

	Properties{
		_MainTex("MainTex", 2D) = ""{}
	}
		SubShader{
		pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"


		float tiling_x;
		float tiling_y;
		float offset_x;
		float offset_y;

		sampler2D _MainTex;

		struct v2f {
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;
		};


		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			o.uv = v.texcoord.xy;
			o.uv.x *= tiling_x;
			o.uv.y *= tiling_y;
			return o;
		}

		fixed4 frag(v2f IN) :COLOR{
			fixed4 color = tex2D(_MainTex,IN.uv);
			return color;
		}
		ENDCG
	}
	}
}


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

public class SetTexture : MonoBehaviour {

    public float tiling_x;
    public float tiling_y;

    public float offset_x;
    public float offset_y;

	// Use this for initialization
	void Start () {
      
    }
	
	// Update is called once per frame
	void Update () {
        //给参数赋值
        GetComponent<Renderer>().material.SetFloat("tiling_x", tiling_x);
        GetComponent<Renderer>().material.SetFloat("tiling_y", tiling_y);
        GetComponent<Renderer>().material.SetFloat("offset_x", offset_x);
        GetComponent<Renderer>().material.SetFloat("offset_y", offset_y);
    }
}

渲染后的效果:




修改tiling x、y的值可以修改平铺的cube中的texture


3、偏移

Shader "Custom/s_texture_two" {

	Properties{
		_MainTex("MainTex", 2D) = ""{}
	}
		SubShader{
		pass {
		CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"


			float tiling_x;
		float tiling_y;
		float offset_x;
		float offset_y;

		sampler2D _MainTex;

		struct v2f {
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;
		};


		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			o.uv = v.texcoord.xy;
			//平铺
			o.uv.x *= tiling_x;
			o.uv.y *= tiling_y;
			//位移
			o.uv.x += offset_x;
			o.uv.y += offset_y;
			return o;
		}

		fixed4 frag(v2f IN) :COLOR{
			fixed4 color = tex2D(_MainTex,IN.uv);
		return color;
		}
			ENDCG
	}
	}
}


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

public class SetTexture : MonoBehaviour {

    public float tiling_x;
    public float tiling_y;

    public float offset_x;
    public float offset_y;

	// Use this for initialization
	void Start () {
      
    }
	
	// Update is called once per frame
	void Update () {
        //给参数赋值
        GetComponent<Renderer>().material.SetFloat("tiling_x", tiling_x);
        GetComponent<Renderer>().material.SetFloat("tiling_y", tiling_y);
        GetComponent<Renderer>().material.SetFloat("offset_x", offset_x);
        GetComponent<Renderer>().material.SetFloat("offset_y", offset_y);
    }
}





4、使用系统默认的值_MainTex_ST


Shader "Custom/s_texture_two" {

	Properties{
		_MainTex("MainTex", 2D) = ""{}
	}
		SubShader{
		pass {
		CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"


			float tiling_x;
		float tiling_y;
		float offset_x;
		float offset_y;

		sampler2D _MainTex;
		//unity总是会帮我们传递这样一个值
		float4 _MainTex_ST;

		struct v2f {
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;
		};


		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			o.uv = v.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw;


			//o.uv = v.texcoord.xy;
			//平铺
			//o.uv.x *= tiling_x;
			//o.uv.y *= tiling_y;
			//位移
			//o.uv.x += offset_x;
			//o.uv.y += offset_y;
			return o;
		}

		fixed4 frag(v2f IN) :COLOR{
			fixed4 color = tex2D(_MainTex,IN.uv);
		return color;
		}
			ENDCG
	}
	}
}




5、使用宏

Shader "Custom/s_texture_two" {

	Properties{
		_MainTex("MainTex", 2D) = ""{}
	}
		SubShader{
		pass {
		CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"


			float tiling_x;
		float tiling_y;
		float offset_x;
		float offset_y;

		sampler2D _MainTex;
		//unity总是会帮我们传递这样一个值
		float4 _MainTex_ST;

		struct v2f {
			float4 pos:POSITION;
			float2 uv:TEXCOORD0;
		};


		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
	
			o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
			return o;
		}

		fixed4 frag(v2f IN) :COLOR{
			fixed4 color = tex2D(_MainTex,IN.uv);
			return color;
		}
			ENDCG
	}
	}
}



6、


注:

1、uv平铺可以简单地理解为几个页面的相乘


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值