L老师 Shader编程教程 学习
Shader "VoidGame/FixedShader" {
Properties{
//颜色
_Color("Color",Color)=(1,1,1,1)
//环境光
_Ambient("Ambient",Color)=(0.3,0.3,0.3,0.3)
//高光反射
_Specular("Specular",Color)=(1,1,1,1)
//光泽
_Shininess("Shininess",range(0,8))=4
//自发光
_Emission("Emision",Color)=(1,1,1,1)
//主纹理
_MainTex("MainTex",2D) = "white"{}
//第二张纹理
_SecondTex("SecondTex",2D) = "white"{}
_ConstantColor("ConstantColor",Color)=(1,1,1,0.3)
}
SubShader{
Tags { "Queue" = "Transparent" }
pass {
Blend SrcAlpha OneMinusSrcAlpha //color(1,1,1,1)
//color[_Color]
material{
diffuse[_Color]
ambient[_Ambient]
specular[_Specular]
shininess[_Shininess]
emission[_Emission]
}
lighting on
separatespecular on settexture[_MainTex]{
combine texture * primary double
}
settexture[_SecondTex]{
constantColor[_ConstantColor]
combine texture * previous double,texture * constant
}
}
}
}
FixedShader
Shader "VoidGame/VertexShader1" {
SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag void vert(in float2 objPos:POSITION,out float4 pos:POSITION,out float4 col:COLOR) {
pos = float4(objPos,0,1);
col = pos;
} void frag(inout float4 col:COLOR) {
col = float4(0,1,0,1);
} ENDCG
}
}
}
VertexShader1
数学函数(Mathematical Functions)
几何函数(Geometric Functions)
纹理映射函数(Texture Map Functions)
偏导函数(Derivative Functions)
调试函数(Debugging Function)
2d旋转
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text; namespace MatrixTransform {
class Triangle {
PointF A, B, C; public Triangle(PointF A,PointF B,PointF C) {
this.A = A;
this.B = B;
this.C = C;
} public void Draw(Graphics g) {
Pen pen = new Pen(Color.Red,);
g.DrawLine(pen,A,B);
g.DrawLine(pen,B,C);
g.DrawLine(pen,C,A);
} public void Rotate(int degree) {
float angle = (float)(degree / 360.0f * Math.PI); float newX = (float)(A.X * Math.Cos(angle) - A.Y * Math.Sin(angle));
float newY = (float)(A.X * Math.Sin(angle) + A.Y * Math.Cos(angle)); A.X = newX;
A.Y = newY; newX = (float)(B.X * Math.Cos(angle) - B.Y * Math.Sin(angle));
newY = (float)(B.X * Math.Sin(angle) + B.Y * Math.Cos(angle)); B.X = newX;
B.Y = newY; newX = (float)(C.X * Math.Cos(angle) - C.Y * Math.Sin(angle));
newY = (float)(C.X * Math.Sin(angle) + C.Y * Math.Cos(angle)); C.X = newX;
C.Y = newY;
}
}
}
Triangle
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace MatrixTransform {
public partial class Form1:Form { Triangle t; public Form1() {
InitializeComponent();
} private void From1_Paint(Object sender,PaintEventArgs e) {
e.Graphics.TranslateTransform(,);
t.Draw(e.Graphics);
} private void Form1_Load(object sender,EventArgs e) {
PointF A = new PointF(,-);
PointF B = new PointF(,);
PointF C = new PointF(-,);
t = new Triangle(A,B,C);
} private void timer1_Tick(object sender,EventArgs e) {
t.Rotate();
this.Invalidate();
}
}
}
Form1
视频:https://pan.baidu.com/s/1nu9yHgl
项目:https://pan.baidu.com/s/1skDuxtj
3d
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using System;
using System.Collections.Generic;
using System.Text; namespace _3DTransform { class Vector4 {
public double x, y, z, w;
public Vector4() { } public Vector4(double x,double y,double z,double w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
} public Vector4(Vector4 v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
this.w = v.w;
}
}
}
Vector4
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using System;
using System.Collections.Generic;
using System.Text; namespace _3DTransform {
class Matrix4x4 { private double[,] pts;
public Matrix4x4() {
pts = new double[,];
} public double this[int i,int j] {
get {
return pts[i - ,j - ];
}
set {
pts[i - ,j - ] = value;
}
} public Matrix4x4 Mul(Matrix4x4 m) {
Matrix4x4 newM = new Matrix4x4();
for(int w = ;w <= ;w++) {
for(int h = ;h <= ;h++) {
for(int n = ;n <= ;n++) {
newM[w,h] += this[w,n] * m[n,h];
}
}
}
return newM;
} public Vector4 Mul(Vector4 v) {
Vector4 newV = new Vector4();
newV.x = v.x * this[,] + v.y * this[,] + v.z * this[,] + v.w * this[,];
newV.y = v.x * this[,] + v.y * this[,] + v.z * this[,] + v.w * this[,];
newV.z = v.x * this[,] + v.y * this[,] + v.z * this[,] + v.w * this[,];
newV.w = v.x * this[,] + v.y * this[,] + v.z * this[,] + v.w * this[,]; return newV;
}
}
}
Matrix4
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text; namespace _3DTransform {
class Triangle3D { private Vector4 a, b, c; public Vector4 A, B, C; public Triangle3D() { } public Triangle3D(Vector4 a,Vector4 b,Vector4 c) {
this.A = this.a = new Vector4(a);
this.B = this.b = new Vector4(b);
this.C = this.c = new Vector4(c);
} public void Transform(Matrix4x4 m) {
this.a = m.Mul(this.A);
this.b = m.Mul(this.B);
this.c = m.Mul(this.C);
} public void Draw(Graphics g) {
g.TranslateTransform(,);
g.DrawLines(new Pen(Color.Red,),this.Get2DPointFArr());
} private PointF[] Get2DPointFArr() {
PointF[] arr = new PointF[];
arr[] = Get2DPointF(this.a);
arr[] = Get2DPointF(this.b);
arr[] = Get2DPointF(this.c);
arr[] = arr[];
return arr;
} private PointF Get2DPointF(Vector4 v) {
PointF p = new PointF();
p.X = (float)(v.x / v.w);
p.Y = (float)(v.y / v.w);
return p;
}
}
}
Triangle3D
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace _3DTransform {
public partial class Form1:Form { int a; Triangle3D triangle; Matrix4x4 m_scale;
Matrix4x4 m_rotation;
Matrix4x4 m_view;
Matrix4x4 m_projection; public Form1() {
InitializeComponent(); m_scale = new Matrix4x4();
m_scale[,] = ;
m_scale[,] = ;
m_scale[,] = ;
m_scale[,] = ; m_rotation = new Matrix4x4(); m_view = new Matrix4x4();
m_view[,] = ;
m_view[,] = ;
m_view[,] = ;
m_view[,] = ;
m_view[,] = ; m_projection = new Matrix4x4();
m_projection[,] = ;
m_projection[,] = ;
m_projection[,] = ;
m_projection[,] = 1.0/;
} private void Form1_Load(object sender,EventArgs e) {
Vector4 a = new Vector4(,-0.5,,);
Vector4 b = new Vector4(0.5,0.5,,);
Vector4 c = new Vector4(-0.5,0.5,,); triangle = new Triangle3D(a,b,c); } private void Form1_Paint(object sender,PaintEventArgs e) {
triangle.Draw(e.Graphics);
} private void timer1_Tick(object sender,EventArgs e) { a += ;
double angle = a / 360.0 * Math.PI; m_rotation[,] = Math.Cos(angle);
m_rotation[,] = Math.Sin(angle);
m_rotation[,] = ;
m_rotation[,] = -Math.Sin(angle);
m_rotation[,] = Math.Cos(angle);
m_rotation[,] = ; Matrix4x4 m = m_scale.Mul(m_rotation);
m = m.Mul(m_view);
m = m.Mul(m_projection); triangle.Transform(m);
this.Invalidate();
} private void trackBar1_Scroll(object sender,EventArgs e) {
m_view[,] = (sender as TrackBar).Value;
}
}
}
Form1
视频:https://pan.baidu.com/s/1slxvCk9
项目:https://pan.baidu.com/s/1dERVVdZ
旋转
Shader "VoidGame/31" {
SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" float4x4 mvp;
float4x4 rm;
float4x4 sm; struct v2f {
float4 pos:POSITION;
}; v2f vert(appdata_base v) {
v2f o; //o.pos = mul(mvp, v.vertex); float4x4 m = mul(UNITY_MATRIX_MVP, sm);
o.pos = mul(m, v.vertex); return o;
} fixed4 frag():COLOR {
return fixed4(, , , );
} ENDCG
}
}
}
31
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using System.Collections; namespace VoidGame { public class MVPTransform : MonoBehaviour { private void Update() { //Matrix4x4 RM = new Matrix4x4();
//RM[0,0] = Mathf.Cos(Time.realtimeSinceStartup);
//RM[0,2] = Mathf.Sin(Time.realtimeSinceStartup);
//RM[1,1] = 1;
//RM[2,0] = -Mathf.Sin(Time.realtimeSinceStartup);
//RM[2,2] = Mathf.Cos(Time.realtimeSinceStartup);
//RM[3,3] = 1; //Matrix4x4 mvp = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix; //mvp *= RM; //GetComponent<Renderer>().material.SetMatrix("mvp",mvp); Matrix4x4 RM = new Matrix4x4();
RM[,] = Mathf.Cos(Time.realtimeSinceStartup);
RM[,] = Mathf.Sin(Time.realtimeSinceStartup);
RM[,] = ;
RM[,] = -Mathf.Sin(Time.realtimeSinceStartup);
RM[,] = Mathf.Cos(Time.realtimeSinceStartup);
RM[,] = ; Matrix4x4 SM = new Matrix4x4();
SM[,] = Mathf.Sin(Time.realtimeSinceStartup) / + 0.5f;
SM[,] = Mathf.Cos(Time.realtimeSinceStartup) / + 0.5f;
SM[,] = Mathf.Sin(Time.realtimeSinceStartup) / + 0.5f;
SM[,] = ; GetComponent<Renderer>().material.SetMatrix("sm",SM);
}
}
}
MVPTransform
颜色
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader "VoidGame/33" {
SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" struct v2f {
float4 pos:POSITION;
fixed4 color : COLOR;
}; v2f vert(appdata_base v) {
v2f o; //o.pos = mul(mvp, v.vertex); o.pos = mul(UNITY_MATRIX_MVP, v.vertex); //if (v.vertex.x > 0) {
// o.color = fixed4(1, 0, 0, 1);
//}
//else {
// o.color = fixed4(0, 0, 1, 1);
//} if (v.vertex.x == 0.5 && v.vertex.y == 0.5 && v.vertex.z == -0.5) {
o.color = fixed4(_SinTime.w / + 0.5, _CosTime.w / + 0.5, _SinTime.y / + 0.5, );
}
else {
o.color = fixed4(, , , );
} //float4 wpos = mul(unity_ObjectToWorld, v.vertex);
//if (wpos.x > 0) {
// o.color = fixed4(1, 0, 0, 1);
//}
//else {
// o.color = fixed4(0, 0, 1, 1);
//} return o;
} fixed4 frag(v2f IN):COLOR {
return IN.color;
} ENDCG
}
}
}
33
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader "VoidGame/34" {
SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" float dis;
float r; struct v2f {
float4 pos:POSITION;
fixed4 color : COLOR;
}; v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); float x = o.pos.x / o.pos.w; if (x > dis && x < dis+r) {
o.color = fixed4(, , , );
}
else {
o.color = fixed4(x / + 0.5, x / + 0.5, x / + 0.5, );
} return o;
} fixed4 frag(v2f IN):COLOR {
return IN.color;
} ENDCG
}
}
}
34
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using System.Collections; namespace VoidGame { public class SetFloat : MonoBehaviour { private float dis = -;
private float r = 0.1f; private void Update() {
dis += Time.deltaTime;
GetComponent<Renderer>().material.SetFloat("dis",dis);
GetComponent<Renderer>().material.SetFloat("r",r);
}
}
}
SetFloat
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader "VoidGame/35" {
Properties{
_R("R",range(,)) =
_OX("OX",range(-,)) =
}
SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" float dis;
float r; float _R;
float _OX; struct v2f {
float4 pos:POSITION;
fixed4 color : COLOR;
}; v2f vert(appdata_base v) { //float2 xy = v.vertex.xz; float4 wpos = mul(unity_ObjectToWorld, v.vertex); float2 xy = wpos.xz;
float d = _R - length(xy - float2(_OX,)); d = d < ? : d;
float height = ;
float4 uppos = float4(v.vertex.x, height*d, v.vertex.z, v.vertex.w); v2f o;
o.pos = mul(UNITY_MATRIX_MVP, uppos); o.color = fixed4(uppos.y, uppos.y, uppos.y, ); return o;
} fixed4 frag(v2f IN):COLOR {
return IN.color;
} ENDCG
}
}
}
35
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader "VoidGame/36" {
SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" struct v2f {
float4 pos:POSITION;
fixed4 color : COLOR;
}; v2f vert(appdata_base v) { float angle = length(v.vertex)*_SinTime.w; //float4x4 m = {
// float4(cos(angle),0,sin(angle),0),
// float4(0,1,0,0),
// float4(-sin(angle),0,cos(angle),0),
// float4(0,0,0,1)
//}; float x = cos(angle)*v.vertex.x + sin(angle)*v.vertex.z;
float z = cos(angle)*v.vertex.z - sin(angle)*v.vertex.x;
v.vertex.x = x;
v.vertex.z = z; //v.vertex = mul(m, v.vertex); v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex); o.color = fixed4(,,,); return o;
} fixed4 frag(v2f IN) :COLOR{
return IN.color;
} ENDCG
}
}
}
36
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader "VoidGame/37" {
SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" struct v2f {
float4 pos:POSITION;
fixed4 color : COLOR;
}; v2f vert(appdata_base v) { //A*sin(omega*x+t); v.vertex.y += 0.2 * sin((v.vertex.x + v.vertex.z) + _Time.y);
v.vertex.y += 0.3 * sin((v.vertex.x - v.vertex.z) + _Time.w); //v.vertex.y += 0.2 * sin(-length(v.vertex.xz)+ _Time.y); v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = fixed4(v.vertex.y, v.vertex.y, v.vertex.y, );
return o;
} fixed4 frag(v2f IN) :COLOR{
return IN.color;
} ENDCG
}
}
}
37
Shader "VoidGame/38" {
SubShader{
Pass{
Tags{ "LightMode" = "ForwardBase" } CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc" struct v2f {
float4 pos:POSITION;
fixed4 color:COLOR;
}; v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); float3 N = normalize(v.normal);
float3 L = normalize(_WorldSpaceLightPos0); N = mul(float4(N, ),unity_WorldToObject).xyz;
N = normalize(N);
//L = mul(unity_WorldToObject, float4(L, 0)).xyz; float ndotl = saturate(dot(N,L));
o.color = _LightColor0 * ndotl; return o;
} fixed4 frag(v2f IN):COLOR{
return IN.color + UNITY_LIGHTMODEL_AMBIENT;
} ENDCG
}
}
}
38
Shader "VoidGame/Texture_01" {
properties{
_MainTex("MainTex",2D)=""{}
} SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; struct v2f {
float4 pos:POSITION;
float2 uv:TEXCOORD0;
}; v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); return o;
} fixed4 frag(v2f IN) :COLOR{
fixed4 color = tex2D(_MainTex,IN.uv);
return color;
} ENDCG
}
}
}
纹理
Shader "VoidGame/Texture_02" {
properties{
_MainTex("MainTex",2D)=""{}
} SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex;
sampler2D unity_Lightmap; float4 _MainTex_ST;
float4 unity_LightmapST; struct v2f {
float4 pos:POSITION;
float2 uv:TEXCOORD0;
float2 uv2:TEXCOORD1;
}; v2f vert(appdata_full v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw; return o;
} fixed4 frag(v2f IN) :COLOR{
float3 lm = DecodeLightmap(tex2D(unity_Lightmap,IN.uv2));
fixed4 color = tex2D(_MainTex,IN.uv);
color.rgb *= lm * ;
return color;
} ENDCG
}
}
}
光照
Shader "VoidGame/Texture_03" {
properties{
_MainTex("MainTex",2D)=""{}
} SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; struct v2f {
float4 pos:POSITION;
float2 uv:TEXCOORD0;
}; v2f vert(appdata_full v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); return o;
} fixed4 frag(v2f IN) :COLOR{
fixed4 color = tex2D(_MainTex,IN.uv);
return color;
} ENDCG
}
}
}
Texture_03
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using System.Collections; namespace VoidGame { public class SetTextureUVST : MonoBehaviour { public int width;
public int height;
public int fps; private int currentIndex; IEnumerator Start() {
Material mat = GetComponent<Renderer>().material; float scale_x = 1.0f / width;
float scale_y = 1.0f / height; while(true) {
float offset_x = currentIndex % width * scale_x;
float offset_y = currentIndex / height * scale_y; mat.SetTextureOffset("_MainTex",new Vector2(offset_x,offset_y));
mat.SetTextureScale("_MainTex",new Vector2(scale_x,scale_y));
yield return new WaitForSeconds(1.0f / fps);
currentIndex = (++currentIndex) % (width * height);
}
} private void Update() { }
}
}
SetTextureUVST
Shader "VoidGame/Texture_04" {
properties{
_MainTex("MainTex",2D)=""{}
_F("F",range(,))=
_A("A",range(,0.1))=0.01
_R("R",range(,))=
} SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST;
float _F;
float _A;
float _R; struct v2f {
float4 pos:POSITION;
float2 uv:TEXCOORD0;
}; v2f vert(appdata_full v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); return o;
} fixed4 frag(v2f IN) :COLOR{
//IN.uv += _Time.x; //IN.uv.x += 0.01 * sin(IN.uv.x * 3.14 * _F + _Time.y);
//IN.uv.y += 0.01 * sin(IN.uv.y * 3.14 * _F + _Time.y); //IN.uv += _A * sin(IN.uv * 3.14 * _F + _Time.y); float2 uv = IN.uv;
float dis = distance(uv, float2(0.5, 0.5));
float scale = ;
//if (dis < _R) {
_A *= saturate( - dis / _R);
scale = _A * sin(-dis * 3.14 * _F + _Time.y);
uv = uv + uv * scale;
//} //fixed4 color = tex2D(_MainTex,IN.uv);
fixed4 color = tex2D(_MainTex, uv) + fixed4(,,,) * saturate(scale) * ;
return color;
} ENDCG
}
}
}
Texture_04
Shader "VoidGame/Texture_05" {
properties{
_MainTex("MainTex",2D)=""{}
_F("F",range(,))=
_A("A",range(,0.1))=0.01
_R("R",range(,))=
} SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0 sampler2D _MainTex; float4 _MainTex_ST;
float _F;
float _A;
float _R; struct v2f {
float4 pos:POSITION;
float2 uv:TEXCOORD0;
float z : TEXCOORD1;
}; v2f vert(appdata_full v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.z = mul(_Object2World, v.vertex).z; return o;
} fixed4 frag(v2f IN) :COLOR{ //float offset_uv = 0.01; //float2 uv = IN.uv;
//fixed4 color = tex2D(_MainTex,uv);
//
//uv.x = IN.uv.x + offset_uv;
//color.rgb += tex2D(_MainTex,uv);
//
//uv.x = IN.uv.x - offset_uv;
//color.rgb += tex2D(_MainTex, uv); //uv.y = IN.uv.y + offset_uv;
//color.rgb += tex2D(_MainTex, uv); //uv.y = IN.uv.y - offset_uv;
//color.rgb += tex2D(_MainTex, uv); //color.rgb /= 5; //float dx = ddx(IN.uv.x) * 10;
//float2 dsdx = float2(dx, dx);
//float dy = ddy(IN.uv.y) * 10;
//float2 dsdy = float2(dy, dy); float2 dsdx = ddx(IN.z) * ;
float2 dsdy = ddy(IN.z) * ; fixed4 color = tex2D(_MainTex,IN.uv,dsdx,dsdy); return color;
} ENDCG
}
}
}
Texture_05
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader "VoidGame/Texture_06" {
Properties{
_MainTex("MainTex",2D)=""{}
_F("F",range(,))=
}
SubShader{
Pass{
CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex;
float _F; struct v2f {
float4 pos:POSITION;
float2 uv:TEXCOORD0;
}; v2f vert(appdata_full v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
return o;
} fixed4 frag(v2f IN) :COLOR {
//float2 uv = IN.uv;
//float offset_uv = 0.05 * sin(IN.uv * _F + _Time.x);
//uv += offset_uv;
//fixed4 color = tex2D(_MainTex,uv); float2 uv = IN.uv;
float offset_uv = 0.05 * sin(IN.uv * _F + _Time.x*); uv += offset_uv;
fixed4 color_1 = tex2D(_MainTex, uv); uv = IN.uv;
uv -= offset_uv * ;
fixed4 color_2 = tex2D(_MainTex, uv); return (color_1 + color_2)/;
} ENDCG
}
}
}
Texture_07
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader "VoidGame/Texture_08" {
Properties{
_MainTex("MainTex",2D)=""{}
_SecondTex("SecondTex",2D)=""{}
_F("F",range(,))=
}
SubShader{
Pass{ colormask r CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex;
sampler2D _SecondTex;
float _F; struct v2f {
float4 pos:POSITION;
float2 uv:TEXCOORD0;
}; v2f vert(appdata_full v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
return o;
} fixed4 frag(v2f IN) :COLOR { fixed4 mainColor = tex2D(_MainTex,IN.uv); float offset_uv = 0.05 * sin(IN.uv * _F + _Time.x * );
float2 uv = IN.uv + offset_uv;
uv.y += 0.3; fixed4 color_1 = tex2D(_SecondTex, uv); mainColor.rgb *= color_1.b;
mainColor.rgb *= ; uv = IN.uv - offset_uv;
fixed4 color_2 = tex2D(_SecondTex, uv);
uv.y += 0.3; mainColor.rgb *= color_2.b;
mainColor.rgb *= ; return mainColor;
} ENDCG
}
}
}
Texture_08
Shader "VoidGame/66" {
Properties{
_MainTex("MainTex",2D) = ""{}
}
SubShader {
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex;
sampler2D _WaveTex; struct v2f {
float4 pos:POSITION;
float2 uv:TEXCOORD;
}; v2f vert(appdata_full v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
return o;
} fixed4 frag(v2f IN) :COLOR{ float2 uv = tex2D(_WaveTex,IN.uv).xy;
uv = uv * - ;
uv *= 0.025; IN.uv += uv; fixed4 color = tex2D(_MainTex,IN.uv);
return color;
} ENDCG
}
}
}
66
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using System.Collections;
using System.Threading; namespace VoidGame { public class WaveTexture : MonoBehaviour { public int waveWidth;
public int waveHeight; float[,] waveA;
float[,] waveB; Color[] ColorBuffer; Texture2D tex_uv; bool isRun = true;
int sleepTime; private void Start() {
waveA = new float[waveWidth,waveHeight];
waveB = new float[waveWidth,waveHeight];
tex_uv = new Texture2D(waveWidth,waveHeight); ColorBuffer = new Color[waveWidth * waveHeight]; GetComponent<Renderer>().material.SetTexture("_WaveTex",tex_uv); //PutDrop(0,0); Thread th = new Thread(new ThreadStart(ComputeWave));
th.Start();
} private void Update() {
sleepTime = (int)(Time.deltaTime * );
tex_uv.SetPixels(ColorBuffer);
tex_uv.Apply(); if(Input.GetMouseButton()) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit)) {
Vector3 pos = hit.point;
pos = transform.worldToLocalMatrix.MultiplyPoint(pos);
//Debug.Log(pos);
int w = (int)((pos.x + 0.5) * waveWidth);
int h = (int)((pos.y + 0.5) * waveHeight);
PutDrop(w,h);
}
}
//ComputeWave();
} private void PutDrop(int x, int y) {
//waveA[waveWidth / 2,waveHeight / 2] = 1;
//waveA[waveWidth / 2 - 1,waveHeight / 2] = 1;
//waveA[waveWidth / 2 + 1,waveHeight / 2] = 1;
//waveA[waveWidth / 2,waveHeight / 2 - 1] = 1;
//waveA[waveWidth / 2,waveHeight / 2 + 1] = 1;
//waveA[waveWidth / 2 - 1,waveHeight / 2 - 1] = 1;
//waveA[waveWidth / 2 - 1,waveHeight / 2 + 1] = 1;
//waveA[waveWidth / 2 + 1,waveHeight / 2 - 1] = 1;
//waveA[waveWidth / 2 + 1,waveHeight / 2 + 1] = 1; int radius = ;
float dist;
for(int i = -radius;i <= radius;i++) {
for(int j = -radius;j < radius;j++) {
if(((x + i >= ) && (x + i < waveWidth - )) && ((y + j >= ) && (y + j < waveHeight - ))) {
dist = Mathf.Sqrt(i * j + j * j);
if(dist < radius) {
waveA[x + i,y + j] = Mathf.Cos(dist * Mathf.PI / radius);
}
}
}
}
} private void ComputeWave() {
while(isRun) {
for(int w = ;w < waveWidth - ;w++) {
for(int h = ;h < waveHeight - ;h++) {
waveB[w,h] = (waveA[w - ,h] + waveA[w + ,h] + waveA[w,h - ] + waveA[w,h + ] + waveA[w - ,h - ] + waveA[w + ,h - ] + waveA[w - ,h + ] + waveA[w + ,h + ]) / - waveB[w,h]; float value = waveB[w,h];
if(value > ) {
waveB[w,h] = ;
}
if(value < -) {
waveB[w,h] = -;
} float offset_u = (waveB[w - ,h] - waveB[w + ,h]) / ;
float offset_v = (waveB[w,h - ] - waveB[w,h + ]) / ; float r = offset_u / + 0.5f;
float g = offset_v / + 0.5f; //tex_uv.SetPixel(w,h,new Color(r,g,0));
ColorBuffer[w + waveWidth * h] = new Color(r,g,); waveB[w,h] -= waveB[w,h] * 0.0025f;
}
} //tex_uv.Apply(); float[,] temp = waveA;
waveA = waveB;
waveB = temp; Thread.Sleep(sleepTime);
}
} private void OnDestroy() {
isRun = false;
}
}
}
WaveTexture
Shader "VoidGame/70_3" {
SubShader{
Tags{ "Queue" = "Transparent" } Pass{ blend srcalpha oneminussrcalpha
ztest greater
zwrite on CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" struct v2f {
float4 pos:POSITION;
}; v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); return o;
} fixed4 frag(v2f IN) :COLOR{
fixed4 color = fixed4(,,,0.5);
return color;
} ENDCG
} Pass{ //blend srcalpha oneminussrcalpha
ztest less
zwrite on CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" struct v2f {
float4 pos:POSITION;
}; v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); return o;
} fixed4 frag(v2f IN) :COLOR{
fixed4 color = fixed4(,,,);
return color;
} ENDCG
}
}
}
70_3
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader "VoidGame/52" {
Properties{
_Scale("Scale",range(,)) =
}
SubShader{
Tags { "Queue" = "Transparent" }
Pass{
blend srcalpha oneminussrcalpha
CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" float _Scale; struct v2f {
float4 pos:POSITION;
float3 normal:TEXCOORD0;
float4 vertex:TEXCOORD1;
}; v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.vertex = v.vertex;
o.normal = v.normal;
return o;
} fixed4 frag(v2f IN) :COLOR{
//float3 N = mul(IN.normal,(float3x3)_World2Object);
float3 N = mul((float3x3)unity_ObjectToWorld, IN.normal);
N = normalize(N);
float3 worldPos = mul(unity_ObjectToWorld, IN.vertex).xyz;
float3 V = _WorldSpaceCameraPos.xyz - worldPos;
V = normalize(V); float bright = 1.0 - saturate(dot(N, V));
bright = pow(bright, _Scale);
return fixed4(,,,) * bright;
} ENDCG
}
}
}
52
Shader "VoidGame/54" {
Properties{
_MainColor("MainColor",color) = (,,,)
_SecondColor("SecondColor",color) = (,,,)
_Center("Center",range(-0.7,0.7)) =
_R("R",range(,0.5)) = 0.2
}
SubShader{
Pass{ CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" float4 _MainColor;
float4 _SecondColor;
float _Center;
float _R; struct v2f {
float4 pos:POSITION;
float y:TEXCOORD;
}; v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.y = v.vertex.y;
return o;
} fixed4 frag(v2f IN) :COLOR{ float d = IN.y - _Center;
float s = abs(IN.y - _Center);
d = d / s;
float f = s / _R;
f = saturate(f);
d *= f; d = d / + 0.5; return lerp(_MainColor, _SecondColor, d); //if (IN.y > _Center + _R) {
// return _MainColor;
//}
//else if (IN.y > _Center && IN.y < _Center + _R) {
// float d = IN.y - _Center;
// d = (1 - d / _R) - 0.5;
// d = saturate(d);
// return lerp(_MainColor, _SecondColor, d);
//} //if (IN.y <= _Center - _R) {
// return _SecondColor;
//}
//else if (IN.y < _Center && IN.y > _Center - _R) {
// float d = _Center - IN.y;
// d = (1 - d / _R) - 0.5;
// d = saturate(d);
// return lerp(_MainColor, _SecondColor, 1 - d);
//} //return lerp(_MainColor, _SecondColor, 0.5); //float d = IN.y - _Center;
//d = d / abs(d);
//d = d / 2 + 0.5;
//return lerp(_MainColor,_SecondColor,d); //if (IN.y > _Center) {
// return _MainColor;
//}
//else {
// return _SecondColor;
//}
} ENDCG
}
}
}
54
项目:https://pan.baidu.com/s/1nvsKklj
L老师 Shader编程教程 学习的更多相关文章
- Shader编程教程
2010-05-13 11:37:14| 分类: DirectX 3D学习|举报|字号 订阅 Shader编程教程1-环境光照 您好,欢迎来到XNA Shader教程1.我的名字叫Petri ...
- Stage3d 由浅到深理解AGAL的管线vertex shader和fragment shader || 简易教程 学习心得 AGAL 非常非常好的入门文章
Everyday Stage3D (一) Everyday Stage3D (二) Triangle Everyday Stage3D (三) AGAL的基本概念 Everyday Stage3D ( ...
- 根据学习廖雪峰老师的git教程做的笔记
根据学习廖雪峰老师的git教程做的笔记 安装git 进行git的配置 配置您的用户名和邮箱地址,使用--global 这个参数表明了在此台机器上的所有仓库都会使用该配置 $ git config -- ...
- 【译】Unity3D Shader 新手教程(2/6) —— 积雪Shader
本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 如果你是一个shader编程的新手,并且你想学到下面这些酷炫的技术,我觉得你可以看看这篇教程: 实现一个积雪效果的 ...
- 【译】Unity3D Shader 新手教程(1/6)
本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 刚开始接触Unity3D Shader编程时,你会发现有关shader的文档相当散,这也造成初学者对Unity3D ...
- 【浅墨Unity3D Shader编程】之一 夏威夷篇:游戏场景的创建 & 第一个Shader的书写
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨) ...
- 【repost】如何学好编程 (精挑细选编程教程,帮助现在在校学生学好编程,让你门找到编程的方向)四个方法总有一个学好编程的方法适合你
方法(一)编了这么久的程序,一直想找机会总结下其中的心得和方法,但回想我这段编程道路,又很难说清楚,如果按照我走过的所有路来说,显然是不可能的!当我看完了云风的<游戏之旅--编程感悟>和梁 ...
- 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&颜色、光照与材质
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40955607 作者:毛星云(浅墨) ...
- 【浅墨Unity3D Shader编程】之中的一个 夏威夷篇:游戏场景的创建 & 第一个Shader的书写
本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨) ...
随机推荐
- 安装babel
http://jamesknelson.com/using-es6-in-the-browser-with-babel-6-and-webpack/
- 2.5 C++类class和结构体struct区别
参考:http://www.weixueyuan.net/view/6337.html 总结: 在C++中,struct类似于class,在其中既可以定义数据成员,又可以定义成员函数. 在C++中,s ...
- Centos7安装配置MySQL5.7
一:安装前准备: 1.1检查linux版本:cat /etc/system-release CentOS Linux release 7.6.1810 (Core) 1.2查看系统是否安装MySQL: ...
- centos7中docker操作
docker部署nginx 1. 下载nginx [root@localhost my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED S ...
- 插入排序算法 Java实现
插入排序算法是算法排序中的一种: 该算法是假设已有序列是有序序列,从首元素(首元素为单个元素,肯定是有序的...)开始分析,对其他元素的位置进行有序的确定: 以算法为例: public class I ...
- CentOS 7部署Java+Mysql步骤
1.工具 putty0.7:用于远程控制服务器 winSCP5.13: ftp工具,用于向远程服务器传送文件 2.安装jdk: yum -y install java-1.8.0-openjdk ja ...
- 排序(N+1种)
from large to small 选择排序: 算法描述: 输入a[n] a[1]~a[n] a[2]~a[n] a[i]~a[n] 找最小的,与a[1]交换 找最小的,与a[2 ...
- socket 映射服务器程序
server #include <stdio.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket ...
- JavaScript条件语句-5--if语句的嵌套
JavaScript条件语句 学习目标 1.掌握length属性的应用 2.掌握if语句的嵌套 length 语法:string.length 功能:获取string字符串的长度 返回值:number ...
- 构建NDK交叉编译链
有时我们需要单独编译个c文件,生成一个ELF在Android上面跑,NDK提供了一个 make-standalone-toolchain.sh 脚本,用于生成一套特定平台的交叉编译工具链 使用方法如下 ...