原文出處:http://gad.qq.com/program/translateview/7173930
這是關於渲染基礎的系列教程的第二部分。這個渲染基礎的系列教程的第一部分是有關矩陣的內容。在這篇文章中我們將編寫我們的第一個着色器代碼並導入紋理。
1
2
3
|
Shader "Custom/My First Shader" {
}
|
1
2
3
4
5
6
|
Shader "Custom/My First Shader" {
SubShader {
}
}
|
1
2
3
4
5
6
7
8
9
|
Shader "Custom/My First Shader" {
SubShader {
Pass {
}
}
}
|
1
2
3
4
5
|
Pass {
CGPROGRAM
ENDCG
}
|
1
2
3
4
5
6
|
CGPROGRAM
#pragma vertex MyVertexProgram
#pragma fragment MyFragmentProgram
ENDCG
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
CGPROGRAM
#pragma vertex MyVertexProgram
#pragma fragment MyFragmentProgram
void MyVertexProgram () {
}
void MyFragmentProgram () {
}
ENDCG
|
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
|
// Compiled shader for custom platforms, uncompressed size: 0.5KB
// Skipping shader variants that would not be included into build of current scene.
Shader "Custom/My First Shader" {
SubShader {
Pass {
GpuProgramID 16807
Program "vp" {
SubProgram "glcore " {
"#ifdef VERTEX
#version 150
#extension GL_ARB_explicit_attrib_location : require
#extension GL_ARB_shader_bit_encoding : enable
void main()
{
return ;
}
#endif
#ifdef FRAGMENT
#version 150
#extension GL_ARB_explicit_attrib_location : require
#extension GL_ARB_shader_bit_encoding : enable
void main()
{
return ;
}
#endif
"
}
}
Program "fp" {
SubProgram "glcore " {
"// shader disassembly not supported on glcore"
}
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#ifdef VERTEX
void main()
{
return ;
}
#endif
#ifdef FRAGMENT
void main()
{
return ;
}
#endif
|
1
2
3
4
5
6
7
8
9
10
11
12
|
Program "vp" {
SubProgram "d3d11 " {
vs_4_0
0: ret
}
}
Program "fp" {
SubProgram "d3d11 " {
ps_4_0
0: ret
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
CGPROGRAM
#pragma vertex MyVertexProgram
#pragma fragment MyFragmentProgram
#include "UnityCG.cginc"
void MyVertexProgram () {
}
void MyFragmentProgram () {
}
ENDCG
|
1
2
3
|
float4 MyVertexProgram () {
return 0;
}
|
1
2
3
|
float4 MyVertexProgram () : SV_POSITION {
return 0;
}
|
1
2
3
|
float4 MyFragmentProgram () {
return 0;
}
|
1
2
3
|
float4 MyFragmentProgram () : SV_TARGET {
return 0;
}
|
1
2
3
|
float4 MyFragmentProgram (float4 position) : SV_TARGET {
return 0;
}
|
1
2
3
4
5
|
float4 MyFragmentProgram (
float4 position : SV_POSITION
) : SV_TARGET {
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#ifdef VERTEX
void main()
{
gl_Position = vec4(0.0, 0.0, 0.0, 0.0);
return ;
}
#endif
#ifdef FRAGMENT
layout(location = 0) out vec4 SV_TARGET0;
void main()
{
SV_TARGET0 = vec4(0.0, 0.0, 0.0, 0.0);
return ;
}
#endif
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Program "vp" {
SubProgram "d3d11 " {
vs_4_0
dcl_output_siv o0.xyzw, position
0: mov o0.xyzw, l(0,0,0,0)
1: ret
}
}
Program "fp" {
SubProgram "d3d11 " {
ps_4_0
dcl_output o0.xyzw
0: mov o0.xyzw, l(0,0,0,0)
1: ret
}
}
|
1
2
3
|
float4 MyVertexProgram (float4 position : POSITION) : SV_POSITION {
return 0;
}
|
1
2
3
|
float4 MyVertexProgram (float4 position : POSITION) : SV_POSITION {
return position;
}
|
1
2
3
4
5
6
|
in vec4 in_POSITION0;
void main()
{
gl_Position = in_POSITION0;
return ;
}
|
1
2
3
4
5
6
|
Bind "vertex" Vertex
vs_4_0
dcl_input v0.xyzw
dcl_output_siv o0.xyzw, position
0: mov o0.xyzw, v0.xyzw
1: ret
|
1
2
3
|
float4 MyVertexProgram (float4 position : POSITION) : SV_POSITION {
return mul(UNITY_MATRIX_MVP, position);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
uniform vec4 _Time;
uniform vec4 _SinTime;
uniform vec4 _CosTime;
uniform vec4 unity_DeltaTime;
uniform vec3 _WorldSpaceCameraPos;
…
in vec4 in_POSITION0;
vec4 t0;
void main()
{
t0 = in_POSITION0.yyyy * glstate_matrix_mvp[1];
t0 = glstate_matrix_mvp[0] * in_POSITION0.xxxx + t0;
t0 = glstate_matrix_mvp[2] * in_POSITION0.zzzz + t0;
gl_Position = glstate_matrix_mvp[3] * in_POSITION0.wwww + t0;
return ;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Bind "vertex" Vertex
ConstBuffer "UnityPerDraw" 352
Matrix 0 [glstate_matrix_mvp]
BindCB "UnityPerDraw" 0
vs_4_0
dcl_constantbuffer cb0[4], immediateIndexed
dcl_input v0.xyzw
dcl_output_siv o0.xyzw, position
dcl_temps 1
0: mul r0.xyzw, v0.yyyy, cb0[1].xyzw
1: mad r0.xyzw, cb0[0].xyzw, v0.xxxx, r0.xyzw
2: mad r0.xyzw, cb0[2].xyzw, v0.zzzz, r0.xyzw
3: mad o0.xyzw, cb0[3].xyzw, v0.wwww, r0.xyzw
4: ret
|
1
2
3
4
5
|
float4 MyFragmentProgram (
float4 position : SV_POSITION
) : SV_TARGET {
return float4(1, 1, 0, 1);
}
|
1
2
3
4
5
6
7
8
9
|
Shader "Custom/My First Shader" {
Properties {
}
SubShader {
…
}
}
|
1
2
3
|
Properties {
_Tint
}
|
1
2
3
|
Properties {
_Tint ( "Tint" , Color)
}
|
1
2
3
|
Properties {
_Tint ( "Tint" , Color) = (1, 1, 1, 1)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include "UnityCG.cginc"
float4 _Tint;
float4 MyVertexProgram (float4 position : POSITION) : SV_POSITION {
return mul(UNITY_MATRIX_MVP, position);
}
float4 MyFragmentProgram (
float4 position : SV_POSITION
) : SV_TARGET {
return _Tint;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
uniform vec4 _Time;
uniform vec4 _SinTime;
uniform vec4 _CosTime;
uniform vec4 unity_DeltaTime;
uniform vec3 _WorldSpaceCameraPos;
…
uniform vec4 _Tint;
layout(location = 0) out vec4 SV_TARGET0;
void main()
{
SV_TARGET0 = _Tint;
return ;
}
|
1
2
3
4
5
6
7
8
|
ConstBuffer "$Globals" 112
Vector 96 [_Tint]
BindCB "$Globals" 0
ps_4_0
dcl_constantbuffer cb0[7], immediateIndexed
dcl_output o0.xyzw
0: mov o0.xyzw, cb0[6].xyzw
1: ret
|
1
2
3
4
5
6
|
float4 MyFragmentProgram (
float4 position : SV_POSITION,
float3 localPosition
) : SV_TARGET {
return float4(localPosition, 1);
}
|
1
2
3
4
5
6
|
float4 MyFragmentProgram (
float4 position : SV_POSITION,
float3 localPosition : TEXCOORD0
) : SV_TARGET {
return float4(localPosition, 1);
}
|
1
2
3
4
5
6
7
8
|
in vec3 vs_TEXCOORD0;
layout(location = 0) out vec4 SV_TARGET0;
void main()
{
SV_TARGET0.xyz = vs_TEXCOORD0.xyz;
SV_TARGET0.w = 1.0;
return ;
}
|
1
2
3
4
5
6
|
ps_4_0
dcl_input_ps linear v0.xyz
dcl_output o0.xyzw
0: mov o0.xyz, v0.xyzx
1: mov o0.w, l(1.000000)
2: ret
|
1
2
3
4
5
6
|
float4 MyVertexProgram (
float4 position : POSITION,
out float3 localPosition : TEXCOORD0
) : SV_POSITION {
return mul(UNITY_MATRIX_MVP, position);
}
|
1
2
3
4
5
6
7
|
float4 MyVertexProgram (
float4 position : POSITION,
out float3 localPosition : TEXCOORD0
) : SV_POSITION {
localPosition = position.xyz;
return mul(UNITY_MATRIX_MVP, position);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
in vec4 in_POSITION0;
out vec3 vs_TEXCOORD0;
vec4 t0;
void main()
{
t0 = in_POSITION0.yyyy * glstate_matrix_mvp[1];
t0 = glstate_matrix_mvp[0] * in_POSITION0.xxxx + t0;
t0 = glstate_matrix_mvp[2] * in_POSITION0.zzzz + t0;
gl_Position = glstate_matrix_mvp[3] * in_POSITION0.wwww + t0;
vs_TEXCOORD0.xyz = in_POSITION0.xyz;
return ;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Bind "vertex" Vertex
ConstBuffer "UnityPerDraw" 352
Matrix 0 [glstate_matrix_mvp]
BindCB "UnityPerDraw" 0
vs_4_0
dcl_constantbuffer cb0[4], immediateIndexed
dcl_input v0.xyzw
dcl_output_siv o0.xyzw, position
dcl_output o1.xyz
dcl_temps 1
0: mul r0.xyzw, v0.yyyy, cb0[1].xyzw
1: mad r0.xyzw, cb0[0].xyzw, v0.xxxx, r0.xyzw
2: mad r0.xyzw, cb0[2].xyzw, v0.zzzz, r0.xyzw
3: mad o0.xyzw, cb0[3].xyzw, v0.wwww, r0.xyzw
4: mov o1.xyz, v0.xyzx
5: ret
|
1
2
3
4
|
struct Interpolators {
float4 position : SV_POSITION;
float3 localPosition : TEXCOORD0;
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
float4 _Tint;
struct Interpolators {
float4 position : SV_POSITION;
float3 localPosition : TEXCOORD0;
};
Interpolators MyVertexProgram (float4 position : POSITION) {
Interpolators i;
i.localPosition = position.xyz;
i.position = mul(UNITY_MATRIX_MVP, position);
return i;
}
float4 MyFragmentProgram (Interpolators i) : SV_TARGET {
return float4(i.localPosition, 1);
}
|