// This is a method that returns a point on the terrain that has been selected with the mouse when pressing a certain key public Vector3 GetTerrainCollisionInEditor(Event currentEvent, KeyCode keysCode) { Vector3 returnCollision = new Vector3(); AttachedRiverScript riverScript = (AttachedRiverScript)target as AttachedRiverScript; Camera SceneCameraReceptor = new Camera(); GameObject terrain = GameObject.Find(riverScript.parentTerrain); Terrain terComponent = (Terrain)terrain.GetComponent(typeof(Terrain)); TerrainData terData = terComponent.terrainData; if (Camera.current != null) { SceneCameraReceptor = Camera.current; RaycastHit raycastHit = new RaycastHit(); Vector2 newMousePosition = new Vector2(currentEvent.mousePosition.x, Screen.height - (currentEvent.mousePosition.y + 25)); Ray terrainRay = SceneCameraReceptor.ScreenPointToRay(newMousePosition); if (Physics.Raycast(terrainRay, out raycastHit, Mathf.Infinity)) { returnCollision = raycastHit.point; returnCollision.x = Mathf.RoundToInt((returnCollision.x / terData.size.x) * terData.heightmapResolution); returnCollision.y = returnCollision.y / terData.size.y; returnCollision.z = Mathf.RoundToInt((returnCollision.z / terData.size.z) * terData.heightmapResolution); } } // Debug.Log(returnCollision); return(returnCollision); }
// This is a method that returns a point on the terrain that has been selected with the mouse when pressing a certain key public Vector3 GetTerrainCollisionInEditor(Event currentEvent, bool bInSceneGUI) { Vector3 returnCollision = new Vector3(); AttachedRiverScript riverScript = (AttachedRiverScript)target as AttachedRiverScript; Ray terrainRay = new Ray(); Camera SceneCameraReceptor = bInSceneGUI ? Camera.current : Camera.main; if (SceneCameraReceptor != null) { RaycastHit raycastHit = new RaycastHit(); Vector2 newMousePosition = new Vector2(currentEvent.mousePosition.x, Screen.height - (currentEvent.mousePosition.y + 25)); terrainRay = SceneCameraReceptor.ScreenPointToRay(newMousePosition); Debug.DrawRay(terrainRay.origin, terrainRay.direction, Color.green); if (Physics.Raycast(terrainRay, out raycastHit, Mathf.Infinity, riverScript.terrainLayer)) { returnCollision = raycastHit.point; //returnCollision.x = Mathf.RoundToInt((returnCollision.x/terData.size.x) * terData.heightmapResolution); //returnCollision.y = returnCollision.y/terData.size.y; //returnCollision.z = Mathf.RoundToInt((returnCollision.z/terData.size.z) * terData.heightmapResolution); } else { Debug.LogError("Error: No collision with terrain to create node"); } } return(returnCollision); }
public void CreateRiver() { GameObject riverObject = new GameObject(); riverObject.name = "River"; riverObject.AddComponent(typeof(MeshFilter)); riverObject.AddComponent(typeof(MeshRenderer)); riverObject.AddComponent <AttachedRiverScript>(); AttachedRiverScript ARS = (AttachedRiverScript)riverObject.GetComponent("AttachedRiverScript"); ARS.riverObject = riverObject; ARS.parentTerrain = gameObject.name; ARS.NewRiver(); }
public void CreateRiver() { GameObject riverObject = new GameObject(); riverObject.name = "River"; riverObject.AddComponent(typeof(MeshFilter)); riverObject.AddComponent(typeof(MeshRenderer)); riverObject.AddComponent <AttachedRiverScript>(); if (transform.parent != null) { riverObject.transform.parent = transform.parent; } AttachedRiverScript ARS = (AttachedRiverScript)riverObject.GetComponent("AttachedRiverScript"); ARS.riverObject = riverObject; ARS.parentTerrain = gameObject; }
void Awake() { riverScript = (AttachedRiverScript)target as AttachedRiverScript; }
public void GenRiverVoxels(AttachedRiverScript riverScript) { MeshCollider mc = riverScript.GetComponent <MeshCollider>(); if (mc == null) { riverScript.CreateMesh(riverScript.riverSmooth); mc = riverScript.GetComponent <MeshCollider>(); } if (mc == null || mc.sharedMesh == null) { Debug.LogError("Can not find mesh collider"); return; } VFVoxelWater.InitSufaceChunkData(); Bounds meshBound = mc.sharedMesh.bounds; #if UNITY_EDITOR EditorUtility.DisplayProgressBar("RiverVoxels...", "Starting gen...", 0); #endif Vector3 boundMin = meshBound.min; Vector3 boundMax = meshBound.max; IntVector4 cpos = new IntVector4(); if (!System.IO.Directory.Exists(outputDir)) { System.IO.Directory.CreateDirectory(outputDir); } curOutputDir = outputDir + "/" + riverScript.name; if (!System.IO.Directory.Exists(curOutputDir)) { System.IO.Directory.CreateDirectory(curOutputDir); } string filePrefix = curOutputDir + "/water"; #if false //test GenARiverChunk(ref boundMin, ref boundMax, mc, new IntVector4(310, 2, 221, 0), filePrefix); #else //float prev = VoxelTerrainConstants._numVoxelsPrefix /(float)VoxelTerrainConstants._numVoxelsPerAxis; //float post = VoxelTerrainConstants._numVoxelsPostfix/(float)VoxelTerrainConstants._numVoxelsPerAxis; for (cpos.w = 0; cpos.w <= LODOctreeMan.MaxLod; cpos.w++) { int step = 1 << cpos.w; int lodPrefix = VoxelTerrainConstants._numVoxelsPrefix << cpos.w; int lodpostfix = VoxelTerrainConstants._numVoxelsPostfix << cpos.w; int sx = (((int)(boundMin.x) - lodPrefix) >> (VoxelTerrainConstants._shift + cpos.w)) << cpos.w; int sy = (((int)(boundMin.y) - lodPrefix) >> (VoxelTerrainConstants._shift + cpos.w)) << cpos.w; int sz = (((int)(boundMin.z) - lodPrefix) >> (VoxelTerrainConstants._shift + cpos.w)) << cpos.w; int ex = (((int)(boundMax.x) + lodpostfix) >> (VoxelTerrainConstants._shift + cpos.w)) << cpos.w; int ey = (((int)(boundMax.y) + lodpostfix) >> (VoxelTerrainConstants._shift + cpos.w)) << cpos.w; int ez = (((int)(boundMax.z) + lodpostfix) >> (VoxelTerrainConstants._shift + cpos.w)) << cpos.w; if (sy < 0) { sy = 0; } int n = (((ex - sx) >> cpos.w) + 1) * (((ey - sy) >> cpos.w) + 1) * (((ez - sz) >> cpos.w) + 1); float progressStep = 1.0f / n; float progress = 0.0f; for (cpos.x = sx; cpos.x <= ex; cpos.x += step) { for (cpos.z = sz; cpos.z <= ez; cpos.z += step) { for (cpos.y = sy; cpos.y <= ey; cpos.y += step) { #if UNITY_EDITOR EditorUtility.DisplayProgressBar("RiverVoxels..." + cpos.w, "Gen " + cpos + "...", progress); progress += progressStep; #endif GenARiverChunk(ref boundMin, ref boundMax, mc, new IntVector4(cpos), filePrefix); } } } } #endif #if UNITY_EDITOR EditorUtility.ClearProgressBar(); #endif }
void Awake() { riverScript = (AttachedRiverScript) target as AttachedRiverScript; }