void GeneratePlanet() { // Thread[] rowOfChunks = new Thread[Universe.chunkSize * Universe.chunkSize * Universe.chunkSize]; // Thread chunkThread; int cubeCount = 1; for (int chunkYIndex = 0; chunkYIndex < planetSize; chunkYIndex++) { for (int chunkZIndex = 0; chunkZIndex < planetSize; chunkZIndex++) { for (int chunkXIndex = 0; chunkXIndex < planetSize; chunkXIndex++) { // this.transform.position = planet's coords ? Vector3 chunkPosition = new Vector3(planet.transform.position.x + (chunkXIndex * chunkSize), planet.transform.position.y + (chunkYIndex * chunkSize), planet.transform.position.z + (chunkZIndex * chunkSize)); // GenerateChunk(chunkPosition); // Debug.Log("Chunk position: " + chunkPosition); // THREADING http://www.albahari.com/threading/ chunkMaterial = GetNextMaterial(cubeCount); PlanetChunk planetChunk = new PlanetChunk(planet.gameObject, this, chunkPosition, chunkMaterial, chunkXIndex, chunkYIndex, chunkZIndex); // CHANGE THIS!!! include parameter stating biome (desert, jungle, etc.) // Debug.Log("Adding chunk to planetChunks : " + planetChunk.planetChunk.name + " at coords : " + chunkPosition); // planetChunk.planetChunk.transform.parent = planet.transform; // TODO: This is not used/needed!!! tested in game and not used! planetChunks.Add(planetChunk.planetChunk.name, planetChunk); planetChunk.BuildTheChunk(); planetChunk.DrawChunk(); cubeCount++; if (cubeCount > 3) { cubeCount = 1; } // t2 = new Thread(() => Console.WriteLine(text)); // Debug.Log("----------------------------"); // Debug.Log("Generating chunk @ " + chunkPosition); // Debug.Log("============================"); // chunkThread = new Thread(() => GenerateChunk(chunkPosition)); // chunkThread.Start(); // chunkThread.IsBackground = true; } } } foreach (KeyValuePair <string, PlanetChunk> c in planetChunks) { // c.Value.DrawChunk(); // draw the entire chunk } /* Make the terrain look more natural * e.g. Pass over the terrain, working out where to apply gradual blending */ foreach (KeyValuePair <string, PlanetChunk> c in planetChunks) { // chunk.Value.MakeTerrainLookReal(); } }
/* * Used for building the terrain */ private void AddCube(int x, int y, int z, PlanetChunk chunk) { DestroyChunk(chunk); // TODO: NEED TO KEEP THE CHANGED STATE!!!! chunk.ReBuildTheChunk(); chunk.CubeIsSolid[x, y, z] = true; // draw the cube chunk.DrawChunk(); }
/* * Used for digging the terrain */ private void RemoveCube(int x, int y, int z, PlanetChunk chunk) { DestroyChunk(chunk); // TODO: NEED TO KEEP THE CHANGED STATE!!!! chunk.ReBuildTheChunk(); chunk.CubeIsSolid[x, y, z] = false; // stop the cube from being drawn chunk.DrawChunk(); }
void InstantiatePlanet() { GameObject planetObj = Instantiate(m_planetPrefab); planetObj.transform.position = m_position; planetObj.name = "Planet1"; Planet planet = planetObj.GetComponent <Planet>(); planet.Initalize(m_chunk_count, m_xyzResolution); Vector3 chunk_offset = Vector3.one * (m_xyzResolution * m_chunk_count / -2f + 0.5f * m_xyzResolution); for (int c_z = 0, id = 0; c_z < m_chunk_count; c_z++) { for (int c_y = 0; c_y < m_chunk_count; c_y++) { for (int c_x = 0; c_x < m_chunk_count; c_x++, id++) { int map_offset = c_x * m_xyzResolution + c_y * m_xyzResolution * m_length + c_z * m_xyzResolution * m_length2; GameObject chunkObj = Instantiate(m_planetChunkPrefab, planetObj.transform, false); chunkObj.name = planetObj.name + "_chunk" + id.ToString(); chunkObj.transform.position = planetObj.transform.position + (chunk_offset + new Vector3(c_x * m_xyzResolution, c_y * m_xyzResolution, c_z * m_xyzResolution)) * m_scale; planet.AddChunk(chunkObj); PlanetChunk chunk = chunkObj.GetComponent <PlanetChunk>(); chunk.m_meshMaterial = m_planetMaterial; chunk.m_lod1Distance = m_lod1; chunk.m_lod2Distance = m_lod2; chunk.m_lod3Distance = m_lod3; chunk.Initalize(id, m_xyzResolution, m_scale, m_shaded, planetObj); SetGeoCoords(chunk, c_x, c_y, c_z); float[] densityMap = new float[m_xyzResolution * m_xyzResolution * m_xyzResolution]; for (int z = 0; z < m_xyzResolution; z++) { for (int y = 0; y < m_xyzResolution; y++) { for (int x = 0; x < m_xyzResolution; x++) { densityMap[x + y * m_xyzResolution + z * m_xyzResolution * m_xyzResolution] = m_densityMap[map_offset + x + y * m_length + z * m_length2]; } } } chunk.SetDensityMap(densityMap); } } } planet.AssignChunkNeighboursAndRefresh(); }
// // Called every frame. 'delta' is the elapsed time since the previous frame. // public override void _Process(float delta) // { // // } // Cube contructor public Cube(PlanetChunk owner, int currentX, int currentY, int currentZ, Vector3 cubePosition, SpatialMaterial cubeMaterial) { this.owner = owner; cubeLocation = cubePosition; this.cubeMaterial = cubeMaterial; cube = new CSGCombiner(); // TODO: keep as CSGCombiner or change to Spatial? this.currentX = currentX; this.currentY = currentY; this.currentZ = currentZ; }
public Vector3 GetBlockPos(RaycastHit hit, PlanetChunk chunk, bool adjacent = false) { Vector3 WorldSpace = new Vector3( MoveWithinBlock(hit.point.x, hit.normal.x, adjacent), MoveWithinBlock(hit.point.y, hit.normal.y, adjacent), MoveWithinBlock(hit.point.z, hit.normal.z, adjacent) ); WorldSpace = new Vector3(WorldSpace.x - (chunk.Position.x * chunklength) - thisx, WorldSpace.y - (chunk.Position.y * chunklength) - thisy, WorldSpace.z - (chunk.Position.z * chunklength) - thisz); return WorldSpace; }
/// <summary> /// Sends data about this block to MeshData so it can be rendered /// </summary> /// <param name="planetchunk">The PlanetChunk that contains this block</param> /// <param name="x">The X coordinate of the Block</param> /// <param name="y">The Y coordinate of the Block</param> /// <param name="z">The Z coordinate of the Block</param> /// <param name="meshData">The MeshData object the mesh is written to</param> /// <returns></returns> public MeshData Blockdata(PlanetChunk planetchunk, int x, int y, int z, MeshData meshData) { if (this.type != BlockTypes.typeEmpty) { GetSolid(planetchunk, x, y, z, meshData); meshData.useRenderDataForCol = true; if (solidsides.up == false) { meshData.BuildSideUp(x, y, z, this.type); } if (solidsides.down == false) { meshData.BuildSideDown(x, y, z, this.type); } if (solidsides.east == false) { meshData.BuildSideEast(x, y, z, this.type); } if (solidsides.west == false) { meshData.BuildSideWest(x, y, z, this.type); } if (solidsides.north == false) { meshData.BuildSideNorth(x, y, z, this.type); } if (solidsides.south == false) { meshData.BuildSideSouth(x, y, z, this.type); } if (type.light == true) { meshData.Light(type.light); if (!solidsides.up && !solidsides.down && !solidsides.east && !solidsides.west && !solidsides.north && !solidsides.south) { meshData.AddLight(x, y, z, type.LR, type.LG, type.LB, type.LA, type.LightRange, type.LightIntensity); } } return meshData; } return meshData; }
// Cube contructor public Cube(GameObject parent, PlanetChunk owner, int currentX, int currentY, int currentZ, Material material, int terrainType, Vector3 cubePosition, string chunkName) { cubeLocation = cubePosition; this.parent = parent; this.owner = owner; cubePhysicalState = CubePhysicalState.SOLID; // default state cube = new GameObject(chunkName + "_" + "Cube_" + Universe.BuildPlanetChunkName(cubeLocation)); this.currentX = currentX; this.currentY = currentY; this.currentZ = currentZ; }
// Use this for initialization public void Start() { localVars.chunkprefab = (GameObject)Resources.Load("Prefabs/PlanetChunkObject"); localVars.ChunkPool = GameObject.FindGameObjectWithTag("Pool").GetComponent<Chunkpool>(); for (int i = 0; i < localVars.poolSize; i++) { chunk = Instantiate(localVars.chunkprefab); script = chunk.GetComponent<PlanetChunk>(); script.meshData = new MeshData(); chunk.SetActive(false); Pool.Add(chunk); } Debug.Log(Pool.Count); }
public PlanetFace(int worldSize, Vector3 localUp) { baseVertexPos = new Vector3[worldSize + 1, worldSize + 1]; Vector3 axisA = new Vector3(localUp.y, localUp.z, localUp.x); Vector3 axisB = Vector3.Cross(localUp, axisA); //for each position on the face calculate the basePosition for (int x = 0; x < worldSize + 1; x++) { for (int y = 0; y < worldSize + 1; y++) { Vector2 percent = new Vector2(x, y) / (worldSize); Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; float x2 = pointOnUnitCube.x * pointOnUnitCube.x; float y2 = pointOnUnitCube.y * pointOnUnitCube.y; float z2 = pointOnUnitCube.z * pointOnUnitCube.z; baseVertexPos[x, y].x = pointOnUnitCube.x * Mathf.Sqrt(1f - y2 / 2f - z2 / 2f + y2 * z2 / 3f); baseVertexPos[x, y].y = pointOnUnitCube.y * Mathf.Sqrt(1f - x2 / 2f - z2 / 2f + x2 * z2 / 3f); baseVertexPos[x, y].z = pointOnUnitCube.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f); } } heightMap = new float[worldSize, worldSize]; for (int x = 0; x < worldSize; x++) { for (int y = 0; y < worldSize; y++) { heightMap[x, y] = worldSize + Random.Range(-2f, 2); } } int numChunks = worldSize / 8; chunks = new PlanetChunk[numChunks, numChunks]; for (int x = 0; x < numChunks; x++) { for (int y = 0; y < numChunks; y++) { chunks[x, y] = new PlanetChunk(); chunks[x, y].mesh = new Mesh(); } } }
// Start is called before the first frame update void Start() { if (CaptureAnchor == null) { Debug.LogError($"Forgot to set {nameof(CaptureAnchor)} anchor!"); } if (MasterRigidbody == null) { Debug.LogError($"Forgot to set {nameof(MasterRigidbody)}!"); } if (Well == null) { Debug.LogError($"Forgot to set {nameof(Well)}!"); } Well.MasterKatamari = this; Ship = GetComponent <Ship>(); Matter = GetComponent <Matter>(); Root = new AttachNode(GetComponent <Matter>()); CapturedObjects = new List <Matter>(); CurrentState = PlayerState.Oblivion; Well.enabled = false; ExistingModel.SetActive(false); DeadModel.SetActive(true); Ship.currentTeam = Ship.Team.Neutral; Ship.enabled = false; GetComponent <SphereCollider>().enabled = false; GetComponent <Matter>().enabled = false; rigidBody = GetComponent <Rigidbody>(); rigidBody.maxAngularVelocity = MaxAngularVelocity; PlanetChunk planetChunk = FindObjectOfType <PlanetChunk>(); if (planetChunk != null) { PlanetMassRequirement = planetChunk.MassThreshold; } }
public void Initialize(PlanetChunk parent, ChunckPosition position, gridVertexMethod method) { if (parent != null) { this.parent = parent; lodLevel = parent.lodLevel + 1; rootPosition = Vector3.right * 10; } else { lodLevel = 0; rootPosition = Vector3.zero; } //TODO on a encore des problems ac les uv ya de la distortion dans les coins... //set one of the main plane to test subdivision here CreateGrid(method); }
private void OnEnable() { /*if (mesh == null) { * mesh = new Mesh(); * mesh.name = "Surface Mesh"; * GetComponent<MeshFilter>().mesh = mesh; * } * Refresh(); */ // TODO : rendre sa plus propre un jour chunks = new PlanetChunk[6]; int i = 0; PlanetChunk p = Instantiate(PlanetChunkPrefab) as PlanetChunk; p.Initialize(null, ChunckPosition.topLeft, gridMethods[(int)gridMethodsNames.topGridVertex]); chunks[i++] = p; p = Instantiate(PlanetChunkPrefab) as PlanetChunk; p.Initialize(null, ChunckPosition.topLeft, gridMethods[(int)gridMethodsNames.bottomGridVertex]); chunks[i++] = p; p = Instantiate(PlanetChunkPrefab) as PlanetChunk; p.Initialize(null, ChunckPosition.topLeft, gridMethods[(int)gridMethodsNames.leftGridVertex]); chunks[i++] = p; p = Instantiate(PlanetChunkPrefab) as PlanetChunk; p.Initialize(null, ChunckPosition.topLeft, gridMethods[(int)gridMethodsNames.rightGridVertex]); chunks[i++] = p; p = Instantiate(PlanetChunkPrefab) as PlanetChunk; p.Initialize(null, ChunckPosition.topLeft, gridMethods[(int)gridMethodsNames.forwardGridVertex]); chunks[i++] = p; p = Instantiate(PlanetChunkPrefab) as PlanetChunk; p.Initialize(null, ChunckPosition.topLeft, gridMethods[(int)gridMethodsNames.backGridVertex]); chunks[i++] = p; }
/* * checks to see if we can add a cube to the game world * * x, y, z - the block location within the chunk (need to see if we are at the extremes of the chunk) * hitChunk - the chunk the Raycast has hit (need to see if this is at the extremes of the planet) * x,y,z = chunk extreme AND hitChunk = chunk at the extremes of the planet, THEN cannot add anymore blocks/cubes */ bool NotAtEndOfWorld(int x, int y, int z, PlanetChunk hitChunk) { // Are we at an end of the planet if (hitChunk.chunkPosition.x == 0 && x < 0) { // we are beyond the planet in negative X return(false); } if (hitChunk.chunkPosition.y == 0 && y < 0) { // we are beyond the planet in negative Y return(false); } if (hitChunk.chunkPosition.z == 0 && z < 0) { // we are beyond the planet in negative Z return(false); } // positive extreme if (hitChunk.chunkPosition.x == Universe.planet.planetSize && x >= Universe.planet.chunkSize) { // we are beyond the planet in positive X return(false); } if (hitChunk.chunkPosition.y == Universe.planet.planetSize && y >= Universe.planet.chunkSize) { // we are beyond the planet in positive Y return(false); } if (hitChunk.chunkPosition.z == Universe.planet.planetSize && z >= Universe.planet.chunkSize) { // we are beyond the planet in positive Z return(false); } // we have gone beyond the end of the world return(true); // must not add anymore blocks/cubes }
public void GeneratePlanet() { int cubeCount = 0; for (int chunkYIndex = 0; chunkYIndex < planetSize; chunkYIndex++) { for (int chunkZIndex = 0; chunkZIndex < planetSize; chunkZIndex++) { for (int chunkXIndex = 0; chunkXIndex < planetSize; chunkXIndex++) { Vector3 chunkPosition = new Vector3(Translation.x + (chunkXIndex * chunkSize), Translation.y + (chunkYIndex * chunkSize), Translation.z + (chunkZIndex * chunkSize)); // THREADING http://www.albahari.com/threading/ chunkMaterial = GetNextMaterial(cubeCount); PlanetChunk planetChunk = new PlanetChunk(this, chunkPosition, chunkMaterial, chunkXIndex, chunkYIndex, chunkZIndex); // CHANGE THIS!!! include parameter stating biome (desert, jungle, etc.) planetChunks.Add(planetChunk.name, planetChunk); planetChunk.BuildTheChunk(); planetChunk.DrawChunk(); AddChild(planetChunk.planetChunk); // add chunk to the planet // place the chunk in its location within the planet/Universe planetChunk.planetChunk.GlobalTranslate(new Vector3(chunkXIndex * chunkSize, chunkYIndex * chunkSize, chunkZIndex * chunkSize)); cubeCount++; if (cubeCount > 3) { cubeCount = 0; } } } } }
void SetGeoCoords(PlanetChunk chunk, int c_x, int c_y, int c_z) { chunk.m_geo[0].Point = new Vector3(c_x * m_xyzResolution, c_y * m_xyzResolution, c_z * m_xyzResolution) - m_planet_center; chunk.m_geo[1].Point = new Vector3((c_x + 1) * m_xyzResolution, c_y * m_xyzResolution, c_z * m_xyzResolution) - m_planet_center; chunk.m_geo[2].Point = new Vector3(c_x * m_xyzResolution, (c_y + 1) * m_xyzResolution, c_z * m_xyzResolution) - m_planet_center; chunk.m_geo[3].Point = new Vector3((c_x + 1) * m_xyzResolution, (c_y + 1) * m_xyzResolution, c_z * m_xyzResolution) - m_planet_center; chunk.m_geo[4].Point = new Vector3(c_x * m_xyzResolution, c_y * m_xyzResolution, (c_z + 1) * m_xyzResolution) - m_planet_center; chunk.m_geo[5].Point = new Vector3((c_x + 1) * m_xyzResolution, c_y * m_xyzResolution, (c_z + 1) * m_xyzResolution) - m_planet_center; chunk.m_geo[6].Point = new Vector3(c_x * m_xyzResolution, (c_y + 1) * m_xyzResolution, (c_z + 1) * m_xyzResolution) - m_planet_center; chunk.m_geo[7].Point = new Vector3((c_x + 1) * m_xyzResolution, (c_y + 1) * m_xyzResolution, (c_z + 1) * m_xyzResolution) - m_planet_center; /*chunk.m_geo[0].LambdaDeg = Vector3.SignedAngle(Vector3.back, new Vector3(c_x * m_xyzResolution, 0, c_z * m_xyzResolution) - m_lambda_center, Vector3.down); * chunk.m_geo[0].PhiDeg = Vector3.SignedAngle(Vector3.back, new Vector3(0, c_y * m_xyzResolution, c_z * m_xyzResolution) - m_phi_center, Vector3.right); * chunk.m_geo[0].Radius = Vector3.Distance(m_planet_center, new Vector3(c_x * m_xyzResolution, c_y * m_xyzResolution, c_z * m_xyzResolution)); * * chunk.m_geo[1].LambdaDeg = Vector3.SignedAngle(Vector3.back, new Vector3((c_x + 1) * m_xyzResolution, 0, c_z * m_xyzResolution) - m_lambda_center, Vector3.down); * chunk.m_geo[1].PhiDeg = Vector3.SignedAngle(Vector3.back, new Vector3(0, c_y * m_xyzResolution, c_z * m_xyzResolution) - m_phi_center, Vector3.right); * chunk.m_geo[1].Radius = Vector3.Distance(m_planet_center, new Vector3((c_x + 1) * m_xyzResolution, c_y * m_xyzResolution, c_z * m_xyzResolution)); * chunk.m_geo[2].LambdaDeg = Vector3.SignedAngle(Vector3.back, new Vector3(c_x * m_xyzResolution, 0, c_z * m_xyzResolution) - m_lambda_center, Vector3.down); * chunk.m_geo[2].PhiDeg = Vector3.SignedAngle(Vector3.back, new Vector3(0, (c_y + 1) * m_xyzResolution, c_z * m_xyzResolution) - m_phi_center, Vector3.right); * chunk.m_geo[2].Radius = Vector3.Distance(m_planet_center, new Vector3(c_x * m_xyzResolution, (c_y + 1) * m_xyzResolution, c_z * m_xyzResolution)); * chunk.m_geo[3].LambdaDeg = Vector3.SignedAngle(Vector3.back, new Vector3((c_x + 1) * m_xyzResolution, 0, c_z * m_xyzResolution) - m_lambda_center, Vector3.down); * chunk.m_geo[3].PhiDeg = Vector3.SignedAngle(Vector3.back, new Vector3(0, (c_y + 1) * m_xyzResolution, c_z * m_xyzResolution) - m_phi_center, Vector3.right); * chunk.m_geo[3].Radius = Vector3.Distance(m_planet_center, new Vector3((c_x + 1) * m_xyzResolution, (c_y + 1) * m_xyzResolution, c_z * m_xyzResolution)); * chunk.m_geo[4].LambdaDeg = Vector3.SignedAngle(Vector3.back, new Vector3(c_x * m_xyzResolution, 0, (c_z + 1) * m_xyzResolution) - m_lambda_center, Vector3.down); * chunk.m_geo[4].PhiDeg = Vector3.SignedAngle(Vector3.back, new Vector3(0, c_y * m_xyzResolution, (c_z + 1) * m_xyzResolution) - m_phi_center, Vector3.right); * chunk.m_geo[4].Radius = Vector3.Distance(m_planet_center, new Vector3(c_x * m_xyzResolution, c_y * m_xyzResolution, (c_z + 1) * m_xyzResolution)); * chunk.m_geo[5].LambdaDeg = Vector3.SignedAngle(Vector3.back, new Vector3((c_x + 1) * m_xyzResolution, 0, (c_z + 1) * m_xyzResolution) - m_lambda_center, Vector3.down); * chunk.m_geo[5].PhiDeg = Vector3.SignedAngle(Vector3.back, new Vector3(0, c_y * m_xyzResolution, (c_z + 1) * m_xyzResolution) - m_phi_center, Vector3.right); * chunk.m_geo[5].Radius = Vector3.Distance(m_planet_center, new Vector3((c_x + 1) * m_xyzResolution, c_y * m_xyzResolution, (c_z + 1) * m_xyzResolution)); * chunk.m_geo[6].LambdaDeg = Vector3.SignedAngle(Vector3.back, new Vector3(c_x * m_xyzResolution, 0, (c_z + 1) * m_xyzResolution) - m_lambda_center, Vector3.down); * chunk.m_geo[6].PhiDeg = Vector3.SignedAngle(Vector3.back, new Vector3(0, (c_y + 1) * m_xyzResolution, (c_z + 1) * m_xyzResolution) - m_phi_center, Vector3.right); * chunk.m_geo[6].Radius = Vector3.Distance(m_planet_center, new Vector3(c_x * m_xyzResolution, (c_y + 1) * m_xyzResolution, (c_z + 1) * m_xyzResolution)); * chunk.m_geo[7].LambdaDeg = Vector3.SignedAngle(Vector3.back, new Vector3((c_x + 1) * m_xyzResolution, 0, (c_z + 1) * m_xyzResolution) - m_lambda_center, Vector3.down); * chunk.m_geo[7].PhiDeg = Vector3.SignedAngle(Vector3.back, new Vector3(0, (c_y + 1) * m_xyzResolution, (c_z + 1) * m_xyzResolution) - m_phi_center, Vector3.right); * chunk.m_geo[7].Radius = Vector3.Distance(m_planet_center, new Vector3((c_x + 1) * m_xyzResolution, (c_y + 1) * m_xyzResolution, (c_z + 1) * m_xyzResolution));*/ }
public void UnloadChunk(PlanetChunk chunk) { if (chunk != null) { if (chunk.modified == true) { BinaryFormatter bf = new BinaryFormatter(); Block[,,] data = chunk.blocks; if (!System.IO.Directory.Exists(Application.persistentDataPath + "/Planet " + planetSeed + "/Chunks/")) { System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Planet " + planetSeed + "/Chunks/"); } else { Debug.Log("Directory found @ " + Application.persistentDataPath + "/Planet " + planetSeed + "/Chunks/"); } FileStream file = File.Create(Application.persistentDataPath + "/Planet " + planetSeed + "/Chunks/" + chunk.Position.x + " " + chunk.Position.y + " " + chunk.Position.z); bf.Serialize(file, data); file.Close(); } planetchunks[(int)chunk.Position.x, (int)chunk.Position.y, (int)chunk.Position.z] = null; ischunkloaded[(int)chunk.Position.x, (int)chunk.Position.y, (int)chunk.Position.z] = false; LoadedChunks.Remove(chunk.gameObject); localVars.ChunkPool.Pool.Add(chunk.gameObject); chunk.gameObject.SetActive(false); } }
public void loadfromfile(Vector3 chunkpos, PlanetChunk script) { gstring chunkpath = chunkpos.x + " " + chunkpos.y + " " + chunkpos.z; script.meshData = new MeshData(); script.coll = script.gameObject.GetComponent<MeshCollider>(); script.filter = script.gameObject.GetComponent<MeshFilter>(); Debug.Log("Loading block data for chunk: " + chunkpath); Block[,,] data = new Block[chunklength, chunklength, chunklength]; BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(path + chunkpath, FileMode.Open); data = (Block[,,])bf.Deserialize(file); file.Close(); script.blocks = data; script.Generated = true; }
public void LoadChunk(Vector3 chunkpos) { using (gstring.Block()) { if (ischunkloaded[(int)chunkpos.x, (int)chunkpos.y, (int)chunkpos.z] == false) { if (localVars.ChunkPool.Pool.Count == 0) { newchunk = (GameObject)Instantiate(localVars.chunkprefab, new Vector3(thisx + (chunkpos.x * chunklength), thisy + (chunkpos.y * chunklength), thisz + (chunkpos.z * chunklength)), this.transform.rotation); fromlist = false; } else { newchunk = localVars.ChunkPool.Pool[0]; localVars.ChunkPool.Pool.Remove(newchunk); fromlist = true; newchunk.transform.position = new Vector3(thisx + (chunkpos.x * chunklength), thisy + (chunkpos.y * chunklength), thisz + (chunkpos.z * chunklength)); } gstring chunkpath = gstring.Concat(chunkpos.x, " ", chunkpos.y, " ", chunkpos.z); newchunk.name = chunkpath; gamenewchunk = newchunk; gamenewchunk.transform.parent = this.gameObject.transform; script = gamenewchunk.GetComponent<PlanetChunk>(); script.planet = this; script.Position = chunkpos; script.StarDistance = this.StarDistance; script.PlanetType = this.PlanetType; script.Temperature = this.Temperature; script.lights = lightsobject; if (System.IO.File.Exists(path + chunkpath) == true) { loadfromfile(chunkpos, script); } else { StartCoroutine(script.Generate()); } planetchunks[(int)chunkpos.x, (int)chunkpos.y, (int)chunkpos.z] = script; ischunkloaded[(int)chunkpos.x, (int)chunkpos.y, (int)chunkpos.z] = true; LoadedChunks.Add(script.gameObject); if (fromlist == true) { gamenewchunk.SetActive(true); } } } }
/* * Check to see if we have crossed over to another chunk (required knowledge for building) * If we have, then we need to make sure we are working on the chunk we are now within * If not, then we sticl with the current chunk * * blockLocation - location of the block/cube that the Raycast has hit * newLocation - location of the block/cube that is to be placed (during building) * hitChunk - chunk that was hit by the Raycast * */ PlanetChunk AtChunkBounds(Vector3 blockLocation, out Vector3 newLocation, PlanetChunk hitChunk) { // initialise cube's new location (initially set to the cube's loaction that the Raycast hit) newLocation.x = blockLocation.x; newLocation.y = blockLocation.y; newLocation.z = blockLocation.z; // Are we beyond the positive extreme of X? if (blockLocation.x > Universe.planet.chunkSize - 1) { // print("We are at the X end"); // if location of block to be placed > PlanetSize * ChunkSize, then next chunk // Get neighbouring chunk string cn = "Chunk_" + Universe.BuildPlanetChunkName(new Vector3(hitChunk.chunkXIndex + 1, hitChunk.chunkYIndex, hitChunk.chunkZIndex)); // does that chunk exist? PlanetChunk newChunk; if (Universe.planet.planetChunks.TryGetValue(cn, out newChunk)) { // Debug.Log("New Chunk FOUND!"); // set the cube's location newLocation.x = 0; newLocation.y = blockLocation.y; newLocation.z = blockLocation.z; return(newChunk); } else { // Debug.Log("New Chunk NOT FOUND!"); // need to account for reaching end of the planet return(null); } } // Are we beyond the positive extreme of Y? if (blockLocation.y > Universe.planet.chunkSize - 1) { // print("We are at the Y end"); // if location of block to be placed > PlanetSize * ChunkSize, then next chunk // Get neighbouring chunk string cn = "Chunk_" + Universe.BuildPlanetChunkName(new Vector3(hitChunk.chunkXIndex, hitChunk.chunkYIndex + 1, hitChunk.chunkZIndex)); // does that chunk exist? PlanetChunk newChunk; if (Universe.planet.planetChunks.TryGetValue(cn, out newChunk)) { // Debug.Log("New Chunk FOUND!"); // set the cube's location newLocation.x = blockLocation.x; newLocation.y = 0; newLocation.z = blockLocation.z; return(newChunk); } else { // Debug.Log("New Chunk NOT FOUND!"); // need to account to reaching end of the planet return(null); } } // Are we beyond the positive extreme of Z? if (blockLocation.z > Universe.planet.chunkSize - 1) { // print("We are at the Z end"); // if location of block to be placed > PlanetSize * ChunkSize, then next chunk // Get neighbouring chunk string cn = "Chunk_" + Universe.BuildPlanetChunkName(new Vector3(hitChunk.chunkXIndex, hitChunk.chunkYIndex, hitChunk.chunkZIndex + 1)); // does that chunk exist? PlanetChunk newChunk; if (Universe.planet.planetChunks.TryGetValue(cn, out newChunk)) { // Debug.Log("New Chunk FOUND!"); // set the cube's location newLocation.x = blockLocation.x; newLocation.y = blockLocation.y; newLocation.z = 0; return(newChunk); } else { // Debug.Log("New Chunk NOT FOUND!"); // need to account to reaching end of the planet return(null); } } // Are we at the negative extreme of X? if (blockLocation.x < 0) // if location of block to be place is negative, then we have moved into another chunk { // print("We are at the X beginning"); // Get neighbouring chunk string cn = "Chunk_" + Universe.BuildPlanetChunkName(new Vector3(hitChunk.chunkXIndex - 1, hitChunk.chunkYIndex, hitChunk.chunkZIndex)); // does that chunk exist? PlanetChunk newChunk; if (Universe.planet.planetChunks.TryGetValue(cn, out newChunk)) { // Debug.Log("New Chunk FOUND!"); // set the cube's location newLocation.x = Universe.planet.chunkSize - 1; newLocation.y = blockLocation.y; newLocation.z = blockLocation.z; return(newChunk); } else { // Debug.Log("New Chunk NOT FOUND!"); // need to account to reaching end of the planet return(null); } } // Are we at the negative extreme of Y? if (blockLocation.y < 0) // if location of block to be place is negative, then we have moved into another chunk { // print("We are at the Y beginning"); // Get neighbouring chunk string cn = "Chunk_" + Universe.BuildPlanetChunkName(new Vector3(hitChunk.chunkXIndex, hitChunk.chunkYIndex - 1, hitChunk.chunkZIndex)); // does that chunk exist? PlanetChunk newChunk; if (Universe.planet.planetChunks.TryGetValue(cn, out newChunk)) { // Debug.Log("New Chunk FOUND!"); // set the cube's location newLocation.x = blockLocation.x; newLocation.y = Universe.planet.chunkSize - 1; newLocation.z = blockLocation.z; return(newChunk); } else { // Debug.Log("New Chunk NOT FOUND!"); // need to account to reaching end of the planet return(null); } } // Are we at the negative extreme of Z? if (blockLocation.z < 0) // if location of block to be place is negative, then we have moved into another chunk { // print("We are at the Z beginning"); // Get neighbouring chunk string cn = "Chunk_" + Universe.BuildPlanetChunkName(new Vector3(hitChunk.chunkXIndex, hitChunk.chunkYIndex, hitChunk.chunkZIndex - 1)); // does that chunk exist? PlanetChunk newChunk; if (Universe.planet.planetChunks.TryGetValue(cn, out newChunk)) { // Debug.Log("New Chunk FOUND!"); // set the cube's location newLocation.x = blockLocation.x; newLocation.y = blockLocation.y; newLocation.z = Universe.planet.chunkSize - 1; return(newChunk); } else { // Debug.Log("New Chunk NOT FOUND!"); return(null); } } // we are not beyond the extremes of the chunk, therefore carry on with the current chunk return(hitChunk); }
public void AssignNeighbour(PlanetChunk chunk, int side) { m_neighbourChunks[side] = chunk; }
public bool BuildPlanet(bool threaded) { int width = worldSize / 8; int chunksPerFace = width * width; int totalNumChunks = 6 * chunksPerFace; //ChunkMeshData[] meshData = new ChunkMeshData[totalNumChunks]; List <BuildChunkJob> chunkJobs = new List <BuildChunkJob>(); NativeArray <JobHandle> chunkJobHandles = new NativeArray <JobHandle>(totalNumChunks, Allocator.TempJob); int tempIndex = 0; if (!threaded) { ////for each face //for (int f = 0; f < 6; f++) //{ // //for each chunk in face // for (int x = 0; x < worldSize / 8; x++) // { // for (int y = 0; y < worldSize / 8; y++) // { // //calc vertex, tris and uvs // meshData[tempIndex] = BuildChunk(f, x, y, false); // tempIndex++; // } // } //} } else { //Parallel.For(0, totalNumChunks, index => //{ // int f = index / chunksPerFace; // int x = (index / width) % width; // int y = index % width; // meshData[index] = BuildChunk(f, x, y, false); //}); for (int i = 0; i < totalNumChunks; i++) { int f = i / chunksPerFace; int x = (i / width) % width; int y = i % width; chunkJobs.Add(new BuildChunkJob() { chunkHeightmap = GetChunkHeightmap(f, x, y), chunkBasePositions = GetChunkBasePositions(f, x, y), faceIndex = f, chunkX = x, chunkY = y, verts = new NativeArray <Vector3>(8 * 8 * 20, Allocator.TempJob), tris = new NativeArray <int>(8 * 8 * 18 * 3, Allocator.TempJob), uvs = new NativeArray <Vector2>(8 * 8 * 20, Allocator.TempJob) }); JobHandle handle = chunkJobs[i].Schedule(); chunkJobHandles[i] = handle; } JobHandle.CompleteAll(chunkJobHandles); } //for each face tempIndex = 0; for (int f = 0; f < 6; f++) { //for each chunk in face for (int x = 0; x < worldSize / 8; x++) { for (int y = 0; y < worldSize / 8; y++) { PlanetChunk chunk = faces[f].chunks[x, y]; //mesh assignment from vars //assign those to the mesh if (chunk.mesh == null) { chunk.mesh = new Mesh(); } else { chunk.mesh.Clear(); } //chunk.mesh.vertices = meshData[tempIndex].verts; //chunk.mesh.triangles = meshData[tempIndex].tris; //chunk.mesh.uv = meshData[tempIndex].uvs; //chunk.mesh.SetVertices(chunkJobs[tempIndex].verts); var layout = new[] { new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3) }; chunk.mesh.SetVertexBufferParams(chunkJobs[tempIndex].verts.Length, layout); chunk.mesh.SetVertexBufferData(chunkJobs[tempIndex].verts, 0, 0, chunkJobs[tempIndex].verts.Length); chunk.mesh.SetTriangles(chunkJobs[tempIndex].tris.ToArray(), 0); chunk.mesh.SetUVs(0, chunkJobs[tempIndex].uvs); //chunk.mesh.SetNormals(); chunk.mesh.RecalculateNormals(); //create object and assign values to it if (chunk.go == null) { chunk.go = new GameObject(f + "|" + x + "," + y); chunk.go.AddComponent <MeshFilter>().sharedMesh = chunk.mesh; chunk.go.AddComponent <MeshRenderer>().sharedMaterial = Resources.Load("Planet") as Material; chunk.go.transform.SetParent(chunkHolder.transform); } else { chunk.go.GetComponent <MeshFilter>().sharedMesh = chunk.mesh; } chunkJobs[tempIndex].chunkBasePositions.Dispose(); chunkJobs[tempIndex].chunkHeightmap.Dispose(); chunkJobs[tempIndex].tris.Dispose(); chunkJobs[tempIndex].verts.Dispose(); chunkJobs[tempIndex].uvs.Dispose(); tempIndex++; } } } chunkJobHandles.Dispose(); return(true); }
//Deferred mesh method //protected MeshData FaceDataUp // (PlanetChunk planetchunk, int x, int y, int z, MeshData meshData) //{ // meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f)); // meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f)); // meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f)); // meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f)); // // meshData.AddQuadTriangles(); // meshData.AddUVRange(FaceUVs(Direction.up)); // return meshData; //} /// <summary> /// Gets whether or not the blocks next to this one are solid. Used to determine which sides to render. /// </summary> /// <param name="planetchunk">The planetchunk this block is contained by</param> /// <param name="x">The X coordinate of the Block</param> /// <param name="y">The Y coordinate of the Block</param> /// <param name="z">The Z coordinate of the Block</param> /// <param name="meshData">The MeshData the data is being sent to (to render the block).</param> public void GetSolid(PlanetChunk planetchunk, int x, int y, int z, MeshData meshData) { if (planetchunk.GetBlock(x, y + 1, z).IsSolid) { solidsides.up = true; } else { solidsides.up = false; } if (planetchunk.GetBlock(x, y - 1, z).IsSolid) { solidsides.down = true; } else { solidsides.down = false; } if (planetchunk.GetBlock(x, y, z + 1).IsSolid) { solidsides.north = true; } else { solidsides.north = false; } if (planetchunk.GetBlock(x, y, z - 1).IsSolid) { solidsides.south = true; } else { solidsides.south = false; } if (planetchunk.GetBlock(x + 1, y, z).IsSolid) { solidsides.east = true; } else { solidsides.east = false; } if (planetchunk.GetBlock(x - 1, y, z).IsSolid) { solidsides.west = true; } else { solidsides.west = false; } }
/* * Destroy the chunk when we wish to add or remove a cube */ void DestroyChunk(PlanetChunk chunk) { DestroyImmediate(chunk.planetChunk.GetComponent <MeshFilter>()); DestroyImmediate(chunk.planetChunk.GetComponent <MeshRenderer>()); DestroyImmediate(chunk.planetChunk.GetComponent <Collider>()); }