Water shader

This commit is contained in:
Anuken 2019-03-09 22:53:16 -05:00
parent 1316c9235a
commit 98212df076
10 changed files with 158 additions and 33 deletions

View File

@ -370,6 +370,7 @@ category.items = Items
category.crafting = Crafting
category.shooting = Shooting
category.optional = Optional Enhancements
setting.animatedwater.name = Animated Water
setting.indicators.name = Ally Indicators
setting.autotarget.name = Auto-Target
setting.fpscap.name = Max FPS

View File

@ -1,19 +0,0 @@
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform sampler2D u_texture;
uniform vec4 u_color;
varying vec4 v_color;
varying vec2 v_texCoord;
void main() {
vec4 c = texture2D(u_texture, v_texCoord.xy);
c = mix(c, vec4(u_color.rgb, c.a), v_color.a);
gl_FragColor = c * vec4(v_color.rgb, 1.0);
}

View File

@ -0,0 +1,77 @@
#ifdef GL_ES
precision highp float;
precision mediump int;
#endif
uniform sampler2D u_texture;
uniform vec2 camerapos;
uniform vec2 screensize;
uniform float time;
varying vec4 v_color;
varying vec2 v_texCoord;
vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
float snoise(vec2 v){
const vec4 C = vec4(0.211324865405187, 0.366025403784439,
-0.577350269189626, 0.024390243902439);
vec2 i = floor(v + dot(v, C.yy) );
vec2 x0 = v - i + dot(i, C.xx);
vec2 i1;
i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
vec4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
i = mod(i, 289.0);
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy),
dot(x12.zw,x12.zw)), 0.0);
m = m*m ;
m = m*m ;
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
vec3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
void main() {
vec2 c = v_texCoord.xy;
vec4 color = texture2D(u_texture, c) * v_color;
vec2 v = vec2(1.0/screensize.x, 1.0/screensize.y);
vec2 coords = vec2(c.x / v.x + camerapos.x, c.y / v.y + camerapos.y);
float stime = time / 5.0;
float mscl = 30.0;
float mth = 5.0;
color = texture2D(u_texture, c + vec2(sin(stime/3.0 + coords.y/0.75) * v.x, 0.0)) * vec4(0.9, 0.9, 1, 1.0);
color.a = 1.0;
float n1 = snoise(coords / 40.0 + vec2(time) / 200.0);
float n2 = snoise((coords + vec2(632.0)) / 25.0 + vec2(0.0, -time) / 190.0);
float r = (n1 + n2) * 3.0;
if(mod(float(int(coords.x + coords.y*1.1 + sin(stime / 8.0 + coords.x/5.0 - coords.y/100.0)*2.0)) +
sin(stime / 20.0 + coords.y/3.0) * 1.0 +
sin(stime / 10.0 + coords.y/2.0) * 2.0 +
sin(stime / 7.0 + coords.y/1.0) * 0.5 +
sin(coords.x + coords.y) +
sin(stime / 20.0 + coords.x/4.0) * 1.0, mscl) + r < mth){
color *= 1.2;
color.a = 1.0;
}
gl_FragColor = color;
}

View File

@ -44,7 +44,7 @@ public class Renderer implements ApplicationListener{
public final MinimapRenderer minimap = new MinimapRenderer();
public final OverlayRenderer overlays = new OverlayRenderer();
private FrameBuffer shieldBuffer = new FrameBuffer(2, 2);
public FrameBuffer shieldBuffer = new FrameBuffer(2, 2);
private Color clearColor;
private float targetscale = io.anuke.arc.scene.ui.layout.Unit.dp.scl(4);
private float camerascale = targetscale;
@ -159,6 +159,10 @@ public class Renderer implements ApplicationListener{
graphics.clear(clearColor);
if(graphics.getWidth() >= 2 && graphics.getHeight() >= 2 && (shieldBuffer.getWidth() != graphics.getWidth() || shieldBuffer.getHeight() != graphics.getHeight())){
shieldBuffer.resize(graphics.getWidth(), graphics.getHeight());
}
Draw.proj(camera.projection());
blocks.floor.drawFloor();
@ -200,10 +204,6 @@ public class Renderer implements ApplicationListener{
drawAndInterpolate(playerGroup, p -> true, Player::drawBuildRequests);
if(EntityDraw.countInBounds(shieldGroup) > 0){
if(graphics.getWidth() >= 2 && graphics.getHeight() >= 2 && (shieldBuffer.getWidth() != graphics.getWidth() || shieldBuffer.getHeight() != graphics.getHeight())){
shieldBuffer.resize(graphics.getWidth(), graphics.getHeight());
}
Draw.flush();
shieldBuffer.begin();
graphics.clear(Color.CLEAR);

View File

@ -1,7 +1,38 @@
package io.anuke.mindustry.graphics;
import io.anuke.arc.Core;
import io.anuke.arc.graphics.Color;
import io.anuke.arc.graphics.g2d.Draw;
import static io.anuke.arc.Core.camera;
import static io.anuke.mindustry.Vars.renderer;
public enum CacheLayer{
water,
water{
@Override
public void begin(){
if(!Core.settings.getBool("animatedwater")) return;
renderer.blocks.floor.endc();
renderer.shieldBuffer.begin();
Core.graphics.clear(Color.CLEAR);
renderer.blocks.floor.beginc();
}
@Override
public void end(){
if(!Core.settings.getBool("animatedwater")) return;
renderer.blocks.floor.endc();
renderer.shieldBuffer.end();
Draw.shader(Shaders.water);
Draw.rect(Draw.wrap(renderer.shieldBuffer.getTexture()), camera.position.x, camera.position.y, camera.width, -camera.height);
Draw.shader();
renderer.blocks.floor.beginc();
}
},
lava,
oil,
space,

View File

@ -95,6 +95,14 @@ public class FloorRenderer{
endDraw();
}
public void beginc(){
cbatch.beginDraw();
}
public void endc(){
cbatch.endDraw();
}
public void beginDraw(){
if(cache == null){
return;
@ -124,8 +132,6 @@ public class FloorRenderer{
int crangex = (int) (camera.width / (chunksize * tilesize)) + 1;
int crangey = (int) (camera.height / (chunksize * tilesize)) + 1;
SpriteBatch batch = Core.batch;
Core.batch = cbatch;
layer.begin();
for(int x = -crangex; x <= crangex; x++){
@ -144,7 +150,6 @@ public class FloorRenderer{
}
layer.end();
Core.batch = batch;
}
private void cacheChunk(int cx, int cy){
@ -191,6 +196,8 @@ public class FloorRenderer{
tile.block().draw(tile);
}else if(floor.cacheLayer == layer && (world.isAccessible(tile.x,tile.y) || tile.block().cacheLayer != CacheLayer.walls || !tile.block().fillsTile)){
floor.draw(tile);
}else if(floor.cacheLayer.ordinal() < layer.ordinal() && layer != CacheLayer.walls){
floor.drawNonLayer(tile);
}
}
}

View File

@ -14,6 +14,7 @@ public class Shaders{
public static UnitBuild build;
public static FogShader fog;
public static MenuShader menu;
public static SurfaceShader water;
public static void init(){
shadow = new Shadow();
@ -22,6 +23,7 @@ public class Shaders{
build = new UnitBuild();
fog = new FogShader();
menu = new MenuShader();
water = new SurfaceShader("water");
}
public static class MenuShader extends LoadShader{
@ -124,6 +126,20 @@ public class Shaders{
}
}
public static class SurfaceShader extends LoadShader{
public SurfaceShader(String frag){
super(frag, "default");
}
@Override
public void apply(){
setUniformf("camerapos", Core.camera.position.x - Core.camera.width / 2, Core.camera.position.y - Core.camera.height / 2);
setUniformf("screensize", Core.camera.width, Core.camera.height);
setUniformf("time", Time.time());
}
}
public static class LoadShader extends Shader{
public LoadShader(String frag, String vert){
super(Core.files.internal("shaders/" + vert + ".vertex"), Core.files.internal("shaders/" + frag + ".fragment"));

View File

@ -195,6 +195,7 @@ public class SettingsMenuDialog extends SettingsDialog{
graphics.checkPref("fps", false);
graphics.checkPref("indicators", true);
graphics.checkPref("animatedwater", !mobile);
graphics.checkPref("lasers", true);
}

View File

@ -114,13 +114,24 @@ public class Floor extends Block{
drawEdges(tile);
}
public void drawNonLayer(Tile tile){
Mathf.random.setSeed(tile.pos());
drawEdges(tile, true);
}
protected void drawEdges(Tile tile){
drawEdges(tile, false);
}
protected void drawEdges(Tile tile, boolean sameLayer){
eq = 0;
for(int i = 0; i < 8; i++){
Point2 point = Geometry.d8[i];
Tile other = tile.getNearby(point);
if(other != null && doEdge(other.floor()) && other.floor().edges() != null){
if(other != null && doEdge(other.floor(), sameLayer) && other.floor().edges() != null){
eq |= (1 << i);
}
}
@ -140,8 +151,8 @@ public class Floor extends Block{
return ((Floor)blendGroup).edges;
}
protected boolean doEdge(Floor other){
return (other.blendGroup.id > blendGroup.id || edges() == null) && other.edgeOnto(this);
protected boolean doEdge(Floor other, boolean sameLayer){
return (other.blendGroup.id > blendGroup.id || edges() == null) && other.edgeOnto(this) && (other.cacheLayer.ordinal() > this.cacheLayer.ordinal() || !sameLayer);
}
protected boolean edgeOnto(Floor other){

View File

@ -43,8 +43,8 @@ public class OreBlock extends Floor{
}
@Override
public boolean doEdge(Floor floor){
return floor != base && super.doEdge(floor);
public boolean doEdge(Floor floor, boolean f){
return floor != base && super.doEdge(floor, f);
}
@Override