MarmosetInput.cginc:
Input结构定义:
  1. struct Input
  2. {
  3. #if defined(MARMO_PACKED_UV) || defined(MARMO_PACKED_VERTEX_OCCLUSION) || defined(MARMO_PACKED_VERTEX_COLOR)
  4. float4 texcoord;
  5. #else
  6. float2 texcoord;
  7. #endif
  8.  
  9. float3 worldNormal;
  10.  
  11. #if defined(MARMO_SPECULAR_DIRECT) || defined(MARMO_SPECULAR_IBL)
  12. float3 viewDir;
  13. #endif
  14.  
  15. #if defined(MARMO_COMPUTE_WORLD_POS)
  16. #ifdef MARMO_U5_WORLD_POS
  17. float3 worldPos; //this is free in Unity 5
  18. #endif
  19. #else
  20. float4 worldP; //lets write our own
  21. #endif
  22. #if defined(MARMO_VERTEX_COLOR) || defined(MARMO_VERTEX_LAYER_MASK)
  23. half4 color : COLOR;
  24. #elif defined(MARMO_VERTEX_OCCLUSION)
  25. half2 color : COLOR;
  26. #endif
  27.  
  28. #ifdef MARMO_DIFFUSE_VERTEX_IBL
  29. float3 vertexIBL;
  30. #endif
  31. INTERNAL_DATA
  32. };

Output结构定义:

  1. struct MarmosetOutput
  2. {
  3. half3 Albedo; //diffuse map RGB
  4. half Alpha; //diffuse map A
  5. half3 Normal; //world-space normal
  6. half3 Emission; //contains IBL contribution
  7. half Specular; //specular exponent (required by Unity)
  8. #ifdef MARMO_SPECULAR_DIRECT
  9. half3 SpecularRGB; //specular mask
  10. #endif
  11. };

Unity自带的Output结构定义:

  1. struct SurfaceOutput
  2. {
  3. fixed3 Albedo;
  4. fixed3 Normal;
  5. fixed3 Emission;
  6. half Specular;
  7. fixed Gloss;
  8. fixed Alpha;
  9. };
  10.  
  11. // Metallic workflow
  12. struct SurfaceOutputStandard
  13. {
  14. fixed3 Albedo; // base (diffuse or specular) color
  15. fixed3 Normal; // tangent space normal, if written
  16. half3 Emission;
  17. half Metallic; // 0=non-metal, 1=metal
  18. // Smoothness is the user facing name, it should be perceptual smoothness but user should not have to deal with it.
  19. // Everywhere in the code you meet smoothness it is perceptual smoothness
  20. half Smoothness; // 0=rough, 1=smooth
  21. half Occlusion; // occlusion (default 1)
  22. fixed Alpha; // alpha for transparencies
  23. };
  24.  
  25. // Specular workflow
  26. struct SurfaceOutputStandardSpecular
  27. {
  28. fixed3 Albedo; // diffuse color
  29. fixed3 Specular; // specular color
  30. fixed3 Normal; // tangent space normal, if written
  31. half3 Emission;
  32. half Smoothness; // 0=rough, 1=smooth
  33. half Occlusion; // occlusion (default 1)
  34. fixed Alpha; // alpha for transparencies
  35. };

MarmosetDirect.cginc:
直接光照的主要代码,去掉deferred lighting相关的代码。
  1. #ifndef MARMOSET_DIRECT_CGINC
  2. #define MARMOSET_DIRECT_CGINC
  3. // Core
  4. inline float3 wrapLighting(float DP, float3 scatter)
  5. {
  6. scatter *= 0.5;
  7. float3 integral = float3(1.0,1.0,1.0)-scatter;
  8. float3 light = saturate(DP * integral + scatter);
  9. float shadow = (DP*0.5+0.5);
  10. shadow *= shadow;
  11. return light * integral * shadow;
  12. }
  13. // NOTE: some intricacy in shader compiler on some GLES2.0 platforms (iOS) needs 'viewDir' & 'h'
  14. // to be mediump instead of lowp, otherwise specular highlight becomes too bright.
  15. inline half4 marmosetLighting (MarmosetOutput s, half3 viewDir, half3 lightDir, half3 lightColor)
  16. {
  17. half4 frag = half4(0.0,0.0,0.0,s.Alpha);
  18. #if defined(MARMO_DIFFUSE_DIRECT) || defined(MARMO_SPECULAR_DIRECT)
  19. half3 L = lightDir;
  20. half3 N = s.Normal;
  21. #ifdef MARMO_HQ
  22. L = normalize(L);
  23. #endif
  24. #endif
  25.  
  26. #ifdef MARMO_DIFFUSE_DIRECT
  27. half dp = saturate(dot(N,L));
  28.  
  29. #ifdef MARMO_DIFFUSE_SCATTER
  30. float4 scatter = _Scatter * _ScatterColor;
  31. half3 diff = wrapLighting(dp, scatter.rgb);
  32. diff *= 2.0 * s.Albedo.rgb; //*2.0 to match Unity
  33. #else
  34. half3 diff = (2.0 * dp) * s.Albedo.rgb; //*2.0 to match Unity
  35. #endif
  36. frag.rgb = diff * lightColor;
  37. #endif
  38.  
  39. #ifdef MARMO_SPECULAR_DIRECT
  40. half3 H = normalize(viewDir+L);
  41. float specRefl = saturate(dot(N,H));
  42. half3 spec = pow(specRefl, s.Specular*512.0);
  43. #ifdef MARMO_HQ
  44. //self-shadowing blinn
  45. #ifdef MARMO_DIFFUSE_DIRECT
  46. spec *= saturate(10.0*dp);
  47. #else
  48. spec *= saturate(10.0*dot(N,L));
  49. #endif
  50. #endif
  51. spec *= lightColor;
  52. frag.rgb += (0.5 * spec) * s.SpecularRGB; //*0.5 to match Unity
  53. #endif
  54. return frag;
  55. }
  56. //forward lighting
  57. inline half4 LightingMarmosetDirect( MarmosetOutput s, half3 lightDir, half3 viewDir, half atten )
  58. {
  59. return marmosetLighting( s, viewDir, lightDir, _LightColor0 * atten);
  60. }
  61. inline half4 LightingMarmosetDirect( MarmosetOutput s, half3 viewDir, UnityGI gi )
  62. {
  63. fixed4 c;
  64. c = marmosetLighting (s, viewDir, gi.light.dir, gi.light.color);
  65.  
  66. #ifdef UNITY_LIGHT_FUNCTION_APPLY_INDIRECT
  67. c.rgb += s.Albedo * gi.indirect.diffuse;
  68. #endif
  69. return c;
  70. }
  71. inline void LightingMarmosetDirect_GI (MarmosetOutput s, UnityGIInput giInput, inout UnityGI gi)
  72. {
  73. gi = UnityGlobalIllumination(giInput, 1.0, s.Normal);
  74. }
  75. #endif

MarmosetSurf.cginc:
旧版本的surf代码:
  1. #ifndef MARMOSET_SURF_CGINC
  2. #define MARMOSET_SURF_CGINC
  3. void MarmosetSurf(Input IN, inout MarmosetOutput OUT)
  4. {
  5. #define uv_diff IN.uv_MainTex
  6. #define uv_spec IN.uv_MainTex
  7. #define uv_bump IN.uv_MainTex
  8. #define uv_glow IN.uv_MainTex
  9. //DIFFUSE
  10. #if defined(MARMO_DIFFUSE_DIRECT) || defined(MARMO_DIFFUSE_IBL)
  11. half4 diff = tex2D( _MainTex, uv_diff );
  12. diff *= _Color;
  13. //camera exposure is built into OUT.Albedo
  14. diff.rgb *= _ExposureIBL.w;
  15. OUT.Albedo = diff.rgb;
  16. OUT.Alpha = diff.a;
  17. #ifdef MARMO_PREMULT_ALPHA
  18. OUT.Albedo *= diff.a;
  19. #endif
  20. #else
  21. OUT.Albedo = half3(0.0,0.0,0.0);
  22. OUT.Alpha = 1.0;
  23. #endif
  24.  
  25. //NORMALS
  26. #ifdef MARMO_NORMALMAP
  27. float3 N = UnpackNormal(tex2D(_BumpMap,uv_bump));
  28. #ifdef MARMO_HQ
  29. N = normalize(N);
  30. #endif
  31. OUT.Normal = N; //N is in tangent-space
  32. #else
  33. //OUT.Normal is not modified when not normalmapping
  34. float3 N = OUT.Normal; //N is in world-space
  35. #ifdef MARMO_HQ
  36. N = normalize(N);
  37. #endif
  38. #endif
  39.  
  40. //SPECULAR
  41. #if defined(MARMO_SPECULAR_DIRECT) || defined(MARMO_SPECULAR_IBL)
  42. half4 spec = tex2D(_SpecTex, uv_spec);
  43. float3 E = IN.viewDir; //E is in whatever space N is
  44. #ifdef MARMO_HQ
  45. E = normalize(E);
  46. half fresnel = splineFresnel(N, E, _SpecInt, _Fresnel);
  47. #else
  48. half fresnel = fastFresnel(N, E, _SpecInt, _Fresnel);
  49. #endif
  50.  
  51. //camera exposure is built into OUT.Specular
  52. spec.rgb *= _SpecColor.rgb * fresnel * _ExposureIBL.w;
  53. OUT.Specular = spec.rgb;
  54. half glossLod = glossLOD(spec.a, _Shininess);
  55. OUT.Gloss = glossExponent(glossLod);
  56. //conserve energy by dividing out specular integral
  57. OUT.Specular *= specEnergyScalar(OUT.Gloss);
  58. #endif
  59.  
  60. //SPECULAR IBL
  61. #ifdef MARMO_SPECULAR_IBL
  62. #ifdef MARMO_NORMALMAP
  63. float3 R = WorldReflectionVector(IN, OUT.Normal);
  64. #else
  65. float3 R = IN.worldRefl;
  66. #endif
  67. #ifdef MARMO_SKY_ROTATION
  68. R = mulVec3(_SkyMatrix,R); //per-fragment matrix multiply, expensive
  69. #endif
  70. #ifdef MARMO_MIP_GLOSS
  71. half3 specIBL = glossCubeLookup(_SpecCubeIBL, R, glossLod);
  72. #else
  73. half3 specIBL = specCubeLookup(_SpecCubeIBL, R)*spec.a;
  74. #endif
  75. OUT.Emission += specIBL.rgb * spec.rgb * _ExposureIBL.y;
  76. #endif
  77.  
  78. //DIFFUSE IBL
  79. #ifdef MARMO_DIFFUSE_IBL
  80. N = WorldNormalVector(IN,N); //N is in world-space
  81. #ifdef MARMO_SKY_ROTATION
  82. N = mulVec3(_SkyMatrix,N); //per-fragment matrix multiply, expensive
  83. #endif
  84. half3 diffIBL = diffCubeLookup(_DiffCubeIBL, N);
  85. OUT.Emission += diffIBL * diff.rgb * _ExposureIBL.x;
  86. #endif
  87.  
  88. //GLOW
  89. #ifdef MARMO_GLOW
  90. half4 glow = tex2D(_Illum, uv_glow);
  91. glow.rgb *= _GlowColor.rgb;
  92. glow.rgb *= _GlowStrength;
  93. glow.a *= _EmissionLM;
  94. glow.rgb += OUT.Albedo * glow.a;
  95. OUT.Emission += glow.rgb * _ExposureIBL.w;
  96. #endif
  97. }
  98. #endif

IBL实现的两个关键函数:

  1. half3 diffCubeLookup(samplerCUBE diffCube, float3 worldNormal)
  2. {
  3. half4 diff = texCUBE(diffCube, worldNormal);
  4. return fromRGBM(diff);
  5. }
  6. half3 specCubeLookup(samplerCUBE specCube, float3 worldRefl)
  7. {
  8. half4 spec = texCUBE(specCube, worldRefl);
  9. return fromRGBM(spec);
  10. }

    IBL.diffuse使用world normal查询,IBL.specular使用world reflection查询。

宏定义解析:
MARMO_DIFFUSE_SPECULAR_COMBINED 
    Diffuse/Specular合并
    _MainTex.RGBA = RGB表示Diffuse,A表示Specular强度和Gloss
MARMO_PACKED_UV
    UV打包
  1. #ifdef MARMO_PACKED_UV
  2. o.texcoord.zw = v.texcoord1.xy;
  3. #endif

MARMO_PACKED_VERTEX_COLOR
    顶点色打包
  1. #ifdef MARMO_PACKED_VERTEX_COLOR
  2. o.texcoord.zw = v.color.rg;
  3. o.worldP.w = v.color.b;
  4. #endif

Skyshop.代码解析的更多相关文章

  1. VBA常用代码解析

    031 删除工作表中的空行 如果需要删除工作表中所有的空行,可以使用下面的代码. Sub DelBlankRow() DimrRow As Long DimLRow As Long Dimi As L ...

  2. [nRF51822] 12、基础实验代码解析大全 · 实验19 - PWM

    一.PWM概述: PWM(Pulse Width Modulation):脉冲宽度调制技术,通过对一系列脉冲的宽度进行调制,来等效地获得所需要波形. PWM 的几个基本概念: 1) 占空比:占空比是指 ...

  3. [nRF51822] 11、基础实验代码解析大全 · 实验16 - 内部FLASH读写

     一.实验内容: 通过串口发送单个字符到NRF51822,NRF51822 接收到字符后将其写入到FLASH 的最后一页,之后将其读出并通过串口打印出数据. 二.nRF51822芯片内部flash知识 ...

  4. [nRF51822] 10、基础实验代码解析大全 · 实验15 - RTC

    一.实验内容: 配置NRF51822 的RTC0 的TICK 频率为8Hz,COMPARE0 匹配事件触发周期为3 秒,并使能了TICK 和COMPARE0 中断. TICK 中断中驱动指示灯D1 翻 ...

  5. [nRF51822] 9、基础实验代码解析大全 · 实验12 - ADC

    一.本实验ADC 配置 分辨率:10 位. 输入通道:5,即使用输入通道AIN5 检测电位器的电压. ADC 基准电压:1.2V. 二.NRF51822 ADC 管脚分布 NRF51822 的ADC ...

  6. java集合框架之java HashMap代码解析

     java集合框架之java HashMap代码解析 文章Java集合框架综述后,具体集合类的代码,首先以既熟悉又陌生的HashMap开始. 源自http://www.codeceo.com/arti ...

  7. Kakfa揭秘 Day8 DirectKafkaStream代码解析

    Kakfa揭秘 Day8 DirectKafkaStream代码解析 今天让我们进入SparkStreaming,看一下其中重要的Kafka模块DirectStream的具体实现. 构造Stream ...

  8. linux内存管理--slab及其代码解析

    Linux内核使用了源自于 Solaris 的一种方法,但是这种方法在嵌入式系统中已经使用了很长时间了,它是将内存作为对象按照大小进行分配,被称为slab高速缓存. 内存管理的目标是提供一种方法,为实 ...

  9. MYSQL常见出错mysql_errno()代码解析

    如题,今天遇到怎么一个问题, 在理论上代码是不会有问题的,但是还是报了如上的错误,把sql打印出來放到DB中却可以正常执行.真是郁闷,在百度里面 渡 了很久没有相关的解释,到时找到几个没有人回复的 & ...

随机推荐

  1. 学习51cto中美团中的小知识点--组件实现按需求加载

    1====>vue.20脚手架的创建 cnpm install --global vue-cli 全局安装脚手架 vue init webpack my-project 创建项目 Use ESL ...

  2. 7.Java基础_Java数据输入

    import java.util.Scanner; public class Output { public static void main(String[] args){ Scanner sc=n ...

  3. 【转】gdb的调试与使用

    转载自:https://www.jianshu.com/p/7a06b0bda2d8 gdb的调试与使用 这篇应该是我见过的总结最详细的gdb调试指南了,这位博主是个很强的人,他的博客对萌新比较友好, ...

  4. ubuntu16.04matlab中文注释乱码的解决办法

    中文注释乱码的原因是windows下的m文件采用的是gb2312编码,只要将所有的m文件转成 utf8文件,显示就正常了. 1.首先安装enca:sudo apt-get install enca 2 ...

  5. luoguP2824 [HEOI2016/TJOI2016]排序(线段树分裂做法)

    题意 所谓线段树分裂其实是本题的在线做法. 考虑如果我们有一个已经排好序的区间的权值线段树,那么就可以通过线段树上二分的方法得到第\(k\)个数是谁. 于是用set维护每个升序/降序区间的左右端点以及 ...

  6. 自定义安装office

    自定义安装office 1.下载office安装包:https://msdn.itellyou.cn 2.下载offiice部署工具:https://www.microsoft.com/en-us/d ...

  7. Nginx企业级优化

    Nginx企业级优化 一.隐藏版本号信息 安装软件前修改,源码包中的版本信息 #切换到源码包目录[root@localhost ~]# cd /usr/src/nginx-1.15.9/[root@l ...

  8. Note | PyTorch1.2 + CUDA10.0 + cuDNN7.6 + Anaconda3配置

    目标: 在2080Ti GPU上,运行PyTorch1.2 GPU版本. 经过确认,PyTorch1.2可以搭配CUDA10.0,而CUDA10.0搭配cuDNN7.6(官网下载页面可以直接看到). ...

  9. B1013

    python语言运行这道题有一个点运行超时,需要对求素数的算法进一步的优化 def isPrime(n): if n <= 1: return False i = 2 while i * i & ...

  10. linux jdk1.8 64位下载永久地址,ubuntu,centos,java

    https://pan.baidu.com/s/1A4cl3vUWCtiHxJ9eHK2ApQ  密码:j8dg