Ответ 1
То, что вы пытаетесь достичь здесь, немного сложнее в libGdx. Однако ваш подход неверен, потому что вы рисуете текст как отдельный объект спрайта поверх вашего шара.
Правильная процедура для достижения этого - нарисовать текст на текстуре, а затем отобразить и привязать текстуру к шару.
Процедуру можно разделить на 4 части -
- Настройка генерации шрифтов и объекта фреймбуфера
- Настройка спрайта
- Рисование текста, отображение текстуры в модель
- Оказание модели
В качестве примечания я хотел бы упомянуть, что можно использовать любую другую текстуру, и если вы не хотите обновлять текст в режиме реального времени, вы можете легко использовать предварительно созданную текстуру с любым текстом, d.
Код выглядит следующим образом:
Настройка создания шрифтов и фреймбуфера
//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 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);
Результат -