C#学习 动态修改Mesh的Shader材质

u3d的shader内置了一种surf的 一种裸的

            v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				// apply fog
				//UNITY_APPLY_FOG(i.fogCoord, col);
				col=fixed4(i.uv,0.,1.);
				return col;
			}

裸的这种 vert就是顶点着色器 frag这个是片元着色器

void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}

另外这种 只需要写片元着色器的一部分 内置了很多东西了已经

创建任意一个品种的shader文件

m_cube.GetComponent<MeshRenderer> ().material = new Material (Shader.Find("Unlit/11"));

这个路径是shader文件的第一行那个地方

这个还可以在编辑器里创建一个材质球 然后把他拖上去

 

U3D的shader有好多东西之前都没见过(Pass SubShader什么的) 还要慢慢学习

Unity2019内置管线(Universal Render Pipeline)提供了可以实现实时折射和反射的Shader。 在URP中,可以使用内置的Lit Shader来实现实时折射和反射。下面是一个简单的示例: ``` Shader "Custom/LitRefractionReflection" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _NormalMap ("Normal Map", 2D) = "bump" {} _Metallic ("Metallic", Range(0,1)) = 0.0 _Smoothness ("Smoothness", Range(0,1)) = 0.5 _Color ("Color", Color) = (1,1,1,1) _RefractiveIndex ("Refractive Index", Range(1.0, 2.0)) = 1.5 } SubShader { Tags { "RenderType"="Opaque" } CGPROGRAM #pragma surface surf Standard struct Input { float3 worldPos; float3 worldNormal; float3 worldRefl; float3 worldViewDir; float2 uv_MainTex; }; sampler2D _MainTex; sampler2D _NormalMap; float _Metallic; float _Smoothness; fixed4 _Color; float _RefractiveIndex; void surf (Input IN, inout SurfaceOutputStandard o) { // Diffuse o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color.rgb; // Metallic o.Metallic = _Metallic; // Smoothness o.Smoothness = _Smoothness; // Normal Map o.Normal = UnpackNormal (tex2D (_NormalMap, IN.uv_MainTex)); // Refraction float3 refractionVector = refract(IN.worldViewDir, IN.worldNormal, _RefractiveIndex); o.Refraction = tex2D(_MainTex, IN.uv_MainTex + refractionVector.xy * 0.1).rgb; // Reflection o.Metallic = 1.0; o.Smoothness = 1.0; o.Reflectivity = 1.0; o.Normal = IN.worldNormal; o.Alpha = 0.0; } ENDCG } FallBack "Diffuse" } ``` 这个Shader实现了基本的漫反射、法线贴图、金属度、光滑度,以及折射和反射效果。可以通过调整_RefractiveIndex来修改材质的折射率,从而改变折射效果。 需要注意的是,在URP中实现实时折射和反射需要使用屏幕空间技术,因此需要在摄像机上添加一个反射探针(Reflection Probe)组件来捕获场景中的反射信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值