Noob to Pro Unity Shader Writing
- Shaderlab – core unity shader language, required in all shaders, very basic
- CG, nvidia, powerful
- GLSL, mobile shader, very similar to CG.
- Float, Half (+-60000 range, 3.3 decimal precision), Fixed +- 2.0 range, 1/256 precision, most optimized
Example Shader Code in Unity 5.4
Flat Color
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/** * Writen by Ruofei Du. Use it, edit it, steal it I don't care. */ Shader "MyShaders/FlatColor" { Properties { _Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0) } SubShader { Pass { CGPROGRAM // pragmas #pragma vertex vert #pragma fragment frag // user defined variables uniform float4 _Color; // base input structs struct vertexInput { float4 vertex : POSITION; }; struct vertexOutput { float4 pos : SV_POSITION; }; // vertex function vertexOutput vert(vertexInput v) { vertexOutput o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); return o; } // fragment function float4 frag(vertexOutput i) : COLOR { return _Color; } ENDCG } } // fallback commendted out during development // Fallback "Diffuse" } |
Projective Texture
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/** * Writen by Ruofei Du. Use it, edit it, steal it I don't care. */ Shader "MyShaders/ProjectiveTexture" { // use arrays later on // https://docs.unity3d.com/Manual/SL-Properties.html Properties { _Tex0("Texture 0", 2D) = "white" {} } SubShader{ Pass { CGPROGRAM // pragmas #pragma vertex vert #pragma fragment frag // user defined variables uniform float4x4 _Cam0; uniform sampler2D _Tex0; // base input structs struct vertexInput { float4 position : POSITION; }; struct vertexOutput { float4 pos : SV_POSITION; float4 texCoord : TEXCOORD0; }; // helper function float2 projectcoord(float4 texture_coord) { return (texture_coord.xy / texture_coord.w + 1.0) * 0.5; } // Built-in shader variables https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html // vertex function vertexOutput vert(vertexInput v) { vertexOutput o; o.pos = mul(UNITY_MATRIX_MVP, v.position); o.texCoord = mul(_Cam0, mul(unity_ObjectToWorld, v.position)); return o; } // fragment function float4 frag(vertexOutput v) : COLOR { float2 vUv = projectcoord(v.texCoord); if (vUv.x < 0.0 || vUv.y < 0.0 || vUv.x > 1.0 || vUv.y > 1.0) discard; float4 cVideo = tex2D(_Tex0, vUv); return cVideo; } ENDCG } } // fallback commendted out during development // Fallback "Diffuse" } |
Fly Camera
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
using UnityEngine; using System.Collections; public class FlyCamera : MonoBehaviour { /** * Writen by Windexglow 11-13-10. Use it, edit it, steal it I don't care. * Converted to C# 27-02-13 - no credit wanted. * Added resetRotation, RF control, improved initial mouse position, 2015-03-11 - Roi Danton. * Simple flycam I made, since I couldn't find any others made public. * Made simple to use (drag and drop, done) for regular keyboard layout * wasdrf : basic movement * shift : Makes camera accelerate * space : Moves camera on X and Z axis only. So camera doesn't gain any height */ float mainSpeed = 2.5f; // Regular speed. float shiftAdd = 6; // Multiplied by how long shift is held. Basically running. float maxShift = 20; // Maximum speed when holding shift. float camSens = .25f; // Camera sensitivity by mouse input. //float mainSpeed = 5; // Regular speed. //float shiftAdd = 12; // Multiplied by how long shift is held. Basically running. //float maxShift = 50; // Maximum speed when holding shift. //float camSens = .25f; // Camera sensitivity by mouse input. private Vector3 lastMouse = new Vector3(Screen.width / 2, Screen.height / 2, 0); // Kind of in the middle of the screen, rather than at the top (play). private float totalRun = 1.0f; bool mouseUpdated = false; void Update() { // Mouse input. if (Input.GetMouseButton(0)) { if (!mouseUpdated) { lastMouse = Input.mousePosition; } lastMouse = Input.mousePosition - lastMouse; lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0); lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0); transform.eulerAngles = lastMouse; lastMouse = Input.mousePosition; mouseUpdated = true; } else { mouseUpdated = false; } // Keyboard commands. Vector3 p = getDirection(); if (Input.GetKey(KeyCode.LeftShift)) { totalRun += Time.deltaTime; p = p * totalRun * shiftAdd; p.x = Mathf.Clamp(p.x, -maxShift, maxShift); p.y = Mathf.Clamp(p.y, -maxShift, maxShift); p.z = Mathf.Clamp(p.z, -maxShift, maxShift); } else { totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f); p = p * mainSpeed; } p = p * Time.deltaTime; Vector3 newPosition = transform.position; if (Input.GetKey(KeyCode.V)) { //If player wants to move on X and Z axis only transform.Translate(p); newPosition.x = transform.position.x; newPosition.z = transform.position.z; transform.position = newPosition; } else { transform.Translate(p); } } private Vector3 getDirection() { Vector3 p_Velocity = new Vector3(); float amount = 0.3f; if (Input.GetKey(KeyCode.W)) { p_Velocity += new Vector3(0, 0, amount); } if (Input.GetKey(KeyCode.S)) { p_Velocity += new Vector3(0, 0, -amount); } if (Input.GetKey(KeyCode.A)) { p_Velocity += new Vector3(-amount, 0, 0); } if (Input.GetKey(KeyCode.D)) { p_Velocity += new Vector3(amount, 0, 0); } if (Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.Q)) { p_Velocity += new Vector3(0, amount, 0); } if (Input.GetKey(KeyCode.F) || Input.GetKey(KeyCode.E)) { p_Velocity += new Vector3(0, -amount, 0); } return p_Velocity; } public void resetRotation(Vector3 lookAt) { transform.LookAt(lookAt); } } |
Trigger
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using UnityEngine; using UnityEngine.UI; using System.Collections; public class PlayerController : MonoBehaviour { public Text countText; public Text winText; public float speed; private Rigidbody rb; private int count; // Use this for initialization void Start () { rb = GetComponent<Rigidbody>(); count = 0; SetCountText(); winText.text = ""; } // Update is called once per frame void Update () { } void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); rb.AddForce(movement * speed); } void OnTriggerEnter(Collider other) { //Destroy(other.gameObject); if (other.gameObject.CompareTag("Pick Up")) { other.gameObject.SetActive(false); ++count; SetCountText(); } } void SetCountText() { countText.text = "Count: " + count.ToString(); if (count >= 12) { winText.text = "You win!"; } } } |
Leave a Reply