Android 在libgdx中在球体上绘制文本
我正在尝试libgdx来渲染一些fbx模型,我成功地做到了这一点,但我在某一点上被卡住了 我在ModelInstance的帮助下渲染篮球fbx文件,现在我想在篮球上绘制一个文本。我能在篮球上画文字,但它是线性的,看起来不是篮球的一部分。 我想要文本以与球相同的曲线方式 这就是我画文本的方式--Android 在libgdx中在球体上绘制文本,android,3d,libgdx,Android,3d,Libgdx,我正在尝试libgdx来渲染一些fbx模型,我成功地做到了这一点,但我在某一点上被卡住了 我在ModelInstance的帮助下渲染篮球fbx文件,现在我想在篮球上绘制一个文本。我能在篮球上画文字,但它是线性的,看起来不是篮球的一部分。 我想要文本以与球相同的曲线方式 这就是我画文本的方式-- Where Ab富饶-->模型的边界框(即篮球)在libGdx中,您试图实现的目标有点复杂。但是,您的方法是不正确的,因为您正在将文本作为单独的精灵对象绘制在球的顶部 实现这一点的正确步骤是将文本绘制到纹
Where Ab富饶-->模型的边界框(即篮球)在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);
渲染模型
//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);
结果-
您正在将文本作为一个单独的实体绘制在同一矩阵上。实现这一点的一种方法是在用于篮球等的纹理位图上绘制文本。它将采用球形。