IEnumerator ProcessQueue() { while (true) { if (!Switch()) { yield return(null); } if (queue.Count > 0) { int index = Dequeue(); GrassDataChunk chunk = new GrassDataChunk(); chunk.center = data.chunkPos [index]; int steps = CalSteps(index); yield return(Builder.GenChunk(chunk, steps, data.texSpawnMap)); chunks [index] = chunk; chunkState [index] = ChunkState.done; } else { yield return(null); } } }
int CalLod(GrassDataChunk chunk) { float dis = Vector3.Distance(chunk.center, playerPos); for (int i = 0; i < LODCount; i++) { if (dis < settings.lodDis [i]) { return(i); } } return(LODCount - 1); }
bool Sort_Need(GrassDataChunk chunk) { if (chunk.sortState == SortState.idle) { chunk.lastDir = playerPos - chunk.center; return(true); } else if (chunk.sortState == SortState.inQueue) { return(false); } else if (chunk.sortState == SortState.done) { Vector3 dir = playerPos - chunk.center; if (Vector3.Angle(dir, chunk.lastDir) > 30) { chunk.lastDir = dir; return(true); } } return(false); }
public IEnumerator GenChunk(GrassDataChunk chunk, int steps, Texture2D spawnMapTex) { if (initColors == false) { initColors = true; colors = spawnMapTex.GetPixels(); } var spawnMapTexWidth = spawnMapTex.width; var spawnMapTexHeight = spawnMapTex.height; var data = holder.data; float channel = 0; var centerVector = chunk.center; Vector2 texUV; Vector2 lmUV; Matrix4x4[] mas = new Matrix4x4[settings.ChunkSize]; Vector4[] uv = new Vector4[settings.ChunkSize]; Vector3[] pos = new Vector3[settings.ChunkSize]; int index = 0; for (int i = 0; i < settings.ChunkSize; i++) { if (steps >= 0 && (i % steps == 0)) { yield return(null); } mOrigon.x = Random.Range(centerVector.x - mHalfLength, centerVector.x + mHalfLength); mOrigon.z = Random.Range(centerVector.z - mHalfLength, centerVector.z + mHalfLength); mRay.origin = mOrigon; mPos = new Vector3(mOrigon.x, 0, mOrigon.z); /////////////////////////// /// Get Color /////////////////////////// float uvx = (mPos.x - (settings.MapCenter.x - settings.MapSize)) / (settings.MapSize * 2); float uvy = (mPos.z - (settings.MapCenter.z - settings.MapSize)) / (settings.MapSize * 2); uvx = Mathf.Clamp(uvx, 0f, 1f); uvy = Mathf.Clamp(uvy, 0f, 1f); int idx = (int)(uvx * spawnMapTexWidth); int idy = (int)(uvy * spawnMapTexHeight); idx = Mathf.Clamp(idx, 0, spawnMapTexWidth); idy = Mathf.Clamp(idy, 0, spawnMapTexHeight); //lsy Todo dont use GetPixel var c = spawnMapTex.GetPixelBilinear(uvx, uvy); //var cid = XYtoIndex(spawnMapTexWidth,spawnMapTexHeight,idx,idy); //var c = colors[cid]; /////////////////////////// /////////////////////////// /////////////////////////// channel = c.r; var height = data.heightMin + c.g * (data.heightMax - data.heightMin); //1 Pos from map mPos = new Vector3(mPos.x, height - 0.03f, mPos.z); //2 Pos from raycast // if (Physics.Raycast (mRay, out mHitInfo, 2000, 1 << 28 | 1 << 17)) { // mPos = mHitInfo.point; // } if (channel > 0 && CanPlace(channel)) { float scaleModi = Mathf.Clamp01(channel); float scaleModi2 = scaleModi * scaleModi; mPos += yDelta * scaleModi; mScale = Random.Range(0.9f, 1.2f) * settings.prefabsLOD [0].transform.localScale; //1 For map //Rotation by terrain up vector var x = c.b * 2 - 1; var z = c.a * 2 - 1; //var up = new Vector3 (x, Mathf.Sqrt( 1-x*x-z*z),z); var up = new Vector3(x, 1, z); mRot = Quaternion.Euler(0, Random.Range(0, 360), 0); //Debug.Log (string.Format("{0} {1} {2}",up.x,up.y,up.z)); mRot = Quaternion.FromToRotation(Vector3.up, up) * mRot; //2 For raycast //mRot = Quaternion.Euler (0, Random.Range (0, 360), 0); transMatrix = Matrix4x4.TRS(mPos, mRot, mScale); //int lmIndex = mHitInfo.collider.GetComponentInChildren<MeshRenderer> ().lightmapIndex; //int lmIndex = data.GetLMIndex(mPos); //CreateMaterial (lmIndex); // if (!chunk.dataLMIndex.Contains (lmIndex)) { // chunk.dataLMIndex.Add (lmIndex); // chunk.data.Add (new GrassDataChunkDataPerLM ()); // } // int id = FindIndex(chunk.dataLMIndex,lmIndex); // if (id >= mas.Count) { // mas.Add (new List<Matrix4x4> ()); // uv.Add (new List<Vector4> ()); // pos.Add (new List<Vector3> ()); // } // mas [id].Add (transMatrix); // pos [id].Add (mPos); // //uv [id].Add (new Vector4 (lmUV.x, lmUV.y, channel, 0)); // uv [id].Add (new Vector4 (0, 0, channel, 0)); mas[index] = transMatrix; pos[index] = mPos; //uv [id].Add (new Vector4 (lmUV.x, lmUV.y, channel, 0)); uv[index] = new Vector4(0, 0, channel, 0); ++index; } } ///////// var item = chunk.data; int count = index; item.count = count; item.mas = mas; item.uv = uv; item.pos = pos; item.masB = new Matrix4x4[count]; item.uvB = new Vector4[count]; item.posB = new Vector3[count]; item.dis = new int[count]; }
void Sort_AddToQueue(GrassDataChunk chunk) { chunk.sortState = SortState.inQueue; sortQueue.Enqueue(chunk); }