<?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/"
	>

<channel>
	<title>Johannes Gotlén</title>
	<atom:link href="http://johannes.gotlen.se/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://johannes.gotlen.se/blog</link>
	<description>٩(- ̮̮̃-̃)۶ interactive hideout</description>
	<lastBuildDate>Wed, 14 Sep 2011 02:44:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=6960</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Meatball Editor in unity3d</title>
		<link>http://johannes.gotlen.se/blog/2011/08/meatball-editor-in-unity3d/</link>
		<comments>http://johannes.gotlen.se/blog/2011/08/meatball-editor-in-unity3d/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 04:20:36 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Workshop]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[marching]]></category>
		<category><![CDATA[meatball]]></category>
		<category><![CDATA[mesh]]></category>
		<category><![CDATA[metaballs]]></category>
		<category><![CDATA[tetraherdons]]></category>
		<category><![CDATA[unity3d]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=217</guid>
		<description><![CDATA[Recent posts inspired me to throw together a meatball editor using marching tetrahedrons. Try to make something nice ;P
]]></description>
			<content:encoded><![CDATA[<p>Recent posts inspired me to throw together a meatball editor using marching tetrahedrons. Try to make something nice ;P<div id="attachment_218" class="wp-caption alignleft" style="width: 475px"><a href="http://spel.gotlen.se/unityMeatball/index.html" rel="shadowbox;width=640;height=480"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/08/ClickForMeatballs2.png" alt="" title="ClickForMeatballs" width="382" height="268" class="alignleft size-full wp-image-228" /></a><p class="wp-caption-text">ClickForMeatballs</p></div></p>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2011/08/meatball-editor-in-unity3d/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Procedural Graphics, Generated meshes in unity3d</title>
		<link>http://johannes.gotlen.se/blog/2011/07/procedural-graphics-generated-meshes-in-unity3d/</link>
		<comments>http://johannes.gotlen.se/blog/2011/07/procedural-graphics-generated-meshes-in-unity3d/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 01:03:01 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Workshop]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[marching]]></category>
		<category><![CDATA[noise]]></category>
		<category><![CDATA[perlin]]></category>
		<category><![CDATA[simplex]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[tetrahedrons]]></category>
		<category><![CDATA[unity3d]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=201</guid>
		<description><![CDATA[A few days ago I was thinking about generating clouds in unity3d, thinking it would be cool to make them as real meshes or a combination of mesh and sprites.
After seeing this, made by my friend and tutor Johannes Norneby. I decided to stick my head into the world of procedural graphics.
Ripping through ancient papers [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I was thinking about generating clouds in unity3d, thinking it would be cool to make them as real meshes or a combination of mesh and sprites.<br />
After seeing <a href="http://johno.se/software/mt.php">this</a>, made by my friend and tutor Johannes Norneby. I decided to stick my head into the world of procedural graphics.<br />
Ripping through ancient papers and good advice, here&#8217;s what I got so far, it takes about a second to generate this setup on my box.<br />
<a href="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedonsSimplexNoise.png" rel="shadowbox[post-201];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedonsSimplexNoise-150x150.png" alt="" title="MarchingTetrahedonsSimplexNoise" width="150" height="150" class="align-left size-thumbnail wp-image-202" /></a><a href="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedonsSimplexNoise2.png" rel="shadowbox[post-201];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedonsSimplexNoise2-150x150.png" alt="" title="MarchingTetrahedronsSimplexNoise2" width="150" height="150" class="align-left size-thumbnail wp-image-203" /></a><a href="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedonsSimplexNoise3.png" rel="shadowbox[post-201];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedonsSimplexNoise3-150x150.png" alt="" title="MarchingTetrahedronsSimplexNoise3" width="150" height="150" class="align-left size-thumbnail wp-image-204" /></a><a href="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedronsSimplexNoise4.png" rel="shadowbox[post-201];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedronsSimplexNoise4-150x150.png" alt="" title="MarchingTetrahedronsSimplexNoise4" width="150" height="150" class="align-left size-thumbnail wp-image-205" /></a><a href="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedronsSimplexNoise5.png" rel="shadowbox[post-201];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/07/MarchingTetrahedronsSimplexNoise5-150x150.png" alt="" title="MarchingTetrahedronsSimplexNoise5" width="150" height="150" class="align-left size-thumbnail wp-image-206" /></a><br />
<br />
<b>Update &#8211; source</b><br />
<span id="more-201"></span></p>
<pre class="brush: csharp; wrap-lines: false">

a pretty straight down c# conversion from the original paper on 

http://paulbourke.net/geometry/polygonise/

using System;
using UnityEngine;

public struct TRIANGLE
{
    public Vector3[] p;// = new XYZ[3];
    public void Init()
    {
        p = new Vector3[3];
    }
}
public struct GRIDCELL
{
    public Vector3[] p;// = new XYZ[8];
    public float[] val;// = new float[8];
    public void Init()
    {
        p = new Vector3[8];
        val = new float[8];
    }

}
public class MarchingTetrahedrons
{
    public static readonly int[] edgeTable = new int[]{
			0x0,			0x109,			0x203,			0x30a,			0x406,			0x50f,			0x605,			0x70c,
			0x80c,			0x905,			0xa0f,			0xb06,			0xc0a,			0xd03,			0xe09,			0xf00,
			0x190,			0x99,			0x393,			0x29a,			0x596,			0x49f,			0x795,			0x69c,
			0x99c,			0x895,			0xb9f,			0xa96,			0xd9a,			0xc93,			0xf99,			0xe90,
			0x230,			0x339,			0x33,			0x13a,			0x636,			0x73f,			0x435,			0x53c,
			0xa3c,			0xb35,			0x83f,			0x936,			0xe3a,			0xf33,			0xc39,			0xd30,
			0x3a0,			0x2a9,			0x1a3,			0xaa,			0x7a6,			0x6af,			0x5a5,			0x4ac,
			0xbac,			0xaa5,			0x9af,			0x8a6,			0xfaa,			0xea3,			0xda9,			0xca0,
			0x460,			0x569,			0x663,			0x76a,			0x66,			0x16f,			0x265,			0x36c,
			0xc6c,			0xd65,			0xe6f,			0xf66,			0x86a,			0x963,			0xa69,			0xb60,
			0x5f0,			0x4f9,			0x7f3,			0x6fa,			0x1f6,			0xff,			0x3f5,			0x2fc,
			0xdfc,			0xcf5,			0xfff,			0xef6,			0x9fa,			0x8f3,			0xbf9,			0xaf0,
			0x650,			0x759,			0x453,			0x55a,			0x256,			0x35f,			0x55,			0x15c,
			0xe5c,			0xf55,			0xc5f,			0xd56,			0xa5a,			0xb53,			0x859,			0x950,
			0x7c0,			0x6c9,			0x5c3,			0x4ca,			0x3c6,			0x2cf,			0x1c5,			0xcc,
			0xfcc,			0xec5,			0xdcf,			0xcc6,			0xbca,			0xac3,			0x9c9,			0x8c0,
			0x8c0,			0x9c9,			0xac3,			0xbca,			0xcc6,			0xdcf,			0xec5,			0xfcc,
			0xcc,			0x1c5,			0x2cf,			0x3c6,			0x4ca,			0x5c3,			0x6c9,			0x7c0,
			0x950,			0x859,			0xb53,			0xa5a,			0xd56,			0xc5f,			0xf55,			0xe5c,
			0x15c,			0x55,			0x35f,			0x256,			0x55a,			0x453,			0x759,			0x650,
			0xaf0,			0xbf9,			0x8f3,			0x9fa,			0xef6,			0xfff,			0xcf5,			0xdfc,
			0x2fc,			0x3f5,			0xff,			0x1f6,			0x6fa,			0x7f3,			0x4f9,			0x5f0,
			0xb60,			0xa69,			0x963,			0x86a,			0xf66,			0xe6f,			0xd65,			0xc6c,
			0x36c,			0x265,			0x16f,			0x66,			0x76a,			0x663,			0x569,			0x460,
			0xca0,			0xda9,			0xea3,			0xfaa,			0x8a6,			0x9af,			0xaa5,			0xbac,
			0x4ac,			0x5a5,			0x6af,			0x7a6,			0xaa,			0x1a3,			0x2a9,			0x3a0,
			0xd30,			0xc39,			0xf33,			0xe3a,			0x936,			0x83f,			0xb35,			0xa3c,
			0x53c,			0x435,			0x73f,			0x636,			0x13a,			0x33,			0x339,			0x230,
			0xe90,			0xf99,			0xc93,			0xd9a,			0xa96,			0xb9f,			0x895,			0x99c,
			0x69c,			0x795,			0x49f,			0x596,			0x29a,			0x393,			0x99,			0x190,
			0xf00,			0xe09,			0xd03,			0xc0a,			0xb06,			0xa0f,			0x905,			0x80c,
			0x70c,			0x605,			0x50f,			0x406,			0x30a,			0x203,			0x109,			0x0,

		};
    public static readonly int[][] triTable = new int[][]{
			new int[]{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1} ,
			new int[]{ 8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1} ,
			new int[]{ 3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1} ,
			new int[]{ 4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1} ,
			new int[]{ 4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1} ,
			new int[]{ 9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1} ,
			new int[]{ 10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1} ,
			new int[]{ 5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1} ,
			new int[]{ 5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1} ,
			new int[]{ 8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1} ,
			new int[]{ 2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1} ,
			new int[]{ 2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1} ,
			new int[]{ 11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1} ,
			new int[]{ 5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1} ,
			new int[]{ 11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1} ,
			new int[]{ 11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1} ,
			new int[]{ 2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1} ,
			new int[]{ 6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1} ,
			new int[]{ 3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1} ,
			new int[]{ 6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1} ,
			new int[]{ 6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1} ,
			new int[]{ 8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1} ,
			new int[]{ 7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1} ,
			new int[]{ 3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1} ,
			new int[]{ 0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1} ,
			new int[]{ 9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1} ,
			new int[]{ 8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1} ,
			new int[]{ 5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1} ,
			new int[]{ 0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1} ,
			new int[]{ 6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1} ,
			new int[]{ 10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1} ,
			new int[]{ 1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1} ,
			new int[]{ 0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1} ,
			new int[]{ 3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1} ,
			new int[]{ 6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1} ,
			new int[]{ 9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1} ,
			new int[]{ 8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1} ,
			new int[]{ 3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1} ,
			new int[]{ 10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1} ,
			new int[]{ 10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1} ,
			new int[]{ 2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1} ,
			new int[]{ 7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1} ,
			new int[]{ 2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1} ,
			new int[]{ 1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1} ,
			new int[]{ 11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1} ,
			new int[]{ 8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1} ,
			new int[]{ 0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1} ,
			new int[]{ 7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1} ,
			new int[]{ 7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1} ,
			new int[]{ 10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1} ,
			new int[]{ 0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1} ,
			new int[]{ 7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1} ,
			new int[]{ 6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1} ,
			new int[]{ 4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1} ,
			new int[]{ 10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1} ,
			new int[]{ 8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1} ,
			new int[]{ 1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1} ,
			new int[]{ 10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1} ,
			new int[]{ 10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1} ,
			new int[]{ 9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1} ,
			new int[]{ 7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1} ,
			new int[]{ 3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1} ,
			new int[]{ 7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1} ,
			new int[]{ 3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1} ,
			new int[]{ 6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1} ,
			new int[]{ 9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1} ,
			new int[]{ 1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1} ,
			new int[]{ 4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1} ,
			new int[]{ 7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1} ,
			new int[]{ 6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1} ,
			new int[]{ 0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1} ,
			new int[]{ 6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1} ,
			new int[]{ 0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1} ,
			new int[]{ 11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1} ,
			new int[]{ 6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1} ,
			new int[]{ 5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1} ,
			new int[]{ 9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1} ,
			new int[]{ 1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1} ,
			new int[]{ 10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1} ,
			new int[]{ 0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1} ,
			new int[]{ 11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1} ,
			new int[]{ 9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1} ,
			new int[]{ 7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1} ,
			new int[]{ 2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1} ,
			new int[]{ 9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1} ,
			new int[]{ 9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1} ,
			new int[]{ 1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1} ,
			new int[]{ 0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1} ,
			new int[]{ 10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1} ,
			new int[]{ 2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1} ,
			new int[]{ 0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1} ,
			new int[]{ 0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1} ,
			new int[]{ 9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1} ,
			new int[]{ 5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1} ,
			new int[]{ 5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1} ,
			new int[]{ 8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1} ,
			new int[]{ 9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1} ,
			new int[]{ 1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1} ,
			new int[]{ 3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1} ,
			new int[]{ 4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1} ,
			new int[]{ 9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1} ,
			new int[]{ 11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1} ,
			new int[]{ 2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1} ,
			new int[]{ 9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1} ,
			new int[]{ 3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1} ,
			new int[]{ 1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1} ,
			new int[]{ 4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1} ,
			new int[]{ 0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1} ,
			new int[]{ 1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ 0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,
			new int[]{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} ,

		};
    public static Vector3 VertexInterp(float isolevel, Vector3 p1, Vector3 p2, float valp1,float valp2)
    {
       Vector3 p;
       float mu = (isolevel - valp1) / (valp2 - valp1);
       if (Math.Abs(isolevel-valp1) < 0.00001)
          return(p1);
       if (Math.Abs(isolevel - valp2) < 0.00001)
          return(p2);
       if (Math.Abs(valp1 - valp2) < 0.00001)
          return(p1);
       mu = (isolevel - valp1) / (valp2 - valp1);
       p.x = p1.x + mu * (p2.x - p1.x);
       p.y = p1.y + mu * (p2.y - p1.y);
       p.z = p1.z + mu * (p2.z - p1.z);

       return(p);
    }
    ///<summary>
    ///    Given a grid cell and an isolevel, calculate the triangular    facets required to represent the isosurface through the cell.    Return the number of triangular facets, the array "triangles"    will be loaded up with the vertices at most 5 triangular facets.  0 will be returned if the grid cell is either totally above    of totally below the isolevel.
    ///</summary>
    ///
    public static int Polygonise(GRIDCELL grid, float isolevel, TRIANGLE[] triangles)
    {
        int i, ntriang;
        int cubeindex;
        Vector3[] vertlist = new Vector3[12];
        //       Determine the index into the edge table which       tells us which vertices are inside of the surface
        cubeindex = 0;
        if (grid.val[0] < isolevel)
        {
            cubeindex |= 1;
        }
        if (grid.val[1] < isolevel)
        {
            cubeindex |= 2;
        }
        if (grid.val[2] < isolevel)
        {
            cubeindex |= 4;
        }
        if (grid.val[3] < isolevel)
        {
            cubeindex |= 8;
        }
        if (grid.val[4] < isolevel)
        {
            cubeindex |= 16;
        }
        if (grid.val[5] < isolevel)
        {
            cubeindex |= 32;
        }
        if (grid.val[6] < isolevel)
        {
            cubeindex |= 64;
        }
        if (grid.val[7] < isolevel)
        {
            cubeindex |= 128;
        }
        // Cube is entirely in/out of the surface
        if (edgeTable[cubeindex] == 0)
        {
            return 0;
        }
        // Find the vertices where the surface intersects the cube
        if ((edgeTable[cubeindex] &#038; 1) != 0x0)
        {
            vertlist[0] = VertexInterp(isolevel, grid.p[0], grid.p[1], grid.val[0], grid.val[1]);
        }
        if ((edgeTable[cubeindex] &#038; 2) != 0x0)
        {
            vertlist[1] = VertexInterp(isolevel, grid.p[1], grid.p[2], grid.val[1], grid.val[2]);
        }
        if ((edgeTable[cubeindex] &#038; 4) != 0x0)
        {
            vertlist[2] = VertexInterp(isolevel, grid.p[2], grid.p[3], grid.val[2], grid.val[3]);
        }
        if ((edgeTable[cubeindex] &#038; <img src='http://johannes.gotlen.se/blog/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> != 0x0)
        {
            vertlist[3] = VertexInterp(isolevel, grid.p[3], grid.p[0], grid.val[3], grid.val[0]);
        }
        if ((edgeTable[cubeindex] &#038; 16) != 0x0)
        {
            vertlist[4] = VertexInterp(isolevel, grid.p[4], grid.p[5], grid.val[4], grid.val[5]);
        }
        if ((edgeTable[cubeindex] &#038; 32) != 0x0)
        {
            vertlist[5] = VertexInterp(isolevel, grid.p[5], grid.p[6], grid.val[5], grid.val[6]);
        }
        if ((edgeTable[cubeindex] &#038; 64) != 0x0)
        {
            vertlist[6] = VertexInterp(isolevel, grid.p[6], grid.p[7], grid.val[6], grid.val[7]);
        }
        if ((edgeTable[cubeindex] &#038; 128) != 0x0)
        {
            vertlist[7] = VertexInterp(isolevel, grid.p[7], grid.p[4], grid.val[7], grid.val[4]);
        }
        if ((edgeTable[cubeindex] &#038; 256) != 0x0)
        {
            vertlist[8] = VertexInterp(isolevel, grid.p[0], grid.p[4], grid.val[0], grid.val[4]);
        }
        if ((edgeTable[cubeindex] &#038; 512) != 0x0)
        {
            vertlist[9] = VertexInterp(isolevel, grid.p[1], grid.p[5], grid.val[1], grid.val[5]);
        }
        if ((edgeTable[cubeindex] &#038; 1024) != 0x0)
        {
            vertlist[10] = VertexInterp(isolevel, grid.p[2], grid.p[6], grid.val[2], grid.val[6]);
        }
        if ((edgeTable[cubeindex] &#038; 2048) != 0x0)
        {
            vertlist[11] = VertexInterp(isolevel, grid.p[3], grid.p[7], grid.val[3], grid.val[7]);
        }
        // Create the triangle
        ntriang = 0;
        for (i = 0; triTable[cubeindex][i] != -1; i += 3)
        {
            triangles[ntriang].p[2] = vertlist[triTable[cubeindex][i]];
            triangles[ntriang].p[1] = vertlist[triTable[cubeindex][i + 1]];
            triangles[ntriang].p[0] = vertlist[triTable[cubeindex][i + 2]];
            (ntriang)++;
        }
        return ntriang;
    }

}
</pre>
<pre class="brush: csharp; wrap-lines: false">
using UnityEngine;
using System;
public class Noise
{
    public class ClassicNoise
    { // Classic Perlin noise in 3D, for comparison
        private static int[][] grad3 = new int[][]{new int[]{1,1,0},new int[]{-1,1,0},new int[]{1,-1,0},new int[]{-1,-1,0},
                           new int[]{1,0,1},new int[]{-1,0,1},new int[]{1,0,-1},new int[]{-1,0,-1},
                           new int[]{0,1,1},new int[]{0,-1,1},new int[]{0,1,-1},new int[]{0,-1,-1}};
        private static int[] p = {151,160,137,91,90,15,
  131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
        // To remove the need for index wrapping, float the permutation table length
        private static int[] perm = new int[512];
        public static void Init() { for (int i = 0; i < 512; i++) perm[i] = p[i &#038; 255]; }

        // This method is a *lot* faster than using (int)Mathf.floor(x)
        private static int fastfloor(float x)
        {
            return x > 0 ? (int)x : (int)x - 1;
        }
        private static float dot(int[] g, float x, float y, float z)
        {
            return g[0] * x + g[1] * y + g[2] * z;
        }
        private static float mix(float a, float b, float t)
        {
            return (1 - t) * a + t * b;
        }
        private static float fade(float t)
        {
            return t * t * t * (t * (t * 6 - 15) + 10);
        }
        // Classic Perlin noise, 3D version
        public static float noise(float x, float y, float z)
        {
            // Find unit grid cell containing point
            int X = fastfloor(x);
            int Y = fastfloor(y);
            int Z = fastfloor(z);
            // Get relative xyz coordinates of point within that cell
            x = x - X;
            y = y - Y;
            z = z - Z;
            // Wrap the integer cells at 255 (smaller integer period can be introduced here)
            X = X &#038; 255;
            Y = Y &#038; 255;
            Z = Z &#038; 255;
            // Calculate a set of eight hashed gradient indices
            int gi000 = perm[X + perm[Y + perm[Z]]] % 12;
            int gi001 = perm[X + perm[Y + perm[Z + 1]]] % 12;
            int gi010 = perm[X + perm[Y + 1 + perm[Z]]] % 12;
            int gi011 = perm[X + perm[Y + 1 + perm[Z + 1]]] % 12;
            int gi100 = perm[X + 1 + perm[Y + perm[Z]]] % 12;
            int gi101 = perm[X + 1 + perm[Y + perm[Z + 1]]] % 12;
            int gi110 = perm[X + 1 + perm[Y + 1 + perm[Z]]] % 12;
            int gi111 = perm[X + 1 + perm[Y + 1 + perm[Z + 1]]] % 12;
            // The gradients of each corner are now:
            // g000 = grad3[gi000];
            // g001 = grad3[gi001];
            // g010 = grad3[gi010];
            // g011 = grad3[gi011];
            // g100 = grad3[gi100];
            // g101 = grad3[gi101];
            // g110 = grad3[gi110];
            // g111 = grad3[gi111];
            // Calculate noise contributions from each of the eight corners
            float n000 = dot(grad3[gi000], x, y, z);
            float n100 = dot(grad3[gi100], x - 1, y, z);
            float n010 = dot(grad3[gi010], x, y - 1, z);
            float n110 = dot(grad3[gi110], x - 1, y - 1, z);
            float n001 = dot(grad3[gi001], x, y, z - 1);
            float n101 = dot(grad3[gi101], x - 1, y, z - 1);
            float n011 = dot(grad3[gi011], x, y - 1, z - 1);
            float n111 = dot(grad3[gi111], x - 1, y - 1, z - 1);
            // Compute the fade curve value for each of x, y, z
            float u = fade(x);
            float v = fade(y);
            float w = fade(z);
            // Interpolate along x the contributions from each of the corners
            float nx00 = mix(n000, n100, u);
            float nx01 = mix(n001, n101, u);
            float nx10 = mix(n010, n110, u);
            float nx11 = mix(n011, n111, u);
            // Interpolate the four results along y
            float nxy0 = mix(nx00, nx10, v);
            float nxy1 = mix(nx01, nx11, v);
            // Interpolate the two last results along z
            float nxyz = mix(nxy0, nxy1, w);
            return nxyz;
        }
    }
    public class SimplexNoise
    { // Simplex noise in 2D, 3D and 4D
        private static int[][] grad3 = new int[][]{
            new int[]{1,1,0},new int[]{-1,1,0},new int[]{1,-1,0},new int[]{-1,-1,0},
            new int[]{1,0,1},new int[]{-1,0,1},new int[]{1,0,-1},new int[]{-1,0,-1},
            new int[]{0,1,1},new int[]{0,-1,1},new int[]{0,1,-1},new int[]{0,-1,-1}
        };
        private static int[][] grad4 = new int[][]{
            new int[]{0,1,1,1},  new int[]{0,1,1,-1},  new int[]{0,1,-1,1}, new int[]{0,1,-1,-1},
            new int[]{0,-1,1,1}, new int[]{0,-1,1,-1}, new int[]{0,-1,-1,1},new int[]{0,-1,-1,-1},
            new int[]{1,0,1,1},  new int[]{1,0,1,-1},  new int[]{1,0,-1,1}, new int[]{1,0,-1,-1},
            new int[]{-1,0,1,1}, new int[]{-1,0,1,-1}, new int[]{-1,0,-1,1},new int[]{-1,0,-1,-1},
            new int[]{1,1,0,1},  new int[]{1,1,0,-1},  new int[]{1,-1,0,1}, new int[]{1,-1,0,-1},
            new int[]{-1,1,0,1}, new int[]{-1,1,0,-1}, new int[]{-1,-1,0,1},new int[]{-1,-1,0,-1},
            new int[]{1,1,1,0},  new int[]{1,1,-1,0},  new int[]{1,-1,1,0}, new int[]{1,-1,-1,0},
            new int[]{-1,1,1,0}, new int[]{-1,1,-1,0}, new int[]{-1,-1,1,0},new int[]{-1,-1,-1,0}
        };
        private static int[] p = {151,160,137,91,90,15,
 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
        // To remove the need for index wrapping, float the permutation table length
        private static int[] perm = new int[512];
        public static void Init() { for (int i = 0; i < 512; i++) perm[i] = p[i &#038; 255]; }
        // A lookup table to traverse the simplex around a given point in 4D.
        // Details can be found where this table is used, in the 4D noise method.
        private static int[][] simplex = {
   new int[]{0,1,2,3},new int[]{0,1,3,2},new int[]{0,0,0,0},new int[]{0,2,3,1},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{1,2,3,0},
   new int[]{0,2,1,3},new int[]{0,0,0,0},new int[]{0,3,1,2},new int[]{0,3,2,1},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{1,3,2,0},
   new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},
   new int[]{1,2,0,3},new int[]{0,0,0,0},new int[]{1,3,0,2},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{2,3,0,1},new int[]{2,3,1,0},
   new int[]{1,0,2,3},new int[]{1,0,3,2},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{2,0,3,1},new int[]{0,0,0,0},new int[]{2,1,3,0},
   new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},
   new int[]{2,0,1,3},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{3,0,1,2},new int[]{3,0,2,1},new int[]{0,0,0,0},new int[]{3,1,2,0},
   new int[]{2,1,0,3},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{0,0,0,0},new int[]{3,1,0,2},new int[]{0,0,0,0},new int[]{3,2,0,1},new int[]{3,2,1,0}};
        // This method is a *lot* faster than using (int)Mathf.floor(x)
        private static int fastfloor(float x)
        {
            return x > 0 ? (int)x : (int)x - 1;
        }
        private static float dot(int[] g, float x, float y)
        {
            return g[0] * x + g[1] * y;
        }
        private static float dot(int[] g, float x, float y, float z)
        {
            return g[0] * x + g[1] * y + g[2] * z;
        }
        private static float dot(int[] g, float x, float y, float z, float w)
        {
            return g[0] * x + g[1] * y + g[2] * z + g[3] * w;
        }
        // 2D simplex noise
        public static float noise(float xin, float yin)
        {
            float n0, n1, n2; // Noise contributions from the three corners
            // Skew the input space to determine which simplex cell we're in
            float F2 = 0.5f * (Mathf.Sqrt(3.0f) - 1.0f);
            float s = (xin + yin) * F2; // Hairy factor for 2D
            int i = fastfloor(xin + s);
            int j = fastfloor(yin + s);
            float G2 = (3.0f - Mathf.Sqrt(3.0f)) / 6.0f;
            float t = (i + j) * G2;
            float X0 = i - t; // Unskew the cell origin back to (x,y) space
            float Y0 = j - t;
            float x0 = xin - X0; // The x,y distances from the cell origin
            float y0 = yin - Y0;
            // For the 2D case, the simplex shape is an equilateral triangle.
            // Determine which simplex we are in.
            int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
            if (x0 > y0) { i1 = 1; j1 = 0; } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
            else { i1 = 0; j1 = 1; }         // upper triangle, YX order: (0,0)->(0,1)->(1,1)
            // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
            // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
            // c = (3-sqrt(3))/6
            float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
            float y1 = y0 - j1 + G2;
            float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
            float y2 = y0 - 1.0f + 2.0f * G2;
            // Work out the hashed gradient indices of the three simplex corners
            int ii = i &#038; 255;
            int jj = j &#038; 255;
            int gi0 = perm[ii + perm[jj]] % 12;
            int gi1 = perm[ii + i1 + perm[jj + j1]] % 12;
            int gi2 = perm[ii + 1 + perm[jj + 1]] % 12;
            // Calculate the contribution from the three corners
            float t0 = 0.5f - x0 * x0 - y0 * y0;
            if (t0 < 0) n0 = 0.0f;
            else
            {
                t0 *= t0;
                n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
            }
            float t1 = 0.5f - x1 * x1 - y1 * y1;
            if (t1 < 0) n1 = 0.0f;
            else
            {
                t1 *= t1;
                n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
            }
            float t2 = 0.5f - x2 * x2 - y2 * y2;
            if (t2 < 0) n2 = 0.0f;
            else
            {
                t2 *= t2;
                n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
            }
            // Add contributions from each corner to get the final noise value.
            // The result is scaled to return values in the interval [-1,1].
            return 70.0f * (n0 + n1 + n2);
        }
        // 3D simplex noise
        public static float noise(float xin, float yin, float zin)
        {
            float n0, n1, n2, n3; // Noise contributions from the four corners
            // Skew the input space to determine which simplex cell we're in
            float F3 = 1.0f / 3.0f;
            float s = (xin + yin + zin) * F3; // Very nice and simple skew factor for 3D
            int i = fastfloor(xin + s);
            int j = fastfloor(yin + s);
            int k = fastfloor(zin + s);
            float G3 = 1.0f / 6.0f; // Very nice and simple unskew factor, too
            float t = (i + j + k) * G3;
            float X0 = i - t; // Unskew the cell origin back to (x,y,z) space
            float Y0 = j - t;
            float Z0 = k - t;
            float x0 = xin - X0; // The x,y,z distances from the cell origin
            float y0 = yin - Y0;
            float z0 = zin - Z0;
            // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
            // Determine which simplex we are in.
            int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
            int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
            if (x0 >= y0)
            {
                if (y0 >= z0)
                { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } // X Y Z order
                else if (x0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } // X Z Y order
                else { i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } // Z X Y order
            }
            else
            { // x0<y0
                if (y0 < z0) { i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1; } // Z Y X order
                else if (x0 < z0) { i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1; } // Y Z X order
                else { i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } // Y X Z order
            }
            //  A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
            //  a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
            //  a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
            //  c = 1/6.
            float x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
            float y1 = y0 - j1 + G3;
            float z1 = z0 - k1 + G3;
            float x2 = x0 - i2 + 2.0f * G3; // Offsets for third corner in (x,y,z) coords
            float y2 = y0 - j2 + 2.0f * G3;
            float z2 = z0 - k2 + 2.0f * G3;
            float x3 = x0 - 1.0f + 3.0f * G3; // Offsets for last corner in (x,y,z) coords
            float y3 = y0 - 1.0f + 3.0f * G3;
            float z3 = z0 - 1.0f + 3.0f * G3;
            // Work out the hashed gradient indices of the four simplex corners
            int ii = i &#038; 255;
            int jj = j &#038; 255;
            int kk = k &#038; 255;
            int gi0 = perm[ii + perm[jj + perm[kk]]] % 12;
            int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]] % 12;
            int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]] % 12;
            int gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] % 12;
            // Calculate the contribution from the four corners
            float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0;
            if (t0 < 0) n0 = 0.0f;
            else
            {
                t0 *= t0;
                n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
            }
            float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1;
            if (t1 < 0) n1 = 0.0f;
            else
            {
                t1 *= t1;
                n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
            }
            float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2;
            if (t2 < 0) n2 = 0.0f;
            else
            {
                t2 *= t2;
                n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
            }
            float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3;
            if (t3 < 0) n3 = 0.0f;
            else
            {
                t3 *= t3;
                n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
            }
            // Add contributions from each corner to get the final noise value.
            // The result is scaled to stay just inside [-1,1]
            return 32.0f * (n0 + n1 + n2 + n3);
        }
        // 4D simplex noise
        float noise(float x, float y, float z, float w)
        {
            // The skewing and unskewing factors are hairy again for the 4D case
            float F4 = (Mathf.Sqrt(5.0f) - 1.0f) / 4.0f;
            float G4 = (5.0f - Mathf.Sqrt(5.0f)) / 20.0f;
            float n0, n1, n2, n3, n4; // Noise contributions from the five corners
            // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
            float s = (x + y + z + w) * F4; // Factor for 4D skewing
            int i = fastfloor(x + s);
            int j = fastfloor(y + s);
            int k = fastfloor(z + s);
            int l = fastfloor(w + s);
            float t = (i + j + k + l) * G4; // Factor for 4D unskewing
            float X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
            float Y0 = j - t;
            float Z0 = k - t;
            float W0 = l - t;
            float x0 = x - X0; // The x,y,z,w distances from the cell origin
            float y0 = y - Y0;
            float z0 = z - Z0;
            float w0 = w - W0;
            // For the 4D case, the simplex is a 4D shape I won't even try to describe.
            // To find out which of the 24 possible simplices we're in, we need to
            // determine the magnitude ordering of x0, y0, z0 and w0.
            // The method below is a good way of finding the ordering of x,y,z,w and
            // then find the correct traversal order for the simplex we?re in.
            // First, six pair-wise comparisons are performed between each possible pair
            // of the four coordinates, and the results are used to add up binary bits
            // for an integer index.
            int c1 = (x0 > y0) ? 32 : 0;
            int c2 = (x0 > z0) ? 16 : 0;
            int c3 = (y0 > z0) ? 8 : 0;
            int c4 = (x0 > w0) ? 4 : 0;
            int c5 = (y0 > w0) ? 2 : 0;
            int c6 = (z0 > w0) ? 1 : 0;
            int c = c1 + c2 + c3 + c4 + c5 + c6;
            int i1, j1, k1, l1; // The integer offsets for the second simplex corner
            int i2, j2, k2, l2; // The integer offsets for the third simplex corner
            int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
            // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
            // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
            // impossible. Only the 24 indices which have non-zero entries make any sense.
            // We use a thresholding to set the coordinates in turn from the largest magnitude.
            // The number 3 in the "simplex" array is at the position of the largest coordinate.
            i1 = simplex[c][0] >= 3 ? 1 : 0;
            j1 = simplex[c][1] >= 3 ? 1 : 0;
            k1 = simplex[c][2] >= 3 ? 1 : 0;
            l1 = simplex[c][3] >= 3 ? 1 : 0;
            // The number 2 in the "simplex" array is at the second largest coordinate.
            i2 = simplex[c][0] >= 2 ? 1 : 0;
            j2 = simplex[c][1] >= 2 ? 1 : 0;
            k2 = simplex[c][2] >= 2 ? 1 : 0;
            l2 = simplex[c][3] >= 2 ? 1 : 0;
            // The number 1 in the "simplex" array is at the second smallest coordinate.
            i3 = simplex[c][0] >= 1 ? 1 : 0;
            j3 = simplex[c][1] >= 1 ? 1 : 0;
            k3 = simplex[c][2] >= 1 ? 1 : 0;
            l3 = simplex[c][3] >= 1 ? 1 : 0;
            // The fifth corner has all coordinate offsets = 1, so no need to look that up.
            float x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
            float y1 = y0 - j1 + G4;
            float z1 = z0 - k1 + G4;
            float w1 = w0 - l1 + G4;
            float x2 = x0 - i2 + 2.0f * G4; // Offsets for third corner in (x,y,z,w) coords
            float y2 = y0 - j2 + 2.0f * G4;
            float z2 = z0 - k2 + 2.0f * G4;
            float w2 = w0 - l2 + 2.0f * G4;
            float x3 = x0 - i3 + 3.0f * G4; // Offsets for fourth corner in (x,y,z,w) coords
            float y3 = y0 - j3 + 3.0f * G4;
            float z3 = z0 - k3 + 3.0f * G4;
            float w3 = w0 - l3 + 3.0f * G4;
            float x4 = x0 - 1.0f + 4.0f * G4; // Offsets for last corner in (x,y,z,w) coords
            float y4 = y0 - 1.0f + 4.0f * G4;
            float z4 = z0 - 1.0f + 4.0f * G4;
            float w4 = w0 - 1.0f + 4.0f * G4;
            // Work out the hashed gradient indices of the five simplex corners
            int ii = i &#038; 255;
            int jj = j &#038; 255;
            int kk = k &#038; 255;
            int ll = l &#038; 255;
            int gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] % 32;
            int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]] % 32;
            int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]] % 32;
            int gi3 = perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]] % 32;
            int gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32;
            // Calculate the contribution from the five corners
            float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
            if (t0 < 0) n0 = 0.0f;
            else
            {
                t0 *= t0;
                n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
            }
            float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
            if (t1 < 0) n1 = 0.0f;
            else
            {
                t1 *= t1;
                n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
            }
            float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
            if (t2 < 0) n2 = 0.0f;
            else
            {
                t2 *= t2;
                n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
            }
            float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
            if (t3 < 0) n3 = 0.0f;
            else
            {
                t3 *= t3;
                n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
            }
            float t4 = 0.6f - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
            if (t4 < 0) n4 = 0.0f;
            else
            {
                t4 *= t4;
                n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
            }
            // Sum up and scale the result to cover the range [-1,1]
            return 27.0f * (n0 + n1 + n2 + n3 + n4);
        }
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2011/07/procedural-graphics-generated-meshes-in-unity3d/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Artsy Update</title>
		<link>http://johannes.gotlen.se/blog/2011/06/artsy-update/</link>
		<comments>http://johannes.gotlen.se/blog/2011/06/artsy-update/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 07:53:04 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=195</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><a href="http://johannes.gotlen.se/blog/wp-content/uploads/2011/06/somehead4.png" rel="shadowbox[post-195];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/06/somehead4-150x150.png" alt="" title="somehead4" width="150" height="150" class="alignleft size-thumbnail wp-image-199" /></a><a href="http://johannes.gotlen.se/blog/wp-content/uploads/2011/06/airbaseoutpost.png" rel="shadowbox[post-195];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/06/airbaseoutpost-150x150.png" alt="" title="airbaseoutpost" width="150" height="150" class="alignleft size-thumbnail wp-image-192" /></a><a href="http://johannes.gotlen.se/blog/wp-content/uploads/2011/06/dfd2.png" rel="shadowbox[post-195];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2011/06/dfd2-150x150.png" alt="" title="dfd2" width="150" height="150" class="alignleft size-thumbnail wp-image-193" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2011/06/artsy-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Refraction? Demo</title>
		<link>http://johannes.gotlen.se/blog/2010/11/refraction/</link>
		<comments>http://johannes.gotlen.se/blog/2010/11/refraction/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 00:07:10 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Workshop]]></category>
		<category><![CDATA[cg]]></category>
		<category><![CDATA[poisson disc]]></category>
		<category><![CDATA[refraction map]]></category>
		<category><![CDATA[shader]]></category>
		<category><![CDATA[unity3d]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=153</guid>
		<description><![CDATA[This weekend I ended up reading about cg shaders and writing a little experiment; I wanted to apply a lens effect in front of a texture, by using a fragment and vertex program.
When time was up I still had some trouble with the quality of the resulting image;

Please leave a response if you have any [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_7" class="wp-caption align-none" style="width: 330px"><br />
<a href="http://spel.gotlen.se/unityRefraction/index.html" rel="shadowbox;width=640;height=480"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/11/refraction_clickToPlay.jpg" alt="Click to load unity player" title="shader, test" width="434" height="328" class="align-left size-full wp-image-185" /><br />
</a><br />
<p class="wp-caption-text">requires unity3d web player, click to play</p></div><br />
This weekend I ended up reading about cg shaders and writing a little experiment; I wanted to apply a lens effect in front of a texture, by using a fragment and vertex program.<br />
When time was up I still had some trouble with the quality of the resulting image;<br />
<a href="http://johannes.gotlen.se/blog/wp-content/uploads/2010/11/artifact.png" rel="shadowbox[post-153];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/11/artifact-150x150.png" alt="" title="artifact" width="150" height="150" class="align-left size-thumbnail wp-image-157" /></a><br />
Please leave a response if you have any ideas of how to make text readable behind the distortion.</p>
<p><strong>Update </strong><br />
With some feedback and a good read from Texel in #unity3d@ freenode; I implemented a poisson disc filter to remove some of the artifacts;<br />
here Is the before and after footage.<br />
<a href="http://johannes.gotlen.se/blog/wp-content/uploads/2010/11/before_after_poissonDisc.png" rel="shadowbox[post-153];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/11/before_after_poissonDisc-150x150.png" alt="" title="before_after_poissonDisc" width="150" height="150" class="align-left size-thumbnail wp-image-177" /></a><br />
<strong>Update </strong>new shader program<br />
<strong>Update </strong>added art made by <a href="http://www.kurotorobert.com" target="_blank">Kuroto Robert</a><br />
Here is the unity material with the shader program included(with poissonDisc),<br />
<span id="more-153"></span></p>
<pre class="brush:as3; wrap-lines: false">
// Per pixel refraction.
// Uses a normal map and vertex normals to distort the image behind
Shader "FX/Glass/RefractionMap"
{
Properties
{

	_ReflectionStartVal ("Reflection", Range (0.0,1.0)) = 0.5	

	_BumpAmt ("Glass Distortion", Range (0.01,1.0)) = 0.5

	_DiscRadius("Normal Blur Radius", Float) = 1
	_MainTexWidth ("NormalMapWidth", Float) = 512
	_MainTexHeight("NormalMapHeight", Float) = 512
	_MainTex ("Background", 2D) = "white" {}
	_BumpMap ("NormalInfo", 2D) = "bump" {}

	_MaskTexture ("FrameTexture", 2D) = "white" {}

	_Color ("Main Color", Color) = (1,1,1,1)
	_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
	_Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
}

Category {
	// We must be transparent, so other objects are drawn before this one.
	Tags { "Queue"="Transparent" "RenderType"="Opaque" }
	SubShader
	{
		Pass
		{
			Name "BASE"
			//Blend SrcAlpha OneMinusSrcAlpha
			//Tags {"LightMode" = "Always"}

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest

			#include "UnityCG.cginc"

			struct v2f {
				float4 pos : SV_POSITION;
				float2	uv		: TEXCOORD0;
				float2	uv2		: TEXCOORD1;
				float3	I		: TEXCOORD2;
				float3	TtoW0 	: TEXCOORD3;
				float3	TtoW1	: TEXCOORD4;
				float3	TtoW2	: TEXCOORD5;
			};

			uniform float4 _MainTex_ST, _BumpMap_ST;
			uniform half _BumpAmt;
			v2f vert(appdata_tan v)
			{
				v2f o;
				o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
				o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
				o.uv2 = TRANSFORM_TEX(v.texcoord,_BumpMap);

				o.I = -WorldSpaceViewDir( v.vertex );

				TANGENT_SPACE_ROTATION;
				o.TtoW0 = mul(rotation, _Object2World[0].xyz * unity_Scale.w);
				o.TtoW1 = mul(rotation, _Object2World[1].xyz * unity_Scale.w) ;
				o.TtoW2 = mul(rotation, _Object2World[2].xyz * unity_Scale.w);

				return o;
			}

			uniform sampler2D _BumpMap;
			uniform sampler2D _MainTex;
			uniform samplerCUBE _Cube;
			uniform float4 _ReflectColor;
			uniform float4 _Color;
			uniform float _ReflectionStartVal;
			uniform float _MainTexHeight;
			uniform float _MainTexWidth;
			uniform float _DiscRadius;
			uniform float _InnerDiscRadius;
			half2 PixelSize()
			{
				return 1f / float2(_MainTexWidth,_MainTexHeight);
			}
			half4 MultiSample5(sampler2D source, half2 pixelSize, half radius, half2 uvTarget )
			{
				float acctualSize = pixelSize * radius;
				half4 resultColor = tex2D(source, uvTarget);
				resultColor += tex2D(source, uvTarget + half2(-0.707106781f, 0.707106781f) * acctualSize); //upLeft
				resultColor += tex2D(source, uvTarget + half2(0.707106781f, 0.707106781f) * acctualSize); //upRight
				resultColor += tex2D(source, uvTarget + half2(-0.707106781f, -0.707106781f) * acctualSize); //downLeft
				resultColor += tex2D(source, uvTarget + half2(0.707106781f, -0.707106781f) * acctualSize); //downRight
				return resultColor * 0.2f; // /5;
			}
			float4 frag (v2f i) : COLOR
			{
				// Sample and expand the normal map texture
				half4 normal = UnpackNormal(MultiSample5(
					_BumpMap,
					PixelSize(),
					_DiscRadius,
					i.uv2
				));

				// transform normal to world space
				half3 wn;
				wn.x = dot(i.TtoW0, normal.xyz);
				wn.y = dot(i.TtoW1, normal.xyz);
				wn.z = dot(i.TtoW2, normal.xyz);

				//create pointSpaceMatrix from world space normal and position

				// calculate reflection vector in world space
				half3 r = reflect(i.I, wn);

				half lookDirectionDotNormal = dot(normalize(i.I),normalize( wn));

				//glass distortion
				half4 texcol = tex2D(_MainTex, i.uv - normal * lookDirectionDotNormal *_BumpAmt);

				half reflectionAmnt = min(1.0f,max(0.0f, _ReflectionStartVal + lookDirectionDotNormal));

				//half4 c = UNITY_LIGHTMODEL_AMBIENT * texcol;
				//c.rgb *= 2;
				half4 c = _Color * texcol;
				half4 reflcolor = texCUBE(_Cube, r) * _ReflectColor * reflectionAmnt;
				return c + reflcolor;
			}

			ENDCG
		}

	}
	// ------------------------------------------------------------------
	// Fallback for older cards and Unity non-Pro
	SubShader {
		Blend DstColor Zero
		Pass {
			Name "BASE"
			SetTexture [_BumpMap] {	combine texture }
		}
	}

}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2010/11/refraction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Animation Editor; Super Sneak Peek</title>
		<link>http://johannes.gotlen.se/blog/2010/10/animation-editor-super-sneak-peek/</link>
		<comments>http://johannes.gotlen.se/blog/2010/10/animation-editor-super-sneak-peek/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 19:39:41 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Workshop]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[manager]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[sprite]]></category>
		<category><![CDATA[unity3d]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=124</guid>
		<description><![CDATA[I will be using this version for a little while and post another version as soon as I accumulated enough problems and fixes;
Please post feedback and I&#8217;ll add it to my change list;
Download Alpha Trials  v0.01
Alfa Trials v0.01
==== Copy files ====
the sprite editor classes goes into your unity editor folder
the sprite manager assembly goes [...]]]></description>
			<content:encoded><![CDATA[<p>I will be using this version for a little while and post another version as soon as I accumulated enough problems and fixes;<br />
Please post feedback and I&#8217;ll add it to my change list;<br />
<a href="http://spel.gotlen.se/unityPlugins/SpriteSheetEditor_a_0.01.rar">Download Alpha Trials  v0.01</a></p>
<p>Alfa Trials v0.01</p>
<p>==== Copy files ====<br />
the sprite editor classes goes into your unity editor folder<br />
the sprite manager assembly goes anywhere but the editor folder</p>
<p>====  Example File Structure ====<br />
unityProject/Assets/<br />
		SpriteManager.dll</p>
<p>unityProject/Assets/Editor/<br />
			spriteAnimationEditor.dll<br />
			SpriteAnimationInspector.cs<br />
			SpriteEditorWindow.cs</p>
<p>==== Some Things that you should be able to do ====<br />
- Using the animation editor to store and load animations into a SpriteManager Object<br />
- Set individual collum/row count for every animation<br />
- Set individual default framerates for each animation<br />
- Make a sprite auto update size according to pixel ratio and animation<br />
- Set auto update pixel ratio on a sprite<br />
- Queue Animations<br />
- Pingpong animation playback</p>
<p>==== Sprite Manager Change Overview ====<br />
- Added Class UVAnimationData, Sprite Manager now stores all animations in one place instead of inside each sprite<br />
- Added Class UVAnimationPlayer, Every Sprite now has its own UVAnimationPlayer object that handles animation playback<br />
- Made some minor internal changes to up performance.</p>
<p>==== Special Notes ====<br />
Please post questions and feedback to http://johannes.gotlen.se</p>
<p>Good Luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2010/10/animation-editor-super-sneak-peek/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Sprite Animation Editor for Unity Sprite Manager</title>
		<link>http://johannes.gotlen.se/blog/2010/10/sprite-animation-editor-for-uniy-sprite-manager/</link>
		<comments>http://johannes.gotlen.se/blog/2010/10/sprite-animation-editor-for-uniy-sprite-manager/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 20:28:45 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Workshop]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Sprite Manager]]></category>
		<category><![CDATA[unity3d]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=112</guid>
		<description><![CDATA[I&#8217;ve been working on quite a few things since I last posted. The one thing that looks like it will be completely finished first is a sprite animation editor that works with Sprite Manager (1)
here is a snapshot of the interface.

Update
After studying the &#8220;Sprite Manager 1&#8243; code; I realized it had some needs of improvement [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on quite a few things since I last posted. The one thing that looks like it will be completely finished first is a sprite animation editor that works with <a href="http://www.unifycommunity.com/wiki/index.php?title=SpriteManager"><del datetime="2010-10-19T17:26:56+00:00">Sprite Manager (1)</del></a><br />
here is a snapshot of the interface.<br />
<a href="http://johannes.gotlen.se/blog/wp-content/uploads/2010/10/SpriteEditor2.png" rel="shadowbox[post-112];player=img;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/10/SpriteEditor2-300x254.png" alt="" title="SpriteEditor" width="300" height="254" class="align-none size-medium wp-image-113" /></a><br />
<strong>Update</strong><br />
After studying the &#8220;Sprite Manager 1&#8243; code; I realized it had some needs of improvement and change to properly fit my needs; hence I&#8217;ll also be releasing a modified version of &#8220;Sprite Manager&#8221; <del datetime="2010-10-22T13:09:00+00:00">ETA next couple of days</del><br />
<strong>Update</strong><br />
Alfa Download; Now Available <a href="http://johannes.gotlen.se/blog/2010/10/animation-editor-super-sneak-peek/"> Here&#8230; </a></p>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2010/10/sprite-animation-editor-for-uniy-sprite-manager/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Boulder Riots Now available on iPhone!</title>
		<link>http://johannes.gotlen.se/blog/2010/07/very-soon/</link>
		<comments>http://johannes.gotlen.se/blog/2010/07/very-soon/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 23:01:16 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/2010/07/very-soon/</guid>
		<description><![CDATA[

Project Home
]]></description>
			<content:encoded><![CDATA[<p><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/07/Screenshot-2010.07.16-00.51.041.png" alt="" title="Screenshot 2010.07.16 00.51.04" class="alignnone size-medium wp-image-101" /><br />
<img src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/07/Screen-shot-2010-07-13-at-4.42.20-PM.png" alt="" title="Screen shot 2010-07-13 at 4.42.20 PM" width="480" height="321" class="alignnone size-full wp-image-107" /><br />
<a href="http://blog.blastdoor.se/">Project Home</a></p>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2010/07/very-soon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pulka Slider, the next-gen game of your dreams</title>
		<link>http://johannes.gotlen.se/blog/2010/05/pulka-slider/</link>
		<comments>http://johannes.gotlen.se/blog/2010/05/pulka-slider/#comments</comments>
		<pubDate>Mon, 17 May 2010 22:03:39 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Workshop]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[collision]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[sprite]]></category>
		<category><![CDATA[z-sorting]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=52</guid>
		<description><![CDATA[Code; Johannes Gotlén
Graphics; Emelie Falk Renström
Pressing &#8216;CR&#8217; reveals a highly advanced level editor&#8230;
]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_53" class="wp-caption alignnone" style="width: 276px"><a href="http://spel.gotlen.se/SquareGame/index.html" rel="shadowbox;height=420;width=580;"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/05/pulkaSlider.png" alt="click to try" title="pulkaSlider" width="266" height="234" class="size-full wp-image-53" /></a><p class="wp-caption-text">click to try</p></div><br />
Code; Johannes Gotlén<br />
Graphics; Emelie Falk Renström</p>
<p><em>Pressing &#8216;CR&#8217; reveals a highly advanced level editor&#8230;</em></p>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2010/05/pulka-slider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Static sphere collision experiment</title>
		<link>http://johannes.gotlen.se/blog/2010/05/static-sphere-collision-experiment/</link>
		<comments>http://johannes.gotlen.se/blog/2010/05/static-sphere-collision-experiment/#comments</comments>
		<pubDate>Sun, 16 May 2010 16:02:18 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Workshop]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[collision]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[physics]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=41</guid>
		<description><![CDATA[
file: AStaticCollisionSphere.as

package engine.collisionSystem
{
	import math.CVec2D;

	public interface AStaticCollisionSphere
	{
		function get position() : CVec2D;
		function set position( position : CVec2D ) : void;
		function get radius() : Number;
		function get collisionDamping() : Number;
	}
}

file: ACollsionSphere.as


package engine.collisionSystem
{
	import math.CVec2D;

	public interface ACollsionSphere extends AStaticCollisionSphere
	{
		function get velocity() : CVec2D;
		function set velocity(pNewVelocity : CVec2D ) : void;
	}
}

file: CCollisionSolver.as

package engine.collisionSystem
{
	import math.CLine2D;
	import math.CVec2D;

	public class CCollisionSolver
	{

		private var _tmpTestVec : CVec2D = [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="shadowbox" href="http://spel.gotlen.se/physicsFun/"><img class="alignnone size-full wp-image-42" title="physicsFun" src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/05/physicsFun.jpg" alt="" width="303" height="240" /></a></p>
<p>file: AStaticCollisionSphere.as</p>
<pre class="brush: as3; wrap-lines: false">
package engine.collisionSystem
{
	import math.CVec2D;

	public interface AStaticCollisionSphere
	{
		function get position() : CVec2D;
		function set position( position : CVec2D ) : void;
		function get radius() : Number;
		function get collisionDamping() : Number;
	}
}
</pre>
<p>file: ACollsionSphere.as<br />
<span id="more-41"></span></p>
<pre class="brush: as3; wrap-lines: false">
package engine.collisionSystem
{
	import math.CVec2D;

	public interface ACollsionSphere extends AStaticCollisionSphere
	{
		function get velocity() : CVec2D;
		function set velocity(pNewVelocity : CVec2D ) : void;
	}
}
</pre>
<p>file: CCollisionSolver.as</p>
<pre class="brush: as3; wrap-lines: false">
package engine.collisionSystem
{
	import math.CLine2D;
	import math.CVec2D;

	public class CCollisionSolver
	{

		private var _tmpTestVec : CVec2D = new CVec2D();
		private var _tmpDeltaVec : CVec2D = new CVec2D();
		private var _tmpVecA : CVec2D = new CVec2D();
		private var _generatedCollisionPoint : CVec2D = new CVec2D();

		public function CCollisionSolver()
		{
		}

		public function CollideSphere( pAgent : ACollsionSphere, pTestAgainstPoint : CVec2D, pTestAgainstRadius : Number ) : Boolean
		{
			_tmpDeltaVec.reset(pTestAgainstPoint.x - pAgent.position.x, pTestAgainstPoint.y - pAgent.position.y);
			if(_tmpDeltaVec.isLengthLessThan(pAgent.radius + pTestAgainstRadius))
			{
				//generate a new collisionPoint on the edge on the pTestAgainstRadius circle
				var dLen : Number = _tmpDeltaVec.length;
				_tmpDeltaVec.normalize();
				_tmpDeltaVec.multiplyEq(dLen - pTestAgainstRadius);
				_generatedCollisionPoint.reset(pAgent.position.x + _tmpDeltaVec.x, pAgent.position.y + _tmpDeltaVec.y);

				ActOnCollidePoint(pAgent, _generatedCollisionPoint);
				return true;
			}
			return false;
		}

		public function CollideLine(pAgent : ACollsionSphere, pTestAgainstLine : CLine2D ) : Boolean
		{
			var distance : Number = pTestAgainstLine.distance(pAgent.position);
			distance = distance < 0 ? -distance : distance;
			if(distance > pAgent.radius)
			{
				//quit early if too far away to collide
				return false;
			}

			//generate a collisionPoint on the line
			_generatedCollisionPoint.copyFrom(pTestAgainstLine.closestPointOnLine(pAgent.position));
			ActOnCollidePoint(pAgent,_generatedCollisionPoint);
			return true;
		}
		public function CollidePoint(pAgent : ACollsionSphere, pTestAgainstPoint : CVec2D ) : Boolean
		{
			return false;
		}	

		private function TestAgainstPoint( pAgent : ACollsionSphere ,pPointInWorld : CVec2D ) : Boolean
		{
			_tmpTestVec.reset(pPointInWorld.x - pAgent.position.x, pPointInWorld.y - pAgent.position.y);
			if(_tmpTestVec.isLengthLessThan(pAgent.radius))
			{
				ActOnCollidePoint(pAgent, pPointInWorld);
				return true;
			}
			return false;
		}
		private function ActOnCollidePoint( pAgent : ACollsionSphere, pPointInWorld : CVec2D ) : void
		{

			//point is inside
			//first ofset the sphere so point is outside
			//we assume that we are colliding with a static object
			_tmpDeltaVec.reset(pPointInWorld.x - pAgent.position.x, pPointInWorld.y - pAgent.position.y);
			var deltaLength : Number = _tmpDeltaVec.length;
			var pointDistanceInsideRadius : Number = pAgent.radius - deltaLength;

			var hasVelocity : Boolean = pAgent.velocity.isLengthGreaterThan(0.1);
			if(!hasVelocity || CVec2D.dot(pAgent.velocity, _tmpDeltaVec) < 0)
			{
				//just move out of harms way!
				_tmpDeltaVec.normalize();
				_tmpDeltaVec.multiplyEq(-pointDistanceInsideRadius);
				pAgent.position.plusEq(_tmpDeltaVec);
				pAgent.position = pAgent.position; //call the set function atleast once
				return; //quit early
			}
			//move backwards using the velocity vector until collsion point is outside radius
			_tmpVecA.reset(pAgent.velocity.x, pAgent.velocity.y);
			_tmpVecA.normalize();
			_tmpVecA.multiplyEq(-pointDistanceInsideRadius);
			pAgent.position.plusEq(_tmpVecA);

			//recalc delta pos
			_tmpDeltaVec.reset(pPointInWorld.x - pAgent.position.x, pPointInWorld.y - pAgent.position.y);

			var speedLength : Number = pAgent.velocity.length;

			//how much of the velocity is in the collision direction
			_tmpDeltaVec.normalize();
			_tmpVecA.reset(pAgent.velocity.x, pAgent.velocity.y);
			_tmpVecA.normalize();
			var deflectionVectorMultiplier : Number = CVec2D.dot(_tmpDeltaVec, _tmpVecA);
			//calculate deflection vector
			_tmpDeltaVec.multiplyEq(-deflectionVectorMultiplier*speedLength);
			//apply speed loss from collision
			_tmpDeltaVec.multiplyEq(pAgent.collisionDamping + 1.0);
			//
			//apply deflection vector to speed
			pAgent.velocity.plusEq(_tmpDeltaVec);
			pAgent.position = pAgent.position; //call the set function atleast once
			pAgent.velocity = pAgent.velocity; //call the set function atleast once
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2010/05/static-sphere-collision-experiment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Media Mingel, Flash Game</title>
		<link>http://johannes.gotlen.se/blog/2010/05/media-mingel-flash-game/</link>
		<comments>http://johannes.gotlen.se/blog/2010/05/media-mingel-flash-game/#comments</comments>
		<pubDate>Sun, 16 May 2010 14:52:59 +0000</pubDate>
		<dc:creator>Johannes</dc:creator>
				<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://johannes.gotlen.se/blog/?p=15</guid>
		<description><![CDATA[
Code / Project Lead; Johannes Gotlén
Graphics; Emlie Falk Renström
Sound; Viktor Falk Renström
]]></description>
			<content:encoded><![CDATA[<p><a href="http://spel.gotlen.se/mediamingel/fallingblocks.html" rel="shadowbox;height=500;width=400"><img src="http://johannes.gotlen.se/blog/wp-content/uploads/2010/05/mediamingel.jpg" alt="" title="mediamingel" width="400" height="500" class="alignnone size-full wp-image-24" /></a></p>
<p>Code / Project Lead; Johannes Gotlén<br />
Graphics; Emlie Falk Renström<br />
Sound; Viktor Falk Renström</p>
]]></content:encoded>
			<wfw:commentRss>http://johannes.gotlen.se/blog/2010/05/media-mingel-flash-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

