Repository navigation
Refresh instanced bounds after source geometry updates #2995
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
Open
toaster0123
wants to merge
2
commits into
jMonkeyEngine:master
Choose a base branch
from
toaster0123:fix/instanced-geometry-bounds-20261002
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
jme3-core/src/test/java/com/jme3/scene/instancing/InstancedBoundsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| /* | ||
| * Copyright (c) 2026 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.scene.instancing; | ||
|
|
||
| import com.jme3.asset.DesktopAssetManager; | ||
| import com.jme3.bounding.BoundingBox; | ||
| import com.jme3.light.DefaultLightFilter; | ||
| import com.jme3.light.LightList; | ||
| import com.jme3.light.PointLight; | ||
| import com.jme3.material.Material; | ||
| import com.jme3.material.MaterialDef; | ||
| import com.jme3.math.Vector3f; | ||
| import com.jme3.renderer.Camera; | ||
| import com.jme3.scene.Geometry; | ||
| import com.jme3.scene.Node; | ||
| import com.jme3.scene.Spatial; | ||
| import com.jme3.scene.shape.Box; | ||
| import com.jme3.shader.VarType; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| /** | ||
| * Verifies that instance bounds follow changes to the source geometries. | ||
| */ | ||
| public class InstancedBoundsTest { | ||
|
|
||
| @Test | ||
| public void movingInstanceUpdatesBounds() { | ||
| checkMovedInstanceBounds(false); | ||
| } | ||
|
|
||
| @Test | ||
| public void movingInstanceAfterItsBatchUpdatesBounds() { | ||
| checkMovedInstanceBounds(true); | ||
| } | ||
|
|
||
| private void checkMovedInstanceBounds(boolean batchFirst) { | ||
| Fixture fixture = new Fixture(false); | ||
| if (batchFirst) { | ||
| fixture.node.swapChildren(0, 1); | ||
| } | ||
| fixture.geometry.move(20, 0, 0); | ||
| fixture.update(); | ||
|
|
||
| assertEquals(new Vector3f(20, 0, 10), fixture.geometry.getWorldBound().getCenter()); | ||
| fixture.assertBounds(); | ||
| } | ||
|
|
||
| @Test | ||
| public void movingNestedParentUpdatesBounds() { | ||
| Fixture fixture = new Fixture(true); | ||
| fixture.geometry.getParent().move(20, 0, 0); | ||
| fixture.update(); | ||
| fixture.assertBounds(); | ||
| } | ||
|
|
||
| @Test | ||
| public void parentTraversalUpdatesInstancedChildBounds() { | ||
| Fixture fixture = new Fixture(false); | ||
| Node root = new Node("root"); | ||
| root.attachChild(fixture.node); | ||
| root.updateGeometricState(); | ||
|
|
||
| fixture.geometry.move(20, 0, 0); | ||
| root.updateLogicalState(0); | ||
| root.updateGeometricState(); | ||
|
|
||
| fixture.assertBounds(); | ||
| assertEquals(fixture.geometry.getWorldBound(), root.getWorldBound()); | ||
| } | ||
|
|
||
| @Test | ||
| public void changingModelBoundUpdatesBounds() { | ||
| Fixture fixture = new Fixture(false); | ||
| fixture.geometry.setModelBound(new BoundingBox(Vector3f.ZERO, 4, 5, 6)); | ||
| fixture.update(); | ||
| fixture.assertBounds(); | ||
| } | ||
|
|
||
| @Test | ||
| public void queryingNodeBoundUpdatesInstanceBounds() { | ||
| Fixture fixture = new Fixture(false); | ||
| fixture.geometry.move(20, 0, 0); | ||
| // A direct bounds query also runs the normal lazy bound update path. | ||
| assertEquals(fixture.geometry.getWorldBound(), fixture.node.getWorldBound()); | ||
| fixture.assertBounds(); | ||
| } | ||
|
|
||
| @Test | ||
| public void movingInstanceKeepsNearbyPointLight() { | ||
| Fixture fixture = new Fixture(false); | ||
| PointLight light = new PointLight(new Vector3f(20, 0, 10)); | ||
| light.setRadius(3); | ||
| fixture.node.addLight(light); | ||
| fixture.update(); | ||
|
|
||
| Camera camera = new Camera(100, 100); | ||
| camera.setFrustum(1, 100, -50, 50, 50, -50); | ||
| camera.lookAtDirection(Vector3f.UNIT_Z, Vector3f.UNIT_Y); | ||
| DefaultLightFilter filter = new DefaultLightFilter(); | ||
| LightList filtered = new LightList(fixture.batch); | ||
| filter.setCamera(camera); | ||
| filter.filterLights(fixture.batch, filtered); | ||
| assertEquals(0, filtered.size(), "The light initially does not reach the instance"); | ||
|
|
||
| fixture.geometry.move(20, 0, 0); | ||
| fixture.update(); | ||
| fixture.batch.updateInstances(camera); | ||
| assertEquals(1, fixture.batch.getNumVisibleInstances()); | ||
| filter.setCamera(camera); | ||
| filtered.clear(); | ||
| filter.filterLights(fixture.batch, filtered); | ||
| assertEquals(1, filtered.size(), "The light now reaches the moved instance"); | ||
| assertSame(light, filtered.get(0)); | ||
| } | ||
|
|
||
| private static class Fixture { | ||
| final InstancedNode node = new InstancedNode("instances"); | ||
| final Geometry geometry = new Geometry("instance", new Box(1, 1, 1)); | ||
| final InstancedGeometry batch; | ||
|
|
||
| Fixture(boolean nested) { | ||
| MaterialDef def = new MaterialDef(new DesktopAssetManager(), "test"); | ||
| def.addMaterialParam(VarType.Boolean, "UseInstancing", false); | ||
| Material material = new Material(def); | ||
| material.setBoolean("UseInstancing", true); | ||
| geometry.setMaterial(material); | ||
| geometry.setLocalTranslation(0, 0, 10); | ||
| Node parent = node; | ||
| if (nested) { | ||
| parent = new Node("nested"); | ||
| node.attachChild(parent); | ||
| } | ||
| parent.attachChild(geometry); | ||
| node.instance(); | ||
| InstancedGeometry found = null; | ||
| for (Spatial child : node.getChildren()) { | ||
| if (child instanceof InstancedGeometry) { | ||
| found = (InstancedGeometry) child; | ||
| } | ||
| } | ||
| assertNotNull(found); | ||
| batch = found; | ||
| update(); | ||
| assertBounds(); | ||
| } | ||
|
|
||
| void update() { | ||
| node.updateLogicalState(0); | ||
| node.updateGeometricState(); | ||
| } | ||
|
|
||
| void assertBounds() { | ||
| assertEquals(geometry.getWorldBound(), batch.getWorldBound()); | ||
| assertEquals(geometry.getWorldBound(), node.getWorldBound()); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick coverage check on this new override: it only catches calls that go through the public no-arg
updateWorldBound(). IfSpatialstill has the internalupdateWorldBound(boolean recurse)entry point (used when a parent node walks its children), that call skips this override, so the groups could keep their stale bounds on that path.Overriding the protected variant instead would cover both, since the public entry point delegates to it:
Not a blocker, just worth confirming which entry points actually reach this code in a normal frame.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the current base and PR head:
updateWorldBound()is already the protected override, and there is noupdateWorldBound(boolean)overload in Spatial or Node.Both paths reach it after updating the source children: Node.updateGeometricState() and the lazy Spatial.checkDoBoundUpdate(). The override then refreshes the instance groups before
super.updateWorldBound()merges their bounds into the node bound.The existing nested test covers a parent inside the InstancedNode. I also added the separate case you suggested, with the InstancedNode under an outer root and updates driven through that root. It fails on the original code and passes with this override. I’m running the full core checks before pushing that test and clarifying the PR wording.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The outer-root regression is now in f16b477. All seven bounds cases fail on the original code and pass with the fix; the full core checks also pass (490 tests, one existing skip). I clarified the PR description to distinguish updating children from merging their bounds. No production change was needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks — that clears it up on my end. I had a stale mental picture of an
updateWorldBound(boolean recurse)variant; it doesn't exist inSpatialorNodein the current base, so this override really is the single hook and both paths (theNode.updateGeometricState()traversal and the lazySpatial.checkDoBoundUpdate()bound query) funnel through it.The outer-root test is a good addition — driving the update through a parent node is exactly the path I was unsure about, and the description wording now matches the actual ordering (refresh groups, then merge into the node bound).
One note: no CI checks are reported on
f16b477yet on my side. Once the core checks come back green on that SHA I'll approve. The code and the seven regression cases look right to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following up on my last note: GitHub isn't reporting any check runs for
f16b477(the combined status is still pending with zero checks), so waiting on CI here wouldn't tell either of us anything new — it may simply not have been kicked off for this branch yet.Since my one substantive concern turned out to be a wrong assumption about the API, and the code plus the seven regression cases hold up to review, I'm going ahead and approving. Happy to revisit if a core run comes back red later.