Code Monkey home page Code Monkey logo

Comments (1)

JOKULLIU avatar JOKULLIU commented on May 22, 2024

烘培已解决,代码这样写(好押韵):
这是根据网上的一个例子改写过来的。
在Pass开始地方添加
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
(然而我并不懂这是什么意思。)
在a2v结构体中添加
half2 uv1 : TEXCOORD1;
个人猜测是用来接收lightmap坐标的。(废话)
在v2f中写上

#ifdef LIGHTMAP_ON
    half2 uvLM : TEXCOORD6;
#endif

网上例子的原文是#ifndef LIGHTMAP_OFF,如果没有定义LIGHTMAP_OFF,为啥要用双重否定这么拗口的文义呢,我改了。
在vert函数里面写

#ifdef LIGHTMAP_ON
    o.uvLM = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;  
#endif 

原文同样是#ifndef LIGHTMAP_OFF,被我改了,下面都是这种情况就不赘述了。这段代码倒是很简单。
在frag里面写

half4 finalColor;
#ifdef LIGHTMAP_ON
    finalColor.rgb = diff.rgb;
#else
    finalColor.rgb = diff.rgb * _LightColor0.rgb * _MainTint.rgb * halfLambert + ambient;
    finalColor.rgb *= atten;
    #if UNITY_SHOULD_SAMPLE_SH
        finalColor.rgb += i.sh;
    #endif
#endif  

这段代码意思我理解是如果光照图已经存在了,那么只需要主纹理就行了,如果光照图不存在,那么主光源、环境光、Lambert光、sh光统统给我加上去……(烘培后这些信息都保存在光照图里面了)。
最后一段代码非常重要,感觉很多人都在这里出错了。

#ifdef LIGHTMAP_ON
    fixed3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uvLM.xy));  
    finalColor.rgb *= lm;  
#endif 
return finalColor;

对光照图进行采样后,乘到finalColor上去。采样的函数不是tex2D()这点很重要。
我直接return lm看了光照图到底保存了哪些信息,这样更加深刻理解了光照图的好处。
以上代码亲测可用,而且和Unity自带的diffuse shader做了对比差异几乎看不出。
只在basepass上添加即可,addpass不用管。
至于DecodeLightmap容我再查查是什么含义。
Decode……看文义好像是解码……
我来填坑了。
DecodeLightmap是定义在UnityCG.cginc中的一个函数,它的作用是解码从lightmap中采样出的颜色。Unity使用了两种编码方式来存储lightmap:
1、doubleLDR,需要一张rgb24贴图
2、RGBM,需要一张rgba32贴图
在移动设备上使用doubleLDR格式,可以获得更快的计算速度。它只用到了lightmap的RGB通道。
PC上则使用RGBM格式,可以获得更广的亮度范围,而牺牲一点速度。它使用了贴图的RGBA通道,而A通道是用来做乘法,所以称为RGBM格式。
这两种格式的差异就是导致不同平台下lightmap表现不同的原因,当然Unity会在切换平台时帮我们对贴图进行转换,而不需要太关心这个差异。
那么,什么是RGBM呢?就是把[0,1]扩展到[0,8]以表现更高的亮度范围的画面效果,乘数存储在A通道里面,所以RGBM是一种更大范围的颜色编码方式。
接下来很多字不想打了。
………………
坑越填越多……
到此为止,不填了。

from unity_shaders_book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.