|
| 1 | + /* |
| 2 | + * Copyright (c) 2009-2025 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.environment.util; |
| 33 | + |
| 34 | +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; |
| 39 | +import com.jme3.material.Material; |
| 40 | +import com.jme3.material.RenderState; |
| 41 | +import com.jme3.math.ColorRGBA; |
| 42 | +import com.jme3.math.FastMath; |
| 43 | +import com.jme3.renderer.queue.RenderQueue; |
| 44 | +import com.jme3.scene.Geometry; |
| 45 | +import com.jme3.scene.Mesh; |
| 46 | +import com.jme3.scene.VertexBuffer.Type; |
| 47 | +import com.jme3.util.BufferUtils; |
| 48 | + |
| 49 | +import java.io.IOException; |
| 50 | +import java.nio.FloatBuffer; |
| 51 | +import java.nio.ShortBuffer; |
| 52 | + |
| 53 | +/** |
| 54 | + * <p>A `Circle` is a 2D mesh representing a circular outline (wireframe). |
| 55 | + * It's defined by a specified number of radial samples, which determine its smoothness.</p> |
| 56 | + * |
| 57 | + * <p>The circle is centered at (0,0,0) in its local coordinate space and has a radius of 1.0.</p> |
| 58 | + * |
| 59 | + * @author capdevon |
| 60 | + */ |
| 61 | +public class Circle extends Mesh { |
| 62 | + |
| 63 | + // The number of segments used to approximate the circle. |
| 64 | + protected int radialSamples = 256; |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates a new `Circle` mesh. |
| 68 | + */ |
| 69 | + public Circle() { |
| 70 | + setGeometryData(); |
| 71 | + setIndexData(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Initializes the vertex buffers for the circle mesh. |
| 76 | + */ |
| 77 | + private void setGeometryData() { |
| 78 | + |
| 79 | + int numVertices = radialSamples + 1; |
| 80 | + |
| 81 | + FloatBuffer posBuf = BufferUtils.createVector3Buffer(numVertices); |
| 82 | + FloatBuffer colBuf = BufferUtils.createFloatBuffer(numVertices * 4); |
| 83 | + FloatBuffer texBuf = BufferUtils.createVector2Buffer(numVertices); |
| 84 | + |
| 85 | + // --- Generate Geometry Data --- |
| 86 | + float angleStep = FastMath.TWO_PI / radialSamples; |
| 87 | + |
| 88 | + // Define the color for the entire circle. |
| 89 | + ColorRGBA color = ColorRGBA.Orange; |
| 90 | + |
| 91 | + // Populate the position, color, and texture coordinate buffers. |
| 92 | + for (int i = 0; i < numVertices; i++) { |
| 93 | + float angle = angleStep * i; |
| 94 | + float cos = FastMath.cos(angle); |
| 95 | + float sin = FastMath.sin(angle); |
| 96 | + |
| 97 | + posBuf.put(cos).put(sin).put(0); |
| 98 | + colBuf.put(color.r).put(color.g).put(color.b).put(color.a); |
| 99 | + texBuf.put(i % 2f).put(i % 2f); |
| 100 | + } |
| 101 | + |
| 102 | + setBuffer(Type.Position, 3, posBuf); |
| 103 | + setBuffer(Type.Color, 4, colBuf); |
| 104 | + setBuffer(Type.TexCoord, 2, texBuf); |
| 105 | + |
| 106 | + setMode(Mode.Lines); |
| 107 | + updateBound(); |
| 108 | + setStatic(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Initializes the index buffer for the circle mesh. |
| 113 | + */ |
| 114 | + private void setIndexData() { |
| 115 | + // allocate connectivity |
| 116 | + int numIndices = radialSamples * 2; |
| 117 | + |
| 118 | + ShortBuffer idxBuf = BufferUtils.createShortBuffer(numIndices); |
| 119 | + setBuffer(Type.Index, 2, idxBuf); |
| 120 | + |
| 121 | + // --- Generate Index Data --- |
| 122 | + for (int i = 0; i < radialSamples; i++) { |
| 123 | + idxBuf.put((short) i); // Start of segment |
| 124 | + idxBuf.put((short) (i + 1)); // End of segment |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Creates a {@link Geometry} object representing a dashed wireframe circle. |
| 130 | + * |
| 131 | + * @param assetManager The application's AssetManager to load materials. |
| 132 | + * @param name The desired name for the Geometry. |
| 133 | + * @return A new Geometry instance with a `Circle` mesh. |
| 134 | + */ |
| 135 | + public static Geometry createShape(AssetManager assetManager, String name) { |
| 136 | + Circle mesh = new Circle(); |
| 137 | + Geometry geom = new Geometry(name, mesh); |
| 138 | + geom.setQueueBucket(RenderQueue.Bucket.Transparent); |
| 139 | + |
| 140 | + Material mat = new Material(assetManager, "Common/MatDefs/Misc/Dashed.j3md"); |
| 141 | + mat.getAdditionalRenderState().setWireframe(true); |
| 142 | + mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); |
| 143 | + mat.getAdditionalRenderState().setDepthWrite(false); |
| 144 | + mat.getAdditionalRenderState().setDepthTest(false); |
| 145 | + mat.getAdditionalRenderState().setLineWidth(2f); |
| 146 | + mat.setColor("Color", ColorRGBA.Orange); |
| 147 | + mat.setFloat("DashSize", 0.5f); |
| 148 | + geom.setMaterial(mat); |
| 149 | + |
| 150 | + return geom; |
| 151 | + } |
| 152 | + |
| 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 | + |
| 167 | +} |
0 commit comments