Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Transparent textures from images

158 views
Skip to first unread message

John Fultz

unread,
Dec 6, 2011, 5:28:24 AM12/6/11
to
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]]

Sincerely,

John Fultz
jfu...@wolfram.com
User Interface Group
Wolfram Research, Inc.


On Sat, 3 Dec 2011 04:14:01 -0500 (EST), Patrick Scheibe wrote:
> Hi,
>
> I sent this already to Wolfram, but maybe someone wants to try this on a
> windows machine.
> Starting from an image with alpha-channel
>
> data = Table[{Sin[x], Sin[y], 1, Sin[x y]}, {y, 0, 2 Pi,
> 2 Pi/127.}, {x, 0, 2 Pi, 2 Pi/127.}];
> img = Image[data, ColorSpace -> "RGB"]
> AlphaChannel[img]
>
> When I use this now as texture on a cube, I expect the walls to be
> transparent according to the alpha-channel
>
> vtc = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
> coords = {{{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}}, {{0, 0,
> 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1}}, {{1, 0, 0}, {1, 1, 0}, {1,
> 1, 1}, {1, 0, 1}}, {{1, 1, 0}, {0, 1, 0}, {0, 1, 1}, {1, 1,
> 1}}, {{0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1}}, {{0, 0,
> 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}}};
> Graphics3D[{Texture[img],
> Polygon[coords, VertexTextureCoordinates -> Table[vtc, {6}]]},
> Boxed -> False]
>
> This does not work here. I get a cube with a white painting where it
> should be transparent. Using the pure {r,g,b,a} data I get the right
> result:
>
> Graphics3D[{Texture[data],
> Polygon[coords, VertexTextureCoordinates -> Table[vtc, {6}]]},
> Boxed -> False]
>
> Can someone check this or give me a hint which part I understood wrong?
>
> Cheers
> Patrick



Barrie Stokes

unread,
Dec 8, 2011, 5:26:44 AM12/8/11
to
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 <2011120608...@smc.vnet.net>, John

JUN

unread,
Dec 24, 2011, 7:08:18 AM12/24/11
to
On Dec 8, 2:26 am, Barrie Stokes <Barrie.Sto...@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...@smc.vnet.net>, JohnFultz<jfu...@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



0 new messages