你試圖在這裏實現是libGdx稍微複雜。然而,你的方法是不正確的,因爲你將文本作爲一個獨立的精靈對象繪製在球的頂部。
實現此目的的正確過程是將文本繪製到紋理上,然後將紋理映射並綁定到球上。
的過程可以分爲4個部分 -
- 設置的字體生成和幀緩衝區對象
- 設置子畫面批次
- 繪製文本,紋理映射到模型
- 渲染模型
作爲說明,我想提及的是,可以使用任何其他紋理,如果您不想實時更新文本,則可以輕鬆使用預先製作的紋理以及任何您喜歡的文字。
的代碼如下 -
設置字體產生和幀緩衝區對象
//Create a new SpriteBatch object
batch = new SpriteBatch();
//Generate font
FreeTypeFontGenerator generator = new
FreeTypeFontGenerator(Gdx.files.internal("font/Lato-Bold.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new
FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 30;
parameter.color = com.badlogic.gdx.graphics.Color.WHITE;
parameter.magFilter = Texture.TextureFilter.Linear; // used for resizing quality
parameter.minFilter = Texture.TextureFilter.Linear;
generator.scaleForPixelHeight(10);
//Get bitmap font
BitmapFont aFont = generator.generateFont(parameter);
aFont.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear,
Texture.TextureFilter.Linear);
//Generate new framebuffer object of 128x128 px. This will be our texture
FrameBuffer lFb = new FrameBuffer(Pixmap.Format.RGBA4444,128,128,false);
設置子畫面批次
//Set the correct resolution for drawing to the framebuffer
lFb.begin();
Gdx.gl.glViewport(0,0,128,128);
Gdx.gl.glClearColor(1f, 1f, 1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Matrix4 lm = new Matrix4();
//Set the correct projection for the sprite batch
lm.setToOrtho2D(0,0,128,128);
batch.setProjectionMatrix(lm);
最後繪製文本到紋理
batch.begin();
aFont.draw(batch,"Goal!",64,64);
batch.end();
lFb.end();
紋理映射到模型
//Generate the material to be applied to the ball
//Notice here how we use the FBO's texture as the material base
Material lMaterial = new Material(TextureAttribute.createDiffuse(lFb.getColorBufferTexture()));
//Since I do not have a sphere model, I'll just create one with the newly
//generated material
ballModel = mb.createSphere(1.0f,1.0f,1.0f,8,8,lMaterial,
VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates);
//Finally instantiate an object of the model
ballInstance = new ModelInstance(ballModel);
渲染模型
mBatch.begin(mCamera);
mBatch.render(ballInstance);
mBatch.end();
//Rotate the ball along the y-axis
ballInstance.transform.rotate(0.0f,1.0f,0.0f,0.5f);
結果 -
您正在將文本繪製爲同一矩陣上的單獨實體。一種方法,你可以實現這一點,你正在使用basketBall等紋理位圖上繪製文本。這將採取球形。 – Qamar