Skip to content

Commit b74c35c

Browse files
committed
fixed terrain editing.
1 parent 6d0ccb5 commit b74c35c

File tree

12 files changed

+103
-61
lines changed

12 files changed

+103
-61
lines changed

src/main/java/com/ss/editor/control/painting/impl/AbstractPaintingControl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import com.jme3.scene.Node;
99
import com.jme3.scene.Spatial;
1010
import com.jme3.scene.control.AbstractControl;
11+
import com.ss.editor.annotation.FromAnyThread;
1112
import com.ss.editor.annotation.JmeThread;
1213
import com.ss.editor.control.painting.PaintingControl;
1314
import com.ss.editor.control.painting.PaintingInput;
1415
import com.ss.editor.util.EditorUtil;
16+
import com.ss.editor.util.LocalObjects;
1517
import org.jetbrains.annotations.NotNull;
1618
import org.jetbrains.annotations.Nullable;
1719

@@ -22,6 +24,9 @@
2224
*/
2325
public class AbstractPaintingControl extends AbstractControl implements PaintingControl {
2426

27+
@NotNull
28+
private static final ThreadLocal<LocalObjects> TOOL_OBJECTS = ThreadLocal.withInitial(LocalObjects::new);
29+
2530
/**
2631
* The current painting input.
2732
*/
@@ -33,6 +38,16 @@ public class AbstractPaintingControl extends AbstractControl implements Painting
3338
*/
3439
private boolean painting;
3540

41+
/**
42+
* Get the local objects.
43+
*
44+
* @return the local objects.
45+
*/
46+
@FromAnyThread
47+
protected @NotNull LocalObjects getLocalObjects() {
48+
return TOOL_OBJECTS.get();
49+
}
50+
3651
@Override
3752
@JmeThread
3853
public void setSpatial(@Nullable final Spatial spatial) {
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package com.ss.editor.ui.component.editing.terrain.control;
1+
package com.ss.editor.control.painting.terrain;
22

33
import static com.ss.rlib.util.ObjectUtils.notNull;
44
import com.jme3.math.Vector2f;
55
import com.jme3.math.Vector3f;
66
import com.jme3.scene.Node;
77
import com.jme3.scene.Spatial;
88
import com.jme3.terrain.Terrain;
9+
import com.ss.editor.annotation.JmeThread;
910
import com.ss.editor.model.undo.editor.ChangeConsumer;
1011
import com.ss.editor.model.undo.editor.ModelChangeConsumer;
1112
import com.ss.editor.ui.component.editing.terrain.TerrainEditingComponent;
@@ -70,26 +71,25 @@ public int hashCode() {
7071
@Nullable
7172
private Spatial copiedTerrain;
7273

73-
/**
74-
* Instantiates a new Change height terrain tool control.
75-
*
76-
* @param component the component
77-
*/
7874
public ChangeHeightTerrainToolControl(@NotNull final TerrainEditingComponent component) {
7975
super(component);
8076
this.originalHeight = DictionaryFactory.newObjectDictionary(0.2F, 1000);
8177
}
8278

8379
/**
80+
* Get the table of original heights.
81+
*
8482
* @return the table of original heights.
8583
*/
84+
@JmeThread
8685
private @NotNull ObjectDictionary<HeightPoint, Float> getOriginalHeight() {
8786
return originalHeight;
8887
}
8988

9089
/**
9190
* Start making changes.
9291
*/
92+
@JmeThread
9393
protected void startChange() {
9494

9595
final ObjectDictionary<HeightPoint, Float> originalHeight = getOriginalHeight();
@@ -103,6 +103,7 @@ protected void startChange() {
103103
*
104104
* @param point the point.
105105
*/
106+
@JmeThread
106107
protected void change(@NotNull final Vector2f point) {
107108

108109
final Terrain terrain = (Terrain) notNull(copiedTerrain);
@@ -116,7 +117,9 @@ protected void change(@NotNull final Vector2f point) {
116117
final HeightPoint heightPoint = new HeightPoint(point.getX(), point.getY(), x, z);
117118

118119
final ObjectDictionary<HeightPoint, Float> originalHeight = getOriginalHeight();
119-
if(originalHeight.containsKey(heightPoint)) return;
120+
if(originalHeight.containsKey(heightPoint)) {
121+
return;
122+
}
120123

121124
final float height = terrain.getHeightmapHeight(point);
122125

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ss.editor.ui.component.editing.terrain.control;
1+
package com.ss.editor.control.painting.terrain;
22

33
import static com.ss.editor.util.EditingUtils.*;
44
import static com.ss.rlib.util.ObjectUtils.notNull;
@@ -10,6 +10,8 @@
1010
import com.jme3.scene.Spatial;
1111
import com.jme3.scene.shape.Sphere;
1212
import com.jme3.terrain.Terrain;
13+
import com.ss.editor.annotation.FromAnyThread;
14+
import com.ss.editor.annotation.JmeThread;
1315
import com.ss.editor.control.painting.PaintingInput;
1416
import com.ss.editor.ui.component.editing.terrain.TerrainEditingComponent;
1517
import com.ss.editor.util.LocalObjects;
@@ -87,12 +89,14 @@ protected void controlUpdate(final float tpf) {
8789
levelMarker.setCullHint(isUseMarker() ? Spatial.CullHint.Never : Spatial.CullHint.Always);
8890
}
8991

92+
@FromAnyThread
9093
@NotNull
9194
@Override
9295
protected ColorRGBA getBrushColor() {
9396
return ColorRGBA.Red;
9497
}
9598

99+
@JmeThread
96100
@Override
97101
public void startPainting(@NotNull final PaintingInput paintingInput, @NotNull final Vector3f contactPoint) {
98102
super.startPainting(paintingInput, contactPoint);
@@ -153,7 +157,7 @@ public void finishPainting(@NotNull final Vector3f contactPoint) {
153157
*/
154158
private void modifyHeight(@NotNull final Vector3f contactPoint) {
155159

156-
final LocalObjects local = LocalObjects.get();
160+
final LocalObjects local = getLocalObjects();
157161
final Node terrainNode = (Node) notNull(getPaintedModel());
158162
final Geometry levelMarker = getLevelMarker();
159163

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ss.editor.ui.component.editing.terrain.control;
1+
package com.ss.editor.control.painting.terrain;
22

33
import static com.ss.rlib.util.ObjectUtils.notNull;
44
import com.jme3.math.ColorRGBA;
@@ -9,6 +9,8 @@
99
import com.jme3.texture.Image;
1010
import com.jme3.texture.Texture;
1111
import com.jme3.util.BufferUtils;
12+
import com.ss.editor.annotation.FromAnyThread;
13+
import com.ss.editor.annotation.JmeThread;
1214
import com.ss.editor.control.painting.PaintingInput;
1315
import com.ss.editor.model.undo.editor.ChangeConsumer;
1416
import com.ss.editor.model.undo.editor.ModelChangeConsumer;
@@ -129,11 +131,13 @@ public PaintTerrainToolControl(@NotNull final TerrainEditingComponent component)
129131
this.colorPoints = ArrayFactory.newArray(ColorPoint.class);
130132
}
131133

134+
@FromAnyThread
132135
@Override
133136
protected @NotNull ColorRGBA getBrushColor() {
134137
return ColorRGBA.Blue;
135138
}
136139

140+
@JmeThread
137141
@Override
138142
public void startPainting(@NotNull final PaintingInput paintingInput, @NotNull final Vector3f contactPoint) {
139143

@@ -395,7 +399,7 @@ private void paintTexture(@NotNull final PaintingInput paintingInput, @NotNull f
395399
final Texture alphaTexture = getAlphaTexture();
396400
if (alphaTexture == null) return;
397401

398-
final LocalObjects local = LocalObjects.get();
402+
final LocalObjects local = getLocalObjects();
399403
final Spatial terrainNode = notNull(getPaintedModel());
400404
final Terrain terrain = (Terrain) terrainNode;
401405
final Image image = alphaTexture.getImage();
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ss.editor.ui.component.editing.terrain.control;
1+
package com.ss.editor.control.painting.terrain;
22

33
import static com.ss.editor.util.EditingUtils.calculateHeight;
44
import static com.ss.editor.util.EditingUtils.isContains;
@@ -9,6 +9,8 @@
99
import com.jme3.scene.Geometry;
1010
import com.jme3.scene.Node;
1111
import com.jme3.terrain.Terrain;
12+
import com.ss.editor.annotation.FromAnyThread;
13+
import com.ss.editor.annotation.JmeThread;
1214
import com.ss.editor.control.painting.PaintingInput;
1315
import com.ss.editor.ui.component.editing.terrain.TerrainEditingComponent;
1416
import com.ss.editor.util.LocalObjects;
@@ -24,30 +26,26 @@
2426
*/
2527
public class RaiseLowerTerrainToolControl extends ChangeHeightTerrainToolControl {
2628

27-
/**
28-
* Instantiates a new Raise lower terrain tool control.
29-
*
30-
* @param component the component
31-
*/
3229
public RaiseLowerTerrainToolControl(@NotNull final TerrainEditingComponent component) {
3330
super(component);
3431
}
3532

36-
@NotNull
3733
@Override
38-
protected ColorRGBA getBrushColor() {
34+
@FromAnyThread
35+
protected @NotNull ColorRGBA getBrushColor() {
3936
return ColorRGBA.Green;
4037
}
4138

4239
@Override
43-
public void startPainting(@NotNull final PaintingInput paintingInput, @NotNull final Vector3f contactPoint) {
44-
super.startPainting(paintingInput, contactPoint);
40+
@JmeThread
41+
public void startPainting(@NotNull final PaintingInput input, @NotNull final Vector3f contactPoint) {
42+
super.startPainting(input, contactPoint);
4543

46-
switch (paintingInput) {
44+
switch (input) {
4745
case MOUSE_PRIMARY:
4846
case MOUSE_SECONDARY: {
4947
startChange();
50-
modifyHeight(paintingInput, contactPoint);
48+
modifyHeight(input, contactPoint);
5149
}
5250
}
5351
}
@@ -83,12 +81,12 @@ public void finishPainting(@NotNull final Vector3f contactPoint) {
8381
/**
8482
* Modify height of terrain points.
8583
*
86-
* @param paintingInput the type of input.
84+
* @param input the type of input.
8785
* @param contactPoint the contact point.
8886
*/
89-
private void modifyHeight(@NotNull final PaintingInput paintingInput, @NotNull final Vector3f contactPoint) {
87+
private void modifyHeight(@NotNull final PaintingInput input, @NotNull final Vector3f contactPoint) {
9088

91-
final LocalObjects local = LocalObjects.get();
89+
final LocalObjects local = getLocalObjects();
9290
final Node terrainNode = (Node) notNull(getPaintedModel());
9391

9492
final Vector3f worldTranslation = terrainNode.getWorldTranslation();
@@ -101,7 +99,7 @@ private void modifyHeight(@NotNull final PaintingInput paintingInput, @NotNull f
10199
final Geometry brush = getBrush();
102100

103101
final float brushSize = getBrushSize();
104-
final float brushPower = paintingInput == PaintingInput.MOUSE_PRIMARY ? getBrushPower() : getBrushPower() * -1F;
102+
final float brushPower = input == PaintingInput.MOUSE_PRIMARY ? getBrushPower() : getBrushPower() * -1F;
105103

106104
final int radiusStepsX = (int) (brushSize / localScale.getX());
107105
final int radiusStepsZ = (int) (brushSize / localScale.getY());
@@ -126,7 +124,12 @@ private void modifyHeight(@NotNull final PaintingInput paintingInput, @NotNull f
126124

127125
terrainLoc.set(locX, locZ);
128126

129-
final float currentHeight = terrain.getHeightmapHeight(terrainLoc) * localScale.getY();
127+
final float heightmapHeight = terrain.getHeightmapHeight(terrainLoc);
128+
if (Float.isNaN(heightmapHeight)) {
129+
continue;
130+
}
131+
132+
final float currentHeight = heightmapHeight * localScale.getY();
130133
// adjust height based on radius of the tool
131134
final float newHeight = calculateHeight(brushSize, brushPower, effectPoint.getX(), effectPoint.getY());
132135

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ss.editor.ui.component.editing.terrain.control;
1+
package com.ss.editor.control.painting.terrain;
22

33
import static com.ss.editor.util.EditingUtils.isContains;
44
import static com.ss.rlib.util.ObjectUtils.notNull;
@@ -19,6 +19,8 @@
1919
import com.jme3.terrain.noise.filter.SmoothFilter;
2020
import com.jme3.terrain.noise.fractal.FractalSum;
2121
import com.jme3.terrain.noise.modulator.NoiseModulator;
22+
import com.ss.editor.annotation.FromAnyThread;
23+
import com.ss.editor.annotation.JmeThread;
2224
import com.ss.editor.control.painting.PaintingInput;
2325
import com.ss.editor.ui.component.editing.terrain.TerrainEditingComponent;
2426
import com.ss.editor.util.LocalObjects;
@@ -50,12 +52,14 @@ public RoughTerrainToolControl(@NotNull final TerrainEditingComponent component)
5052
super(component);
5153
}
5254

55+
@FromAnyThread
5356
@NotNull
5457
@Override
5558
protected ColorRGBA getBrushColor() {
5659
return ColorRGBA.Magenta;
5760
}
5861

62+
@JmeThread
5963
@Override
6064
public void startPainting(@NotNull final PaintingInput paintingInput, @NotNull final Vector3f contactPoint) {
6165
super.startPainting(paintingInput, contactPoint);
@@ -101,7 +105,7 @@ public void finishPainting(@NotNull final Vector3f contactPoint) {
101105
*/
102106
private void modifyHeight(@NotNull final Vector3f contactPoint) {
103107

104-
final LocalObjects local = LocalObjects.get();
108+
final LocalObjects local = getLocalObjects();
105109
final Node terrainNode = (Node) notNull(getPaintedModel());
106110

107111
final Vector3f worldTranslation = terrainNode.getWorldTranslation();
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ss.editor.ui.component.editing.terrain.control;
1+
package com.ss.editor.control.painting.terrain;
22

33
import static com.ss.editor.util.EditingUtils.*;
44
import static com.ss.rlib.util.ObjectUtils.notNull;
@@ -13,6 +13,8 @@
1313
import com.jme3.scene.shape.Line;
1414
import com.jme3.scene.shape.Sphere;
1515
import com.jme3.terrain.Terrain;
16+
import com.ss.editor.annotation.FromAnyThread;
17+
import com.ss.editor.annotation.JmeThread;
1618
import com.ss.editor.control.painting.PaintingInput;
1719
import com.ss.editor.ui.component.editing.terrain.TerrainEditingComponent;
1820
import com.ss.editor.util.LocalObjects;
@@ -72,6 +74,7 @@ public SlopeTerrainToolControl(@NotNull final TerrainEditingComponent component)
7274
this.line.setMaterial(createColoredMaterial(ColorRGBA.White));
7375
}
7476

77+
@FromAnyThread
7578
@NotNull
7679
@Override
7780
protected ColorRGBA getBrushColor() {
@@ -125,6 +128,7 @@ protected void controlUpdate(final float tpf) {
125128
mesh.updatePoints(firstPoint, secondPoint);
126129
}
127130

131+
@JmeThread
128132
@Override
129133
public void startPainting(@NotNull final PaintingInput paintingInput, @NotNull final Vector3f contactPoint) {
130134
super.startPainting(paintingInput, contactPoint);
@@ -197,7 +201,7 @@ public void finishPainting(@NotNull final Vector3f contactPoint) {
197201
*/
198202
private void modifyHeight(@NotNull final Vector3f contactPoint) {
199203

200-
final LocalObjects local = LocalObjects.get();
204+
final LocalObjects local = getLocalObjects();
201205
final Node terrainNode = (Node) notNull(getPaintedModel());
202206
final Geometry baseMarker = getBaseMarker();
203207
final Geometry targetMarker = getTargetMarker();
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ss.editor.ui.component.editing.terrain.control;
1+
package com.ss.editor.control.painting.terrain;
22

33
import static com.ss.editor.util.EditingUtils.isContains;
44
import static com.ss.rlib.util.ObjectUtils.notNull;
@@ -10,6 +10,8 @@
1010
import com.jme3.scene.Geometry;
1111
import com.jme3.scene.Node;
1212
import com.jme3.terrain.Terrain;
13+
import com.ss.editor.annotation.FromAnyThread;
14+
import com.ss.editor.annotation.JmeThread;
1315
import com.ss.editor.control.painting.PaintingInput;
1416
import com.ss.editor.ui.component.editing.terrain.TerrainEditingComponent;
1517
import com.ss.editor.util.LocalObjects;
@@ -34,12 +36,14 @@ public SmoothTerrainToolControl(@NotNull final TerrainEditingComponent component
3436
super(component);
3537
}
3638

39+
@FromAnyThread
3740
@NotNull
3841
@Override
3942
protected ColorRGBA getBrushColor() {
4043
return ColorRGBA.Yellow;
4144
}
4245

46+
@JmeThread
4347
@Override
4448
public void startPainting(@NotNull final PaintingInput paintingInput, @NotNull final Vector3f contactPoint) {
4549
super.startPainting(paintingInput, contactPoint);
@@ -85,7 +89,7 @@ public void finishPainting(@NotNull final Vector3f contactPoint) {
8589
*/
8690
private void modifyHeight(@NotNull final Vector3f contactPoint) {
8791

88-
final LocalObjects local = LocalObjects.get();
92+
final LocalObjects local = getLocalObjects();
8993
final Node terrainNode = (Node) notNull(getPaintedModel());
9094

9195
final Vector3f localScale = terrainNode.getLocalScale();

0 commit comments

Comments
 (0)