Unity 光照着色器
光照着色器需要考虑光照的分类,一般分为漫反射和镜面反射。
漫反射计算基本光照:
float brightness=dot(normal,lightDir) 将法线和光的入射方向进行点积运算,求出光的亮度。
float3 pixelColor=brightness*lightColor*surfaceColor 将光的亮度*光的颜色*物体表面颜色得到我们最终看到的颜色。
Shader "Unlit/DiffuseShader"
{
Properties
{
_Color("Color",Color)=(,,,)
}
SubShader
{
#新加了光照模式标签
Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"}
LOD Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc"
#include "UnityLightingCommon.cginc" fixed4 _Color; struct appdata
{
float4 vertex : POSITION;
float3 normal:NORMAL; #法线方向
}; struct v2f
{
float4 vertex : SV_POSITION;
float3 worldNormal:TXCOORD0; # }; v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
float3 worldNormal=UnityObjectToWorldNormal(v.normal);
o.worldNormal=worldNormal;
return o;
} fixed4 frag (v2f i) : SV_Target
{
float3 normalDirection=normalize(i.worldNormal);
float nl=max(0.0,dot(normalDirection,_WorldSpaceLightPos0.xyz));
float4 diffusTerm=nl*_Color*_LightColor0;
return diffusTerm;
}
ENDCG
}
}
}
镜面反射具体实现:
R=2*(N*L)*N-L;
float3 reflectionVector=reflect(-lightDir,normal); //用入射光线方向和法线求出反射光线方向
float specDot=max(dot(reflectionVector,eyeDir),0.0); //反射光线和眼睛入射方法求点积
float spec=pow(specDot,spectExponent);
fixed4 frag (v2f i) : SV_Target
{
//标准化法向量
float3 normalDirection=normalize(i.worldNormal);
float3 viewDirection=normalize(UnityWorldSpaceViewDir(i.vertexWorld));
float3 lightDirection=normalize(UnityWorldSpaceViewDir(i.vertexWorld)); //采样纹理
float4 tex=tex2D(_DiffuseTex,i.uv); //漫反射计算,使用兰帕特近似方法lambert
float nl=max(0.0,dot(normalDirection,_WorldSpaceLightPos0.xyz));
float4 diffusTerm=nl*_Color*_LightColor0; //镜面反射计算,使用芳式近似方法Phong
float3 reflectionDirection=reflect(-lightDirection,normalDirection);
float3 specularDot=max(0.0,dat(viewDirection,reflectionDirection));
float3 specular=pow(specularDot,_shininess); //计算最终的颜色
float4 specularTerm=float4(specular,)*_SpacColor*_LightColor;
float4 finalColor=diffusTerm*specularTerm; return finalColor;
Unity 光照着色器的更多相关文章
- Unity Shader着色器优化
https://mp.weixin.qq.com/s?__biz=MzU5MjQ1NTEwOA==&mid=2247493518&idx=1&sn=c51b92e9300bcf ...
- Unity 5着色器系统代码介绍(上)
http://forum.china.unity3d.com/thread-25724-1-10.html Unity 5着色器系统代码介绍(上) Unity在着色器开发方面提供了很大的灵活性.有些工 ...
- Unity 几何着色器
Unity 几何着色器 shaderGeometry Shader几何着色器 Unity 几何着色器 如果学习不能带来价值,那将毫无意义 简介 在顶点和片段着色器之间有一个可选的着色器,叫做几 ...
- [Unity] Shader(着色器)输入输出和语义
在Unity5.x后, 已经支持了基于物理的光照模型,也就是常说的次时代引擎所必须具备的功能. 如果在Properties使用2D,CG里要用sampler2D,代表使用的是2维纹理 如果在Prope ...
- [Unity] Shader(着色器)之纹理贴图
在Shader中,我们除了可以设定各种光线处理外,还可以增加纹理贴图. 使用 settexture 命令可以为着色器指定纹理. 示例代码: Shader "Sbin/ff2" { ...
- Unity 5着色器系统代码介绍(下)
http://forum.china.unity3d.com/thread-25738-1-10.html 上一篇对着色器系统的工作原理做了介绍,现在我们将继续深入,将目光聚焦在标准着色器的光照函数. ...
- [Unity] Shader(着色器)之固定管线
在Unity中,固定管线Shader的性能是最好的. 什么是固定管线呢? 固定渲染管线 —— 这是标准的几何&光照(T&L)管线,功能是固定的,它控制着世界.视.投影变换及固定光照控制 ...
- Unity 内置着色器(转)
Unity包括超过40种内置的shader. 标准着色器家族 Normal Shader Family 这些着色器都是Unity基本的着色器.适用于大多数的不透明物体,如果想要物体有透明.发光效果等, ...
- Unity 渲染教程(二):着色器基础
转载:https://www.jianshu.com/p/7db167704056 这是关于渲染基础的系列教程的第二部分.这个渲染基础的系列教程的第一部分是有关矩阵的内容.在这篇文章中我们将编写我们的 ...
随机推荐
- How to install Arch Linux
fdisk -l mkfs.ext4 /dev/sdaX mount /dev/sdaX /mnt mkdir -p /mnt/boot/ mount /dev/sdaY /mnt/boot/ arc ...
- Spring Boot 构建电商基础秒杀项目 (十一) 秒杀
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...
- DeepLearning网络设计总结
检测网络: 1. tiling层可以减少计算量,deconvolution相比tiling性能要好一些
- Clear Linux 为脚本语言提供更高的性能
导读 Clear Linux的领先性能不仅限于C/C++应用程序,而且PHP,R和Python等脚本语言也有很大的提升速度.在一篇新的博客文章中,英特尔的一位开发人员概述了他们对Python的一些性能 ...
- 【C/C++】实现牛顿迭代
#include<bits/stdc++.h> using namespace std; /*(x+2)^2 +1 -2(x-1)^2+7 */ double f(double x){ - ...
- Neutron 网络基本概念
Neutron 网络基本概念 上次我们讨论了 Neutron 提供的功能,今天我们学习 Neutron 模块几个重要的概念. Neutron 管理的网络资源包括 Network,subnet 和 po ...
- 关于i++和i++的左值、右值问题
1.什么是左值和右值? 左值就是出现在表达式左边的值(等号左边),可以被改变,他是存储数据值的那块内存的地址,也称为变量的地址: 右值是指存储在某内存地址中的数据,也称为变量的数据. 左值可以作为右值 ...
- 使用docker部署springboot
首先创建一个简单的springboot web项目 创建一个 DockerController,在其中有一个index()方法,访问时返回:Hello Docker! @RestController ...
- [洛谷P4147] 玉蟾宫
类型:单调栈 传送门:>Here< 题意:求一个$01$矩阵中最大子矩形(全是$1$)的面积 解题思路 单调栈的一个经典应用 考虑维护一个数组$p[i][j]$表示$(i,j)$往上最多有 ...
- Python课程目录
Python基础1 介绍.基本语法.流程控制 Python基础2 列表.字典.集合 Python基础3 基础3-函数