处理:如何在3d对象上显示文本

时间:2014-02-24 16:49:42

标签: geometry processing

我是处理新手,目前我想要显示3D球,并为3D球添加文字标签。 我使用的代码如下:

// this function is called when this sketch is first loaded
void setup()
{
  // choose size (width, height) of your sketch
  size(720, 546, P3D);

  // Choose background color for your sketch
  background(0);
}

    // This function is called repeatedly to draw stuff on screen.
void draw()
{
  lights();
  pushMatrix();
  translate(500, height*0.35, 0);
  fill(236,112,20);
  noStroke();
  sphere(40);
  popMatrix();

  //fill(255);
 fill(255,112,20);
 textAlign(CENTER);
 textSize(15);
 stroke(10);
 text("hihihihi", 500, height*0.35);
    }

事实证明,文字“hihihihi”被球覆盖,但是当我使用2D对象时,它显示出良好的效果。这里有什么问题吗,有人能告诉我代码的问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

请注意,您的球体的半径为40,这意味着直径为80,并且您将文本和球体绘制在相同的水平位置(500)。

将文本向右移动以避免球体遮挡它:

void setup()
{
  size(720, 546, P3D);
  background(0);
}
void draw()
{
  lights();
  pushMatrix();
    translate(500, height*0.35, 0);
    fill(236, 112, 20);
    noStroke();
    sphere(40);
  popMatrix();

  fill(255, 112, 20);
  textAlign(CENTER);
  textSize(15);
  stroke(10);
  text("hihihihi", 585, height*0.35);//make sure the text isn't occluded by anything else(like a sphere) 
}

如果您想在球体上渲染文字,我建议您尝试PShapePGraphics。 这是一个最小的例子:

PShape sphere;
void setup()
{
  size(720, 546, P3D);
  background(0);

  PGraphics sphereTexture = createGraphics(40,40);
  sphereTexture.beginDraw();
  sphereTexture.background(236, 112, 20);
  sphereTexture.fill(255);
  sphereTexture.text("Hi",20,20);
  sphereTexture.endDraw();

  sphere = createShape(SPHERE, 40);
  sphere.disableStyle();
  noStroke();
  sphere.setTexture(sphereTexture); 
}
void draw()
{
  lights();
  pushMatrix();
  translate(500, height*0.35, 0);
  fill(236, 112, 20);
  noStroke();
  shape(sphere,0,0);
  popMatrix();

  fill(255, 112, 20);
  textAlign(CENTER);
  textSize(15);
  stroke(10);
  text("hihihihi", 585, height*0.35);//make sure the text isn't occluded by anything else(like a sphere) 
}