3
$\begingroup$

I have a labelled two-dimensional Polygon that I render with Graphics.

label = {RGBColor[1., 0.92, 0.61], Polygon[{{0, 0}, {4, 0}, {4, -1}, {0, -1}}]};
label = Graphics[{labels, Inset[Text["Some Label", BaseStyle -> {FontSize -> 40}], (1/2) {4, -1}]}];

Picture of the 2D labelled graphics

I want to place this object in a three-dimensional Graphics3D, such that the text also gets projected into the plane. So I want an image that looks like the following but with the code modified so that the text is also present

labels = {RGBColor[1., 0.92, 0.61], Polygon[{{0, 0, 0}, {4, 0, 0}, {4, -1, 0}, {0, -1, 0}}]};
object = {Blue, Cuboid[{0, 0, 0}, {4, 2, 3}]};
Graphics3D[{object, labels}, Boxed -> False]

Picture of 3D graphics with cuboid object and 'label' (but missing text)

$\endgroup$
4
$\begingroup$
labels = {Texture[Rasterize[
     Text[Framed[Style["Some Label", Yellow, Bold, FontSize -> 46, 
        FontFamily -> "Lucida Handwriting"], FrameStyle -> None, 
       ImageMargins -> Scaled[.02]]], Background -> Purple]], 
   Polygon[Reverse @ {{0, 0, 0}, {4, 0, 0}, {4, -1, 0}, {0, -1, 0}}, 
    VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]};

object = {Blue, Cuboid[{0, 0, 0}, {4, 2, 3}]};

Graphics3D[{object, labels}, Boxed -> False, Lighting -> "Neutral"]

enter image description here

See also: this answer for an alternative approach.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thank you :) How do I alter the colour of the text, and the beige polygon underneath? Might sound simple but I can't get it to work $\endgroup$ – Tom Sep 23 '19 at 8:24
  • $\begingroup$ @Tom, you can use Style["Some Label", Red] instead of "Some Label" and Background -> Purple (say) instead of Background -> RGBColor[1., 0.92, 0.61]. $\endgroup$ – kglr Sep 23 '19 at 8:27
  • $\begingroup$ Oh yes. Somehow it works here, but not for my similar answer below. Great thanks :) $\endgroup$ – Tom Sep 23 '19 at 8:38
  • $\begingroup$ Hmm. There's one last small problem with this answer. It has scaled the text to fill the entire box. But in the original label it was centered with some space around it $\endgroup$ – Tom Sep 23 '19 at 8:41
  • 1
    $\begingroup$ @Tom, please see the new version. You can control the spacing around framed text with the option ImageMargins. $\endgroup$ – kglr Sep 23 '19 at 9:20
1
$\begingroup$

I have managed a bodge job, that doesn't quite work, but gets half way there

label = {White, Polygon[{{0, 0}, {4, 0}, {4, -1}, {0, -1}}]};
label = Graphics[{label, 
    Inset[Text["Some Label", 
      BaseStyle -> {FontSize -> 40}], (1/2) {4, -1}]}, 
   ImagePadding -> 0];
label = Image[label];
label3D = {Blue, Texture[label], 
   Polygon[{{0, 0, 0}, {4, 0, 0}, {4, -1, 0}, {0, -1, 0}}, 
    VertexTextureCoordinates -> {{0, 0, 0}, {1, 0, 0}, {1, -1, 
       0}, {0, -1, 0}}]};
object = {Blue, Cuboid[{0, 0, 0}, {4, 2, 3}]};
Graphics3D[{object, label3D}, Boxed -> False]

Result from above code

It uses a trick. It makes an image from the 2D label, and then uses it as a Texture for a three-dimensional polygon. The problem is that I seem unable to control the colour of the resulting polygon. Note the random command Blue in the code that is just ignored.

Here's a slightly modified approach that uses Opacity[0] to achieve the effect I was ultimately after, which is a black text label with no colored box. Hopefully it is useful to someone.

label = {Opacity[0], Polygon[{{0, 0}, {4, 0}, {4, -1}, {0, -1}}]};
label = Graphics[{label, 
    Inset[Text["Some Label", 
      BaseStyle -> {FontSize -> 40}], (1/2) {4, -1}]}, 
   ImagePadding -> 0];
label = Image[label];
label3D = {Texture[label], EdgeForm[White], 
   Polygon[{{0, 0, 0}, {4, 0, 0}, {4, -1, 0}, {0, -1, 0}}, 
    VertexTextureCoordinates -> {{0, 0, 0}, {1, 0, 0}, {1, -1, 
       0}, {0, -1, 0}}]};
object = {Blue, Cuboid[{0, 0, 0}, {4, 2, 3}]};
Graphics3D[{object, label3D}, Boxed -> False]

Result from above code

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.