【unity shader】《unity shader入门精要》 光照模型
来源:互联网 发布:unity3d游戏场景下载 编辑:程序博客网 时间:2024/05/22 12:36
unity shader中的漫反射+高光反射光照模型
光照模型是shader的核心,它描述了光线同物体的交互方式。
对于非透明物体,光照模型一般包含两部分:漫反射和高光反射。
一个常见的包含漫反射光照模型的shader程序如下:
Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level" {Properties {_Diffuse ("Diffuse", Color) = (1, 1, 1, 1)}SubShader {Pass { Tags { "LightMode"="ForwardBase" }CGPROGRAM#pragma vertex vert#pragma fragment frag#include "Lighting.cginc" //预设光照文件fixed4 _Diffuse;struct a2v {float4 vertex : POSITION;float3 normal : NORMAL;};struct v2f {float4 pos : SV_POSITION;float3 worldNormal : TEXCOORD0;};v2f vert(a2v v) {v2f o;// Transform the vertex from object space to projection spaceo.pos = mul(UNITY_MATRIX_MVP, v.vertex);// Transform the normal fram object space to world spaceo.worldNormal = mul(v.normal, (float3x3)_World2Object);return o;}fixed4 frag(v2f i) : SV_Target {// Get ambient termfixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;// Get the normal in world spacefixed3 worldNormal = normalize(i.worldNormal);// Get the light direction in world spacefixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);// Compute diffuse term 反射光颜色的计算是用光源颜色*物体表面的反射系数,即_Diffusefixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));fixed3 color = ambient + diffuse;return fixed4(color, 1.0);}ENDCG}} FallBack "Diffuse"}
实际应用中,很多shader程序员似乎并不严格遵守这一模型,而会根据自身需要对它进行改进。可能是因为在shader世界中,如果它看上去是对的,那它就是对的吧。
0 0
- 【unity shader】《unity shader入门精要》 光照模型
- Unity Shader入门精要总结--基本光照
- Unity Shader入门精要总结--光照衰减
- <unity shader入门精要>
- 【Unity Shader入门精要】— Unity中的基础光照
- Unity Shader自定义光照模型
- Unity Shader 五 光照模型
- Unity Shader入门精要笔记(二):Unity Shader基础
- 【Unity Shader入门精要】— Unity Shader基础
- 【Unity Shader入门精要】— 开始Unity Shader之旅
- Unity Shader入门精要笔记(八):Unity 的基础光照——概念与理论
- Unity Shader入门精要笔记(九):Unity 的基础光照——漫反射的实现
- Unity Shader入门精要读书笔记第一篇
- Unity Shader入门精要读书笔记第二篇
- 【Unity Shader入门精要】学习开篇
- 《Unity shader 入门精要》学习 前言
- Unity Shader入门精要学习笔记
- Unity Shader入门精要学习笔记
- Maven下WEB项目访问index。jsp报500适合myeclipse14
- eclipse不自动弹出提示(alt+/快捷键失效)
- 宿主机使用虚拟机的VPN连接
- MyBatis传入参数与parameterType
- 闲话数据库(一)第一范式/第二范式
- 【unity shader】《unity shader入门精要》 光照模型
- ubuntu 使用 远程桌面软件 Remmina 时遇到“无法连接到服务器”问题
- android如何获得本机地址(包括开启热点的手机)
- 共享文件系统的搭建之ftp
- POJ1273 Drainage Ditches【最大流、增广路算法Edmonds_Karp】
- 图
- redis系列--list命令
- PHP实习(五)
- 腾讯的一笔画游戏