Unity 过度光照贴图

背景:开关窗帘过程,让环境在亮和暗之间过度

 

事先烘培出亮、暗两张Lighting map。然后代码实现,窗帘开关由动作实现,而代码中通过动作执行进度来过度两张Lighting map

技术分享
void OnAnimatorMove()
{
    AnimatorTransitionInfo transitionInfo = animator.GetAnimatorTransitionInfo(0);
    if (transitionInfo.normalizedTime != 0)//状态切换中
    {
    }
    else
    {
        AnimatorStateInfo currentAnimatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);

        // 开窗
        if (currentAnimatorStateInfo.IsName("opening"))
        {
            LightmapBlender.Instance.OpenWindow(currentAnimatorStateInfo.normalizedTime);
        }

        // 关窗
        if (currentAnimatorStateInfo.IsName("closing"))
        {
            LightmapBlender.Instance.CloseWindow(currentAnimatorStateInfo.normalizedTime);
        }
    }
动作控制代码
技术分享
 1 public Texture2D normalTexture2D;
 2 public Texture2D closeWindowTexture2D;
 3 public Texture2D dancingTexture2D;
 4 
 5 public Texture2D blendTexture2D;
 6 
 7 private Color[] normalColors;
 8 private Color[] closeWindowColors;
 9 private Color[] dancingColors;
10 
11 private Color[] blendColors;
12 
13 [SerializeField]
14 private Transform brightTransform, darkTransform;
15 
16 [SerializeField]
17 private Color brightAmbientLight, darkAmbientLight;
18 
19 void Awake ()
20 {
21     normalColors = normalTexture2D.GetPixels();
22     closeWindowColors = closeWindowTexture2D.GetPixels();
23 
24     if (dancingTexture2D != null)
25         dancingColors = dancingTexture2D.GetPixels();
26 
27     blendColors = blendTexture2D.GetPixels();
28 }
29 
30 public void OpenWindow(float t)
31 {
32     brightTransform.gameObject.SetActive(true);
33     darkTransform.gameObject.SetActive(false);
34 
35     Blend2Textures(closeWindowColors, normalColors, t);
36     RenderSettings.ambientLight = Blend2Color(darkAmbientLight, brightAmbientLight, t);
37 }
38 
39 public void CloseWindow(float t)
40 {
41     brightTransform.gameObject.SetActive(false);
42     darkTransform.gameObject.SetActive(true);
43 
44     Blend2Textures(normalColors, closeWindowColors, t);
45 
46     // 过度环境光(影响没烘培在Lighting map 中的对象的明暗)
47     RenderSettings.ambientLight = Blend2Color(brightAmbientLight, darkAmbientLight, t);
48 }
49 
50 private Color Blend2Color(Color from, Color to, float t)
51 {
52     Color blend;
53 
54     blend.r = from.r * (1 - t) + to.r * t;
55     blend.g = from.g * (1 - t) + to.g * t;
56     blend.b = from.b * (1 - t) + to.b * t;
57     blend.a = from.a * (1 - t) + to.a * t;
58 
59     return blend;
60 }
61 
62 private void Blend2Textures(Color[] from, Color[] to, float t)
63 {
64     for (int i = 0; i < blendColors.Length; i++)
65         blendColors[i] = Blend2Color(from[i], to[i], t);
66     
67     blendTexture2D.SetPixels(blendColors);
68     blendTexture2D.Apply();
69 
70     SwitchLightmaps(blendTexture2D);
71 }
72 
73 private void SwitchLightmaps(Texture2D tex)
74 {
75     LightmapData[] lightmaps = LightmapSettings.lightmaps;
76     
77     lightmaps[0].lightmapFar = tex;
78 
79     // 切换 Lighting map
80     LightmapSettings.lightmaps = lightmaps;
81 }
插值过度Lighting map

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。