public static void ShowBlock(CSBlockType type) { instance.handMeshRenderer.enabled = false; instance.blockMeshFilter.mesh = ChunkMeshGenerator.GetBlockMesh(type); instance.blockMeshRenderer.material.mainTexture = ChunkMeshGenerator.GetBlockTexture(type); instance.blockMeshFilter.transform.gameObject.SetActive(true); }
private void SetUp() { //Create Chunks UnityEngine.Debug.Log("Creating chunks"); chunks = new Chunk[width, height, width]; GameObject chunkObj; for (int i = 0; i < Width; i++) { for (int j = 0; j < Height; j++) { for (int k = 0; k < Width; k++) { chunkObj = Instantiate(chunkToSpawn, transform) as GameObject; chunks[i, j, k] = chunkObj.GetComponent <Chunk>(); chunkObj.transform.position = new Vector3(i * Chunk.Width, j * Chunk.Height, k * Chunk.Width); } } } // Set Names SetChunkNames(); //Set Chunk Locality SetChunkLocality(0, width - 1, 0, width - 1); //Generate Block Data DataGenerator.GenWorld(this); //Generate Mesh Data StartCoroutine(ChunkMeshGenerator.Initialize()); ChunkMeshGenerator.DrawWorld(this); }
void CreateSystems() { addEntityProcessor(chunkMeshGenerator = new ChunkMeshGenerator(ChunkTextures)); addEntityProcessor(worldLoader = new WorldLoader(chunkMeshGenerator, 2, 2)); addEntityProcessor(tileEntitySystem = new TileEntitySystem()); addEntityProcessor(world = new World(new SandboxGenerator())); }
private void CreateRandom(Shape shape, bool append = false) { //World world = GameObject.Find("World Spawner").GetComponentInChildren<World>(); Index blockIndex = new Index((int)(Random.value * Chunk.Width), (int)(Random.value * Chunk.Height), (int)(Random.value * Chunk.Width)); Index chunkIndex = new Index((int)(Random.value * world.Width), 0, (int)(Random.value * world.Width)); Work work = new Work(world.chunks[chunkIndex.i, chunkIndex.j, chunkIndex.k], blockIndex, shape); ChunkMeshGenerator.EnqueueWork(work); }
protected override void OnLoad() { GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.CullFace); mesh = new Mesh(new Meshes.VertexAttribute[] { Meshes.VertexAttribute.GenerateAttribute <float>(3, "aPosition", VertexAttribPointerType.Float), Meshes.VertexAttribute.GenerateAttribute <float>(2, "aTexCoord", VertexAttribPointerType.Float) }); _mesh = new ChunkMesh(); _camera = new Camera(Vector3.UnitZ * 3, (float)Size.X / Size.Y); mesh.UpdateProjection(_camera.GetProjectionMatrix()); mesh.UpdateView(_camera.GetViewMatrix()); mesh.UpdateModel(Matrix4.Identity); mesh.SetData(new float[] { 0, 0, -2, 0, 0, 1, 0, -2, 1, 0, 1, 1, -2, 1, 1, 0, 1, -2, 0, 1, }, new uint[] { 0, 1, 2, 0, 2, 3 } ); _mesh.UpdateProjection(_camera.GetProjectionMatrix()); _mesh.UpdateView(_camera.GetViewMatrix()); _mesh.UpdateModel(OpenToolkit.Mathematics.Matrix4.Identity); _mesh.SetTexture("Core/Textures/tilemap.png"); _world = new World(); double timer = 0; ChunkMeshGenerator.GenerateMesh(_mesh, _world._chunks[new Vector3i()], ref timer); Common.VoxelEngine.Spaceship.SmallShip.Chunk chunk = new Common.VoxelEngine.Spaceship.SmallShip.Chunk(); for (int i = 0; i < Common.VoxelEngine.Spaceship.SmallShip.SmallShipComponent.CHUNK_SIZE_CUBE; i++) { if (i % 2 == 0) { chunk.blocks[i] = 1; } } base.OnLoad(); }
private void CreateBlock(RaycastHit hit, Shape shape, bool append = false) { Chunk chunk = hit.transform.GetComponent <Chunk>(); hit.point += offset; blockIndex.i = (int)(append ? hit.normal.x + hit.point.x % Chunk.Width : hit.point.x % Chunk.Width); blockIndex.j = (int)(append ? hit.normal.y + hit.point.y % Chunk.Width : hit.point.y % Chunk.Width); blockIndex.k = (int)(append ? hit.normal.z + hit.point.z % Chunk.Width : hit.point.z % Chunk.Width); ChunkMeshGenerator.EnqueueWork(new Work(chunk, blockIndex, shape)); }
public void Start() { ChunkMeshGenerator = GetComponent <ChunkMeshGenerator>(); Sphere = GetComponent <SdfSphere>(); MeshFilter = GetComponent <MeshFilter>(); MeshRenderer = GetComponent <MeshRenderer>(); Chunk = new Chunk(new Vector3(32, 32, 32), new Vector3(0, 0, 0)); }
void OnEnable() { if (_chunkMeshGenerator == null) { _chunkMeshGenerator = GetComponent <ChunkMeshGenerator>(); } if (_chunkMeshGenerator != null) { _chunkMeshGenerator.enabled = true; } }
void RefreshMesh() { if (Count > 1 && singleMesh) { singleMesh = false; Transform mesh = Instantiate(meshTrans); Destroy(mesh.GetComponent <Animator>()); mesh.parent = meshTrans; mesh.localPosition = ChunkMeshGenerator.IsCubeType(type) ? cubeOffset : plantOffset; mesh.localEulerAngles = Vector3.zero; mesh.localScale = Vector3.one; } }
public void RenderBlock(Block originBlock) { if (meshGenerator == null) { meshGenerator = GetComponent <ChunkMeshGenerator>(); } if (meshGenerator == null) { return; } MyBlock blockOwner = new MyBlock(Vector3Int.zero, originBlock); meshGenerator.ChunkOrigin = Vector3Int.zero; meshGenerator.ChunkSize = 1; meshGenerator.BlockOwner = blockOwner; meshGenerator.GenerateMesh(); }
static bool IsCanLink(Vector3Int pos) { CSBlockType type = ChunkManager.GetBlockType(pos); if (ChunkMeshGenerator.IsWall(type)) { return(true); } else { if (ChunkManager.HasOpaqueBlock(pos)) { return(true); } } return(false); }
// Start is called before the first frame update void Start() { shadowTrans = transform.Find("shadow"); shadow = shadowTrans.GetComponent <Renderer>().material; meshTrans = transform.Find("mesh_parent/mesh"); MeshFilter meshFilter = meshTrans.GetComponent <MeshFilter>(); meshFilter.sharedMesh = ChunkMeshGenerator.GetBlockMesh(type); if (ChunkMeshGenerator.IsCubeType(type)) { meshFilter.transform.localScale = Vector3.one / 2; } meshTrans.GetComponent <MeshRenderer>().material.mainTexture = ChunkMeshGenerator.GetBlockTexture(type); RefreshMesh(); }
public void Draw(Chunk centerChunk, Index centerBlockIndex) { foreach (Process main in mainProcesses) { Index relativeIndex = main.blockIndex; Chunk targetChunk; Index blockIndex = new Index( centerBlockIndex.i + relativeIndex.i, centerBlockIndex.j + relativeIndex.j, centerBlockIndex.k + relativeIndex.k ); NormalizeLocation(out targetChunk, ref blockIndex, centerChunk); if (targetChunk != null) { targetChunk.Blocks[blockIndex.i, blockIndex.j, blockIndex.k] = main.byteData; addToBeingUpdated(targetChunk, blockIndex); } } foreach (Index sideIndex in sideProcesses) { Index relativeIndex = sideIndex; Chunk targetChunk; Index blockIndex = new Index( centerBlockIndex.i + relativeIndex.i, centerBlockIndex.j + relativeIndex.j, centerBlockIndex.k + relativeIndex.k ); NormalizeLocation(out targetChunk, ref blockIndex, centerChunk); if (targetChunk != null) { addToBeingUpdated(targetChunk, blockIndex); } } foreach (BlockLocation targetBlock in blocksToBeUpdated) { ChunkMeshGenerator.UpdateBlock(targetBlock.chunk, targetBlock.blockIndex); } foreach (Chunk targetChunk in chunksToBeUpdated) { ChunkMeshGenerator.SpitMesh(targetChunk); //Debug.Log("updated chunk " + targetChunk.Name); } chunksToBeUpdated.Clear(); blocksToBeUpdated.Clear(); }
public IEnumerator TraverseBack() { if (inTraversal) { UnityEngine.Debug.Log("Already in traversal, breaking"); yield break; } inTraversal = true; transform.position += Vector3.back * Chunk.Width; int lastIndex = width - 1; Chunk[,,] traversalChunks = GetChunks(0, lastIndex, lastIndex, lastIndex); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { for (int k = lastIndex - 1; k >= 0; k--) { chunks[i, j, k].transform.localPosition += Vector3.forward * Chunk.Width; chunks[i, j, k + 1] = chunks[i, j, k]; } } } yield return(null); SetChunks(0, lastIndex, 0, 0, traversalChunks); yield return(null); DataGenerator.GenChunks(this, 0, lastIndex, 0, 0); yield return(null); SetChunkLocality(0, lastIndex, 0, 1); yield return(StartCoroutine(ChunkMeshGenerator.DrawChunks(this, 0, lastIndex, 0, 1))); SetChunkNames(); inTraversal = false; }
void LateUpdate() { if (!_isDirty) { return; } if (_chunkMeshGenerator == null) { _chunkMeshGenerator = GetComponent <ChunkMeshGenerator>(); } if (_chunkMeshGenerator == null) { _isDirty = false; return; } if (_chunkMeshGenerator.TryRefresh()) { _isDirty = false; } }
public void GenerateChunkObject(int x, int y, float[,] chunkHeights) { if (!GetTerrain()) { terrain = new GameObject(terrainGroup).transform; terrain.transform.parent = transform; } GameObject chunk = new GameObject(string.Format("{0},{1}", x, y)); chunk.transform.parent = terrain.transform; chunk.transform.localPosition = new Vector3(x * setting.ChunkSize, 0, y * setting.ChunkSize); if (setting.layerMask > 0) { chunk.layer = setting.layerMask; } Mesh mesh = ChunkMeshGenerator.Generate(chunkHeights, setting.mapScale); chunk.AddComponent <MeshFilter>().mesh = mesh; chunk.AddComponent <MeshCollider>().sharedMesh = mesh; chunk.AddComponent <MeshRenderer>().material = terrainMaterial; GameObjectUtility.SetStaticEditorFlags(chunk, StaticEditorFlags.NavigationStatic); EditorUtility.SetDirty(this); }
/// <summary> /// Generate just the mesh and uv values for the column of chunks /// </summary> protected override void ThreadFunction() { // @todo: breaks here? if (columnLocation.isInitialized && island != null) { for (int y = island.heightInChunks - 1; y >= 0; y--) { Chunk chunk = island.getChunk(new Coordinate(columnLocation.x, y, columnLocation.z)); if (chunk != null && !chunk.isEmpty) { if (!chunk.hasBeenRendered) { if (chunk.hasBeenGenerated) { ChunkMeshGenerator chunkRenderer = new ChunkMeshGenerator(); ChunkMeshGenerator.ChunkMesh mesh = chunkRenderer.generateMeshFor(chunk); outputQueue.Add(mesh); } } } } } }
private void createMeshCreators() { chunkGenerators = new Dictionary <Vector3Int, ChunkMeshGenerator>(); Vector3Int mapSize = GridMap.Instance.GetSize(); int xSize = mapSize.x / chunkSize; int ySize = mapSize.y / chunkSize; int zSize = mapSize.z / chunkSize; if (xSize == 0) { xSize = 1; } if (ySize == 0) { ySize = 1; } if (zSize == 0) { zSize = 1; } for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { for (int z = 0; z < zSize; z++) { GameObject newObj = (GameObject)Instantiate(chunkGeneratorPrefab, transform); newObj.transform.position = Vector3.zero; ChunkMeshGenerator comp = newObj.GetComponent <ChunkMeshGenerator>(); comp.ChunkOrigin = new Vector3Int(x * chunkSize, y * chunkSize, z * chunkSize); comp.ChunkSize = chunkSize; comp.BlockOwner = GridMap.Instance; chunkGenerators[new Vector3Int(x, y, z)] = comp; } } } }
// intput is global position public static bool IsStairs(Vector3Int position) { CSBlockType type = GetBlockType(position); return(ChunkMeshGenerator.IsStair(type)); }
string Render(CSBlockType type, string dir = null) { GameObject go = null; if (type != CSBlockType.Chest) { go = new GameObject(); go.AddComponent <MeshFilter>().sharedMesh = ChunkMeshGenerator.GetBlockMesh(type); go.AddComponent <MeshRenderer>().sharedMaterial = new Material(Shader.Find("Custom/CutoutDiffuse")); go.GetComponent <MeshRenderer>().sharedMaterial.mainTexture = ChunkMeshGenerator.GetBlockTexture(type); } else { GameObject prefab = Resources.Load <GameObject>("Prefabs/Entity/Chest"); go = Instantiate(prefab); } 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("保存", "", type.ToString(), "png"); if (path.Length == 0) { DestroyImmediate(tex); DestroyImmediate(go); return(null); } path = path.Substring(path.IndexOf("Assets")); } else { path = dir + "/" + type.ToString() + ".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; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); AssetDatabase.Refresh(); DestroyImmediate(tex); if (DestroyAfterFinish) { DestroyImmediate(go); } return(path); }
void Update() { DrawWireFrame(); if (acceptInput) { RotateView(); if (Input.GetMouseButtonDown(0)) { handAnimator.SetTrigger("interactTrigger"); if (WireFrameHelper.render) { if (WireFrameHelper.pos.y != 0) { DeleteBlockReq(WireFrameHelper.pos); } } } if (Input.GetMouseButtonDown(1)) { if (WireFrameHelper.render && ItemSelectPanel.curBlockType != CSBlockType.None) { Vector3Int pos = WireFrameHelper.pos; if (ChunkManager.HasCollidableBlock(WireFrameHelper.pos.x, WireFrameHelper.pos.y, WireFrameHelper.pos.z)) { pos = WireFrameHelper.pos + Vector3Int.RoundToInt(hit.normal); } if (!cc.bounds.Intersects(new Bounds(pos, Vector3.one)) && !ChunkManager.HasNotPlantBlock(pos)) { if (ItemSelectPanel.curBlockType == CSBlockType.Torch) { AddBlockReq(Vector3Int.RoundToInt(pos), ItemSelectPanel.curBlockType, WireFrameHelper.pos); } else { CSBlockOrientation orientation = ChunkMeshGenerator.GetBlockOrientation(transform.position, pos, WireFrameHelper.hitPos); AddBlockReq(Vector3Int.RoundToInt(pos), ItemSelectPanel.curBlockType, orientation); } } } } if (Input.GetKeyDown(KeyCode.Space)) { Jump(); } if (Input.GetKeyDown(KeyCode.Q)) { ItemSelectPanel.DropCurItem(); } } if (needUpdate && Time.realtimeSinceStartup - lastUpdateTime > timeInterval) { needUpdate = false; lastUpdateTime = Time.realtimeSinceStartup; CSHeroMoveReq req = new CSHeroMoveReq { Position = new CSVector3 { x = transform.position.x, y = transform.position.y, z = transform.position.z }, Rotation = new CSVector3 { x = 0, y = transform.localEulerAngles.y, z = camera.transform.localEulerAngles.x } }; NetworkManager.SendPkgToServer(ENUM_CMD.CS_HERO_MOVE_REQ, req); } }
protected override void OnLoad() { base.OnLoad(); var(mainCamera, _) = GetAll <Camera>().First(); Set(mainCamera, (Transform)Matrix4x4.CreateTranslation(0, 26, 0)); var meshManager = Processors.GetOrThrow <MeshManager>(); var heartMesh = meshManager.Load(this, "heart.glb"); var swordMesh = meshManager.Load(this, "sword.glb"); for (var x = -12; x <= 12; x++) { for (var z = -12; z <= 12; z++) { var entity = Entities.New(); var position = Matrix4x4.CreateTranslation(x * 2, 24, z * 2); var rotation = Matrix4x4.CreateRotationY(RND.NextFloat(MathF.PI * 2)); Set(entity, (Transform)(rotation * position)); Set(entity, RND.Pick(heartMesh, swordMesh)); } } var textureManager = Processors.GetOrThrow <TextureManager>(); var texture = textureManager.Load(this, "terrain.png"); var stone = Entities.New(); var dirt = Entities.New(); var grass = Entities.New(); Set(stone, TextureCoords4.FromGrid(4, 4, 1, 0)); Set(dirt, TextureCoords4.FromGrid(4, 4, 2, 0)); Set(grass, TextureCoords4.FromGrid(4, 4, 3, 0)); void CreateChunk(ChunkPos pos) { var chunk = Entities.New(); var storage = new ChunkPaletteStorage <Block>(default(Block)); for (var x = 0; x < 16; x++) { for (var y = 0; y < 16; y++) { for (var z = 0; z < 16; z++) { var yy = (pos.Y << 4) | y; if (RND.NextBool(0.5 - yy / 48.0)) { storage[x, y, z] = new Block((yy > 16) ? grass : (yy > -16) ? dirt : stone); } } } } Set(chunk, new Chunk(pos)); Set(chunk, (Transform)Matrix4x4.CreateTranslation(pos.GetOrigin())); Set(chunk, storage); } var chunkMeshGenerator = new ChunkMeshGenerator(this); var chunkStore = (LookupDictionaryStore <ChunkPos, Chunk>)Components.GetStore <Chunk>(); void GenerateChunkMesh(ChunkPos pos) { var chunk = Entities.GetByID(chunkStore.GetEntityID(pos)) !.Value; var chunkMesh = chunkMeshGenerator.Generate(pos); if (chunkMesh == null) { return; } Set(chunk, chunkMesh.Value); Set(chunk, texture); } for (var x = -6; x < 6; x++) { for (var y = -2; y < 2; y++) { for (var z = -6; z < 6; z++) { CreateChunk(new ChunkPos(x, y, z)); } } } for (var x = -6; x < 6; x++) { for (var y = -2; y < 2; y++) { for (var z = -6; z < 6; z++) { GenerateChunkMesh(new ChunkPos(x, y, z)); } } } }
protected override bool EvolutionWithMultithreading(bool forceEvolveArCurrentThread) { ChunkMeshGenerator.PopulateMeshDataToDrawChunk(chunk, chunk.mesh, chunk.chunkData); return(true); }