Skip to content

Commit

Permalink
Reflect the sun light
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennFolker committed Mar 22, 2024
1 parent d242866 commit 5c6c26d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
Binary file modified assets/scenes/confictura/spires.bin
Binary file not shown.
14 changes: 7 additions & 7 deletions assets/scenes/confictura/spires.gltf
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
{
"bufferView":3,
"componentType":5126,
"count":38,
"count":37,
"max":[
1,
2,
Expand All @@ -103,7 +103,7 @@
{
"bufferView":4,
"componentType":5126,
"count":38,
"count":37,
"type":"VEC3"
},
{
Expand Down Expand Up @@ -134,26 +134,26 @@
},
{
"buffer":0,
"byteLength":456,
"byteLength":444,
"byteOffset":1860,
"target":34962
},
{
"buffer":0,
"byteLength":456,
"byteOffset":2316,
"byteLength":444,
"byteOffset":2304,
"target":34962
},
{
"buffer":0,
"byteLength":84,
"byteOffset":2772,
"byteOffset":2748,
"target":34963
}
],
"buffers":[
{
"byteLength":2856,
"byteLength":2832,
"uri":"spires.bin"
}
]
Expand Down
10 changes: 9 additions & 1 deletion assets/shaders/confictura/model-prop.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ attribute vec3 a_normal;
attribute vec4 a_color;

uniform mat4 u_proj;
uniform vec3 u_camPos;
uniform vec3 u_lightDir;
uniform vec4 u_reflectColor;

varying vec4 color;

void main(){
gl_Position = u_proj * vec4(a_position, 1.0);

vec3 diffuse = a_color.rgb * (dot(a_normal, -u_lightDir) * 0.5 + 0.5);
vec3 ref = reflect(normalize(u_lightDir), normalize(a_normal));
vec3 eye = normalize(u_camPos - a_position);

float factor = (dot(eye, ref) + 1.0) * 0.5;
float specular = pow(factor, 4.0) * (1.0 - a_color.a);

vec3 diffuse = u_reflectColor.rgb * u_reflectColor.a * specular + a_color.rgb * (0.15 + smoothstep(-1.0, 1.0, dot(a_normal, -u_lightDir)) * 0.85);
color = vec4(diffuse, 1.0);
}
4 changes: 2 additions & 2 deletions src/confictura/content/CBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void load(){
lightRadius = 48f;
}};

erodedSpire = new ModelProp("eroded-spire", erodedSlateWall, new Color(0x454858ff), () -> CModels.spireSmall1, () -> CModels.spireSmall2);
sharpSpire = new ModelProp("sharp-spire", sharpSlateWall, new Color(0x6e7080ff), () -> CModels.spireSmall1, () -> CModels.spireSmall2);
erodedSpire = new ModelProp("eroded-spire", erodedSlateWall, new Color(0x454858dd), () -> CModels.spireSmall1, () -> CModels.spireSmall2);
sharpSpire = new ModelProp("sharp-spire", sharpSlateWall, new Color(0x6e7080dd), () -> CModels.spireSmall1, () -> CModels.spireSmall2);
}
}
3 changes: 2 additions & 1 deletion src/confictura/graphics/g3d/ModelPropDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ protected void flush(){

shader.bind();
shader.camera = cam;
shader.lightDir.set(-1f, -1f, 1f).nor();
shader.lightDir.set(-1.2f, -0.8f, 0.9f).nor();
shader.reflectColor.set(!state.rules.lighting ? Color.white : state.rules.ambientLight);
shader.apply();

batch.setVertices(vertices, 0, vertexOffset * batch.vertexSize / Float.BYTES);
Expand Down
4 changes: 4 additions & 0 deletions src/confictura/graphics/shaders/ModelPropShader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package confictura.graphics.shaders;

import arc.graphics.*;
import arc.graphics.g3d.*;
import arc.graphics.gl.*;
import arc.math.geom.*;
Expand All @@ -9,6 +10,7 @@
public class ModelPropShader extends Shader{
public Camera3D camera;
public Vec3 lightDir = new Vec3();
public Color reflectColor = new Color();

public ModelPropShader(){
super(file("model-prop.vert"), file("model-prop.frag"));
Expand All @@ -17,6 +19,8 @@ public ModelPropShader(){
@Override
public void apply(){
setUniformMatrix4("u_proj", camera.combined.val);
setUniformf("u_camPos", camera.position);
setUniformf("u_lightDir", lightDir);
setUniformf("u_reflectColor", reflectColor);
}
}
15 changes: 3 additions & 12 deletions src/confictura/world/blocks/environment/ModelProp.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import confictura.world.blocks.*;
import mindustry.world.*;

import static arc.Core.*;
import static confictura.ConficturaMod.*;

public class ModelProp extends Block implements DelegateMapColor{
Expand All @@ -22,7 +21,8 @@ public ModelProp(String name, Block parent, Color color, Prov<Mesh>... meshes){
this.parent = parent;
this.color = color;
this.meshes = meshes;
destructible = breakable = false;
alwaysReplace = breakable = false;
solid = instantBuild = allowRectanglePlacement = true;
}

@Override
Expand All @@ -33,18 +33,9 @@ public Block substitute(){
@Override
public void drawBase(Tile tile){
parent.drawBase(tile);

float z = Draw.z();
Draw.z(ModelPropDrawer.accumLayer);

var reg = atlas.find("square-shadow");
float size = Math.max(reg.width, reg.height) * 1.75f * reg.scl();

Draw.color(0f, 0f, 0f, 0.65f);
Draw.rect(reg, tile.worldx(), tile.worldy(), size * Draw.xscl, size * Draw.yscl);
Draw.reset();

Draw.draw(Draw.z(), () -> modelPropDrawer.draw(
Draw.draw(ModelPropDrawer.accumLayer, () -> modelPropDrawer.draw(
meshes[Mathf.randomSeed(tile.pos(), 0, meshes.length - 1)].get(), tile.worldx(), tile.worldy(),
Mathf.randomSeed(tile.pos() + 1, 0, 4) * 90f, color
));
Expand Down

0 comments on commit 5c6c26d

Please sign in to comment.