[Unity Shaders] gameplay and image effects-create an image effect for night vision effects, unityshaders

Source: Internet
Author: User

[Unity Shaders] gameplay and image effects-create an image effect for night vision effects, unityshaders

This series mainly references the book "Unity Shaders and Effects Cookbook" (thanks to the author of the original book) and adds a bit of personal understanding or expansion.

Here are all the illustrations in this book. Here is the code and resources required for this book (you can also download them from the official website ).

========================================================== ===================================================== =====



Preface


Alas, this book is the last one !!! I'm a little excited... (But there are still a lot of gaps .) I will not talk too much nonsense in the technical articles. I will write a summary later, and the pitfalls will be gradually supplemented (I hope .).


Okay! The special effects we will learn in this section are more common image effects. In many first-person shooting games (FPS) on the market, we can see the effect of night vision, such as Call of Duty and halo. This effect adds a unique yellow-green screen. The night vision effect in Call of Duty is as follows:

 


To achieve this night vision effect, we need to use Photoshop to break down the above effect. We can find some reference images from the Internet and merge them with existing layers to see what kind of blending effect we need and how we want to combine different layers. The following image shows the final result of the above process in Photoshop:



The Shader implementation result is as follows:




Next, we begin to break down the final images in Photoshop into several parts to better understand how these resources are integrated. Then, we will explain the implementation process in depth.



Preparations


Previously, using Photoshop allows us to easily construct layered images so that we can better understand how to get a night vision effect. Now, we break down the night vision effect into several layers.


  • Tinted green): The first layer of the night vision effect is its iconic green color. Almost all night vision images are of this color. This makes our special effects seem to have obvious night vision effects.

  • Scan lines): To increase the authenticity of the night vision effect, a scanning line is added to the above coloring layer. Here, we use a texture created using Photoshop, and then let the user choose the tile factor to control the width, width, and size of the scanning line.

  • Noise): The next layer is a simple noise layer that is tiled over the above two layers to break down the image and add more details for our special effects.

  • Vignette): The last layer of the night vision effect is shadow. From the game called Call of Duty above, we can see that it uses a shadow to simulate a visual effect. We use the following layers as the shadow texture.

Now, we combine the textures above to formally create screen effects for night vision.
Implementation
Now your image special effect script system has been set up. Now let's implement the specific script and Shader.
First, we will enter the main code of the script "maid. cs. Next, we will implement the key Shader part.
The complete script and Shader are as follows:
Run the following code:
using UnityEngine;using System.Collections;[ExecuteInEditMode]public class NightVisionEffect : MonoBehaviour {#region Variablespublic Shader nightVisionShader;public float contrast = 2.0f;public float brightness = 1.0f;public Color nightVisionColor = Color.white;public Texture2D vignetteTexture;public Texture2D scanLineTexture;public float scanLineTileAmount = 4.0f;public Texture2D nightVisionNoise;public float noiseXSpeed = 100.0f;public float noiseYSpeed = 100.0f;public float distortion = 0.2f;public float scale = 0.8f;private Material curMaterial;private float randomValue = 0.0f;#endregion#region Propertiespublic Material material {get {if (curMaterial == null) {curMaterial = new Material(nightVisionShader);curMaterial.hideFlags = HideFlags.HideAndDontSave;}return curMaterial;}}#endregion// Use this for initializationvoid Start () {if (SystemInfo.supportsImageEffects == false) {enabled = false;return;}if (nightVisionShader != null && nightVisionShader.isSupported == false) {enabled = false;}}void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture){if (nightVisionShader != null) {material.SetFloat("_Contrast", contrast);material.SetFloat("_Brightness", brightness);material.SetColor("_NightVisionColor", nightVisionColor);material.SetFloat("_RandomValue", randomValue);material.SetFloat("_Distortion", distortion);material.SetFloat("_Scale", scale);if (vignetteTexture) {material.SetTexture("_VignetteTex", vignetteTexture);}if (scanLineTexture) {material.SetTexture("_ScanLineTex", scanLineTexture);material.SetFloat("_ScanLineTileAmount", scanLineTileAmount);}if (nightVisionNoise) {material.SetTexture("_NoiseTex", nightVisionNoise);material.SetFloat("_NoiseXSpeed", noiseXSpeed);material.SetFloat("_NoiseYSpeed", noiseYSpeed);}Graphics.Blit(sourceTexture, destTexture, material);} else {Graphics.Blit(sourceTexture, destTexture);}}// Update is called once per framevoid Update () {contrast = Mathf.Clamp(contrast, 0.0f, 4.0f);brightness = Mathf.Clamp(brightness, 0.0f, 2.0f);distortion = Mathf.Clamp(distortion, -1.0f, 1.0f);scale = Mathf.Clamp(scale, 0.0f, 3.0f);randomValue = Random.Range(-1.0f, 1.0f);}void OnDisable () {if (curMaterial != null) {DestroyImmediate(curMaterial);}}}

The following figure shows the NightVisionEffectShader:
Shader "Custom/NightVisionEffectShader" {Properties {_MainTex ("Base (RGB)", 2D) = "white" {}_VignetteTex ("Vignette Texture", 2D) = "white" {}_ScanLineTex ("Scan Line Texture", 2D) = "white" {}_ScanLineTileAmount ("Scale Line Tile Amount", Float) = 4.0_NoiseTex ("Noise Texture", 2D) = "white" {}_NoiseXSpeed ("Noise X Speed", Float) = 100.0_NoiseYSpeed ("Noise Y Speed", Float) = 100.0_NightVisionColor ("Night Vision Color", Color) = (1, 1, 1, 1)_Contrast ("Contrast", Range(0, 4)) = 2_Brightness ("Brightness", Range(0, 2)) = 1_RandomValue ("Random Value", Float) = 0_Distortion ("Distortion", Float) = 0.2_Scale ("Scale (Zoom)", Float) = 0.8}SubShader {Pass {CGPROGRAM#pragma vertex vert_img#pragma fragment frag#include "UnityCG.cginc"uniform sampler2D _MainTex;uniform sampler2D _VignetteTex;uniform sampler2D _ScanLineTex;fixed _ScanLineTileAmount;uniform sampler2D _NoiseTex;fixed _NoiseXSpeed;fixed _NoiseYSpeed;fixed4 _NightVisionColor;fixed _Contrast;fixed _Brightness;fixed _RandomValue;fixed _Distortion;fixed _Scale;float2 barrelDistortion(float2 coord) {// Lens distortion algorithm// See http://www.ssontech.com/content/lensalg.htmfloat2 h = coord.xy - float2(0.5, 0.5);float r2 = h.x * h.x + h.y * h.y;float f = 1.0 + r2 * (_Distortion * sqrt(r2));return f * _Scale * h + 0.5;}fixed4 frag(v2f_img i) : COLOR {// Get the colors from the Render Texture and the uv's// from the v2f_img structhalf2 distortedUV = barrelDistortion(i.uv);fixed4 renderTex = tex2D(_MainTex, distortedUV);fixed4 vignetteTex = tex2D(_VignetteTex, i.uv);// Process  scan lines and noisehalf2 scanLinesUV = half2(i.uv.x * _ScanLineTileAmount, i.uv.y * _ScanLineTileAmount);fixed4 scanLineTex = tex2D(_ScanLineTex, scanLinesUV);half2 noiseUV = half2(i.uv.x + (_RandomValue * _SinTime.z * _NoiseXSpeed),i.uv.y + (_Time.x * _NoiseYSpeed));fixed4 noiseTex = tex2D(_NoiseTex, noiseUV);// Get the luminosity values from the render texture using the YIQ valuesfixed lum = dot(fixed3(0.299, 0.587, 0.114), renderTex.rgb);lum += _Brightness;fixed4 finalColor = (lum * 2) + _NightVisionColor;// Final outputfinalColor = pow(finalColor, _Contrast);finalColor *= vignetteTex;finalColor *= scanLineTex * noiseTex;return finalColor;}ENDCG}} FallBack "Diffuse"}

Return to the Unity editor to view the effect. We need to set the corresponding images and attributes in the panel, as shown below:


Extended Link


Lens deformation effect:

  • Http://www.ssontech.com/content/lensalg.html



Career for developing standalone games

Hello! The following is the answer ~ (I think it is mainly for senior people in BC and C language ...)

I. What are the main aspects of game program development?

Game development mainly consists of the following aspects:
1. graphics engine
2. Sound Engine
3. Physical Engine
4. Game Engine
5. Artificial Intelligence or game logic
6. Game GUI (menu)
7. Game Development Tools
8. Network engine development supporting local area network battle
9. Network engine development supporting Internet battles

The following describes each part one by one:
1. graphics engines mainly include scene management and rendering in the game (indoors or outdoors), role Action Management and rendering, special effect management and rendering (particle system, natural simulation (such as water pattern, plant simulation), light and material processing, Level Object Detail management, and development of graphic data conversion tools, these tools are mainly used to use DCC software for artists (such as 3DS Max, Maya, Soft XSI, Soft Image3D, etc) the model and action data produced by the software, as well as the textures produced by tools such as Photo shop or painter, are converted into resource files used in game programs.

2. The Sound engine mainly includes the playback of Sound effects (SE), VOICE (VOICE), and Background music (BGM. SE refers to those that play frequently in the game and have a short playing time, but must be able to play without delay in a timely manner. VOICE refers to the VOICE or VOICE in the game, this part has a high requirement on sound quality, and basically uses a relatively high sampling rate to record and play back the sound. However, like SE, it requires timely playback without delay, sometimes, due to memory capacity problems, SE may reduce the sampling rate without affecting the effect. However, VOICE has a great impact on the effect due to the low sampling rate, therefore, generally VOICE does not adopt the method of reducing the sampling rate. BGM refers to the background music that plays a game for a long period of loop playback (or only once). It is precisely because of this feature of BGM, generally, the background music of a game is played by reading a disk (CD or hard disk. Other advanced audio effects, such as EAX, DTS5.1, and Dolby surround.

3. the physical engine mainly involves the mechanical simulation of collision between objects in the game world and between objects and scenes, and the mechanical simulation of the object skeleton movement after the collision (the famous physical Engine includes the game dynamics sdk of havok and the ODE-open Dynamics Engine of Open source ).

4. the game engine integrates graphics engines, sound engines, and physical engines to create a game system for a game, including the game level editor, the main purpose is to visually adjust the scene, adjust the lighting effect and atomization effect, set events, place items, and set NPCs. In addition, there is a role editor, it is mainly used to edit role attributes and check the correctness of action data. In general, Japanese gaming companies will directly implement the level editor and role editor in the game, and all parameter adjustments will be made in the game through the debugging menu, therefore, they generally make this part of the debugging menu function very powerful, while displaying some important information in real time on the screen, the advantage of this is that the effect of the level editor adjustment is directly the effect of the game, but it may not be very good for the reusability of the program. For example, it is difficult to use another game project, unless the two games are of the same type, you only need to change the scenario and role data, and there is no problem with the next generation of products. You only need to add the debugging menu function according to the style.

5. artificial Intelligence and game logic development are also very different in Japan and Europe and the United States in the game development model, the use of script language development in European and American game companies is very common, therefore, this part of program development is mainly written in the scripting language, and the coupling between the script program and the game program is very low, there is a separate editing, compilation and debugging environment, this is more conducive to the separation of game programs and level design and development, and parallel development, so they generally have a programmer position dedicated to level design. And the remaining full text>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.