summaryrefslogtreecommitdiffstats
path: root/cwd/shaders/block.fs
blob: 15fd217752102082eb48153c743646f35365a995 (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#version 330 core

in VS_OUT {
    vec2 UvPosition;
    vec3 FragmentPosition;
    flat int Block;
    flat int State;
    vec4 ndcPos;
} fs_in;

uniform sampler2D textureAtlas;
uniform float time;
uniform int isInside;
uniform vec2 windowSize;

// TextureIndex: [most significant bit]<-...<-side[3bit]<-id[13]<-state[4]
layout(std140) uniform TextureIndexes { // binding point: 0
  int totalTextures;
  int indexes[1023];
};
layout(std140) uniform TextureData { // binding point: 1
  vec4 textureData[1024];
};

vec4 GetTextureByBlockId();
vec2 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords);
vec4 CheckIndexValidness();
vec4 GetDepthColor();
vec4 GetCheckerColor();
vec4 VTC(int value);

int GetBlockSide(){
    int side=6;
      if (fs_in.FragmentPosition.y==-0.5)
              side=0;
      else if (fs_in.FragmentPosition.y==0.5)
              side=1;
      else if (fs_in.FragmentPosition.x==-0.5)
          side = 3;
      else if (fs_in.FragmentPosition.x==0.5)
          side = 2;
      else if (fs_in.FragmentPosition.z==-0.5)
          side=4;
      else if (fs_in.FragmentPosition.z==0.5)
          side=5;
    return side;
}
int index,side,id,state;

vec3 rgb2hsv(vec3 c)
{
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

vec3 hsv2rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}


void main() {
  vec4 BlockTextureCoords = GetTextureByBlockId();
  vec2 AtlasCoords = TransformTextureCoord(BlockTextureCoords, fs_in.UvPosition);
  gl_FragColor = texture(textureAtlas, AtlasCoords);
  if (gl_FragColor.a<0.1)      discard;
  if (fs_in.Block==2 && side==1 || fs_in.Block==18 || fs_in.Block==31 && state==1 || fs_in.Block==31 && state==2) { //Grass and leaves colorizing
    const float BiomeColor = 0.275;
    vec3 hsvColor = rgb2hsv(gl_FragColor.xyz);
    hsvColor[0]+=BiomeColor;
    hsvColor[1]=0.63;
    hsvColor[2]+=0.1;
    gl_FragColor = vec4(hsv2rgb(hsvColor),1);
  }
}

vec4 GetTextureByBlockId() {
    int BlockSide = GetBlockSide();
    for (int i = 0; i < totalTextures; i++) {
        index = indexes[i];
        side = (index & 0x70000) >> 16;
        id = (index & 0xFF0) >> 4;
        state = index & 0xF;

        if (id != fs_in.Block)
            continue;
        if (state != fs_in.State)
            continue;
        if (side == 6)
            return textureData[i];
        if (BlockSide == side)
            return textureData[i];
        if (side == 6)
            return textureData[i];
        else if (side == BlockSide)
            return textureData[i];
    }
  // Fallback TNT texture
  return vec4(0.0546875, 0.00442477876106194690, 0.0078125,
              0.00442477876106194690);
}

vec2 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords) {
  float x = TextureAtlasCoords.x;
  float y = TextureAtlasCoords.y;
  float w = TextureAtlasCoords.z;
  float h = TextureAtlasCoords.w;
  vec2 A = vec2(x, y);
  vec2 B = vec2(x + w, y + h);

  const bool isTextureFlippedVertically = true;
  if (isTextureFlippedVertically) {
    y = 1 - y;
    A = vec2(x, y - h);
    B = vec2(x + w, y);
  }
  return A + UvCoords * (B - A);
}

vec4 CheckIndexValidness() {
  vec4 color = vec4(0, 1, 0, 1);
  if (totalTextures != 6)
    return vec4(1, 0, 0, 1);
  if (indexes[0] != 393248)
    return vec4(1, 1, 0, 1);
  for (int i = 1; i < 20; i++)
    if (indexes[i] != 0)
      return vec4(0, 0, 1, 1);
  return vec4(0, 1, 0, 1);
}

float near = 1.0;
float far = 100.0;

float LinearizeDepth(float depth) {
  float z = depth * 2.0 - 1.0; // Back to NDC
  return (2.0 * near * far) / (far + near - z * (far - near));
}

vec4 GetDepthColor() {
  float depth =
      LinearizeDepth(gl_FragCoord.z) / far; // divide by far for demonstration
  return vec4(vec3(depth), 1.0f);
}

vec4 GetCheckerColor() {
    if (fs_in.UvPosition.x>0.5 && fs_in.UvPosition.y<0.5 || fs_in.UvPosition.x<0.5 && fs_in.UvPosition.y>0.5)
        return vec4(0.7,0.7,0,1);
    else
        return vec4(0,0,0,1);
}

vec4 VTC(int value){
    switch(value)
    {
        case 0:
            return vec4(0,0,0,1);
        case 1:
            return vec4(1,0,0,1);
        case 2:
            return vec4(0,1,0,1);
        case 3:
            return vec4(0,0,1,1);
        case 4:
            return vec4(1,1,0,1);
        case 5:
            return vec4(1,0,1,1);
        case 6:
            return vec4(0,1,1,1);
    }
    return vec4(1,1,1,1);
}