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

Helper lines #146

Merged
merged 29 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d16d560
Add helper lines configs into debug render dialog
Dgzt Mar 27, 2023
1094c0f
Show temporary lines on map
Dgzt Mar 29, 2023
cba0f13
Show vertical lines
Dgzt Mar 29, 2023
b5ee148
Show horizontal lines
Dgzt Mar 29, 2023
16e8d6e
Enable/disable helper lines on debug render dialog
Dgzt Mar 30, 2023
4e6eca8
Fix line at end of terrain
Dgzt Apr 1, 2023
1f725a3
Editable rectangle width
Dgzt Apr 1, 2023
eea7b76
Show rest of rectangle
Dgzt Apr 1, 2023
bf7edb1
Add terrain vertex changed event
Dgzt Apr 2, 2023
c552387
Add terrain removed event
Dgzt Apr 2, 2023
7195bc3
Add terrain added event
Dgzt Apr 2, 2023
b3046a7
Use same transform variable
Dgzt Apr 2, 2023
a5b4ea4
Fix radio button click listeners
Dgzt Apr 2, 2023
d3cec9b
Show the first half hexagon
Dgzt Apr 5, 2023
a3c86c3
Show top right and top side of hexagon
Dgzt Apr 6, 2023
dce71b3
Refactor hexagon helper lines
Dgzt Apr 7, 2023
2bed7f0
Fix hexagon tile on next pattern
Dgzt Apr 7, 2023
7ff4cbe
Use the full map
Dgzt Apr 8, 2023
e7f1bed
Fix end of hex map
Dgzt Apr 8, 2023
3fa052f
Calculate indices num
Dgzt Apr 8, 2023
f1f7a56
Add copyright to header files and remove startup helper lines
Dgzt Apr 8, 2023
34073ac
Merge remote-tracking branch 'origin/master' into helper-lines
Dgzt Apr 8, 2023
9dedd5d
Add more copyright
Dgzt Apr 8, 2023
533b087
Add tooltip for helper lines
Dgzt Apr 8, 2023
05ae29a
Increase column spinner and fix hexagon line object
Dgzt Apr 8, 2023
7bc3b54
Renamed HelperLineObject to HelperLineShape
Dgzt Apr 13, 2023
b579324
Update CHANGES
Dgzt Apr 13, 2023
689e143
Fix not full hexagons at end of map
Dgzt Apr 15, 2023
c726fef
Merge remote-tracking branch 'origin/master' into helper-lines
Dgzt Apr 19, 2023
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
1 change: 1 addition & 0 deletions editor/CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Show brush selection
- Object picker can use with left mouse button. Switchable in the settings
- Water creation dialog
- Helper lines

[0.4.2] ~ 10/24/2022
- Fix shader compilation error for Terrain when using normal maps
Expand Down
5 changes: 5 additions & 0 deletions editor/src/main/com/mbrlabs/mundus/editor/Editor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ class Editor : Lwjgl3WindowAdapter(), ApplicationListener,
debugRenderer.end()
}

scene.batch.begin(scene.cam)
context.helperLines.render(scene.batch)
scene.batch.end()


toolManager.render()
gizmoManager.render()
compass.render(projectManager.modelBatch, scene.environment)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2023. See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mbrlabs.mundus.editor.core.helperlines

import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g3d.Material
import com.badlogic.gdx.graphics.g3d.ModelInstance
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute
import com.badlogic.gdx.graphics.g3d.model.MeshPart
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder
import com.badlogic.gdx.graphics.glutils.ShaderProgram
import com.badlogic.gdx.utils.Disposable
import com.mbrlabs.mundus.commons.scene3d.components.TerrainComponent
import com.mbrlabs.mundus.commons.terrain.Terrain

abstract class HelperLineShape(width: Int, val terrainComponent: TerrainComponent) : Disposable {

val mesh: Mesh
val modelInstance: ModelInstance

init {
val attribs = VertexAttributes(
VertexAttribute.Position(),
VertexAttribute.Normal(),
VertexAttribute(VertexAttributes.Usage.Tangent, 4, ShaderProgram.TANGENT_ATTRIBUTE),
VertexAttribute.TexCoords(0)
)

val terrain = terrainComponent.terrainAsset.terrain

val numVertices = terrain.vertexResolution * terrain.vertexResolution
val numIndices = calculateIndicesNum(width, terrain)

mesh = Mesh(true, numVertices, numIndices, attribs)

val indices = buildIndices(width, numIndices, terrain)

val material = Material(ColorAttribute.createDiffuse(Color.RED))

mesh.setIndices(indices)
mesh.setVertices(terrain.vertices)

val meshPart = MeshPart(null, mesh, 0, numIndices, GL20.GL_LINES)
meshPart.update()

val mb = ModelBuilder()
mb.begin()
mb.part(meshPart, material)
val model = mb.end()
modelInstance = ModelInstance(model)
modelInstance.transform = terrainComponent.modelInstance.transform
}

fun updateVertices() {
mesh.setVertices(terrainComponent.terrainAsset.terrain.vertices)
}

abstract fun calculateIndicesNum(width: Int, terrain: Terrain): Int

abstract fun fillIndices(width: Int, indices: ShortArray, vertexResolution: Int)

private fun buildIndices(width: Int, numIndices: Int, terrain: Terrain): ShortArray {
val indices = ShortArray(numIndices)
val vertexResolution = terrain.vertexResolution

fillIndices(width, indices, vertexResolution)

return indices
}

override fun dispose() {
modelInstance.model!!.dispose()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mbrlabs.mundus.editor.core.helperlines

enum class HelperLineType {
RECTANGLE,
HEXAGON
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2023. See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mbrlabs.mundus.editor.core.helperlines

import com.badlogic.gdx.graphics.g3d.ModelBatch
import com.mbrlabs.mundus.commons.scene3d.components.TerrainComponent
import com.badlogic.gdx.utils.Array
import com.badlogic.gdx.utils.Disposable
import com.mbrlabs.mundus.editor.events.TerrainAddedEvent
import com.mbrlabs.mundus.editor.events.TerrainRemovedEvent
import com.mbrlabs.mundus.editor.events.TerrainVerticesChangedEvent

class HelperLines : TerrainVerticesChangedEvent.TerrainVerticesChangedEventListener,
TerrainAddedEvent.TerrainAddedEventListener,
TerrainRemovedEvent.TerrainRemovedEventListener,
Disposable {

private val helperLineShapes = Array<HelperLineShape>()
private var width = -1
private var type = HelperLineType.RECTANGLE


fun build(type: HelperLineType, width: Int, terrainComponents: Array<TerrainComponent>) {
this.type = type
this.width = width

for (terrainComponent in terrainComponents) {
addNewHelperLineShape(terrainComponent)
}
}

fun render(batch: ModelBatch) {
for (helperLineObject in helperLineShapes) {
batch.render(helperLineObject.modelInstance)
}
}

fun hasHelperLines() = helperLineShapes.notEmpty()

override fun onTerrainVerticesChanged(event: TerrainVerticesChangedEvent) {
helperLineShapes.filter { it.terrainComponent == event.terrainComponent }.forEach { it.updateVertices() }
}

override fun onTerrainAdded(event: TerrainAddedEvent) {
addNewHelperLineShape(event.terrainComponent)
}

override fun onTerrainRemoved(event: TerrainRemovedEvent) {
helperLineShapes.filter { it.terrainComponent == event.terrainComponent }.forEach {
it.dispose()
helperLineShapes.removeValue(it, true)
}
}

override fun dispose() {
helperLineShapes.forEach { helperLineObject -> helperLineObject.dispose() }
helperLineShapes.clear()
}

private fun addNewHelperLineShape(terrainComponent: TerrainComponent) {
val helperLineShape : HelperLineShape
if (type == HelperLineType.RECTANGLE) {
helperLineShape = RectangleHelperLineShape(width, terrainComponent)
} else {
helperLineShape = HexagonHelperLineShape(width, terrainComponent)
}
helperLineShapes.add(helperLineShape)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2023. See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mbrlabs.mundus.editor.core.helperlines

import com.mbrlabs.mundus.commons.scene3d.components.TerrainComponent
import com.mbrlabs.mundus.commons.terrain.Terrain

class HexagonHelperLineShape(width: Int, terrainComponent: TerrainComponent) : HelperLineShape(width, terrainComponent) {

enum class Vector {
BOTTOM_RIGHT,
TOP_RIGHT,
RIGHT,
}

companion object {
val PATTERN = arrayOf(
arrayOf(arrayOf(Vector.TOP_RIGHT, Vector.BOTTOM_RIGHT), arrayOf(), arrayOf(), arrayOf(Vector.RIGHT)),
arrayOf(arrayOf(), arrayOf(Vector.RIGHT), arrayOf(Vector.TOP_RIGHT, Vector.BOTTOM_RIGHT), arrayOf() )
)
}

override fun calculateIndicesNum(width: Int, terrain: Terrain): Int {
var i = 0
calculate(width, terrain.vertexResolution) { ++i }

return i
}

override fun fillIndices(width: Int, indices: ShortArray, vertexResolution: Int) {
var i = 0
calculate(width, vertexResolution) {pos -> indices[i++] = pos}
}

private fun calculate(width: Int, vertexResolution: Int, method: (pos: Short) -> Unit) {
var patternY = 0

for (y in 0 until vertexResolution + width step width) {
var patternX = 0
for (x in 0 until vertexResolution step width) {
val mainCurrent = y * vertexResolution + x

for (pattern in PATTERN.get(patternY).get(patternX)) {
var current = mainCurrent
for (w in 0 until width) {
val next = getNext(current, pattern, vertexResolution)

if (isOnMap(current, vertexResolution) && isOk(current, next, vertexResolution, pattern) && isOnMap(next, vertexResolution)) {
method.invoke(current.toShort())
method.invoke(next.toShort())
} else if (!isOk(current, next, vertexResolution, pattern)) {
break
}

current = next

}
}

patternX = ++patternX % PATTERN.get(patternY).size
}

patternY = ++patternY % PATTERN.size
}
}

private fun getNext(current: Int, vector: Vector, vertexResolution: Int): Int {
return when(vector) {
Vector.BOTTOM_RIGHT -> current + vertexResolution + 1
Vector.TOP_RIGHT -> current - vertexResolution + 1
Vector.RIGHT -> current + 1
}
}

private fun isOnMap(current: Int, vertexResolution: Int): Boolean = getRow(current, vertexResolution) in 0 until vertexResolution

private fun isOk(current: Int, next: Int, vertexResolution: Int, pattern: Vector): Boolean {
val currentRow = getRow(current, vertexResolution)
val nextRow = getRow(next, vertexResolution)

return when(pattern) {
Vector.BOTTOM_RIGHT -> currentRow + 1 == nextRow
Vector.TOP_RIGHT -> currentRow == nextRow + 1
Vector.RIGHT -> currentRow == nextRow
}
}

private fun getRow(cell: Int, vertexResolution: Int) = cell / vertexResolution
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023. See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mbrlabs.mundus.editor.core.helperlines

import com.mbrlabs.mundus.commons.scene3d.components.TerrainComponent
import com.mbrlabs.mundus.commons.terrain.Terrain

class RectangleHelperLineShape(width: Int,
terrainComponent: TerrainComponent) : HelperLineShape(width, terrainComponent) {

override fun calculateIndicesNum(width: Int, terrain: Terrain): Int {
val vertexResolution = terrain.vertexResolution

return vertexResolution * 2 * ((vertexResolution / width) + 1) * 2
}

override fun fillIndices(width: Int, indices: ShortArray, vertexResolution: Int) {
var i = -1
for (y in 0 until vertexResolution step width) {
for (x in 0 until vertexResolution - 1) {
val current = y * vertexResolution + x
val next = current + 1

indices[++i] = current.toShort()
indices[++i] = next.toShort()
}
}
for (y in 0 until vertexResolution step width) {
for (x in 0 until vertexResolution - 1) {
val current = y + vertexResolution * x
val next = current + vertexResolution

indices[++i] = current.toShort()
indices[++i] = next.toShort()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.mbrlabs.mundus.editor.Mundus;
import com.mbrlabs.mundus.editor.assets.EditorAssetManager;
import com.mbrlabs.mundus.editor.core.EditorScene;
import com.mbrlabs.mundus.editor.core.helperlines.HelperLines;
import com.mbrlabs.mundus.editor.preferences.MundusPreferencesManager;
import com.mbrlabs.mundus.editor.utils.Log;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -54,6 +56,8 @@ public class ProjectContext implements Disposable {
public boolean renderDebug = false;
public boolean renderWireframe = false;

public HelperLines helperLines;

private int idProvider;

/** set by kryo when project is loaded. do not use this */
Expand All @@ -63,7 +67,10 @@ public ProjectContext(int idProvider) {
scenes = new Array<>();
settings = new ProjectSettings();
currScene = new EditorScene();
helperLines = new HelperLines();
this.idProvider = idProvider;

Mundus.INSTANCE.registerEventListener(helperLines);
}

public synchronized int obtainID() {
Expand All @@ -81,6 +88,7 @@ public void dispose() {
if (assetManager != null) {
assetManager.dispose();
}
Mundus.INSTANCE.unregisterEventListener(helperLines);
}

/**
Expand Down
Loading