mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-29 17:23:59 +00:00
Pick procedural parts by clicking blocks in the studio scene view
Authors could not tell which part of a procedural object placed a block. A click on a block in the Procedural view now finds its part and chooses the node that draws it; choosing a node lights up its blocks and draws everything else faintly. SceneView gains hasParts and parts (BlockParts), served as scene-parts, aligned with the scene's blocks. No pins moved: the trace bakes with its own builder and generation is untouched, ProceduralObjectsGoldenTest digests are unchanged. v3: ff0190384
This commit is contained in:
1 parent
a8aaa3c3cf
commit
8136fa857f
24 files changed
+719
-48
No files matched your search
@@ -24,6 +24,7 @@ import com.volmit.geometry.BlockBox;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.tooling.BlockParts;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
@@ -80,6 +81,19 @@ final class SceneEncoding {
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
/**
|
||||
* The part of each block {@link #encode} writes, in the same order, as little endian i32 numbers; a block no part
|
||||
* placed is {@link BlockParts#NO_PART}.
|
||||
*/
|
||||
static byte[] encodeParts(GeneratedObject scene, BlockParts parts) {
|
||||
List<ObjectBlock> drawn = scene.blocks().stream().filter(block -> !isAir(block.state())).toList();
|
||||
ByteBuffer buffer = ByteBuffer.allocate(drawn.size() * Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN);
|
||||
for (ObjectBlock block : drawn) {
|
||||
buffer.putInt(parts.partAt(block.position()));
|
||||
}
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
private static boolean isAir(BlockState state) {
|
||||
return AIR.contains(state.material().toString());
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ import com.volmit.tooling.BlockPattern;
|
||||
import com.volmit.tooling.PatternArea;
|
||||
import com.volmit.tooling.PatternEntry;
|
||||
import com.volmit.tooling.BakeRun;
|
||||
import com.volmit.tooling.BlockParts;
|
||||
import com.volmit.tooling.SceneEntry;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -238,6 +239,7 @@ public final class StudioBridge implements AutoCloseable {
|
||||
case "scene-entries" -> sendJson(exchange, STATUS_OK, sceneEntries(get(exchange).engine(), query));
|
||||
case "scene-bake" -> sendJson(exchange, STATUS_OK, sceneBake(get(exchange).engine(), query));
|
||||
case "scene" -> sendBytes(exchange, "application/octet-stream", scene(get(exchange).engine(), query));
|
||||
case "scene-parts" -> sendBytes(exchange, "application/octet-stream", sceneParts(get(exchange).engine(), query));
|
||||
case "saved-worlds" -> sendJson(exchange, STATUS_OK, savedWorlds(exchange));
|
||||
case "saved-start" -> sendJson(exchange, STATUS_OK, savedStart(exchange, query));
|
||||
case "saved-tile" -> sendBytes(exchange, "application/octet-stream", savedTile(get(exchange).engine(), query));
|
||||
@@ -339,7 +341,7 @@ public final class StudioBridge implements AutoCloseable {
|
||||
StudioEditors editors = StudioEditors.of(engine);
|
||||
StudioScenes scenes = StudioScenes.of(engine);
|
||||
return scenes.views().stream()
|
||||
.map(view -> new SceneViewInfo(view.id(), view.label(), view.description(), editors.edits(view.id()), scenes.isBaked(view.id())))
|
||||
.map(view -> new SceneViewInfo(view.id(), view.label(), view.description(), editors.edits(view.id()), scenes.isBaked(view.id()), scenes.hasParts(view.id())))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -422,6 +424,21 @@ public final class StudioBridge implements AutoCloseable {
|
||||
return SceneEncoding.encode(scene.orElseThrow(() -> new RequestError(STATUS_NOT_FOUND, "No scene " + key)), gson);
|
||||
}
|
||||
|
||||
private static byte[] sceneParts(Engine engine, Query query) {
|
||||
String key = query.text("key");
|
||||
int variant = query.whole("variant", 0);
|
||||
try {
|
||||
StudioScenes scenes = StudioScenes.of(engine);
|
||||
GeneratedObject scene = scenes.scene(query.text("view"), key, variant)
|
||||
.orElseThrow(() -> new RequestError(STATUS_NOT_FOUND, "No scene " + key));
|
||||
BlockParts parts = scenes.parts(query.text("view"), key, variant)
|
||||
.orElseThrow(() -> new RequestError(STATUS_NOT_FOUND, "No parts for " + key));
|
||||
return SceneEncoding.encodeParts(scene, parts);
|
||||
} catch (NoSuchElementException unknown) {
|
||||
throw new RequestError(STATUS_NOT_FOUND, unknown.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static List<PatternEntryWire> patternEntries(Engine engine, Query query) {
|
||||
try {
|
||||
return StudioPatterns.of(engine).entries(query.text("view")).stream().map(PatternEntryWire::of).toList();
|
||||
@@ -751,8 +768,8 @@ public final class StudioBridge implements AutoCloseable {
|
||||
private record ErrorBody(String error) {
|
||||
}
|
||||
|
||||
/** A scene view as the studio lists it; {@code editable} when its entries can be edited as nodes, {@code baked} when their bake can be timed. */
|
||||
private record SceneViewInfo(String id, String label, String description, boolean editable, boolean baked) {
|
||||
/** A scene view as the studio lists it; {@code editable} when its entries can be edited as nodes, {@code baked} when their bake can be timed, {@code parts} when the blocks of a scene can be told by the part that placed them. */
|
||||
private record SceneViewInfo(String id, String label, String description, boolean editable, boolean baked, boolean parts) {
|
||||
}
|
||||
|
||||
/** A {@link SceneEntry} as the studio reads it, with a blank group for none. */
|
||||
|
||||
@@ -21,6 +21,7 @@ package art.arcane.iris.core.studio.scene;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.tooling.BakeRun;
|
||||
import com.volmit.tooling.BlockParts;
|
||||
import com.volmit.tooling.SceneEntry;
|
||||
import com.volmit.tooling.SceneView;
|
||||
|
||||
@@ -58,6 +59,21 @@ public final class StudioScenes {
|
||||
return views.ask(viewId, "build " + key, view -> view.scene(views.world(), key, variant));
|
||||
}
|
||||
|
||||
/** @throws NoSuchElementException when no module registered a scene view {@code viewId} */
|
||||
public boolean hasParts(String viewId) {
|
||||
return views.ask(viewId, "say whether it has parts", SceneView::hasParts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Which part placed each block of the scene {@link #scene} gives for the same arguments; empty when the view has no
|
||||
* such entry or variant, or no parts.
|
||||
*
|
||||
* @throws NoSuchElementException when no module registered a scene view {@code viewId}
|
||||
*/
|
||||
public Optional<BlockParts> parts(String viewId, String key, int variant) {
|
||||
return views.ask(viewId, "find the parts of " + key, view -> view.parts(views.world(), key, variant));
|
||||
}
|
||||
|
||||
/** @throws NoSuchElementException when no module registered a scene view {@code viewId} */
|
||||
public boolean isBaked(String viewId) {
|
||||
return views.ask(viewId, "say whether it bakes", SceneView::isBaked);
|
||||
|
||||
@@ -8,12 +8,14 @@ import com.volmit.geometry.BlockBox;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.tooling.BlockParts;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -40,4 +42,19 @@ public class SceneEncodingTest {
|
||||
body.position(blocksAt + 2 * 16);
|
||||
assertEquals(List.of(0, 2, 0, 1), List.of(body.getInt(), body.getInt(), body.getInt(), body.getInt()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void partsFollowTheBlocksTheSceneDrawsAndLeaveAirOut() {
|
||||
GeneratedObject tree = new GeneratedObject(new BlockBox(0, 0, 0, 3, 3, 3), new BlockPosition(1, 0, 1), List.of(
|
||||
new ObjectBlock(new BlockPosition(1, 0, 1), BlockState.parse("minecraft:oak_log[axis=y]")),
|
||||
new ObjectBlock(new BlockPosition(0, 1, 1), BlockState.parse("minecraft:air")),
|
||||
new ObjectBlock(new BlockPosition(1, 1, 1), BlockState.parse("minecraft:oak_log[axis=y]")),
|
||||
new ObjectBlock(new BlockPosition(1, 2, 1), BlockState.parse("minecraft:oak_leaves"))), List.of());
|
||||
BlockParts parts = new BlockParts(Map.of(new BlockPosition(1, 0, 1), 4, new BlockPosition(0, 1, 1), 9, new BlockPosition(1, 1, 1), 7));
|
||||
|
||||
ByteBuffer encoded = ByteBuffer.wrap(SceneEncoding.encodeParts(tree, parts)).order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
assertEquals(3 * Integer.BYTES, encoded.remaining());
|
||||
assertEquals(List.of(4, 7, BlockParts.NO_PART), List.of(encoded.getInt(), encoded.getInt(), encoded.getInt()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.engine.procedural;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.loader.ResourceLoader;
|
||||
import art.arcane.iris.core.modules.IrisPackResources;
|
||||
import art.arcane.iris.core.modules.JsonFormsCodecs;
|
||||
import art.arcane.iris.engine.UsableBlockPlatform;
|
||||
import art.arcane.iris.engine.procedural.part.PartTrace;
|
||||
import art.arcane.iris.engine.procedural.part.ProceduralObject;
|
||||
import art.arcane.iris.engine.procedural.part.ResolvedProceduralObject;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.volmit.object.PlaceableObject;
|
||||
import com.volmit.tooling.BlockParts;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
/** The parts the studio picks out of a baked variant are the ones that placed the blocks it shows. */
|
||||
public class ProceduralPartTraceTest {
|
||||
private static final String FIXTURE = "/procedural-parts";
|
||||
private static final String DEFINITIONS = "procedural";
|
||||
private static final String[] OBJECTS = {"trees/estranged/baobab-giant", "trees/estranged/stiltroot", "formations/frozen/glacial-arch"};
|
||||
|
||||
private Path pack;
|
||||
private IrisPackResources resources;
|
||||
private Gson gson;
|
||||
|
||||
@Before
|
||||
public void bindGameLikePlatform() throws URISyntaxException {
|
||||
UsableBlockPlatform.bind(new GameLikeBlocks().registries());
|
||||
pack = Path.of(ProceduralPartTraceTest.class.getResource(FIXTURE).toURI());
|
||||
IrisData data = mock(IrisData.class, withSettings().stubOnly().defaultAnswer(CALLS_REAL_METHODS));
|
||||
doReturn(pack.toFile()).when(data).getDataFolder();
|
||||
doReturn(mock(ResourceLoader.class)).when(data).getBlockLoader();
|
||||
doReturn(null).when(data).getEngine();
|
||||
resources = new IrisPackResources(data);
|
||||
doReturn(resources).when(data).getPackResources();
|
||||
gson = new GsonBuilder().registerTypeAdapterFactory(data).registerTypeAdapterFactory(new JsonFormsCodecs()).create();
|
||||
}
|
||||
|
||||
@After
|
||||
public void unbindPlatform() {
|
||||
UsableBlockPlatform.unbind();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void everyTracedBlockIsABlockOfTheVariantAndItsPartIsInTheOutline() throws IOException {
|
||||
for (String path : OBJECTS) {
|
||||
ResolvedProceduralObject object = resolved(path);
|
||||
for (PlaceableObject variant : object.variants()) {
|
||||
PartTrace trace = object.trace(variant);
|
||||
BlockParts parts = object.parts(variant);
|
||||
|
||||
assertFalse(path + " " + variant.name() + " has no part blocks", parts.partsByBlock().isEmpty());
|
||||
assertEquals(path + " " + variant.name() + " traced blocks that are not shown", trace.blockCount(), parts.partsByBlock().size());
|
||||
assertTrue(parts.partsByBlock().values().stream().allMatch(part -> part < object.outline().nodeCount()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aVariantBakedFromAnotherObjectHasNoParts() throws IOException {
|
||||
ResolvedProceduralObject object = resolved(OBJECTS[0]);
|
||||
PlaceableObject foreign = resolved(OBJECTS[1]).variants().get(0);
|
||||
|
||||
assertTrue(object.parts(foreign).partsByBlock().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tracingTwiceFindsTheSameParts() throws IOException {
|
||||
ResolvedProceduralObject object = resolved(OBJECTS[0]);
|
||||
PlaceableObject variant = object.variants().get(0);
|
||||
|
||||
assertEquals(object.parts(variant), object.parts(variant));
|
||||
}
|
||||
|
||||
private ResolvedProceduralObject resolved(String path) throws IOException {
|
||||
String json = Files.readString(pack.resolve(DEFINITIONS).resolve(path + ".json"));
|
||||
return new ResolvedProceduralObject(path, gson.fromJson(json, ProceduralObject.class), resources);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user