gpt4 book ai didi

android - 在 libgdx 中在球体上绘制文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:48 24 4
gpt4 key购买 nike

我正在尝试使用 libgdx 来渲染一些 fbx 模型,我成功地做到了,但我卡在了一个点上。

我在 ModelInstance 的帮助下渲染一个篮球 fbx 文件,现在我想在那个篮球上画一个文本。我可以在篮球上画文字,但它是线性的,看起来不像是那个篮球的一部分。我希望文本以与那个球相同的曲线方式显示。

这就是我绘制文本的方式--

batch = new SpriteBatch();
batch.setProjectionMatrix(camera.combined);

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(3);

BitmapFont aFont = generator.generateFont(parameter);
aFont.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

aFont.draw(batch, options.get(i), aBound.getCenterX(), aBound.getCenterY() + textHeight / 2);

Where aBound --> 模型的 BoundingBox(即 BasketBall)

最佳答案

您在此处尝试实现的目标在 libGdx 中略微复杂。但是,您的方法是不正确的,因为您将文本绘制为球顶部的单独 Sprite 对象。

实现此目的的正确过程是将文本绘制到纹理上,然后将纹理映射并绑定(bind)到球上。

程序可以分为4个部分-

  1. 设置字体生成和帧缓冲区对象
  2. 设置 Sprite 批处理
  3. 绘制文字,将纹理映射到模型
  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);

设置 Sprite 批处理

//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);

结果 -

Render text to sphere libGDX

关于android - 在 libgdx 中在球体上绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43542081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com