Re: Transparent textures from images
- To: mathgroup at smc.vnet.net
- Subject: [mg123864] Re: Transparent textures from images
- From: JUN <noeckel at gmail.com>
- Date: Sat, 24 Dec 2011 07:07:19 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201112060812.DAA18391@smc.vnet.net> <jbq3d4$ij7$1@smc.vnet.net>
On Dec 8, 2:26 am, Barrie Stokes <Barrie.Sto... at newcastle.edu.au> wrote: > Hi Patrick, hi John > > Intrigued by the results of John's fix, I played around a little with features I've never used. > > Using some code form the Help for Texture[], I was delighted to come up with: > > rose = Import[ "ExampleData/rose.gif" ]; > > roses = GraphicsGrid[ Table[ rose, {15}, {15} ] ]; > > ParametricPlot3D[ {1.16^v Cos[ v ] (1 + Cos[ u ]), -1.16^v Sin[ > v ] (1 + Cos[ u ]), -2 1.16^v (1 + Sin[ u ])}, {u, 0, > 2 Pi}, {v, -15, 6}, > PlotStyle -> > Directive[ Specularity[ White, 30 ], > Texture[ ImageData[ roses ] ] ], > TextureCoordinateFunction -> ({#4, 2 #5} &), Lighting -> "Neutral", > Mesh -> None, PlotRange -> All, Boxed -> False, Axes -> False, > PlotPoints -> 50 ] > > Beautiful, I think. > > Barrie > > > > > > > > >>> On 06/12/2011 at 7:12 pm, in message <201112060812.DAA18... at smc.vnet.net>, JohnFultz<jfu... at wolfram.com> wrote: > > Patrick, > > > This is a bug in Mathematica. But, fortunately, it's a very easy bug to > > work > > around. Replace: > > > Texture[img] > > > with > > > Texture[ImageData[img]] > > label3D[s_, pos_, xVec_, tiltAngle_, opts : OptionsPattern[]] := Module[{ra, width, height, r}, ra = Rasterize[ Style[HoldForm[s], FilterRules[{opts}, Options[Style]], Magnification -> 10], Evaluate@Apply[Sequence, FilterRules[{opts}, Options[Rasterize]]] ]; {width, height} = ImageDimensions[ra]; r = SetAlphaChannel[ra, With[ { color = Apply[List, ColorConvert[ "TransparentColor" /. {opts} /. {"TransparentColor" -> Apply[RGBColor, ImageData[ra][[2, 2]]]}, "RGB"] ] }, Binarize[ra, (Norm[# - color] > .005)&]] ]; Translate[(* // to make lefthand corner pos *) Rotate[(* // around z axis *) Rotate[ (* // around y axis *) Rotate[(* // tilt around x axis *) Scale[ (*// to make width equal |xVec| *) {EdgeForm[FrameStyle /. {opts} /. FrameStyle -> None], Texture[ImageData@r], (* // Texture fills polygon initially in the xz plane *) Polygon[{{0, 0, 0}, {width, 0, 0}, {width, 0, height}, {0, 0, height}}, VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]}, Norm[xVec]/width, {0, 0, 0} ], tiltAngle, {1, 0, 0}],(* // x rotation *) Arg[Norm[xVec[[1 ;; 2]]] + I xVec[[3]]], {0, -1, 0}], (* // y rotation *) Arg[xVec[[1]] + I xVec[[2]]], {0, 0, 1} ], (* // z rotation *) pos] ]; SetAttributes[label3D, HoldFirst]; With[{position = {0,0,0}, direction = {1.5, 0, 1.3}, tiltAngle = -.8}, Graphics3D[ { {Green, Specularity[2], Sphere[{1, 1, 1}, .7]}, Map[{Apply[RGBColor, #], Arrow[Tube[{{0, 0, 0}, #}]]} &, 2 IdentityMatrix[3] ], {Glow[White], label3D["This is a label", position, direction, tiltAngle, FontColor -> Orange, FontSize -> 18, FontFamily -> "Helvetica", FontWeight -> Bold, Magnification -> 4, FrameStyle -> Directive[Purple, Thick] ] } }, Boxed -> False, SphericalRegion -> True, Background -> Cyan ] ] Some explanations: As shown in the example plot, the function label3D takes an arbitrary expression and displays it as a textured 3D rectangle with transparent background. The expression is converted to an image without being evaluated (so you aren't limited to string arguments). By default, image regions matching the color at the corner of the image are made transparent. Alternatively, you can explicitly specify which color to make transparent by adding the option "TransparentColor" -> White to label3D (instead of White, the color can be anything you like). The matching colors are found within a certain tolerance, so the background color should be sufficiently different from the other colors. The first argument is the object to be displayed (as I said, I decided to use the unevaluated form). The second argument is the position of the bottom left corner of the label. The third argument is a vector pointing in the direction along which the baseline of the label should be oriented. The length of this vector is taken as the width of the the label. The fourth argument is the angle (in radians) by which the label is rotated around its baseline. The options Magnification (and also ImageSize) determine the resolution of the rasterized label, and also influence the line breaking in wide labels. Without the option FrameStyle, there will be no frame around the label. In the example, I added Glow[White] to make the appearance of the labels independent of the light sources in the plot. I have already found the non-transparent version of these labels very handy in the past, to annotate angles and lengths in 3D plots where I needed full control over the label orientation. So I hope this transparent version (transparent in the graphical sense, at least) is even more useful... Jens
- References:
- Re: Transparent textures from images
- From: John Fultz <jfultz@wolfram.com>
- Re: Transparent textures from images