Shader16 - 片段级模型动态变色

Shader "Sbin/FragColor" 
{

	Properties
	{
		_MainColor("MainColor",color)=(1,1,1,1)
		_SecondColor("SecondColor",color)=(1,1,1,1)
		_Center("Center",Range(-0.51,0.51))=0
		_R("R",Range(0,0.5))=0.2
	}
	SubShader 
	{
	    //透明处理
		tags{"queue"="transparent"}

		pass
		{
		
			CGPROGRAM
		
			#pragma vertex Vert
			#pragma fragment frag 
			#include "unitycg.cginc"

			float4 _MainColor;
			float4 _SecondColor;
			float _R;
			struct v2f
			{
				float4 pos:POSITION;
				float4 y:TEXCOORD0;
			}


			v2f vert(appdata_base v)
			{
				v2f o;
				o.pos=mul(UNITY_MATRIX_MVP,v.Vertex);
				o.y=v.Vertex.y;
				return o;
			}

			fixed frag(v2f IN):COLOR
			{
				float d=IN.y-_Center;
				d=d/abs(d);
				float f=saturate(d/_R);
				d*=f;
				d=d/2+0.5;//范围在0到1之间
				return lerp(_MainColor,_SecondColor,d);//差值运算
			}
			ENDCG
		}
	} 
}

 

挂在汽车上转起来

using UnityEngine;
using System.Collections;
using System.Linq;

public class CheckVertex : MonoBehaviour
{
    public MeshFilter mf1;
    public MeshFilter mf2;
    public MeshFilter mf3;
	// Use this for initialization
	void Start () 
    {
        Vector3[] verts = mf1.mesh.vertices;
        float max = verts.Max(v => v.x);
        float min = verts.Min(v => v.x);
        Debug.Log(max + "   " + min);
	}
	
	// Update is called once per frame
	void Update () 
    {
        transform.Rotate(Vector3.up, Time.deltaTime * 10);
	}
}

 

Shader "Sbin/CarPaintSurface" 
{
	Properties 
	{
		_MainColor("MainColor",color)=(1,1,1,1)
		_SecondColor("SecondColor",color)=(1,1,1,1)
		_Center("Center",Range(-0.51,0.51))=0
		_R("R",Range(0,0.5))=0.2
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
	}
	SubShader 
	{
		Tags { "RenderType"="Opaque" }
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		//制定使用的表面着色器,表面着色器的函数surf,基于物理模型的光照模型standard,
		#pragma surface surf Standard vertex:vert

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		float4 _MainColor;
		float4 _SecondColor;
		float _R;
		half _Glossiness;
		half _Metallic;


		struct Input 
		{
			float2 uv_MainTex;
			float x;
		};

		void vert(inout appdata_full v,out Input o)
		{
			o.uv_MainTex=v.TEXCOORD.xy;
			o.x=v.Vertex.x;
		}

		

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;//漫反射颜色赋值
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;//金属化的级别
			o.Smoothness = _Glossiness;//平滑度
			o.Alpha = c.a;

			float d=IN.x-_Center;
			d=d/abs(d);
			float f=saturate(d/_R);
			d*=f;
			d=d/2+0.5;//范围在0到1之间
			o.Albedo*= lerp(_MainColor,_SecondColor,d)*2;//差值运算
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shader-school是一个用于GLSL着色器和图形编程的workshopper。它提供了关于GLSL着色器的介绍以及如何在浏览器中运行图形编程的教程。要运行这个workshopper,首先需要在浏览器中安装WebGL,以及node.js和git插件的拷贝。它可以帮助你学习和掌握GLSL着色器编程的基础知识和技巧。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [shader-school, 用于GLSL着色器和图形编程的workshopper.zip](https://download.csdn.net/download/weixin_38743737/11769657)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [shader-school的渲染入门系列(二)](https://blog.csdn.net/feetcrack/article/details/78536045)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [shader-school的渲染入门系列(一)](https://blog.csdn.net/feetcrack/article/details/78535349)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值