码迷,mamicode.com
首页 > 编程语言 > 详细

Unity 过度光照贴图

时间:2015-01-24 00:15:28      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

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

 

事先烘培出亮、暗两张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

 

Unity 过度光照贴图

标签:

原文地址:http://www.cnblogs.com/gabo/p/4245248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!