最近在做unity shader forge和marmoset的优化,TA那边遇到了一个阴影显示的问题,具体如下:

  在Forward Rendering状态下,静态场景使用了是shader forge生成的blendlayer类的shader,使用lightmap烘培贴图后,动态角色走到静态物体附近时,方向光在角色上的投影,无法投射到使用shader forge材质的物体上,但其他材质和使用marmoset的材质可以正常接收。查询了一些网站解决方案后,最终确定是custom shader 写法的问题,shader forge生成的shader属于自己实现vs和ps的功能,和写surface shader不同,除了着色用的shader pass("LightMode" = "ForwardBase")外需要Cast shadow用的第2个pass {"LightMode" = "ForwardAdd"}。
 
下面是一个参考方案的代码
  1.  "Test" {
    SubShader {
    Tags { "RenderType" = "Opaque"} // This pass acts the same as the surface shader first pass.
    // Calculates per-pixel the most important directional light with shadows,
    // then per-vertex the next 4 most important lights,
    // then per-vertex spherical harmionics the rest of the lights,
    // and the ambient light value. Pass {
    Tags {"LightMode" = "ForwardBase"}
    CGPROGRAM #pragma multi_compile_fwdbase
    #pragma vertex vert
    #pragma fragment frag
    #pragma fragmentoption ARB_precision_hint_fastest
    #include "UnityCG.cginc"
    #include "AutoLight.cginc" struct Input
    {
    float4 pos : SV_POSITION;
    float3 vlight : COLOR;
    float3 lightDir : TEXCOORD1;
    float3 vNormal : TEXCOORD2;
    LIGHTING_COORDS(,)
    }; Input vert(appdata_full v)
    {
    Input o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    // Calc normal and light dir.
    o.lightDir = normalize(ObjSpaceLightDir(v.vertex));
    o.vNormal = normalize(v.normal).xyz; // Calc spherical harmonics and vertex lights. Ripped from compiled surface shader
    float3 worldPos = mul(_Object2World, v.vertex).xyz;
    float3 worldNormal = mul((float3x3)_Object2World, SCALED_NORMAL);
    o.vlight = float3();
    #ifdef LIGHTMAP_OFF float3 shlight = ShadeSH9(float4(worldNormal, 1.0));
    o.vlight = shlight;
    #ifdef VERTEXLIGHT_ON
    o.vlight += Shade4PointLights ( unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, unity_LightColor[].rgb, unity_LightColor[].rgb, unity_LightColor[].rgb, unity_LightColor[].rgb, unity_4LightAtten0, worldPos, worldNormal ); #endif // VERTEXLIGHT_ON
    #endif // LIGHTMAP_OFF
    TRANSFER_VERTEX_TO_FRAGMENT(o);
    return o;
    } float4 _LightColor0; // Contains the light color for this pass. half4 frag(Input IN) : COLOR
    {
    IN.lightDir = normalize ( IN.lightDir );
    IN.vNormal = normalize ( IN.vNormal ); float atten = LIGHT_ATTENUATION(IN);
    float3 color;
    float NdotL = saturate( dot (IN.vNormal, IN.lightDir ));
    color = UNITY_LIGHTMODEL_AMBIENT.rgb * ; color += IN.vlight; color += _LightColor0.rgb * NdotL * ( atten * );
    return half4(color, 1.0f);
    } ENDCG
    } // Take this pass out if you don't want extra per-pixel lights.
    // Just set the other lights' Render Mode to "Not Important",
    // and they will be calculated as Spherical Harmonics or Vertex Lights in the above pass instead. Pass { Tags {"LightMode" = "ForwardAdd"} CGPROGRAM #pragma multi_compile_fwdadd
    #pragma vertex vert
    #pragma fragment frag
    #pragma fragmentoption ARB_precision_hint_fastest
    #include "UnityCG.cginc"
    #include "AutoLight.cginc" struct Input
    {
    float4 pos : SV_POSITION;
    float3 lightDir : TEXCOORD1;
    float3 vNormal : TEXCOORD2;
    }; Input vert(appdata_full v)
    {
    Input o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex); // Calc normal and light dir.
    o.lightDir = normalize(ObjSpaceLightDir(v.vertex));
    o.vNormal = normalize(v.normal).xyz; // Don't need any ambient or vertex lights in here as this is just an additive pass for each extra light.
    // Shadows won't work here, either.
    return o;
    } float4 _LightColor0; // Contains the light color for this pass. half4 frag(Input IN) : COLOR
    {
    IN.lightDir = normalize ( IN.lightDir );
    IN.vNormal = normalize ( IN.vNormal ); float3 color;
    float NdotL = saturate( dot (IN.vNormal, IN.lightDir ));
    color = _LightColor0.rgb * NdotL;
    return half4(color, 1.0f);
    } ENDCG }
    } FallBack "Diffuse" }
 
而使用surface shader时候,只需要在声明surface时,设置制定的option(fullforwardshadows)就可以了
  1. #pragma surface MarmosetSurfMarmosetDirect vertex:MarmosetVert fullforwardshadows
回过头来说shader forge插件,场景左侧使用sf生成的shader,use lightmap后,实时阴影果断看不到了
 
左边木板为surface shader,右边为VS+PS Shader,阴影无法投射到石头上
 
将shader forge设置为multi-light,他会自动生成ForwardAdd的pass,这样阴影就显示出来了。但照明效果也完全变了样子
 
这个问题,在shader forge的论坛上也有提过,作者也说修改过这个bug了,但实际上最新的0.36还是没有完全解决
 
如果设置ForwardAdd后还没有投影,可以把light的lightmapping 设置为realtimeOnly。或者是再复制一个投影的主光源。
 
结论,Shader forge还是慎用吧,而且shader node也远没有比sky shop这类 uber shader要节省,为了投影,导致使用lightmap和real time的效果差别很大,
完全是得不偿失的。
 
 
 
 
 
 
 

关于Unity动态物体无法向使用使用custom shader和lightmap的物体投射阴影的更多相关文章

  1. Unity 3D动态修改Shader状态,使物体透明等等

    Unity动态改Shader状态透明 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  2. Unity动态加载和内存管理(三合一)

    原址:http://game.ceeger.com/forum/read.php?tid=4394#info 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Re ...

  3. Unity动态批处理和静态批处理学习

    本文转自:http://blog.csdn.net/lyh916/article/details/45725499,请点击链接查看楼主大神原文,尊重楼主版权. 参考链接:Unity圣典:http:// ...

  4. [Unity][Heap sort]用Unity动态演示堆排序的过程(How Heap Sort Works)

    [Unity][Heap sort]用Unity动态演示堆排序的过程 How Heap Sort Works 最近做了一个用Unity3D动态演示堆排序过程的程序. I've made this ap ...

  5. Unity动态字体在手机上出现字体丢失问题解决

    在我们游戏的开发过程中,在部分手机上运行游戏的时候,出现了字体丢失的问题,出问题的手机似乎用的都是高通芯片. 使用的unity是4.2.0版本,ngui是3.4.9版本. 在unity的论坛及unit ...

  6. 【Unity Shaders】使用CgInclude让你的Shader模块化——Unity内置的CgInclude文件

    本系列主要參考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同一时候会加上一点个人理解或拓展. 这里是本书全部的插图. 这里是本书所需的代码 ...

  7. Unity动态改变物体遮挡关系

    在动态创建物体时,通常同父级下先创建的子物体会被后创建的遮挡,此时就需要我们用代码改变对象的层级. GameObject go;go.transform.SetAsLastSibling();//设置 ...

  8. unity动态加载(翻译) .

    AssetBundles are files which you can export from Unity to contain assets of your choice. These files ...

  9. Unity 动态载入Panel并实现淡入淡出

    unity版本:4.5 NGUI版本:3.6.5 参考链接:http://tieba.baidu.com/p/3206366700,作者:百度贴吧 水岸上 动态载入NGUI控件,这里用Panel为例说 ...

随机推荐

  1. The resource identified by this request is only capable of generating responses with characteristics

    [转]今天在调试springMVC的时候,在将一个对象返回为json串的时候,浏览器中出现异常: The resource identified by this request is only cap ...

  2. python日常-list and dict

    什么是list: list 觉得算是python日常编程中用的最多的python自带的数据结构了.但是python重的list跟其他语言中的并不相同. 少年..不知道你听说过python中的appen ...

  3. java-获取随机字符串

    import java.util.Random; public class getRandomString { public static String excute(int length) { St ...

  4. python面试题目

    问题一:以下的代码的输出将是什么? 说出你的答案并解释. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Parent(object):     x = 1   clas ...

  5. 24 映射-Map

    什么是映射(Map) 映射中的每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复 key1 value1 key2 value2 key3 value3 key4 value4 key5 ...

  6. 【BZOJ-2836】魔法树 树链剖分

    2836: 魔法树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 323  Solved: 129[Submit][Status][Discuss] ...

  7. 【poj2478】 Farey Sequence

    http://poj.org/problem?id=2478 (题目链接) 题意 求分母小于等于n的真分数的个数. Solution 现在只能做做水题了,唉,思维僵化. 细节 前缀和开LL 代码 // ...

  8. 树莓派利用PuTTY进行远程登录

    得到树莓派的IP:192.168.199.220 打开PuTTY: 端口为22 选择SSH 点击Open: 输入账号密码:pi/raspberry(注意,在linux下输入密码是看不见的) 如果要使用 ...

  9. 20150706 test2

    净心守志:可会至道.譬如磨镜:垢去明存.断欲无求:当得宿命

  10. CSS基础知识真难啊-position-relative-absolute

    http://blog.csdn.net/libertea/article/details/11662661 -----------position:relative:生成相对定位的元素,相对于其正常 ...