zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    android switch控件的使用
    触摸屏校准tslib的配置文件
    matlab 函数的编写与调用
    penmount串口触摸屏加载
    FPGA保留信号的语句
    ioctl和unlock_ioctl的区别
    内核目录中增加自己的目录
    linux内核打印"BUG: scheduling while atomic
    28335外部中断
    编译QT时出现lib/libQtGui.so: undefined reference to `ts_read_raw'的解决办法
  • 原文地址:https://www.cnblogs.com/gabo/p/4245248.html
Copyright © 2011-2022 走看看