【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1777|回复: 0

4-4 On Alarm 场景阴影和警报灯闪烁的制作

[复制链接]
发表于 2013-9-30 22:06:15 | 显示全部楼层 |阅读模式
如何在游戏中创建警报灯,并通过脚本控制灯光的衰减。
--------------------------------------------------------------------------------------------------------------------------
书本原文节选: QQ图片20131017002914.jpg
--------------------------------------------------------------------------------------------------------------------------
C#代码
  1. using UnityEngine;
  2. using System.Collections;

  3. public class AlarmLight : MonoBehaviour
  4. {
  5.     public float fadeSpeed = 2f;          // 光渐渐淡化的速度.
  6.     public float highIntensity = 2f;       // 报警灯最高的亮度.
  7.     public float lowIntensity = 0.5f;      // 报警灯最低的亮度.
  8.     public float changeMargin = 0.2f;    // 目标强度变化的范围.
  9.     public bool alarmOn;                // 报警灯是否被激活.

  10.     private float targetIntensity;          // 当前光强度
  11.    
  12.    
  13.     void Awake ()
  14.     {
  15.         // 当游戏刚刚启动的时候,报警灯的强度为0,将不会对场景产生影响
  16.         light.intensity = 0f;
  17.         
  18.         // 当第一次警报灯被激活的时候, 警报色应该是最亮的
  19.         targetIntensity = highIntensity;
  20.     }
  21.    
  22.    
  23.     void Update ()
  24.     {
  25.         // 当警报灯激活响起
  26.         if(alarmOn)
  27.         {
  28.             // ... 循环操作光的从当前强度变为targetIntensity强度
  29.             light.intensity = Mathf.Lerp(light.intensity, targetIntensity, fadeSpeed * Time.deltaTime);
  30.             
  31.             // 切换光强变弱还是从弱变强
  32.             CheckTargetIntensity();
  33.         }
  34.         else
  35.             // 如果警报没有激活则将警报色强度渐变为0.
  36.             light.intensity = Mathf.Lerp(light.intensity, 0f, fadeSpeed * Time.deltaTime);
  37.     }
  38.    
  39.    
  40.     void CheckTargetIntensity ()
  41.     {
  42.         // 如果灯光当前的亮度接近亮度范围的一端了,则需要将当targetIntensity颜色变成另外一个端的颜色....
  43.         if(Mathf.Abs(targetIntensity - light.intensity) < changeMargin)
  44.         {
  45.             // ... 如果当前的灯光已经最亮了...
  46.             if(targetIntensity == highIntensity)
  47.                 // ... 则将targetIntensity的值变成最低.
  48.                 targetIntensity = lowIntensity;
  49.             else
  50.                 //否则将targetIntensity的值变成最高.
  51.                 targetIntensity = highIntensity;
  52.         }
  53.     }
  54. }
复制代码
JS代码
  1. #pragma strict

  2. public var fadeSpeed : float = 2f;          // How fast the light fades between intensities.
  3. public var highIntensity : float = 2f;      // The maximum intensity of the light whilst the alarm is on.
  4. public var lowIntensity : float = 0.5f;     // The minimum intensity of the light whilst the alarm is on.
  5. public var changeMargin : float = 0.2f;     // The margin within which the target intensity is changed.
  6. public var alarmOn : boolean;               // Whether or not the alarm is on.


  7. private var targetIntensity : float;        // The intensity that the light is aiming for currently.


  8. function Awake ()
  9. {
  10.     // When the level starts we want the light to be "off".
  11.     light.intensity = 0f;
  12.    
  13.     // When the alarm starts for the first time, the light should aim to have the maximum intensity.
  14.     targetIntensity = highIntensity;
  15. }


  16. function Update ()
  17. {
  18.     // If the light is on...
  19.     if(alarmOn)
  20.     {
  21.         // ... Lerp the light's intensity towards the current target.
  22.         light.intensity = Mathf.Lerp(light.intensity, targetIntensity, fadeSpeed * Time.deltaTime);
  23.         
  24.         // Check whether the target intensity needs changing and change it if so.
  25.         CheckTargetIntensity();
  26.     }
  27.     else
  28.         // Otherwise fade the light's intensity to zero.
  29.         light.intensity = Mathf.Lerp(light.intensity, 0f, fadeSpeed * Time.deltaTime);
  30. }


  31. function CheckTargetIntensity ()
  32. {
  33.     // If the difference between the target and current intensities is less than the change margin...
  34.     if(Mathf.Abs(targetIntensity - light.intensity) < changeMargin)
  35.     {
  36.         // ... if the target intensity is high...
  37.         if(targetIntensity == highIntensity)
  38.             // ... then set the target to low.
  39.             targetIntensity = lowIntensity;
  40.         else
  41.             // Otherwise set the targer to high.
  42.             targetIntensity = highIntensity;
  43.     }
  44. }
复制代码
涉及到的知识点:
灯光 Light

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-11-27 11:00

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表