Placing text in 3D (not facing viewer) - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-23T06:27:35Z https://mathematica.stackexchange.com/feeds/question/131798 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/131798 3 Placing text in 3D (not facing viewer) Nicholas G https://mathematica.stackexchange.com/users/43662 2016-11-22T15:25:21Z 2016-11-23T10:13:12Z <p>I am trying to adapt the solution <a href="https://mathematica.stackexchange.com/questions/121075/text-on-a-plane-without-rasterize">here</a>, which places text in 3D on the xy plane, so that the text can be placed in any orientation. So, I wrapped it in three <code>Rotate</code> statements, first by tilt around the x axis, then by elevation around the y-axis, then by longitude around the z-axis. However, this rotation happens after the text has been placed, away from the center of the axes. Trying to put the Rotation statements inside the meat of the function runs into the problem that I cannot tell from where the x and y coefficients are deriving so as to manipulate them using <code>Rotate</code>. Anyway, the original is</p> <pre><code>xyText[str_, scaling_: 1, location_: {0, 0, 0}] := Module[{mesh = DiscretizeGraphics[Text[Style[str, FontFamily -&gt; "Times"]], _Text, MaxCellMeasure -&gt; 0.2]}, Join[{EdgeForm[]}, MeshPrimitives[mesh, 2] /. {x_?NumberQ, y_?NumberQ} :&gt; (scaling {x, y, 0} + location), {Black}, MeshPrimitives[BoundaryMesh[mesh], 1] /. {x_?NumberQ, y_?NumberQ} :&gt; (scaling {x, y, 0} + location)]] </code></pre> <p>and my inadequate adaptation is </p> <pre><code>ClearAll[xyText] xyText[str_, scaling_: 1, location_: {0, 0, 0}, longitude_, elevation_, tilt_] := Module[{mesh = DiscretizeGraphics[Text[Style[str, FontFamily -&gt; "Times"]], _Text, MaxCellMeasure -&gt; 0.2]}, Rotate[ Rotate[ Rotate[ Join[{EdgeForm[]}, MeshPrimitives[mesh, 2] /. {x_?NumberQ, y_?NumberQ} :&gt; (scaling {x, y, 0} + location) , {Black}, MeshPrimitives[BoundaryMesh[mesh], 1] /. {x_?NumberQ, y_?NumberQ} :&gt; (scaling {x, y, 0} + location)], tilt, {1, 0, 0}], elevation, {0, 1, 0}], longitude, {0, 0, 1}]] </code></pre> <p>The objective is for the yellow and the red "Dodecahedron" texts to intersect each other in this:</p> <pre><code>meshedSol = Graphics3D[{Antialiasing -&gt; True, Yellow, Opacity[.8], PolyhedronData["Dodecahedron", "Faces"], xyText["Dodecahedron", 0.2, {1, 1, 1}, 0, 0, 0], Red, xyText["Dodecahedron", 0.2, {1, 1, .9}, 0, 0, \[Pi]/2]}, ViewPoint -&gt; {-2, -2, 2}] </code></pre> <p>But instead, the red one is rotated after having been placed.</p> <p><strong>Update:</strong></p> <p>I gathered that x and y come from MeshPrimitives, so I made that 3D and wove in rotation matrices. Till we get a more elegant answer, I moved the result to be an answer.</p> https://mathematica.stackexchange.com/questions/131798/placing-text-in-3d-not-facing-viewer/131842#131842 4 Answer by Nicholas G for Placing text in 3D (not facing viewer) Nicholas G https://mathematica.stackexchange.com/users/43662 2016-11-23T01:39:26Z 2016-11-23T10:13:12Z <p>I wove rotation matrices in the code and increased the resolution and it works, and now with Chip Hurst's improvement not so slow at all. Maybe it will get to be more elegant yet:</p> <pre><code>text3D[str_, location_: {0, 0, 0}, scaling : {_?NumericQ, _?NumericQ} : { 1, 1}, longitude : _?NumericQ : 0, elevation : _?NumericQ : 0, tilt : _?NumericQ : 0, opt : OptionsPattern[]]:=Module[{ mesh=DiscretizeGraphics[ Text[Style[str, opt, FontFamily -&gt; "Times", FontSize -&gt; 12]], _Text, MaxCellMeasure -&gt; 0.04], rotmatrx=RotationMatrix[longitude, {0, 0, 1}]. RotationMatrix[-elevation, {0, 1, 0}].RotationMatrix[tilt, {1, 0, 0}]}, Join[{EdgeForm[]}, MeshPrimitives[mesh,2] /. {x_?NumberQ, y_?NumberQ} :&gt; ( rotmatrx.(scaling~Join~{1} {x, y, 0}) + location) (* ,{Black}, MeshPrimitives[BoundaryMesh[mesh],1]/.{x_?NumberQ,y_?NumberQ}\[RuleDelayed] (rotmatrx.(scaling~Join~{1} {x,y,0})+location) REMOVE REM IF NEEDING OUTLINES*) ]] </code></pre> <p>Then:</p> <pre><code>offset = 12; scale = {.5, .5}; Graphics3D[{ Yellow, text3D["width\[LongRightArrow]", {offset, 0, 0}, scale, FontSlant -&gt; Italic], Blue, text3D["depth\[LongRightArrow]", {0, offset, 0}, scale, \[Pi]/2, FontWeight -&gt; Bold], Black, text3D["height\[LongRightArrow]", {0, 0, offset}, {.5, .8}, 0, \[Pi]/2, \[Pi]/2, FontFamily -&gt; "Brush Script Std"], Red, Sphere[{offset, offset, offset/4}, 3] }, Boxed -&gt; False] </code></pre> <p>produces:</p> <p><a href="https://i.stack.imgur.com/TIqU4.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TIqU4.gif" alt="enter image description here"></a></p>