Class Mesh

java.lang.Object
com.jme3.scene.Mesh
All Implemented Interfaces:
Savable, JmeCloneable, Cloneable
Direct Known Subclasses:
AbstractBox, ArmatureInterJointsWire, Arrow, BoundingSphereDebug, CenterQuad, CenterQuad, Curve, Cylinder, Dome, Grid, JointShape, Line, ParticleMesh, PQTorus, Quad, RectangleMesh, SkeletonInterBoneWire, SkeletonPoints, SkeletonWire, Sphere, Surface, Torus, WireBox, WireFrustum, WireSphere

public class Mesh extends Object implements Savable, Cloneable, JmeCloneable
Mesh is used to store rendering data.

All visible elements in a scene are represented by meshes. Meshes may contain three types of geometric primitives:

  • Constructor Details

  • Method Details

    • clone

      public Mesh clone()
      Create a shallow clone of this Mesh. The vertex buffers are shared between this and the clone mesh, the rest of the data is cloned.
      Overrides:
      clone in class Object
      Returns:
      A shallow clone of the mesh
    • deepClone

      public Mesh deepClone()
      Creates a deep clone of this mesh. The vertex buffers and the data inside them is cloned.
      Returns:
      a deep clone of this mesh.
    • cloneForAnim

      public Mesh cloneForAnim()
      Clone the mesh for animation use. This creates a shallow clone of the mesh, sharing most of the vertex buffer data, however the VertexBuffer.Type.Position, VertexBuffer.Type.Normal, and VertexBuffer.Type.Tangent buffers are deeply cloned.
      Returns:
      A clone of the mesh for animation use.
    • jmeClone

      public Mesh jmeClone()
      Called internally by com.jme3.util.clone.Cloner. Do not call directly.
      Specified by:
      jmeClone in interface JmeCloneable
      Returns:
      a new instance
    • cloneFields

      public void cloneFields(Cloner cloner, Object original)
      Called internally by com.jme3.util.clone.Cloner. Do not call directly.
      Specified by:
      cloneFields in interface JmeCloneable
      Parameters:
      cloner - The cloner that is performing the cloning operation. The cloneFields method can call back into the cloner to make clones of its subordinate fields.
      original - The original object from which this object was cloned. This is provided for the very rare case that this object needs to refer to its original for some reason. In general, all of the relevant values should have been transferred during the shallow clone, and this object need only clone what it wants.
    • generateBindPose

      @Deprecated public void generateBindPose(boolean forSoftwareAnim)
      Deprecated.
      use generateBindPose();
      Parameters:
      forSoftwareAnim - ignored
    • generateBindPose

      public void generateBindPose()
      Generates the VertexBuffer.Type.BindPosePosition, VertexBuffer.Type.BindPoseNormal, and VertexBuffer.Type.BindPoseTangent buffers for this mesh by duplicating them based on the position and normal buffers already set on the mesh. This method does nothing if the mesh has no bone weight or index buffers.
    • prepareForAnim

      public void prepareForAnim(boolean forSoftwareAnim)
      Prepares the mesh for software skinning by converting the bone index and weight buffers to heap buffers.
      Parameters:
      forSoftwareAnim - Should be true to enable the conversion.
    • setLodLevels

      public void setLodLevels(VertexBuffer[] lodLevels)
      Set the LOD (level of detail) index buffers on this mesh.
      Parameters:
      lodLevels - The LOD levels to set
    • getNumLodLevels

      public int getNumLodLevels()
      Returns:
      The number of LOD levels set on this mesh, including the main index buffer, returns zero if there are no lod levels.
    • getLodLevel

      public VertexBuffer getLodLevel(int lod)
      Returns the lod level at the given index.
      Parameters:
      lod - The lod level index, this does not include the main index buffer.
      Returns:
      The LOD index buffer at the index
      Throws:
      IndexOutOfBoundsException - If the index is outside of the range [0, getNumLodLevels()].
      See Also:
    • getElementLengths

      public int[] getElementLengths()
      Get the element lengths for Mesh.Mode.Hybrid mesh mode.
      Returns:
      element lengths
    • setElementLengths

      public void setElementLengths(int[] elementLengths)
      Set the element lengths for Mesh.Mode.Hybrid mesh mode.
      Parameters:
      elementLengths - The element lengths to set
    • getModeStart

      public int[] getModeStart()
      Set the mode start indices for Mesh.Mode.Hybrid mesh mode.
      Returns:
      mode start indices
    • setModeStart

      public void setModeStart(int[] modeStart)
      Get the mode start indices for Mesh.Mode.Hybrid mesh mode.
      Parameters:
      modeStart - the pre-existing array
    • getMode

      public Mesh.Mode getMode()
      Returns the mesh mode
      Returns:
      the mesh mode
      See Also:
    • setMode

      public void setMode(Mesh.Mode mode)
      Change the Mesh's mode. By default the mode is Mesh.Mode.Triangles.
      Parameters:
      mode - The new mode to set
      See Also:
    • getMaxNumWeights

      public int getMaxNumWeights()
      Returns the maximum number of weights per vertex on this mesh.
      Returns:
      maximum number of weights per vertex
      See Also:
    • setMaxNumWeights

      public void setMaxNumWeights(int maxNumWeights)
      Set the maximum number of weights per vertex on this mesh. Only relevant if this mesh has bone index/weight buffers. This value should be between 0 and 4.
      Parameters:
      maxNumWeights - the desired number (between 0 and 4, inclusive)
    • getPointSize

      @Deprecated public float getPointSize()
      Deprecated.
      Always returns 1.0 since point size is determined in the vertex shader.
      Returns:
      1.0
    • getLineWidth

      @Deprecated public float getLineWidth()
      Returns the line width for line meshes.
      Returns:
      the line width
    • setLineWidth

      @Deprecated public void setLineWidth(float lineWidth)
      Specify the line width for meshes of the line modes, such as Mesh.Mode.Lines. The line width is specified as on-screen pixels, the default value is 1.0.
      Parameters:
      lineWidth - The line width
    • setStatic

      public void setStatic()
      Indicates to the GPU that this mesh will not be modified (a hint). Sets the usage mode to VertexBuffer.Usage.Static for all vertex buffers on this Mesh.
    • setDynamic

      public void setDynamic()
      Indicates to the GPU that this mesh will be modified occasionally (a hint). Sets the usage mode to VertexBuffer.Usage.Dynamic for all vertex buffers on this Mesh.
    • setStreamed

      public void setStreamed()
      Indicates to the GPU that this mesh will be modified every frame (a hint). Sets the usage mode to VertexBuffer.Usage.Stream for all vertex buffers on this Mesh.
    • setInterleaved

      @Deprecated public void setInterleaved()
      Deprecated.
      Interleaves the data in this mesh. This operation cannot be reversed. Some GPUs may prefer the data in this format, however it is a good idea to avoid using this method as it disables some engine features.
    • updateCounts

      public void updateCounts()
      Update the vertex and triangle counts for this mesh based on the current data. This method should be called after the capacities of the mesh's vertex buffers has been altered.
      Throws:
      IllegalStateException - If this mesh is in interleaved format.
    • getTriangleCount

      public int getTriangleCount(int lod)
      Returns the triangle count for the given LOD level.
      Parameters:
      lod - The lod level to look up
      Returns:
      The triangle count for that LOD level
    • getTriangleCount

      public int getTriangleCount()
      Returns how many triangles or elements are on this Mesh. This value is only updated when updateCounts() is called. If the mesh mode is not a triangle mode, then this returns the number of elements/primitives, e.g. how many lines or how many points, instead of how many triangles.
      Returns:
      how many triangles/elements are on this Mesh.
    • getVertexCount

      public int getVertexCount()
      Returns the number of vertices on this mesh. The value is computed based on the position buffer, which must be set on all meshes.
      Returns:
      Number of vertices on the mesh
    • getInstanceCount

      public int getInstanceCount()
      Returns the number of instances this mesh contains. The instance count is based on any VertexBuffers with instancing set.
      Returns:
      the number of instances
    • getTriangle

      public void getTriangle(int index, Vector3f v1, Vector3f v2, Vector3f v3)
      Gets the triangle vertex positions at the given triangle index and stores them into the v1, v2, v3 arguments.
      Parameters:
      index - The index of the triangle. Should be between 0 and getTriangleCount().
      v1 - Vector to contain first vertex position
      v2 - Vector to contain second vertex position
      v3 - Vector to contain third vertex position
    • getTriangle

      public void getTriangle(int index, Triangle tri)
      Gets the triangle vertex positions at the given triangle index and stores them into the Triangle argument. Also sets the triangle index to the index argument.
      Parameters:
      index - The index of the triangle. Should be between 0 and getTriangleCount().
      tri - The triangle to store the positions in
    • getTriangle

      public void getTriangle(int index, int[] indices)
      Gets the triangle vertex indices at the given triangle index and stores them into the given int array.
      Parameters:
      index - The index of the triangle. Should be between 0 and getTriangleCount().
      indices - Indices of the triangle's vertices
    • getId

      public int getId()
      Returns the mesh's VAO ID. Internal use only.
      Returns:
      the array ID
    • setId

      public void setId(int id)
      Sets the mesh's VAO ID. Internal use only.
      Parameters:
      id - the array ID
    • createCollisionData

      public void createCollisionData()
    • clearCollisionData

      public void clearCollisionData()
      Clears any previously generated collision data. Use this if the mesh has changed in some way that invalidates any previously generated BIHTree.
    • collideWith

      public int collideWith(Collidable other, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results)
      Handles collision detection, internal use only. User code should only use collideWith() on scene graph elements such as Spatials.
      Parameters:
      other - the other Collidable
      worldMatrix - the world matrix
      worldBound - the world bound
      results - storage for the results
      Returns:
      the number of collisions detected (≥0)
    • setBuffer

      public void setBuffer(VertexBuffer vb)
      Sets the VertexBuffer on the mesh. This will update the vertex/triangle counts if needed.
      Parameters:
      vb - The buffer to set
      Throws:
      IllegalArgumentException - If the buffer type is already set
    • clearBuffer

      public void clearBuffer(VertexBuffer.Type type)
      Unsets the VertexBuffer set on this mesh with the given type. Does nothing if the vertex buffer type is not set initially.
      Parameters:
      type - The buffer type to remove
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, VertexBuffer.Format format, Buffer buf)
      Creates a VertexBuffer for the mesh or modifies the existing one per the parameters given.
      Parameters:
      type - The type of the buffer
      components - Number of components
      format - Data format
      buf - The buffer data
      Throws:
      UnsupportedOperationException - If the buffer already set is incompatible with the parameters given.
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, FloatBuffer buf)
      Set a floating point VertexBuffer on the mesh.
      Parameters:
      type - The type of VertexBuffer, e.g. VertexBuffer.Type.Position, VertexBuffer.Type.Normal, etc.
      components - Number of components on the vertex buffer, should be between 1 and 4.
      buf - The floating point data to contain
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, float[] buf)
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, IntBuffer buf)
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, int[] buf)
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, ShortBuffer buf)
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, byte[] buf)
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, ByteBuffer buf)
    • setBuffer

      public void setBuffer(VertexBuffer.Type type, int components, short[] buf)
    • getBuffer

      public VertexBuffer getBuffer(VertexBuffer.Type type)
      Get the VertexBuffer stored on this mesh with the given type.
      Parameters:
      type - The type of VertexBuffer
      Returns:
      the VertexBuffer data, or null if not set
    • getFloatBuffer

      public FloatBuffer getFloatBuffer(VertexBuffer.Type type)
      Get the VertexBuffer data stored on this mesh in float format.
      Parameters:
      type - The type of VertexBuffer
      Returns:
      the VertexBuffer data, or null if not set
    • getShortBuffer

      public ShortBuffer getShortBuffer(VertexBuffer.Type type)
      Get the VertexBuffer data stored on this mesh in short format.
      Parameters:
      type - The type of VertexBuffer
      Returns:
      the VertexBuffer data, or null if not set
    • getIndicesAsList

      public IndexBuffer getIndicesAsList()
      Acquires an index buffer that will read the vertices on the mesh as a list.
      Returns:
      A virtual or wrapped index buffer to read the data as a list
    • getIndexBuffer

      public IndexBuffer getIndexBuffer()
      Get the index buffer for this mesh. Will return null if no index buffer is set.
      Returns:
      The index buffer of this mesh.
      See Also:
    • extractVertexData

      public void extractVertexData(Mesh other)
      Extracts the vertex attributes from the given mesh into this mesh, by using this mesh's index buffer to index into the attributes of the other mesh. Note that this will also change this mesh's index buffer so that the references to the vertex data match the new indices.
      Parameters:
      other - The mesh to extract the vertex data from
    • scaleTextureCoordinates

      public void scaleTextureCoordinates(Vector2f scaleFactor)
      Scales the texture coordinate buffer on this mesh by the given scale factor.

      Note that values above 1 will cause the texture to tile, while values below 1 will cause the texture to stretch.

      Parameters:
      scaleFactor - The scale factor to scale by. Every texture coordinate is multiplied by this vector to get the result.
      Throws:
      IllegalStateException - If there's no texture coordinate buffer on the mesh
      UnsupportedOperationException - If the texture coordinate buffer is not in 2D float format.
    • updateBound

      public void updateBound()
      Updates the bounding volume of this mesh. The method does nothing if the mesh has no VertexBuffer.Type.Position buffer. It is expected that the position buffer is a float buffer with 3 components.
    • getBound

      public BoundingVolume getBound()
      Returns the BoundingVolume of this Mesh. By default the bounding volume is a BoundingBox.
      Returns:
      the bounding volume of this mesh
    • setBound

      public void setBound(BoundingVolume modelBound)
      Sets the BoundingVolume for this Mesh. The bounding volume is recomputed by calling updateBound().
      Parameters:
      modelBound - The model bound to set
    • getBuffers

      public IntMap<VertexBuffer> getBuffers()
      Returns a map of all vertex buffers on this Mesh. The integer key for the map is the ordinal of the vertex buffer's VertexBuffer.Type. Note that the returned map is a reference to the map used internally, modifying it will cause undefined results.
      Returns:
      map of vertex buffers on this mesh.
    • getBufferList

      public SafeArrayList<VertexBuffer> getBufferList()
      Returns a list of all vertex buffers on this Mesh. Using a list instead an IntMap via the getBuffers() method is better for iteration as there's no need to create an iterator instance. Note that the returned list is a reference to the list used internally, modifying it will cause undefined results.
      Returns:
      list of vertex buffers on this mesh.
    • isAnimated

      public boolean isAnimated()
      Determines if the mesh uses bone animation. A mesh uses bone animation if it has bone index / weight buffers such as VertexBuffer.Type.BoneIndex or VertexBuffer.Type.HWBoneIndex.
      Returns:
      true if the mesh uses bone animation, false otherwise
    • isAnimatedByBone

      @Deprecated public boolean isAnimatedByBone(int boneIndex)
      Deprecated.
      use isAnimatedByJoint
      Parameters:
      boneIndex - the bone's index in its skeleton
      Returns:
      true if animated by that bone, otherwise false
    • isAnimatedByJoint

      public boolean isAnimatedByJoint(int jointIndex)
      Test whether the specified bone animates this mesh.
      Parameters:
      jointIndex - the bone's index in its skeleton
      Returns:
      true if the specified bone animates this mesh, otherwise false
    • setPatchVertexCount

      public void setPatchVertexCount(int patchVertexCount)
      Sets the count of vertices used for each tessellation patch
      Parameters:
      patchVertexCount - the desired count
    • getPatchVertexCount

      public int getPatchVertexCount()
      Gets the amount of vertices used for each patch;
      Returns:
      the count (≥0)
    • addMorphTarget

      public void addMorphTarget(MorphTarget target)
    • removeMorphTarget

      public boolean removeMorphTarget(MorphTarget target)
      Remove the given MorphTarget from the Mesh
      Parameters:
      target - The MorphTarget to remove
      Returns:
      If the MorphTarget was removed
    • removeMorphTarget

      public MorphTarget removeMorphTarget(int index)
      Remove the MorphTarget from the Mesh at the given index
      Parameters:
      index - Index of the MorphTarget to remove
      Returns:
      The MorphTarget that was removed
      Throws:
      IndexOutOfBoundsException - if the index outside the number of morph targets
    • getMorphTarget

      public MorphTarget getMorphTarget(int index)
      Get the MorphTarget at the given index
      Parameters:
      index - The index of the morph target to get
      Returns:
      The MorphTarget at the index
      Throws:
      IndexOutOfBoundsException - if the index outside the number of morph targets
    • getMorphTargets

      public MorphTarget[] getMorphTargets()
    • getMorphTargetNames

      public String[] getMorphTargetNames()
      Get the name of all morphs in order. Morphs without names will be null
      Returns:
      an array
    • hasMorphTargets

      public boolean hasMorphTargets()
    • getMorphIndex

      public int getMorphIndex(String morphName)
      Get the index of the morph that has the given name.
      Parameters:
      morphName - The name of the morph to search for
      Returns:
      The index of the morph, or -1 if not found.
    • write

      public void write(JmeExporter ex) throws IOException
      Specified by:
      write in interface Savable
      Throws:
      IOException
    • read

      public void read(JmeImporter im) throws IOException
      Specified by:
      read in interface Savable
      Throws:
      IOException