找回密码
 立即注册
查看: 703|回复: 1

[特效/Shader/Mesh] 圆润变平的shader smooth to flat unity

[复制链接]
发表于 2020-4-10 13:25 | 显示全部楼层 |阅读模式
资源信息 Asset Information
资源名称: 圆润变平的shader smooth to flat unity (发帖教程)
版本: (版本)
资源等级: 1
资源格式: .unitypackage (链接失效请点击帖子右下方举报通知管理员)
资源大小: 1kb (默认:MB)
下载地址: (购买积分)
点击查看原图
美丽分割线
  1. Shader "Custom/Geometry/FlatShading"
  2. {
  3.     Properties
  4.     {
  5.         _Color("Color", Color) = (1,1,1,1)
  6.         _MainTex("Albedo", 2D) = "white" {}
  7.     }
  8.    
  9.     SubShader
  10.     {

  11.         Tags{ "Queue"="Geometry" "RenderType"= "Opaque" "LightMode" = "ForwardBase" }

  12.         Pass
  13.         {
  14.             CGPROGRAM

  15.             #include "UnityCG.cginc"
  16.             #pragma vertex vert
  17.             #pragma geometry geom
  18.             #pragma fragment frag

  19.             float4 _Color;
  20.             sampler2D _MainTex;

  21.             struct v2g
  22.             {
  23.                 float4 pos : SV_POSITION;
  24.                 float2 uv : TEXCOORD0;
  25.                 float3 vertex : TEXCOORD1;
  26.             };

  27.             struct g2f
  28.             {
  29.                 float4 pos : SV_POSITION;
  30.                 float2 uv : TEXCOORD0;
  31.                 float light : TEXCOORD1;
  32.             };

  33.             v2g vert(appdata_full v)
  34.             {
  35.                 v2g o;
  36.                 o.vertex = v.vertex;
  37.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  38.                 o.uv = v.texcoord;
  39.                 return o;
  40.             }

  41.             [maxvertexcount(3)]
  42.             void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream)
  43.             {
  44.                 g2f o;

  45.                 // Compute the normal
  46.                 float3 vecA = IN[1].vertex - IN[0].vertex;
  47.                 float3 vecB = IN[2].vertex - IN[0].vertex;
  48.                 float3 normal = cross(vecA, vecB);
  49.                 normal = normalize(mul(normal, (float3x3) unity_WorldToObject));

  50.                 // Compute diffuse light
  51.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  52.                 o.light = max(0., dot(normal, lightDir));

  53.                 // Compute barycentric uv
  54.                 o.uv = (IN[0].uv + IN[1].uv + IN[2].uv) / 3;

  55.                 for(int i = 0; i < 3; i++)
  56.                 {
  57.                     o.pos = IN[i].pos;
  58.                     triStream.Append(o);
  59.                 }
  60.             }

  61.             half4 frag(g2f i) : COLOR
  62.             {
  63.                 float4 col = tex2D(_MainTex, i.uv);
  64.                 col.rgb *= i.light * _Color;
  65.                 return col;
  66.             }

  67.             ENDCG
  68.         }
  69.     }
  70.     Fallback "Diffuse"
  71. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2020-4-12 23:30 | 显示全部楼层

感谢楼主的无私分享!
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 05:21 , Processed in 0.102580 second(s), 32 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表