public void TestDropout() { GameState gameState = TestGameState(5, 5); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood); const Tile f = Tile.Food; const Tile _ = Tile.Land; gameState.Set(new Tile[, ] { { _, _, _, _, _ }, { _, _, _, _, _ }, { _, _, f, _, _ }, { _, _, _, _, _ }, { _, _, _, _, _ } }); dut.Propagate(); gameState.Set(new Tile[, ] { { _, _, _, _, _ }, { _, _, _, _, _ }, { _, _, _, _, _ }, { _, _, _, _, _ }, { _, _, _, _, _ } }); dut.Propagate(); Assert.AreEqual(DistanceField <GameState.Tile> .Max, dut.GetDistance(2, 2)); Assert.AreEqual(DistanceField <GameState.Tile> .Max, dut.GetDistance(3, 2)); Assert.AreEqual(DistanceField <GameState.Tile> .Max, dut.GetDistance(4, 2)); }
public void ProblemCase1() { GameState gameState = TestGameState(7, 10); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood); const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; const Tile u = Tile.Unseen; Tile[,] map = new Tile[, ] { { _, _, _, u, _, _, w }, { _, f, _, u, _, _, w }, { _, _, _, w, _, _, w }, { _, _, _, w, _, _, w }, { _, _, _, w, _, _, w }, { _, _, _, w, w, _, w }, { _, _, _, _, w, f, w }, { _, _, _, w, w, _, w }, { _, _, _, _, w, _, w }, { w, w, w, w, w, w, w }, }; gameState.Set(map); dut.Propagate(); { Vector2i r = dut.GetDescent(3, 6).First(); Assert.AreEqual(-1, r.x); Assert.AreEqual(0, r.y); } }
static void Import() { foreach (Object o in Selection.GetFiltered(typeof(Font), SelectionMode.DeepAssets)) { string path = AssetDatabase.GetAssetPath(o.GetInstanceID()); string basePath = path.Substring(0, path.LastIndexOf(".")); Font fnt = (Font)o; Texture2D tex = (Texture2D)fnt.material.mainTexture; //Create distance field from texture Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(tex, InputTextureChannel, tex.width / DistanceFieldScaleFactor); //Save distance field as png byte[] pngData = distanceField.EncodeToPNG(); string outputPath = basePath + "_dist.png"; System.IO.File.WriteAllBytes(outputPath, pngData); AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport); //Set correct texture format TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath); texImp.textureType = TextureImporterType.Advanced; texImp.isReadable = true; texImp.textureFormat = TextureImporterFormat.Alpha8; AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport); Material mat = new Material(Shader.Find("BitmapFont/Outline")); mat.mainTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(outputPath, typeof(Texture2D)); AssetDatabase.CreateAsset(mat, basePath + "_dist.mat"); } }
public void TestWater() { GameState gameState = TestGameState(7, 5); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood); const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; Tile[,] map = new Tile[, ] { { _, w, _, w, _, w, _ }, { _, w, _, w, _, w, _ }, { _, w, f, w, _, _, _ }, { _, _, w, w, w, w, _ }, { _, w, _, _, _, w, _ } }; gameState.Set(map); dut.Propagate(); Assert.AreEqual(0, dut.GetDistance(2, 2)); Assert.AreEqual(1, dut.GetDistance(2, 1)); Assert.AreEqual(2, dut.GetDistance(2, 0)); Assert.AreEqual(12, dut.GetDistance(6, 4)); }
public void TestBasic_NotSquare() { DistanceField dut = new DistanceField(7, 5, Tile.Food); const Tile f = Tile.Food; const Tile _ = Tile.Land; Tile[,] map = new Tile[, ] { { _, _, _, _, _, _, _ }, { _, _, _, _, _, _, _ }, { _, _, f, _, _, _, _ }, { _, _, _, _, _, _, _ }, { _, _, _, _, _, _, _ } }; dut.Propagate(map, 10); Assert.AreEqual(0, dut.GetDistance(2, 2)); Assert.AreEqual(1, dut.GetDistance(3, 2)); Assert.AreEqual(2, dut.GetDistance(4, 2)); Assert.AreEqual(1, dut.GetDistance(2, 3)); Assert.AreEqual(2, dut.GetDistance(2, 4)); Assert.AreEqual(1, dut.GetDistance(2, 1)); Assert.AreEqual(2, dut.GetDistance(2, 0)); Assert.AreEqual(1, dut.GetDistance(1, 2)); Assert.AreEqual(2, dut.GetDistance(0, 2)); }
public void TestWrap() { DistanceField dut = new DistanceField(7, 5, Tile.Food); const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; Tile[,] map = new Tile[, ] { { f, _, _, _, _, _, _ }, { _, _, _, _, _, _, _ }, { _, _, _, _, _, _, _ }, { _, _, _, _, _, _, _ }, { _, _, _, _, _, _, _ } }; dut.Propagate(map, 20); Assert.AreEqual(0, dut.GetDistance(0, 0)); Assert.AreEqual(1, dut.GetDistance(0, 1)); Assert.AreEqual(1, dut.GetDistance(1, 0)); Assert.AreEqual(1, dut.GetDistance(0, 4)); Assert.AreEqual(1, dut.GetDistance(6, 0)); }
public void Exploration1() { GameState gameState = TestGameState(7, 10); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.terrain == GameState.Terrain.Unknown); const Tile u = Tile.Unseen; const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; Tile[,] map = new Tile[, ] { { w, w, w, w, w, w, w }, { w, _, _, _, _, _, _ }, { w, _, _, _, _, _, _ }, { w, _, _, _, _, _, _ }, { w, _, _, _, _, _, _ }, { w, _, _, _, _, _, _ }, { w, _, _, _, _, _, _ }, { w, _, _, _, u, _, _ }, { w, _, _, _, _, _, _ }, { w, _, _, _, _, _, _ }, }; gameState.Set(map); dut.Propagate(); { Assert.AreEqual(0, dut.GetDistance(4, 7)); Assert.AreEqual(1, dut.GetDistance(3, 7)); Assert.AreEqual(2, dut.GetDistance(2, 7)); Assert.AreEqual(3, dut.GetDistance(2, 6)); Assert.AreEqual(4, dut.GetDistance(2, 5)); } }
public void TestBasic_NotSquare() { GameState gameState = TestGameState(7, 5); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood); const Tile f = Tile.Food; const Tile _ = Tile.Land; Tile[,] map = new Tile[, ] { { _, _, _, _, _, _, _ }, { _, _, _, _, _, _, _ }, { _, _, f, _, _, _, _ }, { _, _, _, _, _, _, _ }, { _, _, _, _, _, _, _ } }; gameState.Set(map); dut.Propagate(); Assert.AreEqual(0, dut.GetDistance(2, 2)); Assert.AreEqual(1, dut.GetDistance(3, 2)); Assert.AreEqual(2, dut.GetDistance(4, 2)); Assert.AreEqual(1, dut.GetDistance(2, 3)); Assert.AreEqual(2, dut.GetDistance(2, 4)); Assert.AreEqual(1, dut.GetDistance(2, 1)); Assert.AreEqual(2, dut.GetDistance(2, 0)); Assert.AreEqual(1, dut.GetDistance(1, 2)); Assert.AreEqual(2, dut.GetDistance(0, 2)); }
public static void GenerateVectors([In][IsReadOnly] ref int size, [In][IsReadOnly] ref float[] distanceField, ref Vector2[] vectorField) { for (int i = 1; i < size - 1; i++) { for (int j = 1; j < size - 1; j++) { float single = DistanceField.SampleClamped(distanceField, size, i - 1, j - 1); float single1 = DistanceField.SampleClamped(distanceField, size, i - 1, j); float single2 = DistanceField.SampleClamped(distanceField, size, i - 1, j + 1); float single3 = DistanceField.SampleClamped(distanceField, size, i, j - 1); float single4 = DistanceField.SampleClamped(distanceField, size, i, j + 1); float single5 = DistanceField.SampleClamped(distanceField, size, i + 1, j - 1); float single6 = DistanceField.SampleClamped(distanceField, size, i + 1, j); float single7 = DistanceField.SampleClamped(distanceField, size, i + 1, j + 1); float single8 = single2 + 2f * single4 + single7 - (single + 2f * single3 + single5); Vector2 vector2 = new Vector2(-(single5 + 2f * single6 + single7 - (single + 2f * single1 + single2)), -single8); Vector2 vector21 = vector2.normalized; vectorField[j * size + i] = new Vector2(vector21.x, vector21.y); } } for (int k = 1; k < size - 1; k++) { vectorField[k] = DistanceField.SampleClamped(vectorField, size, k, 1); vectorField[(size - 1) * size + k] = DistanceField.SampleClamped(vectorField, size, k, size - 2); } for (int l = 0; l < size; l++) { vectorField[l * size] = DistanceField.SampleClamped(vectorField, size, 1, l); vectorField[l * size + size - 1] = DistanceField.SampleClamped(vectorField, size, size - 2, l); } }
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { var data = new DistanceField { model = model, switchTimer = switchTimerLength, switchTimerValue = switchTimerLength }; dstManager.AddComponentData(entity, data); }
public void TestWrap5y() { DistanceField dut = new DistanceField(5, 7, Tile.Food); Vector2i result = dut.Wrap(new Vector2i(2, 6)); Assert.AreEqual(2, result.x); Assert.AreEqual(6, result.y); }
public void TestWrap4x() { DistanceField dut = new DistanceField(5, 7, Tile.Food); Vector2i result = dut.Wrap(new Vector2i(0, 3)); Assert.AreEqual(0, result.x); Assert.AreEqual(3, result.y); }
static void Generate(string path) { BMFont.IntFontInfo fntInfo = BMFont.ParseFromPath(path); if (fntInfo == null) { return; } //Process Texture string imagePath = System.IO.Path.GetDirectoryName(path) + "/" + fntInfo.texName; Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D)); //Make sure font texture is readable TextureImporter inputTextureImp = (TextureImporter)TextureImporter.GetAtPath(imagePath); inputTextureImp.textureType = TextureImporterType.Advanced; inputTextureImp.isReadable = true; inputTextureImp.maxTextureSize = 4096; AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceSynchronousImport); Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, InputTextureChannel, inputTexture.width / DistanceFieldScaleFactor); //Save distance field as png byte[] pngData = distanceField.EncodeToPNG(); string outputPath = imagePath.Substring(0, imagePath.LastIndexOf('.')) + "_dist.png"; System.IO.File.WriteAllBytes(outputPath, pngData); AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport); //Set correct texture format TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath); texImp.textureType = TextureImporterType.GUI; texImp.isReadable = true; texImp.textureFormat = TextureImporterFormat.Alpha8; AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport); string newTexName = fntInfo.texName.Substring(0, fntInfo.texName.LastIndexOf(".")) + "_dist.png"; fntInfo.texName = newTexName; fntInfo.scaleW /= DistanceFieldScaleFactor; fntInfo.scaleH /= DistanceFieldScaleFactor; fntInfo.lineHeight /= DistanceFieldScaleFactor; foreach (BMFont.IntChar c in fntInfo.chars) { c.x /= DistanceFieldScaleFactor; c.y /= DistanceFieldScaleFactor; c.width /= DistanceFieldScaleFactor; c.height /= DistanceFieldScaleFactor; c.xoffset /= DistanceFieldScaleFactor; c.yoffset /= DistanceFieldScaleFactor; c.xadvance /= DistanceFieldScaleFactor; } outputPath = path.Substring(0, path.LastIndexOf('.')) + "_dist.fnt"; BMFont.SaveToXML(fntInfo, outputPath); }
public void TestWrap5y() { GameState gameState = TestGameState(5, 7); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood); Vector2i result = dut.Wrap(new Vector2i(2, 6)); Assert.AreEqual(2, result.x); Assert.AreEqual(6, result.y); }
public void TestDescent() { GameState gameState = TestGameState(7, 5); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood); const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; Tile[,] map = new Tile[, ] { { _, w, _, w, _, w, _ }, { _, w, _, w, _, w, _ }, { _, w, f, w, _, _, _ }, { _, _, w, w, w, w, _ }, { _, w, _, _, _, w, _ } }; gameState.Set(map); dut.Propagate(); { Vector2i r = dut.GetDescent(6, 4).First(); Assert.AreEqual(-1, r.y); Assert.AreEqual(0, r.x); } { Vector2i r = dut.GetDescent(6, 3).First(); Assert.AreEqual(-1, r.y); Assert.AreEqual(0, r.x); } { Vector2i r = dut.GetDescent(6, 2).First(); Assert.AreEqual(-1, r.x); Assert.AreEqual(0, r.y); } { Vector2i r = dut.GetDescent(5, 2).First(); Assert.AreEqual(-1, r.x); Assert.AreEqual(0, r.y); } { Vector2i r = dut.GetDescent(4, 2).First(); Assert.AreEqual(0, r.x); Assert.AreEqual(-1, r.y); } { Vector2i r = dut.GetDescent(2, 1).First(); Assert.AreEqual(0, r.x); Assert.AreEqual(1, r.y); } }
public void InitialiseVoxelObject(int a_size, float a_spacing, Pipeline a_pipeline) { m_pipeline = a_pipeline; m_distanceField = new DistanceField <Voxel>(a_size, a_size, a_size, a_spacing); m_update = 0; m_model = new Model(m_pipeline); }
public void TestDescent() { DistanceField dut = new DistanceField(7, 5, Tile.Food); const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; Tile[,] map = new Tile[, ] { { _, w, _, w, _, w, _ }, { _, w, _, w, _, w, _ }, { _, w, f, w, _, _, _ }, { _, _, w, w, w, w, _ }, { _, w, _, _, _, w, _ } }; dut.Propagate(map, 20); { Vector2i r = dut.GetDescent(6, 4).First(); Assert.AreEqual(-1, r.y); Assert.AreEqual(0, r.x); } { Vector2i r = dut.GetDescent(6, 3).First(); Assert.AreEqual(-1, r.y); Assert.AreEqual(0, r.x); } { Vector2i r = dut.GetDescent(6, 2).First(); Assert.AreEqual(-1, r.x); Assert.AreEqual(0, r.y); } { Vector2i r = dut.GetDescent(5, 2).First(); Assert.AreEqual(-1, r.x); Assert.AreEqual(0, r.y); } { Vector2i r = dut.GetDescent(4, 2).First(); Assert.AreEqual(0, r.x); Assert.AreEqual(-1, r.y); } { Vector2i r = dut.GetDescent(2, 1).First(); Assert.AreEqual(0, r.x); Assert.AreEqual(1, r.y); } }
void LoadObject(byte[] a_bytes, Chunk a_chunk) { DistanceField <Chunk.Voxel> distField = a_chunk.DistanceField; int cellSize = DistanceField <Chunk.Voxel> .Cell.DataSize; for (int i = 0; i < distField.Cells.Length; ++i) { distField.Cells[i] = DistanceField <Chunk.Voxel> .Cell.FromBytes(a_bytes, i *cellSize); } }
public static void Main() { try { var df = DistanceField.LoadFromJson(@"d:/test_scene.json"); Console.WriteLine(JsonConvert.SerializeObject(df, Formatting.Indented)); var fn = ExpressionParser.Compile <Func <Vec3, Vec3> >(@"(Vec3 pos) => new Vec3(1.0D, 2.0, 3.0)", "System", "RayTracer"); } catch (Exception err) { Console.WriteLine(err.StackTrace); } Console.ReadKey(); }
public void Exploration2() { GameState gameState = TestGameState(7, 10); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.terrain == GameState.Terrain.Unknown); const Tile u = Tile.Unseen; const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; Tile[,] map = new Tile[, ] { { w, w, w, w, w, w, w }, { w, _, _, _, _, _, u }, { w, _, _, _, _, _, u }, { w, _, _, _, _, _, u }, { w, _, _, _, _, _, u }, { w, _, _, _, _, _, u }, { w, _, _, _, _, _, u }, { w, w, w, w, w, _, u }, { w, _, _, _, _, _, u }, { w, u, u, u, u, u, u }, }; gameState.Set(map); dut.Propagate(); { Assert.AreEqual(5, dut.GetDistance(1, 1)); Assert.AreEqual(5, dut.GetDistance(1, 2)); Assert.AreEqual(0, dut.GetDistance(6, 2)); Assert.AreEqual(1, dut.GetDistance(5, 2)); Assert.AreEqual(5, dut.GetDistance(1, 6)); } { Vector2i result = dut.GetDescent(1, 6).First(); Assert.AreEqual(1, result.x); Assert.AreEqual(0, result.y); } }
void SaveObject(Chunk a_chunk) { if (m_fileSystem != null) { if (a_chunk.Updated) { string fileName = "obj/" + GetFileName(a_chunk); DistanceField <Chunk.Voxel> distance = a_chunk.DistanceField; int size = DistanceField <Chunk.Voxel> .Cell.DataSize; int len = distance.Cells.Length; byte[] bytes = new byte[len * size]; for (int i = 0; i < distance.Cells.Length; ++i) { Array.Copy(distance.Cells[i].GetBytes(), 0, bytes, i * size, size); } m_fileSystem.Save(fileName, bytes); } } }
void FixedUpdate() { for (int i = 0; i < orbiters.Length; i++) { Orbiter orbiter = orbiters[i]; Vector3 normal; float dist = DistanceField.GetDistance(orbiter.position.x, orbiter.position.y, orbiter.position.z, out normal); orbiter.velocity -= normal.normalized * attraction * Mathf.Clamp(dist, -1f, 1f); orbiter.velocity += Random.insideUnitSphere * jitter; orbiter.velocity *= .99f; orbiter.position += orbiter.velocity; Color targetColor; if (dist > 0f) { targetColor = Color.Lerp(surfaceColor, exteriorColor, dist / exteriorColorDist); } else { targetColor = Color.Lerp(surfaceColor, interiorColor, -dist / interiorColorDist); } orbiter.color = Color.Lerp(orbiter.color, targetColor, Time.deltaTime * colorStiffness); orbiters[i] = orbiter; } }
// what's the slope of the distance field at a given point? /*public static Vector3 GetGradient(float x, float y, float z,float baseValue) { * float eps = 0.01f; * float dx = GetDistance(x + eps,y,z) - baseValue; * float dy = GetDistance(x,y + eps,z) - baseValue; * float dz = GetDistance(x,y,z + eps) - baseValue; * * return new Vector3(dx / eps,dy / eps,dz / eps); * }*/ private void Start() { instance = this; modelCount = System.Enum.GetValues(typeof(DistanceFieldModel)).Length; }
// Use this for initialization void Start() { _shuriken = GetComponent <ParticleSystem>(); _distanceField = GetComponent <DistanceField>(); }
public void ParsePages(BitmapFont fnt, string fontFile) { string[] pages = fnt.pages; Texture2D[] texturePages = new Texture2D[pages.Length * (fnt.packed?4:1)]; int index = 0; //用在带通道的字体上,提取通道值后的颜色应该是白色的 Color defaultColor = Color.white; foreach (string pageImagePath in pages) { //Find original font texture string imagePath = Path.GetDirectoryName(fontFile) + "/" + pageImagePath; Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D)); //Make sure font texture is readable TextureImporter inputTextureImp = (TextureImporter)AssetImporter.GetAtPath(imagePath); inputTextureImp.textureType = TextureImporterType.Default; inputTextureImp.isReadable = true; inputTextureImp.maxTextureSize = 4096; inputTextureImp.mipmapEnabled = false; inputTextureImp.textureFormat = TextureImporterFormat.RGBA32; AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceSynchronousImport); //Create distance field from texture //处理通道 if (fnt.packed) { Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, DistanceField.TextureChannel.BLUE, inputTexture.width, defaultColor); //byte[] buff = distanceField.EncodeToPNG(); //File.WriteAllBytes(Path.GetDirectoryName(fontFile) + "/index_" + index + ".png", buff); texturePages[index++] = distanceField; distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, DistanceField.TextureChannel.GREEN, inputTexture.width, defaultColor); //buff = distanceField.EncodeToPNG(); //File.WriteAllBytes(Path.GetDirectoryName(fontFile) + "/index_" + index + ".png", buff); texturePages[index++] = distanceField; distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, DistanceField.TextureChannel.RED, inputTexture.width, defaultColor); //buff = distanceField.EncodeToPNG(); //File.WriteAllBytes(Path.GetDirectoryName(fontFile) + "/index_" + index + ".png", buff); texturePages[index++] = distanceField; distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, DistanceField.TextureChannel.ALPHA, inputTexture.width, defaultColor); //buff = distanceField.EncodeToPNG(); //File.WriteAllBytes(Path.GetDirectoryName(fontFile) + "/index_" + index + ".png", buff); texturePages[index] = distanceField; } else { Texture2D distanceField = inputTexture; //DistanceField.CreateDistanceFieldTexture(inputTexture, InputTextureChannel, inputTexture.width / DistanceFieldScaleFactor); texturePages[index] = distanceField; } index++; } //Create texture atlas if (texturePages.Length > 1) { Texture2D pageAtlas = new Texture2D(0, 0); fnt.pageOffsets = pageAtlas.PackTextures(texturePages, 0); //foreach(Rect r in fnt.pageOffsets) //{ // Debug.Log(r); //} //Save atlas as png byte[] pngData = pageAtlas.EncodeToPNG(); string outputPath = fontFile.Substring(0, fontFile.LastIndexOf('.')) + "_pak.png"; File.WriteAllBytes(outputPath, pngData); AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport); //Set correct texture format TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath); texImp.textureType = TextureImporterType.Default; texImp.isReadable = true; texImp.textureFormat = TextureImporterFormat.RGBA32; texImp.mipmapEnabled = false; AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport); //Load the saved texture atlas fnt.pageAtlas = AssetDatabase.LoadAssetAtPath <Texture2D>(outputPath); } else { fnt.pageAtlas = texturePages[0]; } }
public FluidSim(int width, int height, float scale) { Scale = 0.5f; GWidth = (int)(width / scale) + 1; GHeight = (int)(height / scale) + 1; Grid = new GridCell[GHeight, GWidth]; GridCoeff = 1f; GravityX = 0f; GravityY = (9.81f / 0.5f) * (1f / 900f); SDF = new DistanceField(); SDF.Create(256, GWidth, GHeight); SDF.SubRect(0, 0, 128, 128); SDF.AddCircle(64, 128, 10); SDF.Blur(); }
public void ProblemCase3_AntHills() { GameState gameState = TestGameState(7, 10); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isEnemyHill); const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; const Tile h = Tile.TheirHill; const Tile u = Tile.Unseen; gameState.Set(new Tile[, ] { { _, w, w, _, _, _, w }, { _, w, w, h, _, _, w }, { _, w, w, _, _, _, w }, { w, w, w, _, _, _, w }, { w, w, w, _, _, _, w }, { _, _, _, _, _, _, w }, { _, _, _, _, _, _, w }, { _, _, _, _, _, _, w }, { _, _, _, _, _, _, w }, { w, w, w, w, w, w, w }, }); dut.Propagate(); Assert.AreEqual(1, dut.GetDistance(3, 2)); { Vector2i r = dut.GetDescent(2, 5).First(); Assert.AreEqual(1, r.x); Assert.AreEqual(0, r.y); } { Vector2i r = dut.GetDescent(1, 5).First(); Assert.AreEqual(1, r.x); Assert.AreEqual(0, r.y); } gameState.Set(new Tile[, ] { { _, w, w, _, _, _, w }, { _, w, w, u, _, _, w }, { _, w, w, _, _, _, w }, { w, w, w, _, _, _, w }, { w, w, w, _, _, _, w }, { _, _, _, _, _, _, w }, { _, _, _, _, _, _, w }, { _, _, _, _, _, _, w }, { _, _, _, _, _, _, w }, { w, w, w, w, w, w, w }, }); dut.Propagate(); Assert.AreEqual(1, dut.GetDistance(3, 2)); { Vector2i r = dut.GetDescent(2, 5).First(); Assert.AreEqual(1, r.x); Assert.AreEqual(0, r.y); } { Vector2i r = dut.GetDescent(1, 5).First(); Assert.AreEqual(1, r.x); Assert.AreEqual(0, r.y); } }
public void UpdateData() { int width = m_distanceField.Width; int height = m_distanceField.Height; int depth = m_distanceField.Depth; int halfWidth = width / 2; int halfHeight = height / 2; int halfDepth = depth / 2; DistanceField <Voxel> .Cell[] voxels = new DistanceField <Voxel> .Cell[8]; DistanceField <Voxel> .Cell[] cells = m_distanceField.Cells; List <ChunkVertex> dirtyVerts = new List <ChunkVertex>(); for (int x = 0; x < m_distanceField.Width - 1; ++x) { int x1 = x + 1; for (int y = 0; y < m_distanceField.Height - 1; ++y) { int y1 = y + 1; for (int z = 0; z < m_distanceField.Depth - 1; ++z) { int z1 = z + 1; voxels[0] = cells[(z * width * height) + (y * width) + x]; voxels[1] = cells[(z * width * height) + (y * width) + x1]; voxels[2] = cells[(z1 * width * height) + (y * width) + x1]; voxels[3] = cells[(z1 * width * height) + (y * width) + x]; voxels[4] = cells[(z * width * height) + (y1 * width) + x]; voxels[5] = cells[(z * width * height) + (y1 * width) + x1]; voxels[6] = cells[(z1 * width * height) + (y1 * width) + x1]; voxels[7] = cells[(z1 * width * height) + (y1 * width) + x]; MarchingCubes.Polygonise(m_distanceField, voxels, x - halfWidth, y - halfHeight, z - halfDepth, 0.0f, dirtyVerts); } } } int count = dirtyVerts.Count; List <uint> indicies = new List <uint>(); List <ChunkVertex> vertices = new List <ChunkVertex>(); Dictionary <ChunkVertex, uint> values = new Dictionary <ChunkVertex, uint>(); for (int i = 0; i < count; i += 3) { ChunkVertex vertA = dirtyVerts[i]; ChunkVertex vertB = dirtyVerts[i + 1]; ChunkVertex vertC = dirtyVerts[i + 2]; Vector3 v1 = vertC.Position.Xyz - vertA.Position.Xyz; Vector3 v2 = vertB.Position.Xyz - vertA.Position.Xyz; Vector3 normal = Vector3.Cross(v2, v1); uint index; if (values.TryGetValue(vertA, out index)) { ChunkVertex vertex = vertices[(int)index]; vertex.Normal += normal; vertices[(int)index] = vertex; indicies.Add(index); } else { index = (uint)vertices.Count; vertA.Normal = normal; indicies.Add(index); values.Add(vertA, index); vertices.Add(vertA); } if (values.TryGetValue(vertB, out index)) { ChunkVertex vertex = vertices[(int)index]; vertex.Normal += normal; vertices[(int)index] = vertex; indicies.Add(index); } else { index = (uint)vertices.Count; vertB.Normal = normal; indicies.Add(index); values.Add(vertB, index); vertices.Add(vertB); } if (values.TryGetValue(vertC, out index)) { ChunkVertex vertex = vertices[(int)index]; vertex.Normal += normal; vertices[(int)index] = vertex; indicies.Add(index); } else { index = (uint)vertices.Count; vertC.Normal = normal; indicies.Add(index); values.Add(vertC, index); vertices.Add(vertC); } } float maxRadius = 0.0f; int vertexCount = vertices.Count; for (int i = 0; i < vertexCount; ++i) { ChunkVertex vert = vertices[i]; vert.Normal = vert.Normal.Normalized(); vertices[i] = vert; float distSqr = vert.Position.LengthSquared; if (distSqr > maxRadius) { maxRadius = distSqr; } } float radius = (float)Math.Sqrt(maxRadius); ModelVertexInfo[] vertexInfoCollection = new ModelVertexInfo[3]; vertexInfoCollection[0].Offset = Marshal.OffsetOf <ChunkVertex>("Position"); vertexInfoCollection[0].Count = 4; vertexInfoCollection[0].Type = e_FieldType.Float; vertexInfoCollection[0].Normalize = false; vertexInfoCollection[1].Offset = Marshal.OffsetOf <ChunkVertex>("Normal"); vertexInfoCollection[1].Count = 3; vertexInfoCollection[1].Type = e_FieldType.Float; vertexInfoCollection[1].Normalize = false; vertexInfoCollection[2].Offset = Marshal.OffsetOf <ChunkVertex>("Color"); vertexInfoCollection[2].Count = 4; vertexInfoCollection[2].Type = e_FieldType.UnsignedByte; vertexInfoCollection[2].Normalize = true; m_model.SetModelData <ChunkVertex>(vertices.ToArray(), indicies.ToArray(), radius, vertexInfoCollection); }
public void Exploration3() { GameState gameState = TestGameState(7, 10); DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.terrain == GameState.Terrain.Unknown); const Tile u = Tile.Unseen; const Tile f = Tile.Food; const Tile _ = Tile.Land; const Tile w = Tile.Water; gameState.Set(new Tile[, ] { { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { w, w, w, _, w, w, w }, { w, w, w, _, w, w, w }, { w, w, w, _, w, w, w }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u } }); dut.Propagate(); { Assert.AreEqual(0, dut.GetDistance(3, 2)); Assert.AreEqual(1, dut.GetDistance(3, 3)); Assert.AreEqual(2, dut.GetDistance(3, 4)); Assert.AreEqual(1, dut.GetDistance(3, 5)); Assert.AreEqual(0, dut.GetDistance(3, 6)); } gameState.Set(new Tile[, ] { { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { w, w, w, _, w, w, w }, { w, w, w, _, w, w, w }, { w, w, w, _, w, w, w }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u } }); dut.Propagate(); { Assert.AreEqual(0, dut.GetDistance(3, 1)); Assert.AreEqual(1, dut.GetDistance(3, 2)); Assert.AreEqual(2, dut.GetDistance(3, 3)); Assert.AreEqual(2, dut.GetDistance(3, 4)); Assert.AreEqual(1, dut.GetDistance(3, 5)); Assert.AreEqual(0, dut.GetDistance(3, 6)); Vector2i r = dut.GetDescent(3, 2).First(); Assert.AreEqual(0, r.x); Assert.AreEqual(-1, r.y); } gameState.Set(new Tile[, ] { { u, u, u, u, u, u, u }, { w, w, w, _, w, w, w }, { w, w, w, _, w, w, w }, { w, w, w, _, w, w, w }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u }, { u, u, u, u, u, u, u } }); dut.Propagate(); { Assert.AreEqual(0, dut.GetDistance(3, 0)); Assert.AreEqual(1, dut.GetDistance(3, 1)); Assert.AreEqual(2, dut.GetDistance(3, 2)); Assert.AreEqual(3, dut.GetDistance(3, 3)); Assert.AreEqual(2, dut.GetDistance(3, 4)); Assert.AreEqual(1, dut.GetDistance(3, 5)); Assert.AreEqual(0, dut.GetDistance(3, 6)); Vector2i r = dut.GetDescent(3, 2).First(); Assert.AreEqual(0, r.x); Assert.AreEqual(-1, r.y); } }
// Use this for initialization void Start() { _shuriken = GetComponent<ParticleSystem>(); _distanceField = GetComponent<DistanceField>(); }
/* .fnt parser taken from * */ static BitmapFont UpdateBitmapFont(string path, BitmapFont fnt) { XmlDocument doc = new XmlDocument(); doc.Load(path); //Read font info XmlNode info = doc.SelectSingleNode("/font/info"); fnt.Size = ReadFloatAttribute(info, "size"); XmlNode common = doc.SelectSingleNode("/font/common"); fnt.LineHeight = ReadFloatAttribute(common, "lineHeight"); fnt.Base = ReadFloatAttribute(common, "base"); fnt.ScaleH = ReadFloatAttribute(common, "scaleW"); fnt.ScaleW = ReadFloatAttribute(common, "scaleH"); //Read character info XmlNodeList chars = doc.SelectNodes("/font/chars/char"); fnt.Chars = new BitmapChar[chars.Count]; int index = 0; foreach (XmlNode char_node in chars) { fnt.Chars[index] = new BitmapChar(); fnt.Chars[index].Id = ReadIntAttribute(char_node, "id"); fnt.Chars[index].Position = ReadVector2Attributes(char_node, "x", "y"); fnt.Chars[index].Size = ReadVector2Attributes(char_node, "width", "height"); fnt.Chars[index].Offset = ReadVector2Attributes(char_node, "xoffset", "yoffset"); fnt.Chars[index].XAdvance = ReadFloatAttribute(char_node, "xadvance"); fnt.Chars[index].Page = ReadIntAttribute(char_node, "page"); index++; } //Load texture pages and convert to distance fields XmlNodeList pages = doc.SelectNodes("/font/pages/page"); Texture2D[] texturePages = new Texture2D[pages.Count]; index = 0; foreach (XmlNode page in pages) { //Find original font texture string imagePath = System.IO.Path.GetDirectoryName(path) + "/" + page.Attributes["file"].Value; Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D)); //Make sure font texture is readable TextureImporter inputTextureImp = (TextureImporter)TextureImporter.GetAtPath(imagePath); inputTextureImp.textureType = TextureImporterType.Advanced; inputTextureImp.isReadable = true; inputTextureImp.maxTextureSize = 4096; AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceSynchronousImport); //Create distance field from texture Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, InputTextureChannel, inputTexture.width / DistanceFieldScaleFactor); texturePages[index] = distanceField; index++; } //Create texture atlas Texture2D pageAtlas = new Texture2D(0, 0); fnt.PageOffsets = pageAtlas.PackTextures(texturePages, 0); //Save atlas as png byte[] pngData = pageAtlas.EncodeToPNG(); string outputPath = path.Substring(0, path.LastIndexOf('.')) + "_dist.png"; System.IO.File.WriteAllBytes(outputPath, pngData); AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport); //Set correct texture format TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath); texImp.textureType = TextureImporterType.Advanced; texImp.isReadable = true; texImp.textureFormat = TextureImporterFormat.Alpha8; AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport); //Load the saved texture atlas fnt.PageAtlas = (Texture2D)AssetDatabase.LoadAssetAtPath(outputPath, typeof(Texture2D)); //Load kerning info XmlNodeList kernings = doc.SelectNodes("/font/kernings/kerning"); fnt.Kernings = new BitmapCharKerning[kernings.Count]; index = 0; foreach (XmlNode kerning in kernings) { BitmapCharKerning krn = new BitmapCharKerning(); krn.FirstChar = ReadIntAttribute(kerning, "first"); krn.SecondChar = ReadIntAttribute(kerning, "second"); krn.Amount = ReadFloatAttribute(kerning, "amount"); fnt.Kernings[index] = krn; index++; } return(fnt); }
private void GenerateShoreVector(out float[] distances, out Vector2[] vectors) { object obj; object obj1; float single = this.terrainSize / (float)this.shoreMapSize; Vector3 position = this.terrain.GetPosition(); int layer = LayerMask.NameToLayer("Terrain"); NativeArray <RaycastHit> raycastHits = new NativeArray <RaycastHit>(this.shoreMapSize * this.shoreMapSize, Allocator.TempJob, NativeArrayOptions.ClearMemory); NativeArray <RaycastCommand> raycastCommands = new NativeArray <RaycastCommand>(this.shoreMapSize * this.shoreMapSize, Allocator.TempJob, NativeArrayOptions.ClearMemory); for (int i = 0; i < this.shoreMapSize; i++) { for (int j = 0; j < this.shoreMapSize; j++) { float single1 = ((float)j + 0.5f) * single; float single2 = ((float)i + 0.5f) * single; Vector3 vector3 = new Vector3(position.x, 0f, position.z) + new Vector3(single1, 1000f, single2); Vector3 vector31 = Vector3.down; raycastCommands[i * this.shoreMapSize + j] = new RaycastCommand(vector3, vector31, Single.MaxValue, -5, 1); } } JobHandle jobHandle = new JobHandle(); RaycastCommand.ScheduleBatch(raycastCommands, raycastHits, 1, jobHandle).Complete(); byte[] numArray = new byte[this.shoreMapSize * this.shoreMapSize]; distances = new float[this.shoreMapSize * this.shoreMapSize]; vectors = new Vector2[this.shoreMapSize * this.shoreMapSize]; int num = 0; int num1 = 0; while (num < this.shoreMapSize) { int num2 = 0; while (num2 < this.shoreMapSize) { RaycastHit item = raycastHits[num * this.shoreMapSize + num2]; bool flag = item.collider.gameObject.layer == layer; byte[] numArray1 = numArray; int num3 = num1; if (flag) { obj = 255; } else { obj = null; } numArray1[num3] = (byte)obj; float[] singleArray = distances; int num4 = num1; if (flag) { obj1 = 256; } else { obj1 = null; } singleArray[num4] = (float)obj1; num2++; num1++; } num++; } byte num5 = 127; DistanceField.Generate(ref this.shoreMapSize, ref num5, ref numArray, ref distances); DistanceField.ApplyGaussianBlur(this.shoreMapSize, distances, 0); DistanceField.GenerateVectors(ref this.shoreMapSize, ref distances, ref vectors); raycastHits.Dispose(); raycastCommands.Dispose(); }