Skip to content

Commit

Permalink
Merge pull request #4 from yaRnMcDonuts/main
Browse files Browse the repository at this point in the history
BlendLayers Shader Feature Updates
  • Loading branch information
capdevon committed Mar 12, 2024
2 parents 4c1a3f6 + 9e97168 commit c277de0
Show file tree
Hide file tree
Showing 27 changed files with 2,625 additions and 158 deletions.
545 changes: 545 additions & 0 deletions src/main/java/com/github/shader/demo/Test_CharacterEffects3.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.shader.demo.states;

import com.github.shader.demo.utils.BlendLayerEffect;
import com.github.shader.demo.utils.blendlayers.ShaderBlendLayer;
import com.github.tools.SpinnerIntegerModel;
import com.github.tools.editor.ReflectedEditorBuilder;
import com.github.tools.material.MatPropertyPanelBuilder;
Expand Down Expand Up @@ -61,35 +61,35 @@ public void visit(Geometry geom) {
private void initEffects() {

String dirName = "Models/Cracked_Ice/DefaultMaterial_";
BlendLayerEffect ice = new BlendLayerEffect("Freeze", 0, model);
ShaderBlendLayer ice = new ShaderBlendLayer("Freeze", 0, model);
ice.setBaseColorMap(assetManager.loadTexture(dirName + "baseColor.png"));
ice.setNormalMap(assetManager.loadTexture(dirName + "normal.png"));
ice.setMetallicRoughnessAoMap(assetManager.loadTexture(dirName + "occlusionRoughnessMetallic.png"));

dirName = "Models/Cracked_Stone/DefaultMaterial_";
BlendLayerEffect stone = new BlendLayerEffect("Petrify", 1, model);
ShaderBlendLayer stone = new ShaderBlendLayer("Petrify", 1, model);
stone.setBaseColorMap(assetManager.loadTexture(dirName + "baseColor.png"));
stone.setNormalMap(assetManager.loadTexture(dirName + "normal.png"));
stone.setMetallicRoughnessAoMap(assetManager.loadTexture(dirName + "occlusionRoughnessMetallic.png"));

dirName = "Models/Shield_Armor/DefaultMaterial_";
BlendLayerEffect shield = new BlendLayerEffect("Shield", 2, model);
ShaderBlendLayer shield = new ShaderBlendLayer("Shield", 2, model);
shield.setBaseColorMap(assetManager.loadTexture(dirName + "baseColor.png"));
shield.setNormalMap(assetManager.loadTexture(dirName + "normal.png"));
shield.setMetallicRoughnessAoMap(assetManager.loadTexture(dirName + "occlusionRoughnessMetallic.png"));
shield.setEmissiveMap(assetManager.loadTexture(dirName + "emissive.png"));
shield.setBlendAlpha(true);

BlendLayerEffect dissolve = new BlendLayerEffect("Dissolve", 3, model);
ShaderBlendLayer dissolve = new ShaderBlendLayer("Dissolve", 3, model);
dissolve.setBaseColor(new ColorRGBA(1.0f, 0.0f, 1.0f, 0.0f));

registerEffects(ice, stone, shield, dissolve);
}

private void registerEffects(BlendLayerEffect... effects) {
private void registerEffects(ShaderBlendLayer... effects) {
Container container = new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.None, FillMode.Even));

for (BlendLayerEffect effect : effects) {
for (ShaderBlendLayer effect : effects) {
ConfigurationBuilder config = new ConfigurationBuilder();
//config.addConstraint("linearBlendVal", new SpinnerFloatModel(0f, 1f, 0.1f));
config.addConstraint("debugMode", new SpinnerIntegerModel(-1, 5, 1));
Expand Down
64 changes: 62 additions & 2 deletions src/main/java/com/github/shader/demo/states/DefaultSceneState.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
import com.jme3.scene.shape.Box;
import com.jme3.shadow.DirectionalLightShadowFilter;
import com.jme3.shadow.EdgeFilteringMode;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.texture.Image;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.texture.TextureArray;
import com.jme3.util.SkyFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jme3utilities.sky.SkyControl;
import jme3utilities.sky.StarsOption;
Expand All @@ -37,7 +43,7 @@ public class DefaultSceneState extends MySimpleState {
protected void initialize(Application app) {
super.initialize(app);

initFloor();
initTerrainFloor();
initFilters();

if (dynamicSky) {
Expand Down Expand Up @@ -93,7 +99,61 @@ private void initFilters() {
viewPort.addProcessor(fpp);
}

private void initFloor() {
private void initTerrainFloor() {
Material pbr = new Material(assetManager, "Common/MatDefs/Terrain/AdvancedPBRTerrain.j3md");


Texture albedoMap = assetManager.loadTexture("Models/SwampyGrass/DefaultMaterial_baseColor.png");
Texture normalMap = assetManager.loadTexture("Models/SwampyGrass/DefaultMaterial_normal.png");
Texture metallicRoughnessAoEiMap = assetManager.loadTexture("Models/SwampyGrass/DefaultMaterial_occlusionRoughnessMetallic.png");

TerrainQuad terrain = new TerrainQuad("Terrain", 5, 9, null);

pbr.setFloat("AlbedoMap_0_scale", 1.85f);

pbr.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alpha1.png"));


TextureArray albedoTextureArray = makeSingleImageTextureArray(albedoMap);
TextureArray normalTextureArray = makeSingleImageTextureArray(normalMap);
TextureArray metallicRoughnessTextureArray = makeSingleImageTextureArray(metallicRoughnessAoEiMap);

((Texture)albedoTextureArray).setMagFilter(Texture.MagFilter.Bilinear);
((Texture)normalTextureArray).setMagFilter(Texture.MagFilter.Bilinear);
((Texture)metallicRoughnessTextureArray).setMagFilter(Texture.MagFilter.Bilinear);

((Texture)metallicRoughnessTextureArray).setMinFilter(Texture.MinFilter.Trilinear);
((Texture)normalTextureArray).setMinFilter(Texture.MinFilter.Trilinear);
((Texture)metallicRoughnessTextureArray).setMinFilter(Texture.MinFilter.Trilinear);

pbr.setInt("AlbedoMap_0", 0);
pbr.setInt("NormalMap_0", 0);
pbr.setInt("MetallicRoughnessMap_0", 0);

pbr.setFloat("Roughness_0", 1.0f);
pbr.setFloat("Metallic_0", 1.0f);

pbr.setTexture("AlbedoTextureArray", albedoTextureArray);
pbr.setTexture("NormalParallaxTextureArray", normalTextureArray);
pbr.setTexture("MetallicRoughnessAoEiTextureArray", metallicRoughnessTextureArray);

// pbr.setColor("BaseColor", ColorRGBA.Gray);
pbr.setFloat("Metallic_0", 0.04f);
pbr.setFloat("Roughness_0", 0.95f);


terrain.setMaterial(pbr);

rootNode.attachChild(terrain);
}

private TextureArray makeSingleImageTextureArray(Texture texture){
TextureArray texArray = new TextureArray(new ArrayList<Image>(Arrays.asList(texture.getImage())));
texArray.setWrap(WrapMode.Repeat);
return texArray;
}

private void initGridFloor() {
Material pbr = new Material(assetManager, "Common/MatDefs/Light/PBRLighting.j3md");
Texture texture = assetManager.loadTexture("Textures/default_grid.png");
texture.setWrap(WrapMode.Repeat);
Expand Down
161 changes: 161 additions & 0 deletions src/main/java/com/github/shader/demo/utils/blendlayers/BlendGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.github.shader.demo.utils.blendlayers;

import com.jme3.bounding.BoundingBox;
import com.jme3.bounding.BoundingSphere;
import com.jme3.bounding.BoundingVolume;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import java.util.ArrayList;

/**
*
* @author ryan
*/
public class BlendGroup{

public Spatial spatial = null;

public BlendGroup(Spatial spatial, ArrayList<Material> materials) {

this.spatial = spatial;
this.materials = materials;

for(Material material : materials){
this.setBlendGroupMatParams(material);
}
}

private ArrayList<Material> materials;


public ShaderBlendLayer[] getRegisteredLayers() { return currentLayers; }
private int reservedLayerCount = 0; //number of layers to reserve at the start of the blend-stack. all dynamic layers will be added ontop of these layers based on priority (and then duration if priority is tied)



public int getReservedLayerCount() { return reservedLayerCount; }

private ShaderBlendLayer[] currentLayers = new ShaderBlendLayer[100];

public void setReservedLayerCount(int reservedLayerCount) { this.reservedLayerCount = reservedLayerCount; }

private int currentDynamicLayerCount;


public int getCurrentDynamicLayerCount() { return currentDynamicLayerCount; }
public int getCurrentLayerCount() { return currentDynamicLayerCount + reservedLayerCount; }

public void setReservedLayer(){

}

public void setBlendLayer(int layerIndex, ShaderBlendLayer blendLayer){

if(layerIndex >= reservedLayerCount){
if(currentLayers[layerIndex] == null){
currentDynamicLayerCount ++;
}
}
blendLayer.clearMaterials();
blendLayer.setLayerIndex(layerIndex);
blendLayer.setMaterials(materials);

currentLayers[layerIndex] = blendLayer;

}

public void swapBlendLayers(ShaderBlendLayer layerA, ShaderBlendLayer layerB){
int layerIndexA = layerA.getLayerIndex();
int layerIndexB = layerB.getLayerIndex();

this.setBlendLayer(layerIndexB, layerA);
this.setBlendLayer(layerIndexA, layerB);
}

//set a new layer at the desired index, and shift all other above it up to make room
public void setBlendLayerAndShiftUp(ShaderBlendLayer layer, int layerIndex){
for(int i = currentLayers.length - 1; i >= layerIndex; i--){

int shiftedIndex = i + 1;
ShaderBlendLayer layerToShift = currentLayers[i];
if(layerToShift != null){
setBlendLayer(shiftedIndex, layerToShift);
}

}
setBlendLayer(layerIndex, layer);

}

public void removeBlendLayer(ShaderBlendLayer blendLayer){
blendLayer.clearMaterials();

int layerIndex = blendLayer.getLayerIndex();

if(layerIndex > -1 && layerIndex < currentLayers.length){
if(currentLayers[layerIndex] == blendLayer){ //make sure the layer was indeed at the set index in this BlendGroup before shifting other layers, to avoid breaking everything else if that is done by mistake
//shift down all layes that were above the removed layer
for(int i = layerIndex; i <currentLayers.length - 1; i++){
int layerToShiftIndex = i + 1;
ShaderBlendLayer layerToShiftDown = currentLayers[layerToShiftIndex];
if(layerToShiftDown != null){
this.setBlendLayer(i, layerToShiftDown);
}
}

if(layerIndex >= reservedLayerCount){
currentDynamicLayerCount--;
System.out.println(" CLC 11111 :: " + currentDynamicLayerCount);
}

currentLayers[layerIndex] = null;

}
}
}

private final Vector3f spatialDimensions = new Vector3f();
private final Vector3f spatialWorldTranslation = new Vector3f();

public void update(float tpf){
for (ShaderBlendLayer blendLayer : currentLayers) {

if(blendLayer != null){
blendLayer.update(tpf);
}

if(spatial != null){
BoundingVolume bounds = spatial.getWorldBound();
float yDim, xDim, zDim;
if(bounds instanceof BoundingBox){
yDim = ((BoundingBox) bounds).getYExtent() * 2;
xDim = ((BoundingBox) bounds).getXExtent()* 2;
zDim = ((BoundingBox) bounds).getZExtent() * 2;
spatialDimensions.set(xDim, yDim, zDim);

}else if(bounds instanceof BoundingSphere){
float diameter = ((BoundingSphere) bounds).getRadius() * 2;
spatialDimensions.set(diameter, diameter, diameter);
}

if(spatialWorldTranslation != spatial.getWorldTranslation()){
spatialWorldTranslation.set(spatial.getWorldTranslation());
}
}
}
}

public void setBlendGroupMatParams(Material material){

material.setVector3("BlendGroup_SpatialDimensions", spatialDimensions);
material.setVector3("BlendGroup_SpatialOriginOffset", spatialWorldTranslation);

}


}
Loading

0 comments on commit c277de0

Please sign in to comment.