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

Render events #132

Merged
merged 6 commits into from
Feb 23, 2023
Merged

Render events #132

merged 6 commits into from
Feb 23, 2023

Conversation

Dgzt
Copy link
Collaborator

@Dgzt Dgzt commented Feb 9, 2023

Hi,

I added render event handling. I want to render the part of building in dept capturing but render the whole building in the other render process. I've tried to figure out a generic solution for it.

building.mp4

Example code:

...

public class LibgdxMundusBuilding extends ApplicationAdapter {
	private ModelBatch batch;
	private Mundus mundus;
	private Scene scene;
	private TopDownCameraController controller;
	private BlendingAttribute blendingAttribute;
	private boolean show = true;
	
	@Override
	public void create () {
		...

		scene.sceneGraph.getRoot().getChildren().get(1).getComponents();
		for (final GameObject child : scene.sceneGraph.getRoot().getChildren()) {
			if (child.name.equals("house.gltf")) {
				final ModelComponent modelComponent = (ModelComponent) child.getComponents().get(0);

				blendingAttribute = new BlendingAttribute();

				final Material material = modelComponent.getModelInstance().getMaterial("Material.002");
				material.set(blendingAttribute);

				modelComponent.addEvent(new BeforeRenderEvent() {
					@Override
					public void action() {
						if (!show) {
							blendingAttribute.opacity = 0.0f;
						}
					}
				});

				modelComponent.addEvent(new AfterRenderEvent() {
					@Override
					public void action() {
						scene.batch.end();
						scene.batch.begin(scene.cam);
						blendingAttribute.opacity = 1.0f;
					}
				});

			}
		}
	}

	@Override
	public void render () {
		...

		if (Gdx.input.isKeyJustPressed(Input.Keys.R)) {
			show = !show;
		}

                ...
	}
	
	...

Example project:
libgdx-mundus-building.zip

@Dgzt Dgzt added the enhancement New feature or request label Feb 9, 2023
@Dgzt
Copy link
Collaborator Author

Dgzt commented Feb 11, 2023

This won't work with object what use model cache. A trick is needed here.

@Dgzt
Copy link
Collaborator Author

Dgzt commented Feb 11, 2023

I implemented this feature for ModelCacheManager too. I updated the example code and reuploaded the example project.

@JamesTKhan
Copy link
Owner

This looks interesting I will test it out soon

@JamesTKhan
Copy link
Owner

JamesTKhan commented Feb 20, 2023

Checked out the demo and code. I am open to merging it, not sure how many actual use cases it has but I do not see any harm in having custom events for before or after a render.

In your example code though I think it wouldn't scale well if you had lots of houses because every post event is flushing the batch.

Two questions

  1. Any reason to not put all the render event logic in to AbstractComponent instead of CullableComponent?
  2. Have you tested this on HTML yet just to make sure it all compiles? Otherwise I can probably test later today.

@Dgzt
Copy link
Collaborator Author

Dgzt commented Feb 20, 2023

Checked out the demo and code. I am open to merging it, not sure how many actual use cases it has but I do not see any harm in having custom events for before or after a render.

In your example code though I think it wouldn't scale well if you had lots of houses because every post event is flushing the batch.

Yes, I'm sure that this is not the best solution, but currently I don't know better solution to render whole model in dept rendering and render part of model in other rendering.

1. Any reason to not put all the render event logic in to AbstractComponent instead of CullableComponent?

The LightComponent also extends AbstractComponent what has not a valid render method where can trigger events, so I implemented it in CullableComponent.

2. Have you tested this on HTML yet just to make sure it all compiles? Otherwise I can probably test later today.

I've not tested this feature on HTML. Thanks if you test it. The weekend at the earliest when I can test it on HTML.

@JamesTKhan
Copy link
Owner

JamesTKhan commented Feb 21, 2023

I may have a suggestion that will prevent having to flush the batch like that. Have a render event that is executed pre-render and a separate render event executed pre-depth render. That way, on the pre-depth render you would switch the opacity back to 1.0.

I just did a quick test and it works without flushing the batch.

Something like picture below May not even need the after render event anymore? Just a BeforeRenderEvent and BeforeDepthRenderEvent?

Something like so
image

@Dgzt
Copy link
Collaborator Author

Dgzt commented Feb 21, 2023

I may have a suggestion that will prevent having to flush the batch like that. Have a render event that is executed pre-render and a separate render event executed pre-depth render. That way, on the pre-depth render you would switch the opacity back to 1.0.

I just did a quick test and it works without flushing the batch.

Something like picture below May not even need the after render event anymore? Just a BeforeRenderEvent and BeforeDepthRenderEvent?

Something like so image

Thanks, I've implemented it and works.

Here is the example code:

modelComponent.addEvent(new BeforeRenderEvent() {
	@Override
	public void action() {
		if (!show) {
			blendingAttribute.opacity = 0.0f;
		}
	}
});

modelComponent.addEvent(new BeforeDeptRenderEvent() {
	@Override
	public void action() {
		blendingAttribute.opacity = 1.0f;
	}
});

The testing on HTML is missing, maybe I can do it on the next days.

@Dgzt
Copy link
Collaborator Author

Dgzt commented Feb 22, 2023

I've fixed the code for HTML.

Here is the example project:
libgdx-mundus-building-html.zip

@JamesTKhan
Copy link
Owner

Everything looks good, one last thing. Could you fix the spelling on "Dept" should be "Depth"

@Dgzt
Copy link
Collaborator Author

Dgzt commented Feb 23, 2023

Done.

@JamesTKhan
Copy link
Owner

Thanks!

@JamesTKhan JamesTKhan merged commit d0e3b89 into JamesTKhan:master Feb 23, 2023
@Dgzt Dgzt deleted the render-events branch March 1, 2023 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants