Skip to content

Commit 84a81a7

Browse files
author
gabriel
committed
Basic shadow implement
1 parent e65f161 commit 84a81a7

6 files changed

Lines changed: 116 additions & 31 deletions

File tree

res/raw/core_native_view.frag

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
precision highp float;
2+
3+
uniform float alpha; // View.getAlpha()
4+
uniform vec2 size; // View.getWidth(), View.getHeight()
5+
6+
/** FOR SHADOW **/
7+
uniform vec4 shadowColor; // Shadow Color
8+
uniform float elevation; // View.getElevation()
9+
10+
11+
/** GENERAL **/
12+
uniform int mode;
13+
uniform sampler2D __texture__;
14+
varying vec2 xuv;
15+
16+
vec4 drawNormal() {
17+
vec4 pixel = texture2D(__texture__, xuv);
18+
return pixel;
19+
}
20+
21+
vec4 blur(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {
22+
vec2 center = vec2(0.5, 0.5);
23+
vec4 color = vec4(0.0);
24+
vec2 us = gl_FragCoord.xy / resolution;
25+
float px = (1.0 / size.x);
26+
float py = (1.0 / size.y);
27+
28+
int count = 1;
29+
for(float ix = -elevation; ix < elevation; ix++) {
30+
for(float iy = -elevation; iy < elevation; iy++) {
31+
color.a += texture2D(image, uv + vec2(ix*px, iy*py)).a;
32+
count++;
33+
}
34+
}
35+
36+
color.a /= float(count);
37+
38+
return color;
39+
}
40+
41+
vec4 drawShadow() {
42+
vec4 color = blur(__texture__, xuv, size, vec2(elevation));
43+
color.a *= shadowColor.a;
44+
color.rgb = shadowColor.rgb;
45+
46+
return color;
47+
}
48+
49+
void main(){
50+
vec4 color = vec4(1.0);
51+
if(mode == 0) {
52+
color = drawNormal();
53+
} else if(mode == 1) {
54+
color = drawShadow();
55+
}
56+
57+
color.a *= alpha;
58+
59+
if(color.a <= 0.001)
60+
discard;
61+
62+
gl_FragColor = color;
63+
}

res/raw/core_native_view.vert

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
attribute vec4 __position__;
2+
attribute vec2 __uv__;
3+
4+
varying vec2 xuv;
5+
6+
void main(){
7+
gl_Position = vec4(__position__.xy,0.0, 1.0);
8+
xuv = __uv__ ;
9+
}

src/br/nullexcept/mux/core/texel/GLProgramView.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
11
package br.nullexcept.mux.core.texel;
22

3+
import br.nullexcept.mux.utils.BufferUtils;
4+
35
class GLProgramView extends GLProgram {
46
private final String fragment;
57
private final String vertex;
68

79
GLProgramView(){
8-
fragment =
9-
"precision mediump float;\n" +
10-
"\n" +
11-
"uniform float alpha;\n" +
12-
"uniform float time;\n" +
13-
"uniform sampler2D __texture__;\n" +
14-
"varying vec2 xuv;\n" +
15-
"\n" +
16-
"void main(){\n" +
17-
" vec4 pixel = texture2D(__texture__, xuv);\n" +
18-
" pixel.a *= alpha;\n" +
19-
" if(pixel.a < 0.001) discard; " +
20-
" gl_FragColor = pixel;\n" +
21-
"}";
22-
vertex =
23-
"attribute vec4 __position__;\n" +
24-
"attribute vec2 __uv__;\n" +
25-
"\n" +
26-
"varying vec2 xuv;\n" +
27-
"\n" +
28-
"void main(){\n" +
29-
" gl_Position = vec4(__position__.xy,0.0, 1.0);\n" +
30-
" xuv = __uv__ ;\n" +
31-
"}";
10+
fragment = BufferUtils.utfFromStream(getClass().getResourceAsStream("/res/raw/core_native_view.frag"));
11+
vertex = BufferUtils.utfFromStream(getClass().getResourceAsStream("/res/raw/core_native_view.vert"));
3212
}
3313

3414
@Override

src/br/nullexcept/mux/core/texel/GLTexel.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package br.nullexcept.mux.core.texel;
22

33
import br.nullexcept.mux.graphics.Color;
4+
import br.nullexcept.mux.lang.Function;
5+
import br.nullexcept.mux.lang.Function2;
6+
import br.nullexcept.mux.view.View;
47
import org.lwjgl.BufferUtils;
58

69
import java.nio.FloatBuffer;
10+
import java.util.List;
711

812
import static br.nullexcept.mux.hardware.GLES.*;
913

@@ -52,9 +56,9 @@ public static void drawTexture(float x, float y, float width, float height, GLTe
5256
drawTexture(x, y, width, height, GLShaderList.TEXTURE, texture.getTexture());
5357
}
5458

55-
public static void drawViewLayers(float[][] vertices, int[] textures, float[] alphas){
59+
public static void drawViewLayers(float[][] vertices, int[] textures, View[] views){
5660
glEnable(GL_BLEND);
57-
glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
61+
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
5862
GLProgram program = GLShaderList.VIEW;
5963

6064
program.bind();
@@ -64,11 +68,15 @@ public static void drawViewLayers(float[][] vertices, int[] textures, float[] al
6468
int uTexture = program.uniform(GLProgram.UNIFORM_TEXTURE);
6569
int uAlpha = program.uniform("alpha");
6670
int uTime = program.uniform("time");
71+
int uMode = program.uniform("mode");
72+
int uSize = program.uniform("size");
73+
int uElevation = program.uniform("elevation");
74+
int uShadowColor = program.uniform("shadowColor");
6775

6876
glEnableVertexAttribArray(vPosition);
6977
glEnableVertexAttribArray(vTextureCoords);
7078

71-
for (int i = 0; i < vertices.length; i++) {
79+
Function2<Integer, Integer> draw = (i, mode) -> {
7280
bufferRect.put(vertices[i]);
7381
bufferRect.position(0);
7482

@@ -77,10 +85,27 @@ public static void drawViewLayers(float[][] vertices, int[] textures, float[] al
7785
glVertexAttribPointer(vTextureCoords, 2, GL_FLOAT, false, 0, bufferUV);
7886

7987
glUniform1i(uTexture, GL_TEXTURE_2D);
80-
glUniform1f(uAlpha, alphas[i]);
88+
glUniform1i(uMode, mode);
89+
glUniform1f(uElevation, views[i].getElevation());
90+
glUniform1f(uAlpha, views[i].getAlpha());
91+
glUniform4f(uShadowColor,
92+
Color.red(views[i].getShadowColor()) / 255.0f,
93+
Color.green(views[i].getShadowColor()) / 255.0f,
94+
Color.blue(views[i].getShadowColor()) / 255.0f,
95+
Color.alpha(views[i].getShadowColor()) / 255.0f
96+
);
97+
98+
glUniform2f(uSize, views[i].getWidth(), views[i].getHeight());
8199
glUniform1f(uTime, (System.currentTimeMillis() & 1000)/1000.0f);
82100
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
83101
glDisable(GL_DEPTH_TEST);
102+
};
103+
104+
for (int i = 0; i < views.length; i++) {
105+
if (views[i].getElevation() > 0.001f) {
106+
draw.run(i, 1); // Draw shadow
107+
}
108+
draw.run(i, 0);
84109
}
85110

86111
program.unbind();

src/br/nullexcept/mux/core/texel/ViewRenderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ public void drawInternal(CanvasTexel canvas, View view){
4040
}
4141
}
4242
float[][] borders = new float[drawables][2*4];
43-
float[] alphas = new float[drawables];
43+
View[] list = new View[drawables];
4444
int[] textures = new int[drawables];
4545
int index = 0;
4646
for (View child: children){
4747
if (child.isVisible()){
4848
CanvasTexel texel = registry.get(child.hashCode()).canvas;
4949
borders[index] = createMesh(canvas, child);
50-
alphas[index] = child.getAlpha();
50+
list[index] = child;
5151
textures[index] = texel.getFramebuffer().getTexture().getTexture();
5252
index++;
5353
}
5454
}
5555
canvas.getFramebuffer().bind();
56-
GLTexel.drawViewLayers(borders,textures, alphas);
56+
GLTexel.drawViewLayers(borders,textures, list);
5757
canvas.getFramebuffer().unbind();
5858
canvas.begin();
5959
}

src/br/nullexcept/mux/view/View.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,14 @@ public Object getTag() {
530530
return tag;
531531
}
532532

533+
public int getShadowColor() {
534+
return Color.argb(20,1,0,0);
535+
}
536+
537+
public float getElevation() {
538+
return 0.0f;
539+
}
540+
533541
public interface OnClickListener {
534542
void onClick(View view);
535543
}

0 commit comments

Comments
 (0)