-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdefaultshader.cpp
More file actions
115 lines (97 loc) · 3.11 KB
/
defaultshader.cpp
File metadata and controls
115 lines (97 loc) · 3.11 KB
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
#include <fea/rendering/defaultshader.hpp>
namespace fea
{
std::string DefaultShader::vertexSource = R"(
uniform mat4 projection;
uniform vec2 camZoom;
uniform vec2 camPosition;
uniform mat2 camRotation;
uniform vec2 halfViewSize;
attribute vec4 vertex;
attribute vec2 texCoords;
attribute vec4 colors;
uniform vec2 position;
uniform vec2 origin;
uniform float rotation;
uniform vec2 scaling;
uniform vec2 parallax;
varying vec2 vTex;
varying vec4 vColor;
void main()
{
float sinRot = sin(rotation);
float cosRot = cos(rotation);
mat2 rotMat = mat2(cosRot, -sinRot, sinRot, cosRot);
vec2 worldSpaceCoords = (rotMat * (scaling * (vertex.xy - origin))) + position;
vec2 transformedPoint = camRotation * (camZoom * (worldSpaceCoords - camPosition * parallax)) + halfViewSize;
gl_Position = projection* vec4(transformedPoint.xy, vertex.zw);
vTex = texCoords;
vColor = colors;
})";
#ifdef EMSCRIPTEN
std::string DefaultShader::fragmentSource = R"(
precision mediump float;
uniform sampler2D texture;
uniform vec4 constraints;
uniform vec3 color;
uniform float opacity;
uniform vec2 textureScroll; //hmmmm
varying vec2 vTex;
varying vec4 vColor;
float boundBetween(float val, float lowerBound, float upperBound)
{
if(lowerBound > upperBound)
{
float temp = lowerBound;
lowerBound = upperBound;
upperBound = temp;
}
val = val - lowerBound;
float rangeSize = upperBound - lowerBound;
if(rangeSize == 0.0)
{
return upperBound;
}
return val - (rangeSize * floor(val/rangeSize)) + lowerBound;
}
void main()
{
vec2 constraintSize = abs(vec2(constraints[1] - constraints[0] , constraints[3] - constraints[2]));
vec2 texCoords = constraintSize * vTex.st + vec2(constraints[0], constraints[2]) - textureScroll;
texCoords = vec2(boundBetween(texCoords.s, constraints[0], constraints[1]), boundBetween(texCoords.t, constraints[2], constraints[3]));
gl_FragColor = texture2D(texture, texCoords) * (vec4(color, opacity) * vColor);
})";
#else
std::string DefaultShader::fragmentSource = R"(
uniform sampler2D texture;
uniform vec4 constraints;
uniform vec3 color;
uniform float opacity;
uniform vec2 textureScroll; //hmmmm
varying vec2 vTex;
varying vec4 vColor;
float boundBetween(float val, float lowerBound, float upperBound)
{
if(lowerBound > upperBound)
{
float temp = lowerBound;
lowerBound = upperBound;
upperBound = temp;
}
val = val - lowerBound;
float rangeSize = upperBound - lowerBound;
if(rangeSize == 0.0)
{
return upperBound;
}
return val - (rangeSize * floor(val/rangeSize)) + lowerBound;
}
void main()
{
vec2 constraintSize = abs(vec2(constraints[1] - constraints[0] , constraints[3] - constraints[2]));
vec2 texCoords = constraintSize * vTex.st + vec2(constraints[0], constraints[2]) - textureScroll;
texCoords = vec2(boundBetween(texCoords.s, constraints[0], constraints[1]), boundBetween(texCoords.t, constraints[2], constraints[3]));
gl_FragColor = texture2D(texture, texCoords) * (vec4(color, opacity) * vColor);
})";
#endif
}