Skip to content
Open
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
Expand Up @@ -199,6 +199,17 @@ public InstancedNode(String name) {
addControl(control);
}

@Override
protected void updateWorldBound() {
// Source geometries can change their bounds without changing the
// transform of the InstancedGeometry that renders them. Refresh the
// aggregate bounds after all source children have been updated.
for (InstancedGeometry ig : instancesMap.values()) {
ig.updateWorldBound();
}
super.updateWorldBound();

Copy link
Copy Markdown
Contributor

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(). If Spatial still has the internal updateWorldBound(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:

    @Override
    protected void updateWorldBound(boolean recurse) {
        for (InstancedGeometry ig : instancesMap.values()) {
            ig.updateWorldBound();
        }
        super.updateWorldBound(recurse);
    }

Not a blocker, just worth confirming which entry points actually reach this code in a normal frame.

Copy link
Copy Markdown
Contributor Author

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 no updateWorldBound(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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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 in Spatial or Node in the current base, so this override really is the single hook and both paths (the Node.updateGeometricState() traversal and the lazy Spatial.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 f16b477 yet 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.

Copy link
Copy Markdown
Contributor

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.

}

private void renderFromControl(Camera cam) {
for (InstancedGeometry ig : instancesMap.values()) {
ig.updateInstances(cam);
Expand Down
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());
}
}
}