Skip to content

Commit bae0784

Browse files
authored
Circle: add read/write methods
1 parent bb18432 commit bae0784

File tree

1 file changed

+30
-45
lines changed
  • jme3-core/src/main/java/com/jme3/environment/util

1 file changed

+30
-45
lines changed

jme3-core/src/main/java/com/jme3/environment/util/Circle.java

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
package com.jme3.environment.util;
3333

3434
import com.jme3.asset.AssetManager;
35+
import com.jme3.export.InputCapsule;
36+
import com.jme3.export.JmeExporter;
37+
import com.jme3.export.JmeImporter;
38+
import com.jme3.export.OutputCapsule;
3539
import com.jme3.material.Material;
3640
import com.jme3.material.RenderState;
3741
import com.jme3.math.ColorRGBA;
@@ -42,6 +46,7 @@
4246
import com.jme3.scene.VertexBuffer.Type;
4347
import com.jme3.util.BufferUtils;
4448

49+
import java.io.IOException;
4550
import java.nio.FloatBuffer;
4651
import java.nio.ShortBuffer;
4752

@@ -66,22 +71,6 @@ public Circle() {
6671
setIndexData();
6772
}
6873

69-
/**
70-
* Creates a new `Circle` mesh with the specified number of radial samples.
71-
* The circle will be centered at (0,0,0) with a radius of 1.0.
72-
*
73-
* @param radialSamples The number of segments (vertices) to use for the circle's perimeter.
74-
* @throws IllegalArgumentException if `radialSamples` is less than 2.
75-
*/
76-
public Circle(int radialSamples) {
77-
if (radialSamples < 2) {
78-
throw new IllegalArgumentException("radialSamples must be at least 2 for a valid circle.");
79-
}
80-
this.radialSamples = radialSamples;
81-
setGeometryData();
82-
setIndexData();
83-
}
84-
8574
/**
8675
* Initializes the vertex buffers for the circle mesh.
8776
*/
@@ -91,35 +80,30 @@ private void setGeometryData() {
9180
int numVertices = radialSamples + 1;
9281

9382
FloatBuffer posBuf = BufferUtils.createVector3Buffer(numVertices);
94-
FloatBuffer colBuf = BufferUtils.createFloatBuffer(numVertices * 4); // 4 floats per color (RGBA)
83+
FloatBuffer colBuf = BufferUtils.createFloatBuffer(numVertices * 4);
9584
FloatBuffer texBuf = BufferUtils.createVector2Buffer(numVertices);
9685

97-
setBuffer(Type.Position, 3, posBuf);
98-
setBuffer(Type.Color, 4, colBuf);
99-
setBuffer(Type.TexCoord, 2, texBuf);
100-
10186
// --- Generate Geometry Data ---
102-
float[] sin = new float[numVertices];
103-
float[] cos = new float[numVertices];
10487
float angleStep = FastMath.TWO_PI / radialSamples;
10588

106-
// Populate sine and cosine arrays for all sample points.
107-
for (int i = 0; i <= radialSamples; i++) {
108-
float angle = angleStep * i;
109-
cos[i] = FastMath.cos(angle);
110-
sin[i] = FastMath.sin(angle);
111-
}
112-
11389
// Define the color for the entire circle.
11490
ColorRGBA color = ColorRGBA.Orange;
11591

11692
// Populate the position, color, and texture coordinate buffers.
11793
for (int i = 0; i < numVertices; i++) {
118-
posBuf.put(cos[i]).put(sin[i]).put(0);
94+
float angle = angleStep * i;
95+
float cos = FastMath.cos(angle);
96+
float sin = FastMath.sin(angle);
97+
98+
posBuf.put(cos).put(sin).put(0);
11999
colBuf.put(color.r).put(color.g).put(color.b).put(color.a);
120100
texBuf.put(i % 2f).put(i % 2f);
121101
}
122102

103+
setBuffer(Type.Position, 3, posBuf);
104+
setBuffer(Type.Color, 4, colBuf);
105+
setBuffer(Type.TexCoord, 2, texBuf);
106+
123107
updateBound();
124108
setStatic();
125109
}
@@ -149,20 +133,7 @@ private void setIndexData() {
149133
* @return A new Geometry instance with a `Circle` mesh.
150134
*/
151135
public static Geometry createShape(AssetManager assetManager, String name) {
152-
return createShape(assetManager, name, 256);
153-
}
154-
155-
/**
156-
* Creates a {@link Geometry} object representing a dashed wireframe circle
157-
* with a specified number of radial samples.
158-
*
159-
* @param assetManager The application's AssetManager to load materials.
160-
* @param name The desired name for the Geometry.
161-
* @param radialSamples The number of segments to use for the circle's perimeter.
162-
* @return A new Geometry instance with a `Circle` mesh.
163-
*/
164-
public static Geometry createShape(AssetManager assetManager, String name, int radialSamples) {
165-
Circle mesh = new Circle(radialSamples);
136+
Circle mesh = new Circle();
166137
Geometry geom = new Geometry(name, mesh);
167138
geom.setQueueBucket(RenderQueue.Bucket.Transparent);
168139

@@ -179,4 +150,18 @@ public static Geometry createShape(AssetManager assetManager, String name, int r
179150
return geom;
180151
}
181152

153+
@Override
154+
public void write(JmeExporter ex) throws IOException {
155+
super.write(ex);
156+
OutputCapsule oc = ex.getCapsule(this);
157+
oc.write(radialSamples, "radialSamples", 256);
158+
}
159+
160+
@Override
161+
public void read(JmeImporter im) throws IOException {
162+
super.read(im);
163+
InputCapsule ic = im.getCapsule(this);
164+
radialSamples = ic.readInt("radialSamples", 256);
165+
}
166+
182167
}

0 commit comments

Comments
 (0)