<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>XNA Panic</title>
	<atom:link href="https://xnapanic.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://xnapanic.wordpress.com</link>
	<description>... being a journal of my messing about in XNA</description>
	<lastBuildDate>Tue, 30 Dec 2008 18:46:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='xnapanic.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://s0.wp.com/i/buttonw-com.png</url>
		<title>XNA Panic</title>
		<link>https://xnapanic.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://xnapanic.wordpress.com/osd.xml" title="XNA Panic" />
	<atom:link rel='hub' href='https://xnapanic.wordpress.com/?pushpress=hub'/>
	<item>
		<title>Blending in</title>
		<link>https://xnapanic.wordpress.com/2008/12/29/blending-in/</link>
					<comments>https://xnapanic.wordpress.com/2008/12/29/blending-in/#comments</comments>
		
		<dc:creator><![CDATA[karlagius]]></dc:creator>
		<pubDate>Mon, 29 Dec 2008 23:43:29 +0000</pubDate>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Blender]]></category>
		<category><![CDATA[Materials]]></category>
		<guid isPermaLink="false">http://xnapanic.wordpress.com/2008/12/29/blending-in/</guid>

					<description><![CDATA[Download the sample project Alpha blending is, to say the least, handy. In 3d modelling, you can find plenty of uses for it; say, creating hair or leaf textures, translucent or transparent materials, and so on. Needless to say, XNA can do it. For this example, we&#8217;ll use a terrain model that&#8217;s made up of [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a title="Download the sample project [Opens in a new window]" href="http://cid-f9403e3f2829cab9.skydrive.live.com/self.aspx/XNA%20Panic/Blending%20in/AlphaTransparency.zip" target="_blank"><strong>Download the sample project</strong></a></p>
<p>Alpha blending is, to say the least, handy. In 3d modelling, you can find plenty of uses for it; say, creating hair or leaf textures, translucent or transparent materials, and so on. Needless to say, XNA can do it.</p>
<p>For this example, we&#8217;ll use a terrain model that&#8217;s made up of two meshes; a grid, which will represent the ground, and a cube, which will represent the water. The <a title="Blender home page [Opens in a new window]" href="http://www.blender.org/" target="_blank">Blender</a> file for this model can be <a title="Blender file for the terrain model [Opens in a new window]" href="http://cid-f9403e3f2829cab9.skydrive.live.com/self.aspx/XNA%20Panic/Blending%20in/terrain.blend" target="_blank">downloaded here</a>. Yes, I know it&#8217;s ugly.</p>
<p><a href="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/render.png"><img src="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/render-thumb.png?w=240&#038;h=173" alt="The model, rendered in Blend. I know, I know" width="240" height="173" /></a></p>
<p><span id="more-16"></span></p>
<p>The material applied to the water mesh has an alpha of 0.572, which means that it&#8217;s only partially opaque. Materials with low alpha values are more translucent, with 0 being totally transparent. Materials with higher alpha values are more opaque, with 1 being totally solid. Depending on the tools or frameworks in question, these ranges may vary (WPF brushes describe alpha as a value between 0 and 255 for instance, while some tools use a percentage value), but the two extremes hold true in whatever case.</p>
<p>To load the model into our XNA project, we can export it from Blender using the .FBX exporter (File-&gt;Export-&gt;Autodesk FXB). Once the file is exported, we can add it to the content in our project, and draw it as we would any other model. Except that, unfortunately, what we get is this:</p>
<p><a href="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/no-transparency.png"><img src="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/no-transparency-thumb.png?w=240&#038;h=177" alt="Where'd the seabed go?" width="240" height="177" /></a></p>
<h4>Let&#8217;s be clear</h4>
<p>What happened to the transparency? Well, the framework normally just ignores transparencies, since they can be quite an overhead to work out. To enable transparency, you need to enable it explicitly when you are drawing your model:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> GraphicsDevice.RenderState.AlphaBlendEnable = <span style="color:#0000ff;">true</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#008000;">// Do stuff with the mesh effects here.</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#008000;">// Render the mesh.</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> GraphicsDevice.RenderState.AlphaBlendEnable = <span style="color:#0000ff;">false</span>;</pre>
</div>
</div>
<p><strong>Note:</strong> Line 8 is your friend. Never, ever, ever leave that out. Tattoo it on the insides of your eyelids if you have to. For the purposes of this demo it&#8217;s not going to do much, but if you&#8217;re going to go for more complex blending effects, leaving it out could leave you seriously wierded out.</p>
<p>Once we add the above, we get the effect we were after:</p>
<p><a href="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/transparency.png"><img src="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/transparency-thumb.png?w=240&#038;h=180" alt="Better. Sort of." width="240" height="180" /></a></p>
<h4>Function over form</h4>
<p>Of course this is simply a straight forward effect; there are other blending functions you can play with, defined in the aptly named BlendFunction property of the RenderState. You can also combine this with different SourceBlend and DestinationBlend values for various effects. The image below shows the same scene as above, except that the water now looks substantially more horrid:</p>
<p><a href="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/yucky-water.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" src="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/yucky-water-thumb.png?w=244&#038;h=184" border="0" alt="To my untrained eye, that gloop looks highly toxic" width="244" height="184" /></a></p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> GraphicsDevice.RenderState.DestinationBlend = Blend.DestinationColor;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> GraphicsDevice.RenderState.BlendFunction = BlendFunction.Add;</pre>
</div>
</div>
<h4>Order, order</h4>
<p>While I was preparing this model and playing about with transparency, I learned something very important: the order in which the meshes are rendered is vital when you have transparency effects. Any opaque meshes need to be rendered first, and then the transparent ones can be painted on top. Don&#8217;t just take my word for it; in the sample project, load up terrain-1 instead of terrain-2. The files are identical, except for the loading order of the meshes &#8211; the grid is loaded before the cube, so it gets rendered first. No amount of AlphaBlendEnable is going to get you transparency this way.</p>
<p>I&#8217;m not very familiar with Blender, so I don&#8217;t really know if there is a cleaner way to do this, but, to sort this out (pun intended&#8230; sorry) you can open the FBX file in a text editor, and search for &#8220;Connections:&#8221;. In the case of terrain-1.fbx, you should find (around line 30,107):</p>
<p>Connections:  {<br />
Connect: &#8220;OO&#8221;, &#8220;Model::blend_root&#8221;, &#8220;Model::Scene&#8221;<br />
Connect: &#8220;OO&#8221;, &#8220;Model::Cube&#8221;, &#8220;Model::blend_root&#8221;<br />
Connect: &#8220;OO&#8221;, &#8220;Model::Grid&#8221;, &#8220;Model::blend_root&#8221;<br />
Connect: &#8220;OO&#8221;, &#8220;Material::Material.002&#8221;, &#8220;Model::Cube&#8221;<br />
Connect: &#8220;OO&#8221;, &#8220;Material::Material.003&#8221;, &#8220;Model::Grid&#8221;<br />
}</p>
<p>Moving the line saying &#8216;Connect: &#8220;OO&#8221;, &#8220;Model::Grid&#8221;, &#8220;Model::blend_root&#8221;&#8216; to the second position will make the grid mesh load first, solving the transparency problem.</p>
<p>I&#8217;d be interested in knowing if there&#8217;s any way to set this order explicitly from Blender. Any advice on where to look would be greatly appreciated.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fxnapanic.wordpress.com%2f2008%2f12%2f29%2fblending-in%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fxnapanic.wordpress.com%2f2008%2f12%2f29%2fblending-in%2f&amp;bgcolor=7D8B5A&amp;cfgcolor=FFFFFF&amp;cbgcolor=5B6938" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://xnapanic.wordpress.com/2008/12/29/blending-in/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
		
		<media:content url="https://2.gravatar.com/avatar/85f927d2a11fcd5b86cd2332b2f0e242fa4b752ffe35b8797ea23def50a39b30?s=96&#38;d=https%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=X" medium="image">
			<media:title type="html">Amaranth</media:title>
		</media:content>

		<media:content url="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/render-thumb.png" medium="image">
			<media:title type="html">The model, rendered in Blend. I know, I know</media:title>
		</media:content>

		<media:content url="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/no-transparency-thumb.png" medium="image">
			<media:title type="html">Where&#039;d the seabed go?</media:title>
		</media:content>

		<media:content url="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/transparency-thumb.png" medium="image">
			<media:title type="html">Better. Sort of.</media:title>
		</media:content>

		<media:content url="https://xnapanic.wordpress.com/wp-content/uploads/2008/12/yucky-water-thumb.png" medium="image">
			<media:title type="html">To my untrained eye, that gloop looks highly toxic</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fxnapanic.wordpress.com%2f2008%2f12%2f29%2fblending-in%2f&#038;bgcolor=7D8B5A&#038;cfgcolor=FFFFFF&#038;cbgcolor=5B6938" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
	</channel>
</rss>
