public void InitCellsFast() { this.mapByte = new byte[this.SavedTerrainData.DensityMaps.Count][]; for (int i = 0; i < this.SavedTerrainData.DensityMaps.Count; i++) { this.mapByte[i] = this.SavedTerrainData.DensityMaps[i].mapByte; } this.Cells = new GrassCell[this.SavedTerrainData.Cells.Length]; this.CellContent = new GrassCellContent[this.SavedTerrainData.CellContent.Length]; this.maxBucketDensity = this.SavedTerrainData.maxBucketDensity; int num = 0; int num2 = this.SavedTerrainData.Cells.Length; for (int j = 0; j < num2; j++) { this.Cells[j] = new GrassCell(); this.Cells[j].index = j; this.Cells[j].Center = this.SavedTerrainData.Cells[j].Center; this.Cells[j].CellContentIndexes = this.SavedTerrainData.Cells[j].CellContentIndexes; this.Cells[j].CellContentCount = this.SavedTerrainData.Cells[j].CellContentCount; int cellContentCount = this.Cells[j].CellContentCount; for (int k = 0; k < cellContentCount; k++) { this.CellContent[num] = new GrassCellContent(); GrassCellContent grassCellContent = this.CellContent[num]; GrassCellContent grassCellContent2 = this.SavedTerrainData.CellContent[num]; grassCellContent.index = num; grassCellContent.Layer = grassCellContent2.Layer; if (grassCellContent2.SoftlyMergedLayers.Length > 0) { grassCellContent.SoftlyMergedLayers = grassCellContent2.SoftlyMergedLayers; } else { grassCellContent.SoftlyMergedLayers = null; } grassCellContent.GrassMatrixBufferPID = this.GrassMatrixBufferPID; grassCellContent.Center = grassCellContent2.Center; grassCellContent.Pivot = grassCellContent2.Pivot; grassCellContent.PatchOffsetX = grassCellContent2.PatchOffsetX; grassCellContent.PatchOffsetZ = grassCellContent2.PatchOffsetZ; grassCellContent.Instances = grassCellContent2.Instances; grassCellContent.block = new MaterialPropertyBlock(); grassCellContent.argsBuffer = new ComputeBuffer(1, 20, ComputeBufferType.DrawIndirect); num++; } } }
public void BurstInit() { if (this.Cam == null) { this.Cam = Camera.main; if (this.Cam == null) { return; } } this.CamTransform = this.Cam.transform; if ((this.TerrainPosition - this.CamTransform.position).sqrMagnitude > this.SqrTerrainCullingDist) { return; } int num = this.Cells.Length; for (int i = 0; i < num; i++) { GrassCell grassCell = this.Cells[i]; float num2 = Vector3.Distance(this.CamTransform.position, grassCell.Center); if (num2 < this.BurstRadius) { int cellContentCount = grassCell.CellContentCount; for (int j = 0; j < cellContentCount; j++) { GrassCellContent grassCellContent = this.CellContent[grassCell.CellContentIndexes[j]]; int layer = grassCellContent.Layer; grassCellContent.v_mat = this.v_mat[layer]; grassCellContent.v_mesh = this.v_mesh[layer]; grassCellContent.ShadowCastingMode = this.ShadowCastingMode[layer]; } this.InitCellContent(grassCell.index); this.ThreadIsRunning = false; for (int k = 0; k < cellContentCount; k++) { GrassCellContent grassCellContent2 = this.CellContent[grassCell.CellContentIndexes[k]]; grassCellContent2.InitCellContent_Delegated(); } grassCell.state = 3; } } }
public float InitCellContent(int cellIndex) { GrassCell grassCell = this.Cells[cellIndex]; int cellContentCount = grassCell.CellContentCount; for (int i = 0; i < cellContentCount; i++) { int num = grassCell.CellContentIndexes[i]; GrassCellContent grassCellContent = this.CellContent[num]; int num2 = grassCellContent.Layer; int num3 = 0; this.samplePosition.x = grassCellContent.Pivot.x; this.samplePosition.y = grassCellContent.Pivot.z; this.tempSamplePosition = this.samplePosition; int num4 = (int)this.InstanceRotation[num2]; bool flag = this.WriteNormalBuffer[num2]; float num5 = this.Noise[num2]; float num6 = this.MinSize[num2]; float num7 = this.MaxSize[num2] - num6; int num8 = 1; if (grassCellContent.SoftlyMergedLayers != null) { num8 += grassCellContent.SoftlyMergedLayers.Length; } for (int j = 0; j < num8; j++) { this.tempSamplePosition = this.samplePosition; if (j > 0) { num2 = grassCellContent.SoftlyMergedLayers[j - 1]; num5 = this.Noise[num2]; num6 = this.MinSize[num2]; num7 = this.MaxSize[num2] - num6; } for (int k = 0; k < this.NumberOfBucketsPerCell; k++) { for (int l = 0; l < this.NumberOfBucketsPerCell; l++) { int num9 = cellIndex + num2 + j * 55; GrassManager.ATGSeed = (uint)((float)(num9 - (k * this.NumberOfBucketsPerCell + l)) * 0.0001f * 2.14748365E+09f); Vector2 vector; vector.x = this.tempSamplePosition.x; vector.y = this.tempSamplePosition.y; int num10 = (int)this.mapByte[num2][grassCellContent.PatchOffsetX + grassCellContent.PatchOffsetZ + k * (int)this.TerrainDetailSize.y + l]; num10 = (int)Math.Ceiling((double)((float)num10 * this.CurrentDetailDensity)); float num11 = (vector.x >= this.TerrainSizeOverHeightmap) ? this.OneOverHeightmapWidth : 0f; float num12 = (vector.x < this.OneOverHeightmapWidthRight) ? this.OneOverHeightmapWidth : 0f; float num13 = (vector.y >= this.TerrainSizeOverHeightmap) ? this.OneOverHeightmapHeight : 0f; float num14 = (vector.y < this.OneOverHeightmapHeightUp) ? this.OneOverHeightmapHeight : 0f; for (int m = 0; m < num10; m++) { float atgrandomNext = GrassManager.GetATGRandomNext(); float num15 = atgrandomNext * this.BucketSize; atgrandomNext = GrassManager.GetATGRandomNext(); float num16 = atgrandomNext * this.BucketSize; Vector2 vector2; vector2.x = (vector.x + num15) * this.OneOverTerrainSize.x; vector2.y = (vector.y + num16) * this.OneOverTerrainSize.z; float num17 = vector2.x * (float)(this.TerrainHeightmapWidth - 1); float num18 = vector2.y * (float)(this.TerrainHeightmapHeight - 1); int num19 = (int)num17; int num20 = (int)num18; int num21 = (int)(num17 + 1f); int num22 = (int)(num18 + 1f); float num23 = num17 - (float)num19; float num24 = (float)num21 - num17; float num25 = num18 - (float)num20; float num26 = (float)num22 - num18; num19 *= this.TerrainHeightmapHeight; num21 *= this.TerrainHeightmapHeight; float num27 = this.TerrainHeights[num19 + num20] * num24; num27 += this.TerrainHeights[num21 + num20] * num23; float num28 = this.TerrainHeights[num19 + num22] * num24; num28 += this.TerrainHeights[num21 + num22] * num23; this.tempPosition.y = num27 * num26 + num28 * num25; float num29 = this.GetfilteredHeight((vector2.x - num11) * (float)(this.TerrainHeightmapWidth - 1), vector2.y * (float)(this.TerrainHeightmapHeight - 1)); float num30 = this.GetfilteredHeight((vector2.x + num12) * (float)(this.TerrainHeightmapWidth - 1), vector2.y * (float)(this.TerrainHeightmapHeight - 1)); float num31 = this.GetfilteredHeight(vector2.x * (float)(this.TerrainHeightmapWidth - 1), (vector2.y + num14) * (float)(this.TerrainHeightmapHeight - 1)); float num32 = this.GetfilteredHeight(vector2.x * (float)(this.TerrainHeightmapWidth - 1), (vector2.y - num13) * (float)(this.TerrainHeightmapHeight - 1)); Vector3 vector3; vector3.x = -2f * (num30 - num29); if (num4 != 2 && num4 != 4) { vector3.y = 6.283184f * this.TerrainSizeOverHeightmap; } else { vector3.y = 4f * this.TerrainSizeOverHeightmap; } vector3.z = (num31 - num32) * -2f; float num33 = vector3.x * vector3.x + vector3.y * vector3.y + vector3.z * vector3.z; float num34 = (float)Math.Sqrt((double)num33); float num35 = 1f / num34; vector3.x *= num35; vector3.y *= num35; vector3.z *= num35; this.tempPosition.x = this.tempSamplePosition.x + num15 + this.TerrainPosition.x; this.tempPosition.z = this.tempSamplePosition.y + num16 + this.TerrainPosition.z; float num36 = num6 + Mathf.PerlinNoise(this.tempPosition.x * num5, this.tempPosition.z * num5) * num7; this.tempScale.x = num36; this.tempScale.y = num36; this.tempScale.z = num36; Quaternion zeroQuat = this.ZeroQuat; if (num4 != 2) { zeroQuat.x = vector3.z; zeroQuat.y = 0f; zeroQuat.z = -vector3.x; zeroQuat.w = (float)Math.Sqrt((double)(1f + vector3.y)); float num37 = (float)(1.0 / Math.Sqrt((double)(zeroQuat.w * zeroQuat.w + zeroQuat.x * zeroQuat.x + zeroQuat.y * zeroQuat.y + zeroQuat.z * zeroQuat.z))); zeroQuat.w *= num37; zeroQuat.x *= num37; zeroQuat.y *= num37; zeroQuat.z *= num37; } float num38 = GrassManager.GetATGRandomNext() * 180f; float num39 = (float)Math.Cos((double)num38); float num40 = (float)Math.Sin((double)num38); Quaternion quaternion = zeroQuat; zeroQuat.x = quaternion.x * num39 - quaternion.z * num40; zeroQuat.y = quaternion.w * num40 + quaternion.y * num39; zeroQuat.z = quaternion.z * num39 + quaternion.x * num40; zeroQuat.w = quaternion.w * num39 - quaternion.y * num40; if (num4 == 1) { num38 = GrassManager.GetATGRandomNext() * 180f; float num41 = (float)Math.Sin((double)num38); float num42 = (float)Math.Cos((double)num38); quaternion = zeroQuat; zeroQuat.x = quaternion.w * num41 + quaternion.x * num42; zeroQuat.y = quaternion.y * num42 + quaternion.z * num41; zeroQuat.z = quaternion.z * num42 - quaternion.y * num41; zeroQuat.w = quaternion.w * num42 - quaternion.x * num41; } this.tempMatrix.m03 = this.tempPosition.x; this.tempMatrix.m13 = this.tempPosition.y + this.TerrainPosition.y; this.tempMatrix.m23 = this.tempPosition.z; float num43 = 2f * zeroQuat.x * zeroQuat.x; float num44 = 2f * zeroQuat.y * zeroQuat.y; float num45 = 2f * zeroQuat.z * zeroQuat.z; this.tempMatrix.m00 = 1f - num44 - num45; this.tempMatrix.m01 = 2f * zeroQuat.x * zeroQuat.y - 2f * zeroQuat.z * zeroQuat.w; this.tempMatrix.m02 = 2f * zeroQuat.x * zeroQuat.z + 2f * zeroQuat.y * zeroQuat.w; this.tempMatrix.m10 = 2f * zeroQuat.x * zeroQuat.y + 2f * zeroQuat.z * zeroQuat.w; this.tempMatrix.m11 = 1f - num43 - num45; this.tempMatrix.m12 = 2f * zeroQuat.y * zeroQuat.z - 2f * zeroQuat.x * zeroQuat.w; this.tempMatrix.m20 = 2f * zeroQuat.x * zeroQuat.z - 2f * zeroQuat.y * zeroQuat.w; this.tempMatrix.m21 = 2f * zeroQuat.y * zeroQuat.z + 2f * zeroQuat.x * zeroQuat.w; this.tempMatrix.m22 = 1f - num43 - num44; this.tempMatrix.m00 = this.tempMatrix.m00 * this.tempScale.x; this.tempMatrix.m01 = this.tempMatrix.m01 * this.tempScale.y; this.tempMatrix.m02 = this.tempMatrix.m02 * this.tempScale.z; this.tempMatrix.m10 = this.tempMatrix.m10 * this.tempScale.x; this.tempMatrix.m11 = this.tempMatrix.m11 * this.tempScale.y; this.tempMatrix.m12 = this.tempMatrix.m12 * this.tempScale.z; this.tempMatrix.m20 = this.tempMatrix.m20 * this.tempScale.x; this.tempMatrix.m21 = this.tempMatrix.m21 * this.tempScale.y; this.tempMatrix.m22 = this.tempMatrix.m22 * this.tempScale.z; if (num4 == 2 && flag) { Vector3 vector4 = vector3; float num46 = -num38; num39 = (float)Math.Cos((double)num46); num40 = (float)Math.Sin((double)num46); float num47 = num40 * 2f; float num48 = num40 * num47; float num49 = num39 * num47; vector4.x = (1f - num48) * vector3.x + num49 * vector3.z; vector4.z = -num49 * vector3.x + (1f - num48) * vector3.z; this.tempMatrix.m30 = vector4.x; this.tempMatrix.m31 = vector4.y; this.tempMatrix.m32 = vector4.z; } else { this.tempMatrix.m30 = 0f; this.tempMatrix.m31 = 0f; this.tempMatrix.m32 = 0f; } this.tempMatrix.m33 = (float)j + this.tempScale.x * 0.01f; this.tempMatrixArray[num3] = this.tempMatrix; num3++; } this.tempSamplePosition.y = this.tempSamplePosition.y + this.BucketSize; } this.tempSamplePosition.y = this.samplePosition.y; this.tempSamplePosition.x = this.tempSamplePosition.x + this.BucketSize; } } grassCellContent.v_matrices = new Matrix4x4[num3]; Array.Copy(this.tempMatrixArray, grassCellContent.v_matrices, num3); } grassCell.state = 2; return(1f); }
private void DrawGrass() { if (this.Cam == null) { this.Cam = Camera.main; if (this.Cam == null) { return; } } this.CamTransform = this.Cam.transform; this.cullingGroup.targetCamera = this.Cam; if ((this.TerrainPosition - this.CamTransform.position).sqrMagnitude > this.SqrTerrainCullingDist) { return; } if (this.CameraSelection == GrassCameras.AllCameras) { this.CameraInWichGrassWillBeDrawn = null; } else { this.CameraInWichGrassWillBeDrawn = this.Cam; } this.cullingGroup.SetDistanceReferencePoint(this.CamTransform.position); if (this.IngnoreOcclusion) { this.numResults = this.cullingGroup.QueryIndices(0, this.resultIndices, 0); } else { this.numResults = this.cullingGroup.QueryIndices(true, 0, this.resultIndices, 0); } if (this.numResults == this.TotalCellCount) { return; } this.numOfVisibleCells = 0; for (int i = 0; i < this.numResults; i++) { if (!this.IngnoreOcclusion || this.isVisibleBoundingSpheres[this.resultIndices[i]]) { this.numOfVisibleCells++; GrassCell grassCell = this.Cells[this.resultIndices[i]]; int state = grassCell.state; int cellContentCount = grassCell.CellContentCount; switch (state) { case 0: if (grassCell.CellContentCount > 0) { grassCell.state = 1; this.CellsOrCellContentsToInit.Add(grassCell.index); } break; case 2: for (int j = 0; j < cellContentCount; j++) { this.CellContent[grassCell.CellContentIndexes[j]].InitCellContent_Delegated(); } grassCell.state = 3; for (int k = 0; k < cellContentCount; k++) { this.CellContent[grassCell.CellContentIndexes[k]].DrawCellContent_Delegated(this.CameraInWichGrassWillBeDrawn, this.CameraLayer); } break; case 3: for (int l = 0; l < cellContentCount; l++) { this.CellContent[grassCell.CellContentIndexes[l]].DrawCellContent_Delegated(this.CameraInWichGrassWillBeDrawn, this.CameraLayer); } break; } } } int num = this.CellsOrCellContentsToInit.Count; if (!this.ThreadIsRunning) { this.wt_cellindexList.Clear(); this.wt_cellindexListCount = 0; for (int m = 0; m < this.CellsPerFrame; m++) { if (num <= 0) { break; } if (this.Cells[this.CellsOrCellContentsToInit[0]].state != 1) { this.CellsOrCellContentsToInit.RemoveAt(0); if (num == 1) { return; } } GrassCell grassCell = this.Cells[this.CellsOrCellContentsToInit[0]]; int cellContentCount = grassCell.CellContentCount; for (int n = 0; n < cellContentCount; n++) { GrassCellContent grassCellContent = this.CellContent[grassCell.CellContentIndexes[n]]; int layer = grassCellContent.Layer; grassCellContent.v_mat = this.v_mat[layer]; grassCellContent.v_mesh = this.v_mesh[layer]; grassCellContent.ShadowCastingMode = this.ShadowCastingMode[layer]; } this.wt_cellindex = this.CellsOrCellContentsToInit[0]; this.wt_cellindexList.Add(this.CellsOrCellContentsToInit[0]); this.CellsOrCellContentsToInit.RemoveAt(0); num--; this.wt_cellindexListCount++; } if (this.wt_cellindexListCount > 0) { this.WorkerThreadWait.Set(); } } }
public void InitCells() { this.NumberOfLayers = this.terData.detailPrototypes.Length; this.OrigNumberOfLayers = this.NumberOfLayers; int[][] array = new int[this.OrigNumberOfLayers][]; int[][] array2 = new int[this.OrigNumberOfLayers][]; for (int i = 0; i < this.OrigNumberOfLayers; i++) { int num = this.LayerToMergeWith[i]; int num2 = num - 1; if (num != 0 && num != i + 1 && this.LayerToMergeWith[num2] == 0) { if (array[num2] == null) { array[num2] = new int[this.OrigNumberOfLayers - 1]; } if (this.DoSoftMerge[i] && array2[num2] == null) { array2[num2] = new int[this.OrigNumberOfLayers - 1]; } for (int j = 0; j < this.OrigNumberOfLayers - 1; j++) { if (array[num2][j] == 0) { array[num2][j] = i + 1; break; } } if (this.DoSoftMerge[i]) { for (int k = 0; k < this.OrigNumberOfLayers - 1; k++) { if (array2[num2][k] == 0) { array2[num2][k] = i + 1; break; } } } } } this.Cells = new GrassCell[this.NumberOfCells * this.NumberOfCells]; List <GrassCellContent> list = new List <GrassCellContent>(); list.Capacity = this.NumberOfCells * this.NumberOfCells * this.NumberOfLayers; this.mapByte = new byte[this.NumberOfLayers][]; for (int l = 0; l < this.NumberOfLayers; l++) { if (this.LayerToMergeWith[l] == 0 || this.DoSoftMerge[l]) { this.mapByte[l] = new byte[(int)(this.TerrainDetailSize.x * this.TerrainDetailSize.y)]; bool flag = false; if (array[l] != null && !this.DoSoftMerge[l]) { flag = true; } for (int m = 0; m < (int)this.TerrainDetailSize.x; m++) { for (int n = 0; n < (int)this.TerrainDetailSize.y; n++) { int[,] detailLayer = this.terData.GetDetailLayer(m, n, 1, 1, l); this.mapByte[l][m * (int)this.TerrainDetailSize.y + n] = Convert.ToByte(detailLayer[0, 0]); if (flag) { for (int num3 = 0; num3 < this.OrigNumberOfLayers - 1; num3++) { if (array[l][num3] == 0) { break; } detailLayer = this.terData.GetDetailLayer(m, n, 1, 1, array[l][num3] - 1); this.mapByte[l][m * (int)this.TerrainDetailSize.y + n] = Convert.ToByte((int)this.mapByte[l][m * (int)this.TerrainDetailSize.y + n] + detailLayer[0, 0]); } } } } } } int num4 = 0; int num5 = 0; int num6 = 0; for (int num7 = 0; num7 < this.NumberOfCells; num7++) { for (int num8 = 0; num8 < this.NumberOfCells; num8++) { int num9 = num7 * this.NumberOfCells + num8; Vector2 vector; vector.x = ((float)num7 * this.CellSize + 0.5f * this.CellSize) * this.OneOverTerrainSize.x; vector.y = ((float)num8 * this.CellSize + 0.5f * this.CellSize) * this.OneOverTerrainSize.z; float interpolatedHeight = this.terData.GetInterpolatedHeight(vector.x, vector.y); this.Cells[num9] = new GrassCell(); this.Cells[num9].index = num9; Vector3 center; center.x = (float)num7 * this.CellSize + 0.5f * this.CellSize + this.TerrainPosition.x; center.y = interpolatedHeight + this.TerrainPosition.y; center.z = (float)num8 * this.CellSize + 0.5f * this.CellSize + this.TerrainPosition.z; this.Cells[num9].Center = center; for (int num10 = 0; num10 < this.NumberOfLayers; num10++) { if (this.LayerToMergeWith[num10] == 0) { for (int num11 = 0; num11 < this.NumberOfBucketsPerCell; num11++) { for (int num12 = 0; num12 < this.NumberOfBucketsPerCell; num12++) { int num13 = (int)this.mapByte[num10][num7 * this.NumberOfBucketsPerCell * (int)this.TerrainDetailSize.y + num11 * (int)this.TerrainDetailSize.y + num8 * this.NumberOfBucketsPerCell + num12]; if (num13 > this.maxBucketDensity) { this.maxBucketDensity = num13; } num4 += num13; } } int num14 = 0; if (array2[num10] != null) { for (int num15 = 0; num15 < this.OrigNumberOfLayers - 1; num15++) { if (array2[num10][num15] == 0) { break; } int num16 = array2[num10][num15] - 1; int num17 = 0; for (int num18 = 0; num18 < this.NumberOfBucketsPerCell; num18++) { for (int num19 = 0; num19 < this.NumberOfBucketsPerCell; num19++) { int num13 = (int)this.mapByte[num16][num7 * this.NumberOfBucketsPerCell * (int)this.TerrainDetailSize.y + num18 * (int)this.TerrainDetailSize.y + num8 * this.NumberOfBucketsPerCell + num19]; num17 += num13; } } if (num17 > 0) { num14++; num4 += num17; } } if (num14 * 16 > this.maxBucketDensity) { this.maxBucketDensity = num14 * 16 + 32; } } if (num4 > 0) { this.Cells[num9].CellContentIndexes.Add(num5); this.Cells[num9].CellContentCount++; GrassCellContent grassCellContent = new GrassCellContent(); grassCellContent.index = num5; grassCellContent.Layer = num10; grassCellContent.GrassMatrixBufferPID = this.GrassMatrixBufferPID; grassCellContent.Center = center; grassCellContent.Pivot = new Vector3((float)num7 * this.CellSize, interpolatedHeight, (float)num8 * this.CellSize); grassCellContent.PatchOffsetX = num7 * this.NumberOfBucketsPerCell * (int)this.TerrainDetailSize.y; grassCellContent.PatchOffsetZ = num8 * this.NumberOfBucketsPerCell; grassCellContent.Instances = num4; grassCellContent.block = new MaterialPropertyBlock(); grassCellContent.argsBuffer = new ComputeBuffer(1, 20, ComputeBufferType.DrawIndirect); if (num14 > 0) { List <int> list2 = new List <int>(); for (int num20 = 0; num20 < this.OrigNumberOfLayers - 1; num20++) { if (array2[num10][num20] != 0) { list2.Add(array2[num10][num20] - 1); } } grassCellContent.SoftlyMergedLayers = list2.ToArray(); } list.Add(grassCellContent); num5++; } num4 = 0; } } } num6 += (int)this.TerrainDetailSize.x; } this.CellContent = list.ToArray(); list.Clear(); }