| | 1 | | using System; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using DCL.Camera; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class VoxelController : MonoBehaviour |
| | 11 | | { |
| | 12 | | [Header("References")] |
| | 13 | | public VoxelPrefab voxelPrefab; |
| | 14 | |
|
| | 15 | | public BuilderInWorldController buildModeController; |
| | 16 | | public BIWOutlinerController outlinerController; |
| | 17 | | public BuilderInWorldEntityHandler builderInWorldEntityHandler; |
| | 18 | | public FreeCameraMovement freeCameraMovement; |
| | 19 | | public ActionController actionController; |
| | 20 | |
|
| | 21 | | public LayerMask groundLayer; |
| | 22 | |
|
| | 23 | | DCLBuilderInWorldEntity lastVoxelCreated; |
| | 24 | |
|
| | 25 | | GameObject editionGO; |
| | 26 | | bool mousePressed = false, isVoxelModelActivated = false, isCreatingMultipleVoxels = false; |
| | 27 | | Vector3Int lastVoxelPositionPressed; |
| | 28 | | Vector3 lastMousePosition; |
| 198 | 29 | | Dictionary<Vector3Int, VoxelPrefab> createdVoxels = new Dictionary<Vector3Int, VoxelPrefab>(); |
| | 30 | | ParcelScene currentScene; |
| | 31 | |
|
| | 32 | | const float RAYCAST_MAX_DISTANCE = 10000f; |
| | 33 | | const float VOXEL_BOUND_ERROR = 0.05f; |
| | 34 | |
|
| | 35 | | private void Start() |
| | 36 | | { |
| 0 | 37 | | BuilderInWorldInputWrapper.OnMouseDown += MouseDown; |
| 0 | 38 | | BuilderInWorldInputWrapper.OnMouseUp += MouseUp; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | private void OnDestroy() |
| | 42 | | { |
| 0 | 43 | | BuilderInWorldInputWrapper.OnMouseDown -= MouseDown; |
| 0 | 44 | | BuilderInWorldInputWrapper.OnMouseUp -= MouseUp; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | private void Update() |
| | 48 | | { |
| 0 | 49 | | if (!mousePressed || !isVoxelModelActivated || !isCreatingMultipleVoxels) |
| 0 | 50 | | return; |
| | 51 | |
|
| 0 | 52 | | Vector3Int currentPosition = Vector3Int.zero; |
| 0 | 53 | | VoxelEntityHit voxelHit = buildModeController.GetCloserUnselectedVoxelEntityOnPointer(); |
| | 54 | |
|
| 0 | 55 | | if (voxelHit != null && voxelHit.entityHitted.tag == BuilderInWorldSettings.VOXEL_TAG && !voxelHit.entityHitted. |
| | 56 | | { |
| 0 | 57 | | Vector3Int position = ConverPositionToVoxelPosition(voxelHit.entityHitted.rootEntity.gameObject.transform.po |
| 0 | 58 | | position += voxelHit.hitVector; |
| | 59 | |
|
| 0 | 60 | | currentPosition = position; |
| 0 | 61 | | FillVoxels(lastVoxelPositionPressed, currentPosition); |
| 0 | 62 | | } |
| | 63 | | else |
| | 64 | | { |
| | 65 | | RaycastHit hit; |
| 0 | 66 | | UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); |
| | 67 | |
|
| 0 | 68 | | if (Physics.Raycast(ray, out hit, RAYCAST_MAX_DISTANCE, groundLayer)) |
| | 69 | | { |
| 0 | 70 | | currentPosition = ConverPositionToVoxelPosition(hit.point); |
| 0 | 71 | | FillVoxels(lastVoxelPositionPressed, currentPosition); |
| | 72 | | } |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| 0 | 76 | | public void SetSceneToEdit(ParcelScene scene) { currentScene = scene; } |
| | 77 | |
|
| | 78 | | public void SetEditObjectLikeVoxel() |
| | 79 | | { |
| 0 | 80 | | if (mousePressed || !isVoxelModelActivated) |
| 0 | 81 | | return; |
| | 82 | |
|
| 0 | 83 | | VoxelEntityHit voxelHit = buildModeController.GetCloserUnselectedVoxelEntityOnPointer(); |
| | 84 | |
|
| 0 | 85 | | if (voxelHit != null && voxelHit.entityHitted.IsSelected) |
| 0 | 86 | | return; |
| | 87 | |
|
| 0 | 88 | | if (voxelHit != null && voxelHit.entityHitted.tag == BuilderInWorldSettings.VOXEL_TAG) |
| | 89 | | { |
| 0 | 90 | | Vector3 position = ConverPositionToVoxelPosition(voxelHit.entityHitted.rootEntity.gameObject.transform.posit |
| 0 | 91 | | position += voxelHit.hitVector; |
| 0 | 92 | | editionGO.transform.position = position; |
| 0 | 93 | | } |
| | 94 | | else |
| | 95 | | { |
| | 96 | | RaycastHit hit; |
| 0 | 97 | | UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); |
| | 98 | |
|
| 0 | 99 | | if (Physics.Raycast(ray, out hit, RAYCAST_MAX_DISTANCE, groundLayer)) |
| | 100 | | { |
| 0 | 101 | | Vector3 position = hit.point; |
| 0 | 102 | | editionGO.transform.position = ConverPositionToVoxelPosition(hit.point); |
| | 103 | | } |
| | 104 | | } |
| 0 | 105 | | } |
| | 106 | |
|
| 0 | 107 | | public void SetEditionGO(GameObject editionGO) { this.editionGO = editionGO; } |
| | 108 | |
|
| 0 | 109 | | public bool IsActive() { return isVoxelModelActivated; } |
| | 110 | |
|
| 0 | 111 | | public void SetActiveMode(bool isActive) { isVoxelModelActivated = isActive; } |
| | 112 | |
|
| | 113 | | public void EndMultiVoxelSelection() |
| | 114 | | { |
| 0 | 115 | | List<DCLBuilderInWorldEntity> voxelEntities = builderInWorldEntityHandler.GetAllVoxelsEntities(); |
| | 116 | |
|
| 0 | 117 | | foreach (DCLBuilderInWorldEntity voxelEntity in voxelEntities) |
| | 118 | | { |
| 0 | 119 | | if (BuilderInWorldUtils.IsWithInSelectionBounds(voxelEntity.gameObject.transform, lastMousePosition, Input.m |
| | 120 | | { |
| 0 | 121 | | builderInWorldEntityHandler.SelectEntity(voxelEntity); |
| | 122 | | } |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | outlinerController.SetOutlineCheckActive(true); |
| 0 | 126 | | outlinerController.CancelAllOutlines(); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | void FillVoxels(Vector3Int firstPosition, Vector3Int lastPosition) |
| | 130 | | { |
| 0 | 131 | | int xDifference = Mathf.Abs(firstPosition.x - lastPosition.x); |
| 0 | 132 | | int yDifference = Mathf.Abs(firstPosition.y - lastPosition.y); |
| 0 | 133 | | int zDifference = Mathf.Abs(firstPosition.z - lastPosition.z); |
| | 134 | |
|
| 0 | 135 | | List<Vector3Int> mustContainVoxelList = new List<Vector3Int>(); |
| 0 | 136 | | List<DCLBuilderInWorldEntity> voxelEntities = builderInWorldEntityHandler.GetAllVoxelsEntities(); |
| 0 | 137 | | List<DCLBuilderInWorldEntity> allEntities = builderInWorldEntityHandler.GetAllEntitiesFromCurrentScene(); |
| | 138 | |
|
| 0 | 139 | | for (int x = 0; x <= xDifference; x++) |
| | 140 | | { |
| 0 | 141 | | int contX = x; |
| 0 | 142 | | if (firstPosition.x > lastPosition.x) |
| 0 | 143 | | contX = -contX; |
| | 144 | |
|
| 0 | 145 | | for (int y = 0; y <= yDifference; y++) |
| | 146 | | { |
| 0 | 147 | | int contY = y; |
| 0 | 148 | | if (firstPosition.y > lastPosition.y) |
| 0 | 149 | | contY = -contY; |
| | 150 | |
|
| 0 | 151 | | for (int z = 0; z <= zDifference; z++) |
| | 152 | | { |
| 0 | 153 | | int contZ = z; |
| 0 | 154 | | if (firstPosition.z > lastPosition.z) |
| 0 | 155 | | contZ = -contZ; |
| | 156 | |
|
| 0 | 157 | | Vector3Int positionOfVoxel = new Vector3Int(firstPosition.x + contX, firstPosition.y + contY, firstP |
| 0 | 158 | | if (positionOfVoxel == firstPosition) |
| | 159 | | continue; |
| 0 | 160 | | if (ExistVoxelAtPosition(positionOfVoxel, voxelEntities)) |
| | 161 | | continue; |
| 0 | 162 | | CreateVoxel(positionOfVoxel); |
| 0 | 163 | | mustContainVoxelList.Add(positionOfVoxel); |
| | 164 | | } |
| | 165 | | } |
| | 166 | | } |
| | 167 | |
|
| | 168 | |
|
| 0 | 169 | | List<Vector3Int> voxelToRemove = new List<Vector3Int>(); |
| 0 | 170 | | foreach (Vector3Int position in createdVoxels.Keys) |
| | 171 | | { |
| 0 | 172 | | if (!mustContainVoxelList.Contains(position)) |
| 0 | 173 | | voxelToRemove.Add(position); |
| | 174 | | } |
| | 175 | |
|
| 0 | 176 | | foreach (Vector3Int vector in voxelToRemove) |
| | 177 | | { |
| 0 | 178 | | Destroy(createdVoxels[vector].gameObject); |
| 0 | 179 | | createdVoxels.Remove(vector); |
| | 180 | | } |
| | 181 | |
|
| 0 | 182 | | foreach (VoxelPrefab keyValuePair in createdVoxels.Values) |
| | 183 | | { |
| 0 | 184 | | if (IsVoxelAtValidPoint(keyValuePair, allEntities)) |
| 0 | 185 | | keyValuePair.SetAvailability(true); |
| | 186 | | else |
| 0 | 187 | | keyValuePair.SetAvailability(false); |
| | 188 | | } |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | bool ExistVoxelAtPosition(Vector3Int position, List<DCLBuilderInWorldEntity> voxelEntities) |
| | 192 | | { |
| 0 | 193 | | foreach (DCLBuilderInWorldEntity voxelEntity in voxelEntities) |
| | 194 | | { |
| 0 | 195 | | if (position == ConverPositionToVoxelPosition(voxelEntity.transform.position)) |
| 0 | 196 | | return true; |
| | 197 | | } |
| | 198 | |
|
| 0 | 199 | | return false; |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | VoxelPrefab CreateVoxel(Vector3Int position) |
| | 203 | | { |
| 0 | 204 | | if (!createdVoxels.ContainsKey(position)) |
| | 205 | | { |
| 0 | 206 | | VoxelPrefab go = Instantiate(voxelPrefab.gameObject, position, lastVoxelCreated.rootEntity.gameObject.transf |
| 0 | 207 | | createdVoxels.Add(position, go); |
| 0 | 208 | | return go; |
| | 209 | | } |
| | 210 | |
|
| 0 | 211 | | return null; |
| | 212 | | } |
| | 213 | |
|
| | 214 | | private void MouseUp(int buttonID, Vector3 position) |
| | 215 | | { |
| 0 | 216 | | if (!mousePressed || buttonID != 0) |
| 0 | 217 | | return; |
| | 218 | |
|
| 0 | 219 | | if (isCreatingMultipleVoxels) |
| | 220 | | { |
| 0 | 221 | | lastVoxelCreated.transform.SetParent(null); |
| 0 | 222 | | bool canVoxelsBeCreated = true; |
| | 223 | |
|
| 0 | 224 | | foreach (VoxelPrefab voxel in createdVoxels.Values) |
| | 225 | | { |
| 0 | 226 | | if (!voxel.IsAvailable()) |
| | 227 | | { |
| 0 | 228 | | canVoxelsBeCreated = false; |
| 0 | 229 | | break; |
| | 230 | | } |
| | 231 | | } |
| | 232 | |
|
| 0 | 233 | | BuildInWorldCompleteAction buildAction = new BuildInWorldCompleteAction(); |
| 0 | 234 | | buildAction.actionType = BuildInWorldCompleteAction.ActionType.CREATE; |
| | 235 | |
|
| 0 | 236 | | List<BuilderInWorldEntityAction> entityActionList = new List<BuilderInWorldEntityAction>(); |
| | 237 | |
|
| 0 | 238 | | foreach (Vector3Int voxelPosition in createdVoxels.Keys) |
| | 239 | | { |
| 0 | 240 | | if (canVoxelsBeCreated) |
| | 241 | | { |
| 0 | 242 | | IDCLEntity entity = builderInWorldEntityHandler.DuplicateEntity(lastVoxelCreated).rootEntity; |
| 0 | 243 | | entity.gameObject.tag = BuilderInWorldSettings.VOXEL_TAG; |
| 0 | 244 | | entity.gameObject.transform.position = voxelPosition; |
| | 245 | |
|
| 0 | 246 | | BuilderInWorldEntityAction builderInWorldEntityAction = new BuilderInWorldEntityAction(entity, entit |
| 0 | 247 | | entityActionList.Add(builderInWorldEntityAction); |
| | 248 | | } |
| | 249 | |
|
| 0 | 250 | | Destroy(createdVoxels[voxelPosition].gameObject); |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | if (!canVoxelsBeCreated) |
| | 254 | | { |
| 0 | 255 | | builderInWorldEntityHandler.DeleteEntity(lastVoxelCreated); |
| 0 | 256 | | } |
| | 257 | | else |
| | 258 | | { |
| 0 | 259 | | buildAction.CreateActionType(entityActionList, BuildInWorldCompleteAction.ActionType.CREATE); |
| 0 | 260 | | actionController.AddAction(buildAction); |
| | 261 | | } |
| | 262 | |
|
| 0 | 263 | | createdVoxels.Clear(); |
| 0 | 264 | | builderInWorldEntityHandler.DeselectEntities(); |
| | 265 | |
|
| 0 | 266 | | lastVoxelCreated = null; |
| 0 | 267 | | isCreatingMultipleVoxels = false; |
| | 268 | |
|
| 0 | 269 | | mousePressed = false; |
| 0 | 270 | | freeCameraMovement.SetCameraCanMove(true); |
| | 271 | | } |
| 0 | 272 | | } |
| | 273 | |
|
| | 274 | | void MouseDown(int buttonID, Vector3 position) |
| | 275 | | { |
| 0 | 276 | | if (buttonID != 0 || !isVoxelModelActivated || lastVoxelCreated == null) |
| 0 | 277 | | return; |
| | 278 | |
|
| 0 | 279 | | lastVoxelPositionPressed = ConverPositionToVoxelPosition(lastVoxelCreated.transform.position); |
| 0 | 280 | | mousePressed = true; |
| 0 | 281 | | freeCameraMovement.SetCameraCanMove(false); |
| 0 | 282 | | isCreatingMultipleVoxels = true; |
| 0 | 283 | | } |
| | 284 | |
|
| | 285 | | public void SetVoxelSelected(DCLBuilderInWorldEntity decentralandEntityToEdit) |
| | 286 | | { |
| 0 | 287 | | lastVoxelCreated = decentralandEntityToEdit; |
| 0 | 288 | | lastVoxelCreated.transform.localPosition = Vector3.zero; |
| 0 | 289 | | } |
| | 290 | |
|
| | 291 | | public Vector3Int ConverPositionToVoxelPosition(Vector3 rawPosition) |
| | 292 | | { |
| 0 | 293 | | Vector3Int position = Vector3Int.zero; |
| 0 | 294 | | position.x = Mathf.CeilToInt(rawPosition.x); |
| 0 | 295 | | position.y = Mathf.CeilToInt(rawPosition.y); |
| 0 | 296 | | position.z = Mathf.CeilToInt(rawPosition.z); |
| 0 | 297 | | return position; |
| | 298 | | } |
| | 299 | |
|
| | 300 | | bool IsVoxelAtValidPoint(VoxelPrefab voxelPrefab, List<DCLBuilderInWorldEntity> entitiesToCheck) |
| | 301 | | { |
| 0 | 302 | | if (!currentScene.IsInsideSceneBoundaries(voxelPrefab.meshRenderer.bounds)) |
| 0 | 303 | | return false; |
| 0 | 304 | | Bounds bounds = voxelPrefab.meshRenderer.bounds; |
| 0 | 305 | | bounds.size -= Vector3.one * VOXEL_BOUND_ERROR; |
| 0 | 306 | | foreach (DCLBuilderInWorldEntity entity in entitiesToCheck) |
| | 307 | | { |
| 0 | 308 | | if (entity.rootEntity.meshesInfo == null || entity.rootEntity.meshesInfo.renderers == null) |
| | 309 | | continue; |
| 0 | 310 | | if (bounds.Intersects(entity.rootEntity.meshesInfo.mergedBounds)) |
| 0 | 311 | | return false; |
| | 312 | | } |
| | 313 | |
|
| 0 | 314 | | bounds.size += Vector3.one * VOXEL_BOUND_ERROR; |
| 0 | 315 | | return true; |
| 0 | 316 | | } |
| | 317 | | } |