void MobileControls() { if (clickState != ClickState.NONE || clickState != ClickState.UP) { Touch touch = Input.GetTouch(0); Ray ray = cam.ScreenPointToRay(touch.position); scale = Vector3.one * ((radiusIndex + 0.5f) * voxelSize * 2f); RaycastHit singleRayHitInfo; if (Physics.Raycast(ray, out singleRayHitInfo)) { Vector3 singleRayCenter = transform.InverseTransformPoint(singleRayHitInfo.point); gizmoCenter = singleRayCenter; Vector3 center = singleRayCenter; // Debug.DrawRay(ray.origin, ray.direction * 20, Color.red); center.x += halfSize; center.y += halfSize; if (clickState == ClickState.HOLD) { CylinderCast(touch.position); VoxelStencil activeStencil = stencils[stencilIndex]; activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize); activeStencil.SetCenter(center.x, center.y); activeLayer.EditVoxels(center, activeStencil); } } } }
private void Update() { Transform visualization = stencilVisualizations[stencilIndex]; RaycastHit hitInfo; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo) && hitInfo.collider.gameObject == gameObject) { //used for stencil visualization calculations Vector2 center = transform.InverseTransformPoint(hitInfo.point); center.x += halfSize; center.y += halfSize; //---------------- if (Input.GetMouseButton(0)) { VoxelStencil activeStencil = stencils[stencilIndex]; activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize); activeStencil.SetCenter(center.x, center.y); EditVoxels(center, activeStencil); } //used for stencil visualization calculations center.x -= halfSize; center.y -= halfSize; visualization.localPosition = center; Vector3 tempScale = Vector3.one * ((radiusIndex + 0.5f) * voxelSize * 2f); visualization.localScale = new Vector3(tempScale.x, 0.01f, tempScale.z); //---------------- visualization.gameObject.SetActive(true); } else { visualization.gameObject.SetActive(false); } }
private void EditVoxels(Vector2 center) { VoxelStencil activeStencil = stencils[stencilIndex]; activeStencil.Initialize( fillTypeIndex, (radiusIndex + 0.5f) * voxelSize); activeStencil.SetCenter(center.x, center.y); EditVoxels(center, activeStencil); }
private void EditVoxels(Vector2 center) { VoxelStencil activeStencil = stencils[stencilIndex]; activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize); activeStencil.SetCenter(center.x, center.y); int xStart = (int)((activeStencil.XStart - voxelSize) / chunkSize); if (xStart < 0) { xStart = 0; } int xEnd = (int)((activeStencil.XEnd + voxelSize) / chunkSize); if (xEnd >= chunkResolution) { xEnd = chunkResolution - 1; } int yStart = (int)((activeStencil.YStart - voxelSize) / chunkSize); if (yStart < 0) { yStart = 0; } int yEnd = (int)((activeStencil.YEnd + voxelSize) / chunkSize); if (yEnd >= chunkResolution) { yEnd = chunkResolution - 1; } for (int y = yEnd; y >= yStart; y--) { int i = y * chunkResolution + xEnd; for (int x = xEnd; x >= xStart; x--, i--) { activeStencil.SetCenter(center.x - x * chunkSize, center.y - y * chunkSize); chunks[i].Apply(activeStencil); } } }
void CutStart() { Vector2 center = new Vector2(chunkResolutionX * chunkSize / 2, chunkResolutionY * chunkSize); VoxelStencil activeStencil = stencils[1]; activeStencil.Initialize(false, (4 + 0.5f) * voxelSize); activeStencil.SetCenter(center.x, center.y); EditVoxels(center, activeStencil); }
private void EditVoxels(Vector3 point) { int centerX = (int)((point.x + halfSize) / voxelSize); int centerY = (int)((point.y + halfSize) / voxelSize); int xStart = (centerX - radiusIndex - 1) / voxelResolution; if (xStart < 0) { xStart = 0; } int xEnd = (centerX + radiusIndex) / voxelResolution; if (xEnd >= chunkResolution) { xEnd = chunkResolution - 1; } int yStart = (centerY - radiusIndex - 1) / voxelResolution; if (yStart < 0) { yStart = 0; } int yEnd = (centerY + radiusIndex) / voxelResolution; if (yEnd >= chunkResolution) { yEnd = chunkResolution - 1; } VoxelStencil activeStencil = stencils[stencilIndex]; activeStencil.Initialize(fillTypeIndex == 0, radiusIndex); int voxelYOffset = yEnd * voxelResolution; for (int y = yEnd; y >= yStart; y--) { int i = y * chunkResolution + xEnd; int voxelXOffset = xEnd * voxelResolution; for (int x = xEnd; x >= xStart; x--, i--) { activeStencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset); chunks[i].Apply(activeStencil); voxelXOffset -= voxelResolution; } voxelYOffset -= voxelResolution; } }
void PCcontrolsCylinder() { Transform visualization = stencilVisualizations[stencilIndex]; Ray ray = cam.ScreenPointToRay(Input.mousePosition); scale = Vector3.one * 2;// * ((radiusIndex + 0.5f) * voxelSize * 2f); RaycastHit singleRayHitInfo; if (Physics.Raycast(ray, out singleRayHitInfo)) { Vector3 singleRayCenter = transform.InverseTransformPoint(singleRayHitInfo.point); gizmoCenter = singleRayCenter; Vector3 center = singleRayCenter; Debug.DrawRay(ray.origin, ray.direction * 20, Color.red); #region used for stencil visualization calculations center.x += halfSize; center.y += halfSize; #endregion if (clickState == ClickState.HOLD) { CylinderCast(Input.mousePosition); VoxelStencil activeStencil = stencils[stencilIndex]; activeStencil.Initialize(fillTypeIndex == 0, 1); activeStencil.SetCenter(center.x, center.y); activeLayer.EditVoxels(center, activeStencil); } #region used for stencil visualization calculations center.x -= halfSize; center.y -= halfSize; center.z -= 0.30f; visualization.localPosition = center; visualization.localScale = new Vector3(scale.x, 0.05f, scale.z); #endregion visualization.gameObject.SetActive(true); } else { visualization.gameObject.SetActive(false); //Debug.Log(hitInfo.collider.gameObject.name); } }
private void EditVoxels(Vector3 point) { int centerX = (int)((point.x + _halfSize) / _voxelSize); int centerY = (int)((point.y + _halfSize) / _voxelSize); int xStart = (centerX - _radiusIndex) / VoxelResolution; if (xStart < 0) { xStart = 0; } int xEnd = (centerX + _radiusIndex) / VoxelResolution; if (xEnd >= ChunkResolution) { xEnd = ChunkResolution - 1; } int yStart = (centerY - _radiusIndex) / VoxelResolution; if (yStart < 0) { yStart = 0; } int yEnd = (centerY + _radiusIndex) / VoxelResolution; if (yEnd >= ChunkResolution) { yEnd = ChunkResolution - 1; } int chunkX = centerX / VoxelResolution; int chunkY = centerY / VoxelResolution; centerX -= chunkX * VoxelResolution; centerY -= chunkY * VoxelResolution; VoxelStencil activeStencil = new VoxelStencil(); activeStencil.Initialize(_fillTypeIndex == 0, _radiusIndex); activeStencil.SetCenter(centerX,centerY); _chunks[chunkY * ChunkResolution + chunkX].Apply(activeStencil); int voxelYOffset = yStart * VoxelResolution; for (int y = yStart; y <= yEnd; y++) { int i = y * ChunkResolution + xStart; int voxelXOffset = xStart * VoxelResolution; for (int x = xStart; x <= xEnd; x++, i++) { activeStencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset); _chunks[i].Apply(activeStencil); voxelXOffset += VoxelResolution; } voxelYOffset += VoxelResolution; } Debug.Log(centerX + ", " + centerY + " in chunk " + chunkX + ", " + chunkY); }
private void PaintVoxels(Vector3 point, VoxelStencil stencil) { int centerX = (int)((point.x + voxelMap.halfSize) / voxelMap.voxelSize); int centerY = (int)((point.y + voxelMap.halfSize) / voxelMap.voxelSize); Debug.Log(centerX + " - " + centerY + " Chunk(" + ")"); stencil.Initialize(fillTypeIndex == 0, radiusIndex); int xStart = (centerX - radiusIndex - 1) / voxelMap.gridResolution; if (xStart < 0) { xStart = 0; } int xEnd = (centerX + radiusIndex) / voxelMap.gridResolution; if (xEnd >= voxelMap.resolution) { xEnd = voxelMap.resolution - 1; } int yStart = (centerY - radiusIndex - 1) / voxelMap.gridResolution; if (yStart < 0) { yStart = 0; } int yEnd = (centerY + radiusIndex) / voxelMap.gridResolution; if (yEnd >= voxelMap.resolution) { yEnd = voxelMap.resolution - 1; } int voxelYOffset = yEnd * voxelMap.gridResolution; for (int y = yEnd; y >= yStart; y--) { int i = y * voxelMap.resolution + xEnd; int voxelXOffset = xEnd * voxelMap.gridResolution; for (int x = xEnd; x >= xStart; x--, i--) { stencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset); ApplyChunkStencil(voxelMap.chunks[i], stencil); voxelXOffset -= voxelMap.gridResolution; } voxelYOffset -= voxelMap.gridResolution; } }
private void EditVoxels(Vector2 center, VoxelStencil activeStencil) { int xStart = (int)((activeStencil.XStart - voxelSize) / chunkSize); if (xStart < 0) { xStart = 0; } int xEnd = (int)((activeStencil.XEnd + voxelSize) / chunkSize); if (xEnd >= chunkResolution) { xEnd = chunkResolution - 1; } int yStart = (int)((activeStencil.YStart - voxelSize) / chunkSize); if (yStart < 0) { yStart = 0; } int yEnd = (int)((activeStencil.YEnd + voxelSize) / chunkSize); if (yEnd >= chunkResolution) { yEnd = chunkResolution - 1; } for (int y = yEnd; y >= yStart; y--) { int i = y * chunkResolution + xEnd; for (int x = xEnd; x >= xStart; x--, i--) { activeStencil.SetCenter( center.x - x * chunkSize, center.y - y * chunkSize); chunks[i].Apply(activeStencil); } } }
public void EditVoxels(Vector2 center, VoxelStencil stencilToUse) { int xStart = (int)((stencilToUse.XStart - voxelSize) / chunkSize); if (xStart < 0) { xStart = 0; } int xEnd = (int)((stencilToUse.XEnd + voxelSize) / chunkSize); if (xEnd >= chunkResolutionX) { xEnd = chunkResolutionX - 1; } int yStart = (int)((stencilToUse.YStart - voxelSize) / chunkSize); if (yStart < 0) { yStart = 0; } int yEnd = (int)((stencilToUse.YEnd + voxelSize) / chunkSize); if (yEnd >= chunkResolutionY) { yEnd = chunkResolutionY - 1; } for (int y = yEnd; y >= yStart; y--) { int i = y * chunkResolutionX + xEnd; for (int x = xEnd; x >= xStart; x--, i--) { stencilToUse.SetCenter(center.x - x * chunkSize, center.y - y * chunkSize); chunks[i].Apply(stencilToUse, ref activeVoxelsCount); } } }
private void EditVoxels(Vector3 point) { chunkPos = new Vector3(Mathf.Floor(point.x / voxelResolution), Mathf.Floor(point.y / voxelResolution)); diff = new Vector2Int((int)Mathf.Abs(point.x - (chunkPos * voxelResolution).x), (int)Mathf.Abs(point.y - (chunkPos * voxelResolution).y)); xStart = (diff.x - radiusIndex - 1) / voxelResolution; if (xStart <= -chunkResolution) { xStart = -chunkResolution + 1; } xEnd = (diff.x + radiusIndex) / voxelResolution; if (xEnd >= chunkResolution) { xEnd = chunkResolution - 1; } yStart = (diff.y - radiusIndex - 1) / voxelResolution; if (yStart <= -chunkResolution) { yStart = -chunkResolution + 1; } yEnd = (diff.y + radiusIndex) / voxelResolution; if (yEnd >= chunkResolution) { yEnd = chunkResolution - 1; } activeStencil = stencils[stencilIndex]; activeStencil.Initialize(fillTypeIndex, radiusIndex); var voxelYOffset = yEnd * voxelResolution; Vector2Int checkChunk; updateChunkPositions.Clear(); var result = false; for (var y = yStart - 1; y < yEnd + 1; y++) { var voxelXOffset = xEnd * voxelResolution; for (var x = xStart - 1; x < xEnd + 1; x++) { activeStencil.SetCenter(diff.x - voxelXOffset, diff.y - voxelYOffset); checkChunk = new Vector2Int((int)Mathf.Floor((point.x + voxelXOffset) / voxelResolution), (int)Mathf.Floor((point.y + voxelYOffset) / voxelResolution)); if (existingChunks.ContainsKey(checkChunk)) { var currentChunk = existingChunks[checkChunk]; var tempRes = currentChunk.Apply(activeStencil); if (!result && tempRes) { result = true; } } voxelXOffset -= voxelResolution; } voxelYOffset -= voxelResolution; } if (result) { checkChunk = new Vector2Int((int)Mathf.Floor((point.x) / voxelResolution), (int)Mathf.Floor((point.y) / voxelResolution)); EditChunkAndNeighbors(checkChunk, new Vector3(Mathf.Floor(point.x), Mathf.Floor(point.y))); updateChunkPositions.Sort(SortByPosition); foreach (var chunk in from pos in updateChunkPositions where existingChunks.ContainsKey(pos) select existingChunks[pos]) { voxelMesh.TriangulateChunkMesh(chunk); chunkCollider.Generate2DCollider(chunk, chunkResolution); } } }
private void EditVoxels(Vector3 point, bool isBreaking) { var chunkPos = new Vector3(Mathf.Floor(point.x / voxelMap.voxelResolution), Mathf.Floor(point.y / voxelMap.voxelResolution)); var diff = new Vector2Int((int)Mathf.Abs(point.x - (chunkPos * voxelMap.voxelResolution).x), (int)Mathf.Abs(point.y - (chunkPos * voxelMap.voxelResolution).y)); var xStart = (diff.x - radiusIndex - 1) / voxelMap.voxelResolution; if (xStart <= -voxelMap.chunkResolution) { xStart = -voxelMap.chunkResolution + 1; } var xEnd = (diff.x + radiusIndex) / voxelMap.voxelResolution; if (xEnd >= voxelMap.chunkResolution) { xEnd = voxelMap.chunkResolution - 1; } var yStart = (diff.y - radiusIndex - 1) / voxelMap.voxelResolution; if (yStart <= -voxelMap.chunkResolution) { yStart = -voxelMap.chunkResolution + 1; } var yEnd = (diff.y + radiusIndex) / voxelMap.voxelResolution; if (yEnd >= voxelMap.chunkResolution) { yEnd = voxelMap.chunkResolution - 1; } SetupStencil(isBreaking); var voxelYOffset = yEnd * voxelMap.voxelResolution; Vector2Int checkChunkPos; updateChunkPositions.Clear(); var isEditing = false; if ((isBreaking && activeStencil.fillType == 0) || (!isBreaking && activeStencil.fillType != 0)) { for (var y = yStart - 1; y < yEnd + 1; y++) { var voxelXOffset = xEnd * voxelMap.voxelResolution; for (var x = xStart - 1; x < xEnd + 1; x++) { activeStencil.SetCenter(diff.x - voxelXOffset, diff.y - voxelYOffset); checkChunkPos = new Vector2Int((int)Mathf.Floor((point.x + voxelXOffset) / voxelMap.voxelResolution), (int)Mathf.Floor((point.y + voxelYOffset) / voxelMap.voxelResolution)); if (voxelMap.existingChunks.ContainsKey(checkChunkPos)) { var currentChunk = voxelMap.existingChunks[checkChunkPos]; var tempRes = currentChunk.Apply(activeStencil); if (!isEditing && tempRes) { isEditing = true; } } voxelXOffset -= voxelMap.voxelResolution; } voxelYOffset -= voxelMap.voxelResolution; } } if (isEditing) { checkChunkPos = new Vector2Int((int)Mathf.Floor((point.x) / voxelMap.voxelResolution), (int)Mathf.Floor((point.y) / voxelMap.voxelResolution)); EditChunkAndNeighbors(checkChunkPos, new Vector3(Mathf.Floor(point.x), Mathf.Floor(point.y))); updateChunkPositions.Sort(SortByPosition); //TODO: update so it only checks the relevant one // foreach (var item in updateChunkPositions) { // Debug.Log(item); // } foreach (var chunk in from pos in updateChunkPositions where voxelMap.existingChunks.ContainsKey(pos) select voxelMap.existingChunks[pos]) { voxelMesh.TriangulateChunkMesh(chunk); chunkCollider.Generate2DCollider(chunk, voxelMap.chunkResolution); } } }