Skip to content

Commit

Permalink
Merge pull request #132 from Dgzt/render-events
Browse files Browse the repository at this point in the history
Render events
  • Loading branch information
JamesTKhan authored Feb 23, 2023
2 parents 7f920df + cdca930 commit d0e3b89
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 2 deletions.
2 changes: 2 additions & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ protected void renderObjects(float delta) {
// Render objects
batch.begin(cam);
sceneGraph.render(delta, clippingPlaneDisable, 0);
modelCacheManager.triggerBeforeRenderEvent();
batch.render(modelCacheManager.modelCache, environment);
batch.end();
}
Expand Down Expand Up @@ -202,6 +203,7 @@ protected void renderShadowMap(float delta) {
shadowMapper.begin(light.direction);
depthBatch.begin(shadowMapper.getCam());
sceneGraph.renderDepth(delta, clippingPlaneDisable, 0, shadowMapShader);
modelCacheManager.triggerBeforeDepthRenderEvent();
depthBatch.render(modelCacheManager.modelCache, environment, shadowMapShader);
depthBatch.end();
shadowMapper.end();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mbrlabs.mundus.commons.event;

/**
* Calls this event before depth render.
*/
public abstract class BeforeDepthRenderEvent implements Event {

@Override
public EventType getType() {
return EventType.BEFORE_DEPTH_RENDER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mbrlabs.mundus.commons.event;

/**
* Calls this event before render.
*/
public abstract class BeforeRenderEvent implements Event {

@Override
public EventType getType() {
return EventType.BEFORE_RENDER;
}
}
15 changes: 15 additions & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/event/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mbrlabs.mundus.commons.event;

public interface Event {

/**
* The type of event.
*/
EventType getType();

/**
* Calls this method if the event has triggered.
*/
void action();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mbrlabs.mundus.commons.event;

public enum EventType {
BEFORE_RENDER,
BEFORE_DEPTH_RENDER
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public class ModelCacheManager implements Disposable {
protected float modelCacheUpdateInterval = 0.5f;
protected float lastModelCacheRebuild = modelCacheUpdateInterval;
protected boolean modelCacheRebuildRequested = true;
private final Array<ModelEventable> modelEventables;

public ModelCacheManager(Scene scene) {
modelCache = new ModelCache();
this.scene = scene;
this.modelEventables = new Array<>();
}

public void update(float delta) {
Expand All @@ -45,6 +47,7 @@ public void update(float delta) {
* depending on the size of the scene and should only be rebuilt when needed.
*/
public void rebuildModelCache() {
modelEventables.clear();
modelCache.begin(scene.cam);
addModelsToCache(scene.sceneGraph.getGameObjects());
modelCache.end();
Expand Down Expand Up @@ -72,6 +75,10 @@ protected void addModelsToCache(Array<GameObject> gameObjects) {
}

modelCache.add(((ModelCacheable) comp).getModelInstance());

if (comp instanceof ModelEventable) {
modelEventables.add((ModelEventable) comp);
}
}
}

Expand All @@ -97,6 +104,18 @@ public void setModelCacheUpdateInterval(float interval) {
modelCacheUpdateInterval = interval;
}

public void triggerBeforeDepthRenderEvent() {
for (final ModelEventable me : modelEventables) {
me.triggerBeforeDepthRenderEvent();
}
}

public void triggerBeforeRenderEvent() {
for (final ModelEventable me : modelEventables) {
me.triggerBeforeRenderEvent();
}
}

/**
* Rebuild model cache if given GameObject has a cacheable component.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mbrlabs.mundus.commons.scene3d;

import com.mbrlabs.mundus.commons.event.BeforeDepthRenderEvent;
import com.mbrlabs.mundus.commons.event.Event;

/**
* Indicates the object that can be eventable.
*
* @author Dgzt
*/
public interface ModelEventable {

/**
* Adds event to object.
*/
void addEvent(Event event);

/**
* Removes event from object.
*/
void removeEvent(Event event);

/**
* Triggers all {@link BeforeDepthRenderEvent}.
*/
void triggerBeforeDepthRenderEvent();

/**
* Triggers all {@link com.mbrlabs.mundus.commons.event.BeforeRenderEvent}.
*/
void triggerBeforeRenderEvent();

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.utils.Array;
import com.mbrlabs.mundus.commons.env.lights.DirectionalLight;
import com.mbrlabs.mundus.commons.event.Event;
import com.mbrlabs.mundus.commons.event.EventType;
import com.mbrlabs.mundus.commons.scene3d.GameObject;
import com.mbrlabs.mundus.commons.scene3d.ModelCacheable;
import com.mbrlabs.mundus.commons.scene3d.ModelEventable;
import com.mbrlabs.mundus.commons.shadows.ShadowMapper;
import com.mbrlabs.mundus.commons.utils.LightUtils;

Expand All @@ -40,7 +44,7 @@
* @author JamesTKhan
* @version July 18, 2022
*/
public abstract class CullableComponent extends AbstractComponent {
public abstract class CullableComponent extends AbstractComponent implements ModelEventable {
private final static BoundingBox tmpBounds = new BoundingBox();
private final static Vector3 tmpScale = new Vector3();
private static DirectionalLight directionalLight;
Expand All @@ -51,6 +55,7 @@ public abstract class CullableComponent extends AbstractComponent {

// Is it offscreen?
protected boolean isCulled = false;
private Array<Event> events;
private ModelInstance modelInstance = null;

public CullableComponent(GameObject go) {
Expand Down Expand Up @@ -101,6 +106,32 @@ public void update(float delta) {
}
}

@Override
public void addEvent(final Event event) {
if (events == null) {
events = new Array<>(1);
}
events.add(event);
}

@Override
public void removeEvent(final Event event) {
if (events == null) {
events = new Array<>(1);
}
events.removeValue(event, true);
}

@Override
public void triggerBeforeDepthRenderEvent() {
triggerEvent(EventType.BEFORE_DEPTH_RENDER);
}

@Override
public void triggerBeforeRenderEvent() {
triggerEvent(EventType.BEFORE_RENDER);
}

protected void setDimensions(ModelInstance modelInstance) {
if (modelInstance == null) {
Gdx.app.error("CullableComponent", "setDimensions called with null modelInstance");
Expand All @@ -116,6 +147,18 @@ protected void setDimensions(ModelInstance modelInstance) {
radius = dimensions.len() / 2f;
}

private void triggerEvent(final EventType eventType) {
if (events == null) {
return;
}

for (int i = 0; i < events.size; ++i) {
if (eventType == events.get(i).getType()) {
events.get(i).action();
}
}
}

public Vector3 getCenter() {
return center;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void render(float delta) {

if (isCulled || useModelCache) return;

triggerBeforeRenderEvent();
if (shader != null) {
gameObject.sceneGraph.scene.batch.render(modelInstance, gameObject.sceneGraph.scene.environment, shader);
} else {
Expand All @@ -146,6 +147,8 @@ public void renderDepth(float delta, Vector3 clippingPlane, float clipHeight, Sh
((ClippableShader) depthShader).setClippingHeight(clipHeight);
}

triggerBeforeDepthRenderEvent();

if (depthShader instanceof ShadowMapShader)
// Shadow Mapper will use default (PBR's depth shader) for animation support
gameObject.sceneGraph.scene.depthBatch.render(modelInstance, gameObject.sceneGraph.scene.environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void setShader(Shader shader) {
public void render(float delta) {
super.render(delta);
if (isCulled) return;
triggerBeforeRenderEvent();
gameObject.sceneGraph.scene.batch.render(modelInstance, gameObject.sceneGraph.scene.environment);
}

Expand All @@ -93,6 +94,8 @@ public void renderDepth(float delta, Vector3 clippingPlane, float clipHeight, Sh
((ClippableShader) shader).setClippingHeight(clipHeight);
}

triggerBeforeDepthRenderEvent();

gameObject.sceneGraph.scene.depthBatch.render(modelInstance, gameObject.sceneGraph.scene.environment, shader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public boolean usesAsset(Asset assetToCheck) {
public void render(float delta) {
super.render(delta);
if (isCulled) return;
triggerBeforeRenderEvent();
gameObject.sceneGraph.scene.batch.render(waterAsset.water, gameObject.sceneGraph.scene.environment);
}

Expand Down
3 changes: 2 additions & 1 deletion gdx-runtime/CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[0.5.0] ~
- [Breaking Change] Terrain API modified to closer reflect Models
- Added 'addGameObject(ModelInstance, Vector3)' and 'addGameObject(Model, Vector3)' methods to SceneGraph to add external model to scene graph
- Add 'addGameObject(ModelInstance, Vector3)' and 'addGameObject(Model, Vector3)' methods to SceneGraph to add external model to scene graph
- Add render event handling

[0.4.0] ~ 10/12/2022
- [BREAKING CHANGE] The loadScene method for the runtime has changed. A ModelBatch is no longer required to be passed in.
Expand Down

0 comments on commit d0e3b89

Please sign in to comment.