Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Water Shader into Uber Shader #108

Merged
merged 4 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mbrlabs.mundus.commons.terrain.attributes;
package com.mbrlabs.mundus.commons;

import com.badlogic.gdx.utils.Array;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.badlogic.gdx.utils.PropertiesUtils;
import com.mbrlabs.mundus.commons.assets.meta.Meta;
import com.mbrlabs.mundus.commons.water.Water;
import com.mbrlabs.mundus.commons.water.WaterFloatAttribute;
import com.mbrlabs.mundus.commons.water.attributes.WaterFloatAttribute;

import java.io.IOException;
import java.io.Reader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.mbrlabs.mundus.commons.assets.Asset;
import com.mbrlabs.mundus.commons.assets.WaterAsset;
import com.mbrlabs.mundus.commons.scene3d.GameObject;
import com.mbrlabs.mundus.commons.water.WaterFloatAttribute;
import com.mbrlabs.mundus.commons.water.attributes.WaterFloatAttribute;

public class WaterComponent extends CullableComponent implements AssetUsage {

Expand Down Expand Up @@ -56,17 +56,14 @@ public void setShader(Shader shader) {

@Override
public boolean usesAsset(Asset assetToCheck) {
if (assetToCheck == waterAsset)
return true;

return false;
return assetToCheck == waterAsset;
}

@Override
public void render(float delta) {
super.render(delta);
if (isCulled) return;
gameObject.sceneGraph.scene.batch.render(waterAsset.water, gameObject.sceneGraph.scene.environment, shader);
gameObject.sceneGraph.scene.batch.render(waterAsset.water, gameObject.sceneGraph.scene.environment);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.Shader;
import com.mbrlabs.mundus.commons.terrain.attributes.TerrainTextureAttribute;
import com.mbrlabs.mundus.commons.water.attributes.WaterMaterialAttribute;
import net.mgsx.gltf.scene3d.shaders.PBRShader;
import net.mgsx.gltf.scene3d.shaders.PBRShaderConfig;
import net.mgsx.gltf.scene3d.shaders.PBRShaderProvider;
Expand All @@ -22,6 +23,8 @@ public MundusPBRShaderProvider(PBRShaderConfig config) {
protected Shader createShader(Renderable renderable) {
if (renderable.material.has(TerrainTextureAttribute.ATTRIBUTE_SPLAT0))
return createTerrainShader(renderable);
else if (renderable.material.has(WaterMaterialAttribute.WaterMaterial))
return createWaterShader(renderable);

return super.createShader(renderable);
}
Expand All @@ -37,4 +40,11 @@ protected Shader createTerrainShader(Renderable renderable) {
Gdx.app.log(MundusPBRShader.class.getSimpleName(), "Terrain Shader Compiled");
return shader;
}

private Shader createWaterShader(Renderable renderable) {
Shader shader = new WaterUberShader(renderable);
shaders.add(shader);
Gdx.app.log(MundusPBRShader.class.getSimpleName(), "Water Shader Compiled");
return shader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public TerrainUberShader(Renderable renderable, DefaultShader.Config config) {

TerrainTextureAttribute textureAttribute = renderable.material.get(TerrainTextureAttribute.class, TerrainTextureAttribute.ATTRIBUTE_SPLAT0);

attributesMask = combineAttributeMasks(renderable);
attributesMask = ShaderUtils.combineAttributeMasks(renderable);
terrainTextureMask = textureAttribute.terrainTexture.getMask();

String prefix = createPrefixForRenderable(renderable);
Expand Down Expand Up @@ -299,18 +299,12 @@ public int compareTo(Shader other) {

@Override
public boolean canRender(Renderable instance) {
if (combineAttributeMasks(instance) != attributesMask) {
if (ShaderUtils.combineAttributeMasks(instance) != attributesMask) {
return false;
}

TerrainTextureAttribute terrainTextureAttribute = (TerrainTextureAttribute) instance.material.get(TerrainTextureAttribute.ATTRIBUTE_SPLAT0);
return terrainTextureMask == terrainTextureAttribute.terrainTexture.getMask();
}

private static long combineAttributeMasks (final Renderable renderable) {
long mask = 0;
if (renderable.environment != null) mask |= renderable.environment.getMask();
if (renderable.material != null) mask |= renderable.material.getMask();
return mask;
}
}
186 changes: 0 additions & 186 deletions commons/src/main/com/mbrlabs/mundus/commons/shaders/WaterShader.java

This file was deleted.

Loading