博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
L老师 Shader编程教程 学习
阅读量:6515 次
发布时间:2019-06-24

本文共 43926 字,大约阅读时间需要 146 分钟。

 

 

 

1 Shader "VoidGame/FixedShader" { 2     Properties{ 3         //颜色 4         _Color("Color",Color)=(1,1,1,1) 5         //环境光 6         _Ambient("Ambient",Color)=(0.3,0.3,0.3,0.3) 7         //高光反射 8         _Specular("Specular",Color)=(1,1,1,1) 9         //光泽10         _Shininess("Shininess",range(0,8))=411         //自发光12         _Emission("Emision",Color)=(1,1,1,1)13         //主纹理14         _MainTex("MainTex",2D) = "white"{}15         //第二张纹理16         _SecondTex("SecondTex",2D) = "white"{}17         _ConstantColor("ConstantColor",Color)=(1,1,1,0.3)18     }19     SubShader{20         Tags  { "Queue" = "Transparent" }21         pass {22             Blend SrcAlpha OneMinusSrcAlpha23 24             //color(1,1,1,1)25             //color[_Color]26             material{27                 diffuse[_Color]28                 ambient[_Ambient]29                 specular[_Specular]30                 shininess[_Shininess]31                 emission[_Emission]32             }33             lighting on34             separatespecular on35 36             settexture[_MainTex]{37                 combine texture * primary double38             }39             settexture[_SecondTex]{40                 constantColor[_ConstantColor]41                 combine texture * previous double,texture * constant42             }43         }44     }45 }
FixedShader

 

1 Shader "VoidGame/VertexShader1" { 2     SubShader{ 3         Pass{ 4             CGPROGRAM 5  6             #pragma vertex vert 7             #pragma fragment frag 8  9             void vert(in float2 objPos:POSITION,out float4 pos:POSITION,out float4 col:COLOR) {10                 pos = float4(objPos,0,1);11                 col = pos;12             }13 14             void frag(inout float4 col:COLOR) {15                 col = float4(0,1,0,1);16             }    17 18             ENDCG19         }20     }21 }
VertexShader1

 

数学函数(Mathematical Functions)

几何函数(Geometric Functions)

纹理映射函数(Texture Map Functions)

偏导函数(Derivative Functions)

调试函数(Debugging Function)

 

2d旋转

1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using System; 9 using System.Collections.Generic;10 using System.Drawing;11 using System.Text;12 13 namespace MatrixTransform {14     class Triangle {15         PointF A, B, C;16 17         public Triangle(PointF A,PointF B,PointF C) {18             this.A = A;19             this.B = B;20             this.C = C;21         }22 23         public void Draw(Graphics g) {24             Pen pen = new Pen(Color.Red,2);25             g.DrawLine(pen,A,B);26             g.DrawLine(pen,B,C);27             g.DrawLine(pen,C,A);28         }29 30         public void Rotate(int degree) {31             float angle = (float)(degree / 360.0f * Math.PI);32 33             float newX = (float)(A.X * Math.Cos(angle) - A.Y * Math.Sin(angle));34             float newY = (float)(A.X * Math.Sin(angle) + A.Y * Math.Cos(angle));35 36             A.X = newX;37             A.Y = newY;38 39             newX = (float)(B.X * Math.Cos(angle) - B.Y * Math.Sin(angle));40             newY = (float)(B.X * Math.Sin(angle) + B.Y * Math.Cos(angle));41 42             B.X = newX;43             B.Y = newY;44 45             newX = (float)(C.X * Math.Cos(angle) - C.Y * Math.Sin(angle));46             newY = (float)(C.X * Math.Sin(angle) + C.Y * Math.Cos(angle));47 48             C.X = newX;49             C.Y = newY;50         }51     }52 }
Triangle
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8  9 namespace MatrixTransform {10     public partial class Form1:Form {11 12         Triangle t;13 14         public Form1() {15             InitializeComponent();16         }17 18         private void From1_Paint(Object sender,PaintEventArgs e) {19             e.Graphics.TranslateTransform(300,300);20             t.Draw(e.Graphics);21         }22 23         private void Form1_Load(object sender,EventArgs e) {24             PointF A = new PointF(0,-200);25             PointF B = new PointF(200,200);26             PointF C = new PointF(-200,200);27             t = new Triangle(A,B,C);28         }29 30         private void timer1_Tick(object sender,EventArgs e) {31             t.Rotate(1);32             this.Invalidate();33         }34     }35 }
Form1

视频:https://pan.baidu.com/s/1nu9yHgl

项目:https://pan.baidu.com/s/1skDuxtj

3d

1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using System; 9 using System.Collections.Generic;10 using System.Text;11 12 namespace _3DTransform {13 14     class Vector4 {15         public double x, y, z, w;16         public Vector4() {17 18         }19 20         public Vector4(double x,double y,double z,double w) {21             this.x = x;22             this.y = y;23             this.z = z;24             this.w = w;25         }26 27         public Vector4(Vector4 v) {28             this.x = v.x;29             this.y = v.y;30             this.z = v.z;31             this.w = v.w;32         }33     }34 }
Vector4
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using System; 9 using System.Collections.Generic;10 using System.Text;11 12 namespace _3DTransform {13     class Matrix4x4 {14 15         private double[,] pts;16         public Matrix4x4() {17             pts = new double[4,4];18         }19 20         public double this[int i,int j] {21             get {22                 return pts[i - 1,j - 1];23             }24             set {25                 pts[i - 1,j - 1] = value;26             }27         }28 29         public Matrix4x4 Mul(Matrix4x4 m) {30             Matrix4x4 newM = new Matrix4x4();31             for(int w = 1;w <= 4;w++) {32                 for(int h = 1;h <= 4;h++) {33                     for(int n = 1;n <= 4;n++) {34                         newM[w,h] += this[w,n] * m[n,h];35                     }36                 }37             }38             return newM;39         }40 41         public Vector4 Mul(Vector4 v) {42             Vector4 newV = new Vector4();43             newV.x = v.x * this[1,1] + v.y * this[2,1] + v.z * this[3,1] + v.w * this[4,1];44             newV.y = v.x * this[1,2] + v.y * this[2,2] + v.z * this[3,2] + v.w * this[4,2];45             newV.z = v.x * this[1,3] + v.y * this[2,3] + v.z * this[3,3] + v.w * this[4,3];46             newV.w = v.x * this[1,4] + v.y * this[2,4] + v.z * this[3,4] + v.w * this[4,4];47 48             return newV;49         }50     }51 }
Matrix4
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using System; 9 using System.Collections.Generic;10 using System.Drawing;11 using System.Text;12 13 namespace _3DTransform {14     class Triangle3D {15 16 17         private Vector4 a, b, c;18 19         public Vector4 A, B, C;20 21         public Triangle3D() {22 23         }24 25         public Triangle3D(Vector4 a,Vector4 b,Vector4 c) {26             this.A = this.a = new Vector4(a);27             this.B = this.b = new Vector4(b);28             this.C = this.c = new Vector4(c);29         }30 31         public void Transform(Matrix4x4 m) {32             this.a = m.Mul(this.A);33             this.b = m.Mul(this.B);34             this.c = m.Mul(this.C);35         }36 37         public void Draw(Graphics g) {38             g.TranslateTransform(300,300);39             g.DrawLines(new Pen(Color.Red,2),this.Get2DPointFArr());40         }41 42         private PointF[] Get2DPointFArr() {43             PointF[] arr = new PointF[4];44             arr[0] = Get2DPointF(this.a);45             arr[1] = Get2DPointF(this.b);46             arr[2] = Get2DPointF(this.c);47             arr[3] = arr[0];48             return arr;49         }50 51         private PointF Get2DPointF(Vector4 v) {52             PointF p = new PointF();53             p.X = (float)(v.x / v.w);54             p.Y = (float)(v.y / v.w);55             return p;56         }57     }58 }
Triangle3D
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8  9 namespace _3DTransform {10     public partial class Form1:Form {11 12         int a;13 14         Triangle3D triangle;15 16         Matrix4x4 m_scale;17         Matrix4x4 m_rotation;18         Matrix4x4 m_view;19         Matrix4x4 m_projection;20 21         public Form1() {22             InitializeComponent();23 24             m_scale = new Matrix4x4();25             m_scale[1,1] = 250;26             m_scale[2,2] = 250;27             m_scale[3,3] = 250;28             m_scale[4,4] = 1;29 30             m_rotation = new Matrix4x4();31 32             m_view = new Matrix4x4();33             m_view[1,1] = 1;34             m_view[2,2] = 1;35             m_view[3,3] = 1;36             m_view[4,3] = 250;37             m_view[4,4] = 1;38 39             m_projection = new Matrix4x4();40             m_projection[1,1] = 1;41             m_projection[2,2] = 1;42             m_projection[3,3] = 1;43             m_projection[3,4] = 1.0/250;44         }45 46         private void Form1_Load(object sender,EventArgs e) {47             Vector4 a = new Vector4(0,-0.5,0,1);48             Vector4 b = new Vector4(0.5,0.5,0,1);49             Vector4 c = new Vector4(-0.5,0.5,0,1);50 51             triangle = new Triangle3D(a,b,c);52 53         }54 55         private void Form1_Paint(object sender,PaintEventArgs e) {56             triangle.Draw(e.Graphics);57         }58 59         private void timer1_Tick(object sender,EventArgs e) {60 61             a += 2;62             double angle = a / 360.0 * Math.PI;63 64             m_rotation[1,1] = Math.Cos(angle);65             m_rotation[1,3] = Math.Sin(angle);66             m_rotation[2,2] = 1;67             m_rotation[3,1] = -Math.Sin(angle);68             m_rotation[3,3] = Math.Cos(angle);69             m_rotation[4,4] = 1;70 71             Matrix4x4 m = m_scale.Mul(m_rotation);72             m = m.Mul(m_view);73             m = m.Mul(m_projection);74 75             triangle.Transform(m);76             this.Invalidate();77         }78 79         private void trackBar1_Scroll(object sender,EventArgs e) {80             m_view[4,3] = (sender as TrackBar).Value;81         }82     }83 }
Form1

视频:https://pan.baidu.com/s/1slxvCk9

项目:https://pan.baidu.com/s/1dERVVdZ

 

旋转

1 Shader "VoidGame/31" { 2     SubShader{ 3         Pass{ 4             CGPROGRAM 5  6             #pragma vertex vert 7             #pragma fragment frag 8  9             #include "UnityCG.cginc"10 11             float4x4 mvp;12             float4x4 rm;13             float4x4 sm;14 15             struct v2f {16                 float4 pos:POSITION;17             };18 19             v2f vert(appdata_base v) {20                 v2f o;21 22                 //o.pos = mul(mvp, v.vertex);23 24                 float4x4 m = mul(UNITY_MATRIX_MVP, sm);25                 o.pos = mul(m, v.vertex);26 27                 return o;28             }29 30             fixed4 frag():COLOR {31                 return fixed4(1, 1, 1, 1);32             }33 34             ENDCG35         }36     }37 }
31
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using UnityEngine; 9 using System.Collections;10 11 namespace VoidGame {12 13     public class MVPTransform : MonoBehaviour {14 15         private void Update() {16 17             //Matrix4x4 RM = new Matrix4x4();18             //RM[0,0] = Mathf.Cos(Time.realtimeSinceStartup);19             //RM[0,2] = Mathf.Sin(Time.realtimeSinceStartup);20             //RM[1,1] = 1;21             //RM[2,0] = -Mathf.Sin(Time.realtimeSinceStartup);22             //RM[2,2] = Mathf.Cos(Time.realtimeSinceStartup);23             //RM[3,3] = 1;24 25             //Matrix4x4 mvp = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix;26 27             //mvp *= RM;28 29             //GetComponent
().material.SetMatrix("mvp",mvp);30 31 Matrix4x4 RM = new Matrix4x4();32 RM[0,0] = Mathf.Cos(Time.realtimeSinceStartup);33 RM[0,2] = Mathf.Sin(Time.realtimeSinceStartup);34 RM[1,1] = 1;35 RM[2,0] = -Mathf.Sin(Time.realtimeSinceStartup);36 RM[2,2] = Mathf.Cos(Time.realtimeSinceStartup);37 RM[3,3] = 1;38 39 Matrix4x4 SM = new Matrix4x4();40 SM[0,0] = Mathf.Sin(Time.realtimeSinceStartup) / 4 + 0.5f;41 SM[1,1] = Mathf.Cos(Time.realtimeSinceStartup) / 8 + 0.5f;42 SM[2,2] = Mathf.Sin(Time.realtimeSinceStartup) / 6 + 0.5f;43 SM[3,3] = 1;44 45 GetComponent
().material.SetMatrix("sm",SM);46 }47 }48 }
MVPTransform

颜色

1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2  3 Shader "VoidGame/33" { 4     SubShader{ 5         Pass{ 6             CGPROGRAM 7  8             #pragma vertex vert 9             #pragma fragment frag10 11             #include "UnityCG.cginc"12 13 14             struct v2f {15                 float4 pos:POSITION;16                 fixed4 color : COLOR;17             };18 19             v2f vert(appdata_base v) {20                 v2f o;21 22                 //o.pos = mul(mvp, v.vertex);23 24                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);25 26                 //if (v.vertex.x > 0) {27                 //    o.color = fixed4(1, 0, 0, 1);28                 //}29                 //else {30                 //    o.color = fixed4(0, 0, 1, 1);31                 //}32 33                 if (v.vertex.x == 0.5 && v.vertex.y == 0.5 && v.vertex.z == -0.5) {34                     o.color = fixed4(_SinTime.w / 2 + 0.5, _CosTime.w / 2 + 0.5, _SinTime.y / 2 + 0.5, 1);35                 }36                 else {37                     o.color = fixed4(0, 0, 1, 1);38                 }39 40                 //float4 wpos = mul(unity_ObjectToWorld, v.vertex);41                 //if (wpos.x > 0) {42                 //    o.color = fixed4(1, 0, 0, 1);43                 //}44                 //else {45                 //    o.color = fixed4(0, 0, 1, 1);46                 //}47 48                 return o;49             }50 51             fixed4 frag(v2f IN):COLOR {52                 return IN.color;53             }54 55             ENDCG56         }57     }58 }
33

 

1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2  3 Shader "VoidGame/34" { 4     SubShader{ 5         Pass{ 6             CGPROGRAM 7  8             #pragma vertex vert 9             #pragma fragment frag10 11             #include "UnityCG.cginc"12 13             float dis;14             float r;15             16 17             struct v2f {18                 float4 pos:POSITION;19                 fixed4 color : COLOR;20             };21 22             v2f vert(appdata_base v) {23                 v2f o;24                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);25 26                 float x = o.pos.x / o.pos.w;27 28                 if (x > dis && x < dis+r) {29                     o.color = fixed4(1, 0, 0, 1);30                 }31                 else {32                     o.color = fixed4(x / 2 + 0.5, x / 2 + 0.5, x / 2 + 0.5, 1);33                 }34 35                 return o;36             }37 38             fixed4 frag(v2f IN):COLOR {39                 return IN.color;40             }41 42             ENDCG43         }44     }45 }
34
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using UnityEngine; 9 using System.Collections;10 11 namespace VoidGame {12 13     public class SetFloat : MonoBehaviour {14 15         private float dis = -1;16         private float r = 0.1f;17 18         private void Update() {19             dis += Time.deltaTime;20             GetComponent
().material.SetFloat("dis",dis);21 GetComponent
().material.SetFloat("r",r);22 }23 }24 }
SetFloat

1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2  3 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 4  5 Shader "VoidGame/35" { 6     Properties{ 7         _R("R",range(0,5)) = 1 8         _OX("OX",range(-5,5)) = 0 9     }10     SubShader{11         Pass{12             CGPROGRAM13 14             #pragma vertex vert15             #pragma fragment frag16 17             #include "UnityCG.cginc"18 19             float dis;20             float r;21             22             float _R;23             float _OX;24 25             struct v2f {26                 float4 pos:POSITION;27                 fixed4 color : COLOR;28             };29 30             v2f vert(appdata_base v) {31 32                 //float2 xy = v.vertex.xz;33 34                 float4 wpos = mul(unity_ObjectToWorld, v.vertex);35 36                 float2 xy = wpos.xz;37                 float d = _R - length(xy - float2(_OX,0));38 39                 d = d < 0 ? 0 : d;40                 float height = 1;41                 float4 uppos = float4(v.vertex.x, height*d, v.vertex.z, v.vertex.w);42 43                 v2f o;44                 o.pos = mul(UNITY_MATRIX_MVP, uppos);45 46                 o.color = fixed4(uppos.y, uppos.y, uppos.y, 1);47 48                 return o;49             }50 51             fixed4 frag(v2f IN):COLOR {52                 return IN.color;53             }54 55             ENDCG56         }57     }58 }
35

1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2  3 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 4  5 Shader "VoidGame/36" { 6     SubShader{ 7         Pass{ 8             CGPROGRAM 9 10             #pragma vertex vert11             #pragma fragment frag12 13             #include "UnityCG.cginc"14 15             struct v2f {16                 float4 pos:POSITION;17                 fixed4 color : COLOR;18             };19 20             v2f vert(appdata_base v) {21 22                 float angle = length(v.vertex)*_SinTime.w;23 24                 //float4x4 m = {25                 //    float4(cos(angle),0,sin(angle),0),26                 //    float4(0,1,0,0),27                 //    float4(-sin(angle),0,cos(angle),0),28                 //    float4(0,0,0,1)29                 //};30 31                 float x = cos(angle)*v.vertex.x + sin(angle)*v.vertex.z;32                 float z = cos(angle)*v.vertex.z - sin(angle)*v.vertex.x;33                 v.vertex.x = x;34                 v.vertex.z = z;35 36                 //v.vertex = mul(m, v.vertex);37 38                 v2f o;39                 o.pos = mul(UNITY_MATRIX_MVP,v.vertex);40 41                 o.color = fixed4(0,1,1,1);42 43                 return o;44             }45 46             fixed4 frag(v2f IN) :COLOR{47                 return IN.color;48             }49 50             ENDCG51         }52     }53 }
36

1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2  3 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 4  5 Shader "VoidGame/37" { 6     SubShader{ 7         Pass{ 8             CGPROGRAM 9 10             #pragma vertex vert11             #pragma fragment frag12             #include "UnityCG.cginc"13 14             struct v2f {15                 float4 pos:POSITION;16                 fixed4 color : COLOR;17             };18 19             v2f vert(appdata_base v) {20 21                 //A*sin(omega*x+t);22 23                 v.vertex.y += 0.2 * sin((v.vertex.x + v.vertex.z) + _Time.y);24                 v.vertex.y += 0.3 * sin((v.vertex.x - v.vertex.z) + _Time.w);25 26 27                 //v.vertex.y += 0.2 * sin(-length(v.vertex.xz)+ _Time.y);28 29                 v2f o;30                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);31                 o.color = fixed4(v.vertex.y, v.vertex.y, v.vertex.y, 1);32                 return o;33             }34 35             fixed4 frag(v2f IN) :COLOR{36                 return IN.color;37             }38 39             ENDCG40         }41     }42 }
37
1 Shader "VoidGame/38" { 2     SubShader{ 3         Pass{ 4             Tags{ "LightMode" = "ForwardBase" } 5  6             CGPROGRAM 7  8             #pragma vertex vert 9             #pragma fragment frag10             #include "UnityCG.cginc"11             #include "Lighting.cginc"12 13             struct v2f {14                 float4 pos:POSITION;15                 fixed4 color:COLOR;16             };17 18             v2f vert(appdata_base v) {19                 v2f o;20                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);21 22                 float3 N = normalize(v.normal);23                 float3 L = normalize(_WorldSpaceLightPos0);24 25                 N = mul(float4(N, 0),unity_WorldToObject).xyz;26                 N = normalize(N);27                 //L = mul(unity_WorldToObject, float4(L, 0)).xyz;28 29                 float ndotl = saturate(dot(N,L));30                 o.color = _LightColor0 * ndotl;31 32                 return o;33             }34 35             fixed4 frag(v2f IN):COLOR{36                 return IN.color + UNITY_LIGHTMODEL_AMBIENT;37             }38 39             ENDCG40         }41     }42 }
38

 

1 Shader "VoidGame/Texture_01" { 2     properties{ 3         _MainTex("MainTex",2D)=""{} 4     } 5  6     SubShader{ 7         Pass{ 8             CGPROGRAM 9 10             #pragma vertex vert11             #pragma fragment frag12             #include "UnityCG.cginc"13 14             sampler2D _MainTex;15             16             float4 _MainTex_ST;17 18             struct v2f {19                 float4 pos:POSITION;20                 float2 uv:TEXCOORD0;21             };22 23             v2f vert(appdata_base v) {24                 v2f o;25                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);26                 //o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;27                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);28                 29                 return o;30             }31 32             fixed4 frag(v2f IN) :COLOR{33                 fixed4  color = tex2D(_MainTex,IN.uv);34                 return color;35             }36 37             ENDCG38         }39     }40 }
纹理

1 Shader "VoidGame/Texture_02" { 2     properties{ 3         _MainTex("MainTex",2D)=""{} 4     } 5  6     SubShader{ 7         Pass{ 8             CGPROGRAM 9 10             #pragma vertex vert11             #pragma fragment frag12             #include "UnityCG.cginc"13 14             sampler2D _MainTex;15             sampler2D unity_Lightmap;16             17             float4 _MainTex_ST;18             float4 unity_LightmapST;19 20             struct v2f {21                 float4 pos:POSITION;22                 float2 uv:TEXCOORD0;23                 float2 uv2:TEXCOORD1;24             };25 26             v2f vert(appdata_full v) {27                 v2f o;28                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);29                 //o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;30                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);31                 o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;32 33                 return o;34             }35 36             fixed4 frag(v2f IN) :COLOR{37                 float3 lm = DecodeLightmap(tex2D(unity_Lightmap,IN.uv2));38                 fixed4  color = tex2D(_MainTex,IN.uv);39                 color.rgb *= lm * 2;40                 return color;41             }42 43             ENDCG44         }45     }46 }
光照

 

1 Shader "VoidGame/Texture_03" { 2     properties{ 3         _MainTex("MainTex",2D)=""{} 4     } 5  6     SubShader{ 7         Pass{ 8             CGPROGRAM 9 10             #pragma vertex vert11             #pragma fragment frag12             #include "UnityCG.cginc"13 14             sampler2D _MainTex;15             16             float4 _MainTex_ST;17 18             struct v2f {19                 float4 pos:POSITION;20                 float2 uv:TEXCOORD0;21             };22 23             v2f vert(appdata_full v) {24                 v2f o;25                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);26                 //o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;27                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);28 29                 return o;30             }31 32             fixed4 frag(v2f IN) :COLOR{33                 fixed4  color = tex2D(_MainTex,IN.uv);34                 return color;35             }36 37             ENDCG38         }39     }40 }
Texture_03
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using UnityEngine; 9 using System.Collections;10 11 namespace VoidGame {12 13     public class SetTextureUVST : MonoBehaviour {14 15         public int width;16         public int height;17         public int fps;18 19         private int currentIndex;20 21         IEnumerator Start() {22             Material mat = GetComponent
().material;23 24 float scale_x = 1.0f / width;25 float scale_y = 1.0f / height;26 27 while(true) {28 float offset_x = currentIndex % width * scale_x;29 float offset_y = currentIndex / height * scale_y;30 31 mat.SetTextureOffset("_MainTex",new Vector2(offset_x,offset_y));32 mat.SetTextureScale("_MainTex",new Vector2(scale_x,scale_y));33 yield return new WaitForSeconds(1.0f / fps);34 currentIndex = (++currentIndex) % (width * height);35 }36 }37 38 private void Update() {39 40 }41 }42 }
SetTextureUVST

 

1 Shader "VoidGame/Texture_04" { 2     properties{ 3         _MainTex("MainTex",2D)=""{} 4         _F("F",range(1,30))=10 5         _A("A",range(0,0.1))=0.01 6         _R("R",range(0,1))=0 7     } 8  9     SubShader{10         Pass{11             CGPROGRAM12 13             #pragma vertex vert14             #pragma fragment frag15             #include "UnityCG.cginc"16 17             sampler2D _MainTex;18             19             float4 _MainTex_ST;20             float _F;21             float _A;22             float _R;23 24             struct v2f {25                 float4 pos:POSITION;26                 float2 uv:TEXCOORD0;27             };28 29             v2f vert(appdata_full v) {30                 v2f o;31                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);32                 //o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;33                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);34 35                 return o;36             }37 38             fixed4 frag(v2f IN) :COLOR{39                 //IN.uv += _Time.x;40                 41                 //IN.uv.x += 0.01 * sin(IN.uv.x * 3.14 * _F + _Time.y);42                 //IN.uv.y += 0.01 * sin(IN.uv.y * 3.14 * _F + _Time.y);43                 44                 //IN.uv += _A * sin(IN.uv * 3.14 * _F + _Time.y);45                 46 47                 float2 uv = IN.uv;48                 float dis = distance(uv, float2(0.5, 0.5));49                 float scale = 0;50                 //if (dis < _R) {
51 _A *= saturate(1 - dis / _R);52 scale = _A * sin(-dis * 3.14 * _F + _Time.y);53 uv = uv + uv * scale;54 //}55 56 //fixed4 color = tex2D(_MainTex,IN.uv);57 fixed4 color = tex2D(_MainTex, uv) + fixed4(1,1,1,1) * saturate(scale) * 100;58 return color;59 }60 61 ENDCG62 }63 }64 }
Texture_04

 

1 Shader "VoidGame/Texture_05" { 2     properties{ 3         _MainTex("MainTex",2D)=""{} 4         _F("F",range(1,30))=10 5         _A("A",range(0,0.1))=0.01 6         _R("R",range(0,1))=0 7     } 8  9     SubShader{10         Pass{11             CGPROGRAM12 13             #pragma vertex vert14             #pragma fragment frag15             #include "UnityCG.cginc"16             #pragma target 3.017 18             sampler2D _MainTex;19             20             float4 _MainTex_ST;21             float _F;22             float _A;23             float _R;24 25             struct v2f {26                 float4 pos:POSITION;27                 float2 uv:TEXCOORD0;28                 float z : TEXCOORD1;29             };30 31             v2f vert(appdata_full v) {32                 v2f o;33                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);34                 //o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;35                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);36                 o.z = mul(_Object2World, v.vertex).z;37 38                 return o;39             }40 41             fixed4 frag(v2f IN) :COLOR{42                 43                 //float offset_uv = 0.01;44 45                 //float2 uv = IN.uv;46                 //fixed4  color = tex2D(_MainTex,uv);47                 //48                 //uv.x = IN.uv.x + offset_uv;49                 //color.rgb += tex2D(_MainTex,uv);50                 //51                 //uv.x = IN.uv.x - offset_uv;52                 //color.rgb += tex2D(_MainTex, uv);53 54                 //uv.y = IN.uv.y + offset_uv;55                 //color.rgb += tex2D(_MainTex, uv);56 57                 //uv.y = IN.uv.y - offset_uv;58                 //color.rgb += tex2D(_MainTex, uv);59 60                 //color.rgb /= 5;61 62                 //float dx = ddx(IN.uv.x) * 10;63                 //float2 dsdx = float2(dx, dx);64                 //float dy = ddy(IN.uv.y) * 10;65                 //float2 dsdy = float2(dy, dy);66 67                 float2 dsdx = ddx(IN.z) * 10;68                 float2 dsdy = ddy(IN.z) * 10;69 70                 fixed4 color = tex2D(_MainTex,IN.uv,dsdx,dsdy);71                 72 73                 return color;74             }75 76             ENDCG77         }78     }79 }
Texture_05

 

1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2  3 Shader "VoidGame/Texture_06" { 4     Properties{ 5         _MainTex("MainTex",2D)=""{} 6         _F("F",range(1,10))=4 7     } 8     SubShader{ 9         Pass{10             CGPROGRAM11 12             #pragma vertex vert13             #pragma fragment frag14             #include "UnityCG.cginc"15 16             sampler2D _MainTex;17             float _F;18 19             struct v2f {20                 float4 pos:POSITION;21                 float2 uv:TEXCOORD0;22             };23 24             v2f vert(appdata_full v) {25                 v2f o;26                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);27                 o.uv = v.texcoord.xy;28                 return o;29             }30 31             fixed4 frag(v2f IN) :COLOR {32                 //float2 uv = IN.uv;33                 //float offset_uv = 0.05 * sin(IN.uv * _F + _Time.x);34                 //uv += offset_uv;35                 //fixed4 color = tex2D(_MainTex,uv);36 37                 float2 uv = IN.uv;38                 float offset_uv = 0.05 * sin(IN.uv * _F + _Time.x*2);39 40                 uv += offset_uv;41                 fixed4 color_1 = tex2D(_MainTex, uv);42 43                 uv = IN.uv;44                 uv -= offset_uv * 2;45                 fixed4 color_2 = tex2D(_MainTex, uv);46 47                 return (color_1 + color_2)/2;48             }49 50             ENDCG51         }52     }53 }
Texture_07

1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2  3 Shader "VoidGame/Texture_08" { 4     Properties{ 5         _MainTex("MainTex",2D)=""{} 6         _SecondTex("SecondTex",2D)=""{} 7         _F("F",range(1,10))=4 8     } 9     SubShader{10         Pass{11 12             colormask r13 14             CGPROGRAM15 16             #pragma vertex vert17             #pragma fragment frag18             #include "UnityCG.cginc"19 20             sampler2D _MainTex;21             sampler2D _SecondTex;22             float _F;23 24             struct v2f {25                 float4 pos:POSITION;26                 float2 uv:TEXCOORD0;27             };28 29             v2f vert(appdata_full v) {30                 v2f o;31                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);32                 o.uv = v.texcoord.xy;33                 return o;34             }35 36             fixed4 frag(v2f IN) :COLOR {37 38                 fixed4 mainColor = tex2D(_MainTex,IN.uv);39                 40                 float offset_uv = 0.05 * sin(IN.uv * _F + _Time.x * 2);41                 float2 uv = IN.uv + offset_uv;42                 uv.y += 0.3;43 44                 fixed4 color_1 = tex2D(_SecondTex, uv);45 46                 mainColor.rgb *= color_1.b;47                 mainColor.rgb *= 2;48 49                 uv = IN.uv - offset_uv;50                 fixed4 color_2 = tex2D(_SecondTex, uv);51                 uv.y += 0.3;52 53                 mainColor.rgb *= color_2.b;54                 mainColor.rgb *= 2;55 56                 return mainColor;57             }58 59             ENDCG60         }61     }62 }
Texture_08

 

1 Shader "VoidGame/66" { 2     Properties{ 3         _MainTex("MainTex",2D) = ""{} 4     } 5     SubShader { 6         Pass{ 7             CGPROGRAM 8             #pragma vertex vert 9             #pragma fragment frag10             #include "UnityCG.cginc"11 12             sampler2D _MainTex;13             sampler2D _WaveTex;14 15             struct v2f {16                 float4 pos:POSITION;17                 float2 uv:TEXCOORD;18             };19 20             v2f vert(appdata_full v) {21                 v2f o;22                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);23                 o.uv = v.texcoord.xy;24                 return o;25             }26 27             fixed4 frag(v2f IN) :COLOR{28 29                 float2 uv = tex2D(_WaveTex,IN.uv).xy;30                 uv = uv * 2 - 1;31                 uv *= 0.025;32 33                 IN.uv += uv;34 35                 fixed4 color = tex2D(_MainTex,IN.uv);36                 return color;37             }38 39             ENDCG40         }41     }42 }
66
1 /*  2 脚本名称:  3 脚本作者:  4 建立时间:  5 脚本功能:  6 版本号:  7 */  8 using UnityEngine;  9 using System.Collections; 10 using System.Threading; 11  12 namespace VoidGame { 13  14     public class WaveTexture : MonoBehaviour { 15  16         public int waveWidth; 17         public int waveHeight; 18  19         float[,] waveA; 20         float[,] waveB; 21  22         Color[] ColorBuffer; 23  24         Texture2D tex_uv; 25  26         bool isRun = true; 27         int sleepTime; 28  29         private void Start() { 30             waveA = new float[waveWidth,waveHeight]; 31             waveB = new float[waveWidth,waveHeight]; 32             tex_uv = new Texture2D(waveWidth,waveHeight); 33  34             ColorBuffer = new Color[waveWidth * waveHeight]; 35  36             GetComponent
().material.SetTexture("_WaveTex",tex_uv); 37 38 //PutDrop(0,0); 39 40 Thread th = new Thread(new ThreadStart(ComputeWave)); 41 th.Start(); 42 } 43 44 private void Update() { 45 sleepTime = (int)(Time.deltaTime * 1000); 46 tex_uv.SetPixels(ColorBuffer); 47 tex_uv.Apply(); 48 49 if(Input.GetMouseButton(0)) { 50 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 51 RaycastHit hit; 52 if(Physics.Raycast(ray,out hit)) { 53 Vector3 pos = hit.point; 54 pos = transform.worldToLocalMatrix.MultiplyPoint(pos); 55 //Debug.Log(pos); 56 int w = (int)((pos.x + 0.5) * waveWidth); 57 int h = (int)((pos.y + 0.5) * waveHeight); 58 PutDrop(w,h); 59 } 60 } 61 //ComputeWave(); 62 } 63 64 private void PutDrop(int x, int y) { 65 //waveA[waveWidth / 2,waveHeight / 2] = 1; 66 //waveA[waveWidth / 2 - 1,waveHeight / 2] = 1; 67 //waveA[waveWidth / 2 + 1,waveHeight / 2] = 1; 68 //waveA[waveWidth / 2,waveHeight / 2 - 1] = 1; 69 //waveA[waveWidth / 2,waveHeight / 2 + 1] = 1; 70 //waveA[waveWidth / 2 - 1,waveHeight / 2 - 1] = 1; 71 //waveA[waveWidth / 2 - 1,waveHeight / 2 + 1] = 1; 72 //waveA[waveWidth / 2 + 1,waveHeight / 2 - 1] = 1; 73 //waveA[waveWidth / 2 + 1,waveHeight / 2 + 1] = 1; 74 75 int radius = 8; 76 float dist; 77 for(int i = -radius;i <= radius;i++) { 78 for(int j = -radius;j < radius;j++) { 79 if(((x + i >= 0) && (x + i < waveWidth - 1)) && ((y + j >= 0) && (y + j < waveHeight - 1))) { 80 dist = Mathf.Sqrt(i * j + j * j); 81 if(dist < radius) { 82 waveA[x + i,y + j] = Mathf.Cos(dist * Mathf.PI / radius); 83 } 84 } 85 } 86 } 87 } 88 89 private void ComputeWave() { 90 while(isRun) { 91 for(int w = 1;w < waveWidth - 1;w++) { 92 for(int h = 1;h < waveHeight - 1;h++) { 93 waveB[w,h] = (waveA[w - 1,h] + waveA[w + 1,h] + waveA[w,h - 1] + waveA[w,h + 1] + waveA[w - 1,h - 1] + waveA[w + 1,h - 1] + waveA[w - 1,h + 1] + waveA[w + 1,h + 1]) / 4 - waveB[w,h]; 94 95 float value = waveB[w,h]; 96 if(value > 1) { 97 waveB[w,h] = 1; 98 } 99 if(value < -1) {100 waveB[w,h] = -1;101 }102 103 float offset_u = (waveB[w - 1,h] - waveB[w + 1,h]) / 2;104 float offset_v = (waveB[w,h - 1] - waveB[w,h + 1]) / 2;105 106 float r = offset_u / 2 + 0.5f;107 float g = offset_v / 2 + 0.5f;108 109 //tex_uv.SetPixel(w,h,new Color(r,g,0));110 ColorBuffer[w + waveWidth * h] = new Color(r,g,0);111 112 waveB[w,h] -= waveB[w,h] * 0.0025f;113 }114 }115 116 //tex_uv.Apply();117 118 float[,] temp = waveA;119 waveA = waveB;120 waveB = temp;121 122 Thread.Sleep(sleepTime);123 }124 }125 126 private void OnDestroy() {127 isRun = false;128 }129 }130 }
WaveTexture

 

1 Shader "VoidGame/70_3" { 2     SubShader{ 3         Tags{ "Queue" = "Transparent" } 4  5         Pass{ 6  7             blend srcalpha oneminussrcalpha 8             ztest greater 9             zwrite on10 11             CGPROGRAM12             #pragma vertex vert13             #pragma fragment frag14             #include "UnityCG.cginc"15 16             struct v2f {17                 float4 pos:POSITION;18             };19 20             v2f vert(appdata_base v) {21                 v2f o;22                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);23 24                 return o;25             }26 27             fixed4 frag(v2f IN) :COLOR{28                 fixed4 color = fixed4(1,1,0,0.5);29                 return color;30             }31 32             ENDCG33         }34 35         Pass{36 37             //blend srcalpha oneminussrcalpha38             ztest less39             zwrite on40 41             CGPROGRAM42             #pragma vertex vert43             #pragma fragment frag44             #include "UnityCG.cginc"45 46             struct v2f {47                 float4 pos:POSITION;48             };49 50             v2f vert(appdata_base v) {51                 v2f o;52                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);53 54                 return o;55             }56 57             fixed4 frag(v2f IN) :COLOR{58                 fixed4 color = fixed4(0,0,1,1);59             return color;60             }61 62             ENDCG63         }64     }65 }
70_3

 

1 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' 2  3 Shader "VoidGame/52" { 4     Properties{ 5         _Scale("Scale",range(1,8)) = 2 6     } 7     SubShader{ 8         Tags { "Queue" = "Transparent" } 9         Pass{10             blend srcalpha oneminussrcalpha11             CGPROGRAM12 13             #pragma vertex vert14             #pragma fragment frag15             #include "UnityCG.cginc"16 17             float _Scale;18 19             struct v2f {20                 float4 pos:POSITION;21                 float3 normal:TEXCOORD0;22                 float4 vertex:TEXCOORD1;23             };24 25             v2f vert(appdata_base v) {26                 v2f o;27                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);28                 o.vertex = v.vertex;29                 o.normal = v.normal;30                 return o;31             }32 33             fixed4 frag(v2f IN) :COLOR{34                 //float3 N = mul(IN.normal,(float3x3)_World2Object);35                 float3 N = mul((float3x3)unity_ObjectToWorld, IN.normal);36                 N = normalize(N);37                 float3 worldPos = mul(unity_ObjectToWorld, IN.vertex).xyz;38                 float3 V = _WorldSpaceCameraPos.xyz - worldPos;39                 V = normalize(V);40 41                 float bright = 1.0 - saturate(dot(N, V));42                 bright = pow(bright, _Scale);43                 return fixed4(1,0,0,1) * bright;44             }45 46             ENDCG47         }48     }49 }
52

1 Shader "VoidGame/54" { 2     Properties{ 3         _MainColor("MainColor",color) = (1,1,1,1) 4         _SecondColor("SecondColor",color) = (1,1,1,1) 5         _Center("Center",range(-0.7,0.7)) = 0 6         _R("R",range(0,0.5)) = 0.2 7     } 8     SubShader{ 9         Pass{10 11         CGPROGRAM12         #pragma vertex vert13         #pragma fragment frag14         #include "UnityCG.cginc"15 16         float4 _MainColor;17         float4 _SecondColor;18         float _Center;19         float _R;20 21         struct v2f {22             float4 pos:POSITION;23             float y:TEXCOORD;24         };25 26         v2f vert(appdata_base v) {27             v2f o;28             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);29             o.y = v.vertex.y;30             return o;31         }32 33         fixed4 frag(v2f IN) :COLOR{34 35             float d = IN.y - _Center;36             float s = abs(IN.y - _Center);37             d = d / s;38             float f = s / _R;39             f = saturate(f);40             d *= f;41 42             d = d / 2 + 0.5;43 44             return lerp(_MainColor, _SecondColor, d);45 46             //if (IN.y > _Center + _R) {47             //    return _MainColor;48             //}49             //else if (IN.y > _Center && IN.y < _Center + _R) {50             //    float d = IN.y - _Center;51             //    d = (1 - d / _R) - 0.5;52             //    d = saturate(d);53             //    return lerp(_MainColor, _SecondColor, d);54             //}55 56             //if (IN.y <= _Center - _R) {57             //    return _SecondColor;58             //}59             //else if (IN.y  < _Center && IN.y > _Center - _R) {60             //    float d = _Center - IN.y;61             //    d = (1 - d / _R) - 0.5;62             //    d = saturate(d);63             //    return lerp(_MainColor, _SecondColor, 1 - d);64             //}65 66             //return lerp(_MainColor, _SecondColor, 0.5);67 68 69             //float d = IN.y - _Center;70             //d = d / abs(d);71             //d = d / 2 + 0.5;72             //return lerp(_MainColor,_SecondColor,d);73 74 75             //if (IN.y > _Center) {76             //    return _MainColor;77             //}78             //else {79             //    return _SecondColor;80             //}81         }82 83         ENDCG84     }85     }86 }
54

 项目:https://pan.baidu.com/s/1nvsKklj

转载于:https://www.cnblogs.com/revoid/p/6522929.html

你可能感兴趣的文章
git回滚
查看>>
vue2.0 引用qrcode.js实现获取改变二维码的样式
查看>>
Python 判断闰年,判断日期是当前年的第几天
查看>>
activiti 清库脚本(转)
查看>>
如何快速查看服务器配置信息?
查看>>
caffe blob理解
查看>>
特殊字符校验
查看>>
TensorFlow(1)注解入门代码
查看>>
GCC 中 -L、-rpath和-rpath-link的区别
查看>>
RedHat7下PostGIS源码安装
查看>>
亚马逊AWS学习——VPC里面几个概念的关系
查看>>
context.getSystemService的简单说明
查看>>
php中的正则函数:正则匹配,正则替换,正则分割 所有的操作都不会影响原来的字符串....
查看>>
三个小时学会wordpress模板制作
查看>>
【网络协议】TCP协议简单介绍
查看>>
利用SMB jcifs实现对windows中的共享文件夹的操作
查看>>
Spring(十七):Spring AOP(一):简介
查看>>
html5常用属性text-shadow、vertical-align、background如何使用
查看>>
微软正式宣布Azure MongoDB Atlas免费方案
查看>>
Jessica Kerr:高绩效团队简史
查看>>