Stealth - Basic structures and the underlying logic of the scene 01

Copyright notice:

  • This article Original starting in the following website:
  1. Blog Park "excellent dream maker culture" in space: https: //www.cnblogs.com/raymondking123
  2. Excellent dream maker culture official blog: https: //91make.top
  3. Excellent game dream maker culture lecture: https: //91make.ke.qq.com
  4. "Excellent dream maker culture" of micro-channel public number: umaketop
  • You are free to reprint, but must include the full copyright notice

Set up the scene:

  • Scene layout, setting static property, provided Layer layer, adjust attributes (Carmera, Lighting)

  • For general rendering complex scenes we will do very fine (has a lot triangles), but such an internal collision detection event can be a simple point, the scene will have a low modulus mesh characterized crash, reducing performance overhead // mesh collider the convex option indicates that the model is a convex polygon, (generally we create is a convex polygon, like cave ah what might concave polygon)
  • The scenes are set to Static // navigation light shielding is static // participate Lightmaps baking unity of objects are static objects must
  • Increasing light, baked on the scene, as the contents of the texture after baking, attached to the object
  • Set light effects:


Add a light:

  • Lighting Baking: Baking of the Light property to Baked, baking object need to static, can Precomputed realtime GI (Real-time illumination precomputed removed) in Lighting-> Scene // after baking will be completed in the light of the object generated map information (a file have the same name as the folder Scene) affixed to the object is set to the Static
  • Light backed Shadow Radius determines the radius of the back scatter type light shine out of the shadow, but the value is 0, which means no scattering, and a soft shadow hard Shadow this case there is no difference, but the larger the value, the soft shadow shadow scattered more severe
  • Realtime option is selected under the soft shadow of Light in the form of parameters such as the number of reflections is affected, and the need to open the precomputed realtime GI in Lighting-> scene, turn off Baked GI's
  • The Mixed Light illumination pattern, comprising the real-time computing and Baked two functions, when the object is not moving, light did not change, the lighting information baked employed; when the object moves, using real-time calculations precomputer teatime with light shadows // heard much? The object is to achieve a static perspective to shadow the dynamic objects, because the static object does not move, so the shadow is basically fixed, bake good shadow maps, dynamic affixed to a dynamic object?
  • Light Culling Mask expressed in the object tag type lighting effect, do not select the tag from light influence

  • Increasing an alarm lamp lights and scene, the scene lights provide care scenario, when the warning lamp and sound effects found visual alarm after players:

  • Warning light script:
public class AlarmLight : MonoBehaviour
{
    public float fadeSpeed = 2f;  //灯光报警fade的速度(默认2s变化一次)
    public float hightIntensity = 4f; //最高最低亮度
    public float lowIntensity = 0.5f;
    public float changeMargin = 0.2f; //插值阈值
    public bool alarmOn;

    private float targetIntensity; //目标亮度值
    private Light alarmLight;
    private AudioSource audioSource;

    private void Awake()
    {
        alarmLight = GetComponent<Light>();
        audioSource = GetComponent<AudioSource>();
        alarmLight.intensity = 0;
        targetIntensity = hightIntensity;
    }

    // Update is called once per frame
    void Update()
    {
        if (alarmOn)
        {
            if (!audioSource.isPlaying)
                audioSource.Play();

            alarmLight.intensity = Mathf.Lerp(alarmLight.intensity, targetIntensity, fadeSpeed * Time.deltaTime);

            if (Mathf.Abs(targetIntensity - alarmLight.intensity) < changeMargin)
            {
                if (targetIntensity == hightIntensity)
                    targetIntensity = lowIntensity;
                else
                    targetIntensity = hightIntensity;
            }
        }
        else
        {
            if (audioSource.isPlaying)
                audioSource.Stop();

            alarmLight.intensity = Mathf.Lerp(alarmLight.intensity, 0, fadeSpeed * Time.deltaTime);
        }
    }
}

Baking scene Lightmaps

  • // bake baking scene using scene lighting type baked, generate maps attached to the surface of an object for expressing lighting effects

  • Unity's official explanation:
  • Baking meanings: When using a single light source in real time Unity, these rays are not automatically reflected. In order to create a more realistic scenes using global illumination technology, we need to enable the Unity of pre-computed lighting solutions; Unity can calculate complex static lighting effects (using a technique called global illumination (referred to as GI) of) and storing them in said texture map lightmaps by reference. This calculation process is called baking. When lightmaps baked, will calculate the effect of light on the static objects in the scene, and writes the result in the texture, the textures on the cover in the scene geometry to create lighting effects.

  • (These may include both lightmaps direct light irradiated to the surface, it may also include indirect light reflected from other objects or surfaces within the scene. The color of light texture may be the object (albedo) and reliefs (normal) or the like surface information associated with the use of the material shader.
  • When using baking light, these light map can not be changed during the game, so called "static." The light source can overlap in real time and superimposed on the use of light-mapped scene, but can not change the light texture itself in real time.
  • In this way, we can move in the game our illumination light by reducing the amount of computation in real time potentially improve performance, lower performance hardware adaptation, mobile platform)

  • Precomputed real-time global illumination
  • Although static Lightmap not respond to changing conditions of light in the scene, but is expected to be considered real-time GI it does provide a technique for complex scene illumination can be updated in real-time for us.
  • By this method, you can create a rich global illumination light and the reflected light environment, real-time response to illumination change. A typical example of this is the system time of day: the position and color of the light varies with time. If you are using traditional baking light, which can not be achieved

  • Advantages and costs
  • Although you can use both light and baking GI GI real-time pre-calculated, but be aware that while rendering the sum of the performance overhead of the two systems is their cost. We not only need to store two sets of light maps in video memory, but also a process of decoding in the shader.
  • What is the nature and selection of the light depends on the target hardware project performance under what circumstances. For example, the larger the video memory and processing power limitations of the mobile terminal, baking GI illumination methods may have higher performance. On a standalone computer or game console has the latest dedicated graphics hardware, it may be possible to use pre-computed in real-time GI, or even use both systems.
  • We must decide which approach based on the nature of the particular project and the desired target platform. Keep in mind, while facing a range of different hardware, in general, minimal-performance hardware will determine which method to choose.

  • Add a light:
  • Render Mode indicates the type of rendering: Important represents a pixel by pixel rendering of light, Not Impritant vertex shader represented by lights, Auto Auto represents

  • Set rendering quality: Project Setting -> Quality panel, because we play in the scene above normal resources, there may be efficiency, so we render quality we choose Good; Rendering setting in Pixel Light Cout (the number of pixels rendered by light), because the type of light we have selected Auto, and all should Unity will pick six lights per-pixel rendering, the other per-vertex rendering (Unity how to pick ???)

  • Set lightmaps settings, then you can begin the Bake:
  • Specific parameters can refer to the Unity official documents, 2018 start of Lightmapper Unity provides a new Progressive bake, this gradual manner lightmaps baking, a simple scene from the baking time to 10 hours

  • And scenes of the same name can be found in the baking Scenes after the completion of the data file is stored after baking in the folder it will be used as textures, affixed to the object
  • Texture after baking done automatically attached to the object scene, all the lights off at this time, care objects still having an effect


Add Tag management class // static variables used to define a string of Tag

  • Add transition effects // Load the scene gradually brighten, darken gradually withdraw from the scene
  • Use RawImage occlusion achieve this screen, code portions:
public class ScreenFadeInOut : MonoBehaviour
{
    public float fadeSpeed = 1.5f;

    private bool sceneStarting;
    private RawImage rawImage;

    // Start is called before the first frame update
    void Start()
    {
        sceneStarting = true;
        rawImage = this.GetComponent<RawImage>();
    }

    // Update is called once per frame
    void Update()
    {
        if (sceneStarting)
        {
            rawImage.color = Color.Lerp(rawImage.color, Color.clear, fadeSpeed * Time.deltaTime);

            if (rawImage.color.a <= 0.05f)
            {
                rawImage.color = Color.clear;
                sceneStarting = false;
                rawImage.enabled = false;
            }
        }
    }

    public void EndScene()
    {
        rawImage.enabled = true;
        rawImage.color = Color.Lerp(rawImage.color, Color.black, fadeSpeed * Time.deltaTime);
        if (rawImage.color.a > 0.95f)
            SceneManager.LoadScene(0);
    }
}
  • GameController // add a game controller responsible for controlling the background music, role management position
  • Add to GameController Audio files, // here we added two Audio are set to play ON Awake and loop, the difference is that the effect of normal volume when the master volume is 1, Panic was found is 0 // we have to realize is that when when a player is found, the master volume decreased, Panic gradually increase the volume
  • Specifically Screenplay:
public class LastPlayerSighting : MonoBehaviour
{
    public Vector3 position = new Vector3(1000f, 1000f, 1000f); //表示玩家最后一次被发现的位置,如果没有被发现,就设置为默认值
    public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f);
    public float lightHighIntensity = 0.25f;  //主灯光的亮度范围
    public float lightLowIntensity = 0f;
    public float lightFadeSpeed = 7f;
    public float musicFadeSpeed = 1f;  //音乐变化的fade速率
    public bool isPlayerFound = false;

    private AlarmLight alarmLightScript;
    private Light mainLight;          //主灯光
    private AudioSource mainMusic;  //主音乐和panic时播放的音乐
    private AudioSource panicMusic;
    private AudioSource[] sirens;  //报警音乐
    private const float muteVolume = 0f; //音乐的变化范围
    private const float normalVolume = 0.8f;

    // Start is called before the first frame update
    void Start()
    {
        alarmLightScript = GameObject.FindGameObjectWithTag(Tags.ALARM_LIGHT).GetComponent<AlarmLight>();
        mainLight = GameObject.FindGameObjectWithTag(Tags.MAIN_LIGHT).GetComponent<Light>();
        mainMusic = this.GetComponent<AudioSource>();
        panicMusic = this.transform.Find("Secondary_music").GetComponent<AudioSource>();
        //sirens = new AudioSource[];
    }

    // Update is called once per frame
    void Update()
    {
        isPlayerFound = (position != resetPosition);

        //当玩家被发现时,调低主灯光,打开报警灯,淡出主音乐,淡入panic音乐, 但玩家脱离危险后恢复;
        mainLight.intensity = Mathf.Lerp(mainLight.intensity, isPlayerFound ? lightLowIntensity : lightHighIntensity, lightFadeSpeed * Time.deltaTime);
        alarmLightScript.alarmOn = isPlayerFound;
        mainMusic.volume = Mathf.Lerp(mainMusic.volume, isPlayerFound ? muteVolume : normalVolume, musicFadeSpeed);
        panicMusic.volume = Mathf.Lerp(panicMusic.volume, isPlayerFound ? normalVolume : muteVolume, musicFadeSpeed);
    }
}

Adding CCTV Carmera

  • // // CCTV CCTV collision triggered dependent on Riggdbody components, no Riggdbody will not trigger the trigger and collision (in order to prevent two separate collider trigger each other)
  • Add the model Carmera
  • Adding mesh collider spot light source and
  • Adding collision script:
public class CCTVCollision : MonoBehaviour
{

    private LastPlayerSighting lastPlayerSighting;

    private void Start()
    {
        lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.GAMECONTROLLER).GetComponent<LastPlayerSighting>();
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.tag == Tags.PLAYER)
        {
            lastPlayerSighting.position = other.transform.position;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.tag == Tags.PLAYER)
        {
            lastPlayerSighting.position = lastPlayerSighting.resetPosition;
        }
    }
}
  • Add to rotate Animator, Animator pay attention to adjust the animation curves can be set in the rate of change

Add Laser Grid (laser fences)

  • // get caught on ships Main Camera Audio Listener for a radio (only one scene of one Audio Listener)
  • // unity in the current version Audio Source To set up Spatial Blend to 3D, the 3D Sound Settings below take effect (there will be a space-related attenuation)
  • Add laser model (and matching it and scene), box collider, Light (red point light source) and the Audio Source
  • Add script Laser, laser control switches and collision detection fence:
  • Fence laser switch:
public class LasterBlinking : MonoBehaviour
{
    public float onTime;  //灯灭的时间间隔
    public float offTime;  //灯亮的时间间隔

    private float timer;  //流逝的时间
    private Renderer laserRenerer;
    private Light laserLight;

    // Start is called before the first frame update
    void Start()
    {
        laserRenerer = GetComponent<Renderer>();
        laserLight = GetComponent<Light>();
        timer = 0;
    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;
        if (laserRenerer.enabled && timer >= onTime)
        {
            laserRenerer.enabled = false;
            laserLight.enabled = false;
            timer = 0;
        }
        else if (!laserRenerer.enabled && timer >= offTime)
        {
            laserRenerer.enabled = true;
            laserLight.enabled = true;
            timer = 0;
        }
    }
}
  • Impact checking:
public class LaserPlayerDetection : MonoBehaviour
{
    private void OnTriggerStay(Collider other)
    {
        if (other.tag == Tags.PLAYER && this.GetComponent<Renderer>().enabled)
        {
            LastPlayerSighting.Instance.position = other.transform.position;
        }
    }
}

Guess you like

Origin www.cnblogs.com/raymondking123/p/11491549.html