forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotepad.txt
108 lines (80 loc) · 2.41 KB
/
notepad.txt
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
// My virtual exercise book for GPU programming
float3 cross(float3 a, float3 b)
{
return a.yzx * b.zxy - a.zxy * b.yzx;
}
float lerp (float x, float y, float t)
{
return x+t*(y-x);
}
float remap (float x, float a, float b, float c, float d)
//example parameters value,-1,1,0,1 -> remap value in range(-1,1) to range (0,1)
{
return (x-a)/(b-a)*(d-c) + c;
}
float sinh(float x)
{
return 0.5 * (exp(x)-exp(-x));
}
////////////////////////////////////////////////////////////////////////////
"if" statement can be replace by following formula:
if( x>y ) a=5.0; else a=10.0;
is equivalent to:
a=lerp(5.0,10.0,step(x,y));
another example:
if (uv.x>1.0) fragColor=vec4(0,0,0,0); else fragColor=vec4(1,0,0,0);
fragColor = vec4( mix (vec3(0.0,0.0,0.0),vec3(1.0,0.0,0.0),step(uv.x,1.0)),1.0);
////////////////////////////////////////////////////////////////////////////
Conversion between HLSL and GLSL:
HLSL fmod:
float3 fmod(float3 x, float3 y)
{
return x - y * trunc(x/y);
}
GLSL mod:
vec3 mod(vec3 x, vec3 y)
{
return x - y * floor(x/y);
}
////////////////////////////////////////////////////////////////////////////
How to fix error "Constructors only defined for numeric base types" in HLSL:
Struct declaration (both GLSL and HLSL):
struct LightColor
{
vec3 diffuse;
vec3 specular;
};
struct Material
{
LightColor color;
float shininess;
};
GLSL:
Material black = Material(LightColor(vec3(0.0), vec3(0.5)), 35.0);
HLSL:
Material black;
black.color.diffuse = float3(0.0, 0.0, 0.0);
black.color.specular = float3(0.5, 0.5, 0.5);
black.shininess = 35.0;
////////////////////////////////////////////////////////////////////////////
GLSL lessThan
(https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/lessThan.xhtml)
HLSL:
float4 lessThan (float4 x, float4 y)
{
return 1 - step(y, x);
}
////////////////////////////////////////////////////////////////////////////
GLSL texelFetch:
int px = int(fragCoord.x);
float x = texelFetch(iChannel0, ivec2(px, 0), 0).x;
HLSL:
Texture2D<float4> _BufferA;
float x = _BufferA.Load( int3(px, 0, 0) ).x;
////////////////////////////////////////////////////////////////////////////
Inverse smoothstep function:
float inverse_smoothstep( float x )
{
return 0.5 - sin(asin(1.0-2.0*x)/3.0);
}
////////////////////////////////////////////////////////////////////////////