📄 animbillboard.java
字号:
import javax.microedition.m3g.*;
public class AnimBillboard
{
private Mesh bbMesh;
private Appearance appearance;
private Texture2D[] textures; // holds the images
private int numImages, currImIdx;
private boolean isVisible; // is the animated board visible?
private Group cameraGroup; // used for camera alignment
public AnimBillboard(Image2D[] bbIms, Group camGroup, float size)
{
cameraGroup = camGroup;
// build the mesh
VertexBuffer vertexbuffer = makeGeometry();
int[] INDICES = {1,2,0,3}; // one quad
int[] LENGTH = {4};
IndexBuffer indexbuffer = new TriangleStripArray(INDICES, LENGTH);
numImages = bbIms.length;
currImIdx = 0;
textures = makeTextures(bbIms, numImages);
appearance = makeAppearance( textures[currImIdx] );
bbMesh = new Mesh(vertexbuffer, indexbuffer, appearance);
// scale and position the mesh
float scale2 = size * 0.5f; // to reduce 2 by 2 image to 1 by 1
bbMesh.scale(scale2, scale2, scale2);
bbMesh.setTranslation(0, scale2, 0);
bbMesh.setAlignment(cameraGroup, Node.Z_AXIS, null, Node.NONE);
// only use z-axis alignment with the camera
bbMesh.setPickingEnable(false);
setVisible(false);
} // end of AnimBillboard()
private VertexBuffer makeGeometry()
/* A square centered at the origin on the XZ plane
with sides of 2 units. No normals. */
{
// create vertices
short[] POINTS = {-1,-1,0, 1,-1,0, 1,1,0, -1,1,0};
VertexArray POSITION_ARRAY = new VertexArray(POINTS.length/3, 3, 2);
POSITION_ARRAY.set(0, POINTS.length/3, POINTS);
// create texture coordinates
short[] TEXCOORDS = {0,1, 1,1, 1,0, 0,0};
VertexArray TEXCOORD_ARRAY = new VertexArray(TEXCOORDS.length/2, 2, 2);
TEXCOORD_ARRAY.set(0, TEXCOORDS.length/2, TEXCOORDS);
VertexBuffer vertexbuffer = new VertexBuffer();
vertexbuffer.setPositions(POSITION_ARRAY, 1.0f, null); // no scale, bias
vertexbuffer.setTexCoords(0, TEXCOORD_ARRAY, 1.0f, null);
return vertexbuffer;
} // end of makeGeometry()
private Texture2D[] makeTextures(Image2D[] ims, int num)
// convert the images array into an array of textures
{
Texture2D[] textures = new Texture2D[num];
for (int i=0; i < num; i++) {
if (ims[i] != null) {
textures[i] = new Texture2D(ims[i]);
textures[i].setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
textures[i].setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
textures[i].setBlending(Texture2D.FUNC_REPLACE);
} // the texture's colours and alpha replace the mesh's
else
System.out.println("Image " + i + " is null");
}
return textures;
} // end of makeTextures()
private Appearance makeAppearance(Texture2D texture)
// The appearance comes from the transparent texture in texture.
{
appearance = new Appearance(); // no material
// Only display the opaque parts of the texture
CompositingMode compositingmode = new CompositingMode();
compositingmode.setBlending(CompositingMode.ALPHA);
appearance.setCompositingMode(compositingmode);
appearance.setTexture(0, texture);
return appearance;
} // end of makeAppearance()
// -------------------- animate the board -----------------
public void setPosition(float x, float y, float z)
// position the board
{ bbMesh.setTranslation(x,y,z); }
public void setVisible(boolean visible)
/* Make the board visible or invisible. If the board is made visible,
then its image display index (currImIdx) is reset.
*/
{ isVisible = visible;
if (isVisible) {
bbMesh.setRenderingEnable(true);
currImIdx = 0; // reset the image display index
}
else
bbMesh.setRenderingEnable(false);
} // end of setVisible()
public void update()
/* If the board is visible, show the current image in the
sequence, and finish by incrementing the image index.
When the end of the sequence is reached, make the board
invisible.
*/
{ if (isVisible) {
bbMesh.align(cameraGroup); // update alignment
if (currImIdx == numImages)
setVisible(false);
else
appearance.setTexture(0, textures[currImIdx]); // show image
currImIdx = currImIdx+1; // set to next image index
}
} // end of update()
// -------------------- access methods ----------------------
public Mesh getAnimBillboardMesh()
{ return bbMesh; }
public boolean isVisible()
// is the billboard visible?
{ return isVisible; }
} // end of AnimBillboard Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -