void AddCube(NBTBlock generator, byte blockData, UpdateFlags updateFlag) { UnityEngine.Profiling.Profiler.BeginSample("AddCube"); if (generator is NBTStationaryWater) { if (updateFlag.HasFlag(UpdateFlags.Water)) { generator.AddCube(this, blockData, pos, water); } } else if (generator is NBTPlant || generator is NBTSnowLayer) { if (updateFlag.HasFlag(UpdateFlags.NotCollidable)) { generator.AddCube(this, blockData, pos, notCollidable); } } else { if (updateFlag.HasFlag(UpdateFlags.Collidable)) { generator.AddCube(this, blockData, pos, collidable); } } UnityEngine.Profiling.Profiler.EndSample(); }
bool CanAddBlock(Vector3Int pos) { NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(InventorySystem.items[ItemSelectPanel.curIndex].id); // 手上的 if (generator == null) { return(false); } byte type = NBTHelper.GetBlockByte(pos); NBTBlock targetGenerator = NBTGeneratorManager.GetMeshGenerator(type); if (generator is NBTPlant) { if (generator == targetGenerator) { return(false); } byte belowType = NBTHelper.GetBlockByte(pos + Vector3Int.down); //如果手上拿的是植物,则判断下方是否是否是实体 NBTBlock targetBelowGenerator = NBTGeneratorManager.GetMeshGenerator(belowType); return(targetBelowGenerator != null && !(targetBelowGenerator is NBTPlant)); } else { //如果手上拿的不是植物,则判断碰撞盒是否与玩家相交 return(!cc.bounds.Intersects(new Bounds(pos, Vector3.one))); } }
void InitTileEntity() { if (hasInitTileEntity) { return; } foreach (Vector3Int pos in tileEntityList) { if (!tileEntityObjs.ContainsKey(pos)) { int sectionIndex = pos.y / 16; TagNodeCompound Section = Sections[sectionIndex] as TagNodeCompound; TagNodeByteArray Blocks = Section["Blocks"] as TagNodeByteArray; TagNodeByteArray Data = Section["Data"] as TagNodeByteArray; int yInSection = pos.y % 16; int blockPos = yInSection * 16 * 16 + pos.z * 16 + pos.x; byte rawType = Blocks.Data[blockPos]; NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(rawType); byte blockData = NBTHelper.GetNibble(Data.Data, blockPos); GameObject obj = generator.GetTileEntityGameObject(this, blockData, pos); tileEntityObjs[pos] = obj; } } hasInitTileEntity = true; }
public static void SetBlockByte(int x, int y, int z, byte type) { int chunkX = Mathf.FloorToInt(x / 16f); int chunkZ = Mathf.FloorToInt(z / 16f); int xInChunk = x - chunkX * 16; int zInChunk = z - chunkZ * 16; NBTChunk chunk = GetChunk(chunkX, chunkZ); NBTBlock oldGenerator = NBTGeneratorManager.GetMeshGenerator(chunk.GetBlockByte(xInChunk, y, zInChunk)); UpdateFlags updateFlag = GetUpdateFlags(oldGenerator); if (type == 0) { chunk.GetBlockData(xInChunk, y + 1, zInChunk, out byte topType, out byte topData); NBTBlock topGenerator = NBTGeneratorManager.GetMeshGenerator(topType); if (topGenerator != null && topGenerator is NBTPlant) { BreakBlockEffect.Create(topType, topData, new Vector3(x, y + 1, z)); chunk.SetBlockByte(xInChunk, y + 1, zInChunk, 0); updateFlag |= UpdateFlags.NotCollidable; } } chunk.SetBlockByte(xInChunk, y, zInChunk, type); if (updateFlag.HasFlag(UpdateFlags.Lighting)) { UpdateLighting(x, y, z); } chunk.RebuildMesh(updateFlag); NBTChunk leftChunk = GetChunk(chunkX - 1, chunkZ); if (xInChunk == 0) { leftChunk.RebuildMesh(); } NBTChunk rightChunk = GetChunk(chunkX + 1, chunkZ); if (xInChunk == 15) { rightChunk.RebuildMesh(); } NBTChunk backChunk = GetChunk(chunkX, chunkZ - 1); if (zInChunk == 0) { backChunk.RebuildMesh(); } NBTChunk frontChunk = GetChunk(chunkX, chunkZ + 1); if (zInChunk == 15) { frontChunk.RebuildMesh(); } }
public static void SetSwitch(NBTBlock generator, GameObject go = null) { string material = generator.soundMaterial.ToString(); if (go == null) { go = Camera.main.gameObject; } AkSoundEngine.SetSwitch("Materials", material, go); }
static void AddCube(Vector3Int pos, byte type) { MeshGenerator.pos = pos; generator = NBTGeneratorManager.GetMeshGenerator(type); AddFrontFace(); AddRightFace(); AddLeftFace(); AddBackFace(); AddTopFace(); AddBottomFace(); }
public static void PlayFootstepSound(byte type, GameObject gameObject) { NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(type); if (generator == null) { return; } string material = generator.soundMaterial.ToString(); AkSoundEngine.SetSwitch("Materials", material, gameObject); AkSoundEngine.PostEvent("Player_Footstep", gameObject); }
static UpdateFlags GetUpdateFlags(NBTBlock generator) { if (generator is NBTPlant || generator is NBTSnowLayer) { return(UpdateFlags.NotCollidable); } else if (generator is NBTStationaryWater) { return(UpdateFlags.Water); } else { return(UpdateFlags.Collidable | UpdateFlags.Lighting); } }
void BreakBlock(Vector3Int pos) { if (Time.time - lastBreakTime < 0.1f) { return; } lastBreakTime = Time.time; breakingTime = 0; HideBreakingEffect(); //DeleteBlockReq(WireFrameHelper.pos); NBTBlock generator = WireFrameHelper.generator; if (generator.hasDropItem) { try { string id = generator.GetDropItemByData(WireFrameHelper.data); byte data = generator.GetDropItemData(WireFrameHelper.data); Item.CreateBlockDropItem(id, data, pos); } catch (System.Exception e) { Debug.LogError("create item error, id=" + generator.GetDropItemByData(WireFrameHelper.data) + ",generator=" + generator); } } generator.OnDestroyBlock(WireFrameHelper.pos, WireFrameHelper.data); if (generator.isTileEntity) { NBTChunk chunk = NBTHelper.GetChunk(WireFrameHelper.pos); chunk.RemoveTileEntity(WireFrameHelper.pos); NBTHelper.SetBlockByteNoUpdate(WireFrameHelper.pos, 0); } else { NBTHelper.SetBlockByte(WireFrameHelper.pos, 0); } //Item.CreateBlockDropItem(type, WireFrameHelper.pos); BreakBlockEffect.Create(WireFrameHelper.type, WireFrameHelper.data, WireFrameHelper.pos); SoundManager.PlayBreakSound(WireFrameHelper.type, instance.gameObject); }
public static void Create(byte type, byte data, Vector3 pos) { GameObject prefab = Resources.Load("Prefabs/BreakBlockEffect") as GameObject; GameObject go = Instantiate(prefab); go.transform.localPosition = pos; Destroy(go, 1); int chunkX = Mathf.FloorToInt(pos.x / 16f); int chunkZ = Mathf.FloorToInt(pos.z / 16f); NBTChunk chunk = NBTHelper.GetChunk(chunkX, chunkZ); BreakBlockEffect effect = go.AddComponent <BreakBlockEffect>(); NBTBlock block = NBTGeneratorManager.GetMeshGenerator(type); effect.texturePath = block.GetBreakEffectTexture(chunk, data); effect.tintColor = block.GetFrontTintColorByData(chunk, data); }
void BreakBlock(Vector3Int pos) { breakTime = 0; handAnimator.SetBool("isBreaking", false); HideBreakingEffect(); //DeleteBlockReq(WireFrameHelper.pos); NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(WireFrameHelper.type); if (generator.hasDropItem) { Item.CreateBlockDropItem(generator.GetDropItemByData(WireFrameHelper.data), WireFrameHelper.data, pos); } NBTHelper.SetBlockByte(WireFrameHelper.pos, 0); //Item.CreateBlockDropItem(type, WireFrameHelper.pos); BreakBlockEffect.Create(WireFrameHelper.type, WireFrameHelper.data, WireFrameHelper.pos); SoundManager.PlayBreakSound(WireFrameHelper.type, instance.gameObject); }
public static void Init() { foreach (KeyValuePair <byte, NBTBlock> keyValue in generatorDict) { NBTBlock generator = keyValue.Value; generator.Init(); if (generator.id != null) { id2generator.Add(generator.id, generator); id2type.Add(generator.id, keyValue.Key); } } foreach (NBTItem item in itemList) { if (item.id != null) { id2item.Add(item.id, item); } } }
void UpdateBreakingEffect(NBTBlock generator, Vector3Int pos, int stage) { if (breakingEffect == null) { GameObject prefab = Resources.Load <GameObject>("Prefabs/BreakingEffect"); GameObject go = Instantiate(prefab); breakingEffect = go.GetComponent <MeshRenderer>(); breakingEffectMesh = go.GetComponent <MeshFilter>(); go.SetActive(false); } if (stage == 0) { breakingEffect.gameObject.SetActive(false); } else { breakingEffect.gameObject.SetActive(true); breakingEffect.transform.position = pos; breakingEffectMesh.sharedMesh = generator.GetBreakingEffectMesh(NBTHelper.GetChunk(GetCurrentChunkPos()), pos, WireFrameHelper.data); breakingEffect.material.mainTexture = Resources.Load <Texture2D>("GUI/block/destroy_stage_" + (stage - 1)); } }
protected bool CanAddBlock(Vector3Int pos) { byte type = NBTHelper.GetBlockByte(pos); NBTBlock targetGenerator = NBTGeneratorManager.GetMeshGenerator(type); if (this is NBTPlant) { if (this == targetGenerator) { return(false); } byte belowType = NBTHelper.GetBlockByte(pos + Vector3Int.down); //如果手上拿的是植物,则判断下方是否是否是实体 NBTBlock targetBelowGenerator = NBTGeneratorManager.GetMeshGenerator(belowType); return(targetBelowGenerator != null && !(targetBelowGenerator is NBTPlant)); } else { //如果手上拿的不是植物,则判断碰撞盒是否与玩家相交 return(!PlayerController.instance.cc.bounds.Intersects(new Bounds(pos, Vector3.one))); } }
void OnRightClick() { if (WireFrameHelper.render) { if (WireFrameHelper.generator.canInteract) { WireFrameHelper.generator.OnRightClick(); } else { string id = InventorySystem.items[ItemSelectPanel.curIndex].id; if (id != null) { NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(id); if (generator != null) { generator.OnAddBlock(hit); SoundManager.SetSwitch(generator); SoundManager.Play2DSound("Player_Place"); } } } } }
void OnLeftMousePressed() { //Debug.Log(ChatPanel.HideCode + "OnLeftMousePressed"); int layerMask = 1 << LayerMask.NameToLayer("Monster"); if (Physics.Raycast(Camera.main.ScreenPointToRay(center), out RaycastHit hit, 5f, layerMask)) { if (Time.time - lastAttackTime > attackInterval) { Monster monster = hit.transform.GetComponent <Monster>(); float attackDamage = 1f; if (handItem != null) { attackDamage = handItem.attackDamage; } monster.health -= attackDamage; Vector3 hitForce = transform.forward + Vector3.up; monster.AddForce(hitForce * attackStrength); monster.OnHit(); if (monster.health <= 0) { SoundManager.Play3DSound("Zombie_Death", monster.gameObject); } else { SoundManager.Play3DSound("Zombie_Hurt", monster.gameObject); } //Debug.Log("monster health = " + monster.health); lastAttackTime = Time.time; } } if (WireFrameHelper.render) { handAnimator.CrossFade("hand-interact", 0); breakingTime += Time.deltaTime; NBTBlock targetGenerator = WireFrameHelper.generator; bool match = false; float speed = 1; if (handItem != null) { if (handItem.IsMatch(targetGenerator.blockMaterial)) { match = true; speed = handItem.toolSpeed; } } if (!match) { match = targetGenerator.blockMaterial == BlockMaterial.Ground || targetGenerator.blockMaterial == BlockMaterial.Wood || targetGenerator.blockMaterial == BlockMaterial.Glass; } float breakNeedTime = targetGenerator.hardness * (match ? 1.5f : 5f) / speed; if (breakNeedTime == 0) { BreakBlock(WireFrameHelper.pos); } else if (breakNeedTime > 0) { int curStage = Mathf.FloorToInt(breakingTime / (breakNeedTime / 12)); if (stage != curStage) { stage = curStage; UpdateBreakingEffect(targetGenerator, WireFrameHelper.pos, stage); } if (stage >= 10) { BreakBlock(WireFrameHelper.pos); if (handItem != null && handItem.durability != -1) { InventorySystem.items[ItemSelectPanel.curIndex].damage++; if (InventorySystem.items[ItemSelectPanel.curIndex].damage >= handItem.durability) { InventorySystem.items[ItemSelectPanel.curIndex].id = null; InventorySystem.items[ItemSelectPanel.curIndex].damage = 0; InventorySystem.items[ItemSelectPanel.curIndex].count = 0; SoundManager.Play2DSound("Player_Tool_Break"); } ItemSelectPanel.instance.RefreshUI(); } } } else { breakingTime = 0; HideBreakingEffect(); } } else { breakingTime = 0; HideBreakingEffect(); } }
public void RefreshMeshData(UpdateFlags updateFlag = UpdateFlags.All) { //Debug.Log("RefreshMeshData,chunk=" + x + ",z=" + z); UnityEngine.Profiling.Profiler.BeginSample("RefreshMeshData"); if (updateFlag.HasFlag(UpdateFlags.Collidable)) { collidable.Clear(); } if (updateFlag.HasFlag(UpdateFlags.NotCollidable)) { notCollidable.Clear(); } if (updateFlag.HasFlag(UpdateFlags.Water)) { water.Clear(); } for (int sectionIndex = 0; sectionIndex < Sections.Count; sectionIndex++) { TagNodeCompound Section = Sections[sectionIndex] as TagNodeCompound; TagNodeByteArray Blocks = Section["Blocks"] as TagNodeByteArray; TagNodeByteArray Data = Section["Data"] as TagNodeByteArray; for (int yInSection = 0; yInSection < 16; yInSection++) { for (int zInSection = 0; zInSection < 16; zInSection++) { for (int xInSection = 0; xInSection < 16; xInSection++) { int blockPos = yInSection * 16 * 16 + zInSection * 16 + xInSection; byte rawType = Blocks.Data[blockPos]; NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(rawType); int worldY = yInSection + sectionIndex * 16; if (generator != null) { pos.Set(xInSection, worldY, zInSection); byte blockData = NBTHelper.GetNibble(Data.Data, blockPos); try { if (generator.isTileEntity) { tileEntityList.Add(pos); } else { AddCube(generator, blockData, updateFlag); } } catch (System.Exception e) { int worldX = x * 16 + xInSection; int worldZ = z * 16 + zInSection; Vector3Int worldPos = new Vector3Int(worldX, worldY, worldZ); Debug.LogError(generator.GetType() + ",pos=" + worldPos + "\n" + e.ToString()); } } else if (rawType != 0 && rawType != 11) { int worldX = x * 16 + xInSection; int worldZ = z * 16 + zInSection; Vector3Int worldPos = new Vector3Int(worldX, worldY, worldZ); Debug.LogWarning("generator not exist" + ",pos=" + worldPos + ", type=" + rawType); } } } } } UnityEngine.Profiling.Profiler.EndSample(); }
public static void Init() { generatorDict = new Dictionary <byte, NBTBlock>() { { 1, new NBTStone() }, { 2, new NBTGrassBlock() }, { 3, new NBTDirt() }, { 4, new NBTCobblestone() }, { 5, new NBTPlanks() }, { 6, new NBTSapling() }, { 7, new NBTBedrock() }, { 9, new NBTStationaryWater() }, { 12, new NBTSand() }, { 13, new NBTGravel() }, { 14, new NBTGoldOre() }, { 15, new NBTIronOre() }, { 16, new NBTCoalOre() }, { 17, new NBTLog() }, { 18, new NBTLeaves() }, { 20, new NBTGlass() }, { 21, new NBTLapisOre() }, { 24, new NBTSandStone() }, { 26, new NBTBed() }, { 31, new NBTTallGrass() }, { 32, new NBTDeadBush() }, { 35, new NBTWool() }, { 37, new NBTYellowFlower() }, { 38, new NBTRedFlower() }, { 39, new NBTBrownMushroom() }, { 40, new NBTRedMushroom() }, { 43, new NBTDoubleStoneSlab() }, { 45, new NBTBrick() }, { 47, new NBTBookshelf() }, { 48, new NBTMossyCobblestone() }, { 49, new NBTObsidian() }, { 50, new NBTTorch() }, { 53, new NBTOakStairs() }, { 54, new NBTChest() }, { 56, new NBTDiamondOre() }, { 58, new NBTCraftingTable() }, { 59, new NBTWheat() }, { 60, new NBTFarmland() }, { 61, new NBTFurnace() }, { 62, new NBTLitFurnace() }, { 64, new NBTOakDoor() }, { 65, new NBTLadder() }, { 67, new NBTCobblestoneStairs() }, { 72, new NBTWoodenPressurePlate() }, { 73, new NBTRedstoneOre() }, { 78, new NBTSnowLayer() }, { 81, new NBTCactus() }, { 82, new NBTClay() }, { 83, new NBTSugarCane() }, { 85, new NBTFence() }, { 86, new NBTPumpkin() }, { 97, new NBTMonsterEgg() }, { 99, new NBTBrownMushroomBlock() }, { 100, new NBTRedMushroomBlock() }, { 102, new NBTGlassPane() }, { 108, new NBTBrickStairs() }, { 125, new NBTDoubleWoodenSlab() }, { 126, new NBTWoodenSlab() }, { 129, new NBTEmeraldOre() }, { 134, new NBTSpruceStairs() }, { 135, new NBTBirchStairs() }, { 136, new NBTJungleStairs() }, { 141, new NBTCarrots() }, { 142, new NBTPotatoes() }, { 161, new NBTLeaves2() }, { 162, new NBTLog2() }, { 175, new NBTLargeFlowers() }, { 188, new NBTSpruceFence() }, { 193, new NBTSpruceDoor() }, { 208, new NBTGrassPath() }, }; itemList = new List <NBTItem>() { new NBTShears(), new NBTWheatSeeds(), new NBTWoodenHoe(), new NBTWoodenShovel(), new NBTWoodenPickaxe(), new NBTStonePickaxe(), new NBTIronPickaxe(), new NBTGoldPickaxe(), new NBTDiamondPickaxe(), new NBTWoodenAxe(), new NBTStoneAxe(), new NBTIronAxe(), new NBTGoldAxe(), new NBTDiamondAxe(), new NBTStick(), new NBTBucket(), new NBTWaterBucket(), new NBTDiamondSword(), new NBTGunpowder(), new NBTPoisonousPotato(), new NBTRottonFlesh(), new NBTCoal(), new NBTIronIngot(), new NBTGoldIngot(), new NBTDiamond(), }; id2generator = new Dictionary <string, NBTBlock>(); id2type = new Dictionary <string, byte>(); foreach (KeyValuePair <byte, NBTBlock> keyValue in generatorDict) { NBTBlock generator = keyValue.Value; generator.Init(); if (generator.id != null) { id2generator.Add(generator.id, generator); id2type.Add(generator.id, keyValue.Key); } } id2item = new Dictionary <string, NBTItem>(); foreach (NBTItem item in itemList) { if (item.id != null) { id2item.Add(item.id, item); } } }
void DrawWireFrame() { WireFrameHelper.render = false; Vector3 center = new Vector3(Screen.width / 2, Screen.height / 2, 0); int cubeLayerIndex = LayerMask.NameToLayer("Chunk"); int otherPlayerLayerIndex = LayerMask.NameToLayer("OtherPlayer"); int plantLayerIndex = LayerMask.NameToLayer("Plant"); if (cubeLayerIndex != -1 && otherPlayerLayerIndex != -1 && plantLayerIndex != -1) { int layerMask = 1 << cubeLayerIndex | 1 << otherPlayerLayerIndex | 1 << plantLayerIndex; if (Physics.Raycast(Camera.main.ScreenPointToRay(center), out hit, 5f, layerMask)) { if (hit.transform.gameObject.layer == cubeLayerIndex || hit.transform.gameObject.layer == plantLayerIndex) { Vector3Int pos = Vector3Int.RoundToInt(hit.point - hit.normal / 10); byte type = 0; byte data = 0; NBTHelper.GetBlockData(pos.x, pos.y, pos.z, ref type, ref data); if (type != 0) { if (pos != WireFrameHelper.pos) { breakTime = 0; } WireFrameHelper.render = true; WireFrameHelper.pos = pos; WireFrameHelper.hitPos = hit.point; WireFrameHelper.type = type; WireFrameHelper.data = data; if (leftMouseDown) { handAnimator.SetBool("isBreaking", true); breakTime += Time.deltaTime; NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(type); float breakNeedTime = generator.breakNeedTime; if (breakNeedTime == 0) { BreakBlock(pos); } else { int curStage = Mathf.FloorToInt(breakTime / (breakNeedTime / 12)); if (stage != curStage) { stage = curStage; UpdateBreakingEffect(generator, pos, stage); } if (stage >= 10) { BreakBlock(pos); } } return; } } } } } handAnimator.SetBool("isBreaking", false); }
// Update is called once per frame void Update() { sb.Clear(); sb.Append("Theircraft "); sb.Append(Application.version); sb.Append("\n"); sb.Append(GetFPS()); sb.Append(" fps"); Vector3 pos = PlayerController.instance.transform.position; sb.Append("\nXYZ: "); sb.Append(pos.x); sb.Append(" / "); sb.Append(pos.y); sb.Append(" / "); sb.Append(pos.z); Vector3Int curBlock = PlayerController.GetCurrentBlock(); sb.Append("\nBlock: "); sb.Append(curBlock.x); sb.Append(" "); sb.Append(curBlock.y); sb.Append(" "); sb.Append(curBlock.z); int chunkX = Mathf.FloorToInt(curBlock.x / 16f); int chunkY = Mathf.FloorToInt(curBlock.y / 16f); int chunkZ = Mathf.FloorToInt(curBlock.z / 16f); int xInChunk = curBlock.x - chunkX * 16; int yInChunk = curBlock.y - chunkY * 16; int zInChunk = curBlock.z - chunkZ * 16; sb.Append("\nChunk: "); sb.Append(xInChunk); sb.Append(" "); sb.Append(yInChunk); sb.Append(" "); sb.Append(zInChunk); sb.Append(" in "); sb.Append(chunkX); sb.Append(" "); sb.Append(chunkY); sb.Append(" "); sb.Append(chunkZ); if (WireFrameHelper.render) { sb.Append("\nLooking at: "); sb.Append(WireFrameHelper.pos.x); sb.Append(" "); sb.Append(WireFrameHelper.pos.y); sb.Append(" "); sb.Append(WireFrameHelper.pos.z); sb.Append("\nType: "); NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(WireFrameHelper.type); sb.Append(generator.name); sb.Append("\nData: "); sb.Append(WireFrameHelper.data); } label.text = sb.ToString(); }
public void RefreshMeshData() { //Debug.Log("RefreshMeshData,chunk=" + x + ",z=" + z); collidable.Clear(); notCollidable.Clear(); water.Clear(); Vector3Int pos = new Vector3Int(); for (int sectionIndex = 0; sectionIndex < Sections.Count; sectionIndex++) { TagNodeCompound Section = Sections[sectionIndex] as TagNodeCompound; TagNodeByteArray Blocks = Section["Blocks"] as TagNodeByteArray; TagNodeByteArray Data = Section["Data"] as TagNodeByteArray; TagNodeByteArray SkyLight = Section["SkyLight"] as TagNodeByteArray; for (int yInSection = 0; yInSection < 16; yInSection++) { for (int zInSection = 0; zInSection < 16; zInSection++) { for (int xInSection = 0; xInSection < 16; xInSection++) { int blockPos = yInSection * 16 * 16 + zInSection * 16 + xInSection; byte rawType = Blocks.Data[blockPos]; NBTBlock generator = NBTGeneratorManager.GetMeshGenerator(rawType); if (generator != null) { int worldY = yInSection + sectionIndex * 16; pos.Set(xInSection, worldY, zInSection); byte blockData = NBTHelper.GetNibble(Data.Data, blockPos); byte skyLight = NBTHelper.GetNibble(SkyLight.Data, blockPos); try { if (generator is NBTStationaryWater) { //generator.GenerateMeshInChunk(this, blockData, pos, water); } else if (generator is NBTPlant) { generator.AddCube(this, blockData, skyLight, pos, notCollidable); } else { generator.AddCube(this, blockData, skyLight, pos, collidable); } } catch (System.Exception e) { Debug.LogWarning(generator.GetType() + "\n" + e.ToString()); } } else if (rawType != 0 && rawType != 11) { Debug.LogWarning("generator not exist, type=" + rawType); } } } } } hasBuiltMesh = true; isDirty = false; }
string Render(NBTBlock generator, byte data = 0, string dir = null) { Mesh mesh = generator.GetItemMesh(data); if (go != null) { DestroyImmediate(go); } go = new GameObject(); go.name = generator.GetIconPathByData(data); go.AddComponent <MeshFilter>().sharedMesh = mesh; go.AddComponent <MeshRenderer>().sharedMaterial = generator.GetItemMaterial(0); RenderTexture rt = RenderTexture.GetTemporary(size * 2, size * 2, 24, RenderTextureFormat.ARGB32); Camera.main.targetTexture = rt; Camera.main.Render(); Camera.main.targetTexture = null; Texture2D tex = new Texture2D(size, size, TextureFormat.ARGB32, true); //降分辨率 RenderTexture tempRt = RenderTexture.GetTemporary(tex.width, tex.height, 24, RenderTextureFormat.ARGB32); Graphics.Blit(rt, tempRt); RenderTexture.active = tempRt; tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); tex.Apply(); RenderTexture.active = null; rt.Release(); tempRt.Release(); string path = ""; if (dir == null) { path = EditorUtility.SaveFilePanel("保存", "", go.name, "png"); if (path.Length == 0) { DestroyImmediate(tex); DestroyImmediate(go); return(null); } path = path.Substring(path.IndexOf("Assets")); } else { path = dir + "/" + go.name + ".png"; path = path.Substring(path.IndexOf("Assets")); } byte[] bytes = tex.EncodeToPNG(); System.IO.File.WriteAllBytes(path, bytes); AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); TextureImporter TextureI = AssetImporter.GetAtPath(path) as TextureImporter; TextureI.textureCompression = TextureImporterCompression.Uncompressed; TextureI.mipmapEnabled = false; TextureI.wrapMode = TextureWrapMode.Clamp; TextureI.textureType = TextureImporterType.Sprite; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); AssetDatabase.Refresh(); DestroyImmediate(tex); return(path); }
void AddGridItem(NBTBlock generator, byte data) { generatorList.Add(generator); dataList.Add(data); optionList.Add(generator.ToString() + ", " + data); }