// Update is called once per frame void Update() { if (Camera.current == null) { return; } Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition); // Perform the raycasting. PickSurfaceResult pickResult; bool hit = Picking.PickSurface(terrainVolume, ray.origin, ray.direction, 1000.0f, out pickResult); if (hit) { // Use this value to compute the inner radius as a proportion of the outer radius. float brushInnerRadius = brushOuterRadius * brushInnerScaleFactor; if (Input.GetMouseButton(0)) { float multiplier = 1.0f; if (Input.GetKey(KeyCode.LeftShift)) { multiplier = -1.0f; } TerrainVolumeEditor.SculptTerrainVolume(terrainVolume, pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, brushInnerRadius, brushOuterRadius, brushOpacity * multiplier); } } }
private void DestroyVoxels(int xPos, int yPos, int zPos) { int aux = Mathf.Max(RangeX, RangeY, RangeZ); int rangeSquared = aux * aux; MaterialSet emptyMaterialSet = new MaterialSet(); MaterialSet fillMaterialSet = new MaterialSet(); fillMaterialSet.weights[0] = 255; fillMaterialSet.weights[1] = 255; fillMaterialSet.weights[2] = 255; for (int z = zPos - RangeZ; z < zPos + RangeZ; z++) { for (int y = yPos - RangeY; y < yPos + RangeY; y++) { for (int x = xPos - RangeX; x < xPos + RangeX; x++) { int xDistance = x - xPos; int yDistance = y - yPos; int zDistance = z - zPos; int distSquared = 0; if (Euclidean) { distSquared = SqrEuclideanDistance(xDistance, yDistance, zDistance); } else { distSquared = ManhattanDistance(xDistance, yDistance, zDistance); } if (distSquared < rangeSquared) { if (Erase) { _terrainVolume.data.SetVoxel(x, y, z, emptyMaterialSet); } else { _terrainVolume.data.SetVoxel(x, y, z, fillMaterialSet); } } } } } TerrainVolumeEditor.BlurTerrainVolume(_terrainVolume, new Region( xPos - RangeX + SmoothFactor, yPos - RangeY + SmoothFactor, zPos - RangeZ + SmoothFactor, xPos + RangeX + SmoothFactor, yPos + RangeY + SmoothFactor, zPos + RangeZ + SmoothFactor) ); }
void DestroyVoxels(int xPos, int yPos, int zPos, int range) { // Initialise outside the loop, but we'll use it later. int rangeSquared = range * range; MaterialSet emptyMaterialSet = new MaterialSet(); // Iterage over every voxel in a cubic region defined by the received position (the center) and // the range. It is quite possible that this will be hundreds or even thousands of voxels. for (int z = zPos - range; z < zPos + range; z++) { for (int y = yPos - range; y < yPos + range; y++) { for (int x = xPos - range; x < xPos + range; x++) { // Compute the distance from the current voxel to the center of our explosion. int xDistance = x - xPos; int yDistance = y - yPos; int zDistance = z - zPos; // Working with squared distances avoids costly square root operations. int distSquared = xDistance * xDistance + yDistance * yDistance + zDistance * zDistance; // We're iterating over a cubic region, but we want our explosion to be spherical. Therefore // we only further consider voxels which are within the required range of our explosion center. // The corners of the cubic region we are iterating over will fail the following test. if (distSquared < rangeSquared) { terrainVolume.data.SetVoxel(x, y, z, emptyMaterialSet); } } } } range += 2; TerrainVolumeEditor.BlurTerrainVolume(terrainVolume, new Region(xPos - range, yPos - range, zPos - range, xPos + range, yPos + range, zPos + range)); //TerrainVolumeEditor.BlurTerrainVolume(terrainVolume, new Region(xPos - range, yPos - range, zPos - range, xPos + range, yPos + range, zPos + range)); //TerrainVolumeEditor.BlurTerrainVolume(terrainVolume, new Region(xPos - range, yPos - range, zPos - range, xPos + range, yPos + range, zPos + range)); }
// Update is called once per frame void Update() { // Bail out if we're not attached to a terrain. if (terrainVolume == null) { return; } //code for POP /*try * { * IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0); * byte[] data = listener.Receive(ref remote); * string s = Encoding.ASCII.GetString(data); * String[] tokens = s.Split(','); * * String ax0 = tokens[0]; * String ax1 = tokens[1]; * String ax2 = tokens[2]; * String ax3 = tokens[3]; * String ax4 = tokens[4]; * String ax5 = tokens[5]; * String ax6 = tokens[6]; * * float val1 = float.Parse(ax0, CultureInfo.InvariantCulture.NumberFormat) / 5f; * float val2 = float.Parse(ax1, CultureInfo.InvariantCulture.NumberFormat) / 5f; * float val3 = float.Parse(ax2, CultureInfo.InvariantCulture.NumberFormat) / 5f; * float val4 = float.Parse(ax3, CultureInfo.InvariantCulture.NumberFormat); * float val5 = float.Parse(ax4, CultureInfo.InvariantCulture.NumberFormat); * float val6 = float.Parse(ax5, CultureInfo.InvariantCulture.NumberFormat); * float val7 = float.Parse(ax6, CultureInfo.InvariantCulture.NumberFormat); * * * //Vector3 difference = trackedObj.transform.position - (new Vector3(val1, val2, -val3) * 1.15f); * //Debug.Log("position difference : " + difference); * //trackedObj.transform.position = (new Vector3((val1 - 168), val2 - 459, -val3 + 195)) * 1.15f; * //trackedObj.transform.localEulerAngles = new Vector3(-val5, -val6 - 90, -val4 + 90); * //transform.localEulerAngles = new Vector3(-val5, -val6 - 90, val4 + 90); * //Debug.Log("positions: "+ new Vector3(val1, val2, -val3) +" "+ trackedObj.transform.position); * //Debug.Log("angles: "+transform.localEulerAngles); * } * catch (Exception e) * { * print(e.ToString()); * } * //code for POP ends*/ for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++) { Debug.Log(Player.instance.hands.Length); Hand hand = Player.instance.hands[handIndex]; if (hand != null) { hand.HideController(true); Debug.Log("hide2"); } else { Debug.Log("not able to hide controller"); } } if (Physics.Raycast(trackedObj.transform.position, trackedObj.transform.forward, out hit, 1000f)) { Debug.DrawRay(trackedObj.transform.position, trackedObj.transform.forward, Color.green); Debug.Log("hello"); Vector3 pixel = hit.point; //pixel.x -= 10; //print(pixel); //Ray ray = Camera.main.ScreenPointToRay(new Vector3(pixel.x, pixel.z,0 )); Ray ray = new Ray(trackedObj.transform.position, trackedObj.transform.forward); // Perform the raycasting. PickSurfaceResult pickResult; bool hit1 = Picking.PickSurface(terrainVolume, ray, 100.0f, out pickResult); // If we hit a solid voxel then create an explosion at this point. if (hit1) { Debug.Log("Hell2"); int range = 2; if (SteamVR_Input._default.inActions.dig.GetStateDown(trackedObj.inputSource)) { DestroyVoxels((int)((pickResult.volumeSpacePos.x)), (int)pickResult.volumeSpacePos.y, (int)((pickResult.volumeSpacePos.z)), range); dirt.SetActive(true); } float multiplier = 1.0f; //if (e.modifiers == EventModifiers.Shift) //{ // multiplier = -1.0f; //} // Selected brush is in the range 0 to NoOfBrushes - 1. Convert this to a 0 to 1 range. float brushInnerScaleFactor = (float)selectedBrush / ((float)(NoOfBrushes - 1)); // Use this value to compute the inner radius as a proportion of the outer radius. float brushInnerRadius = brushOuterRadius * brushInnerScaleFactor; if (SteamVR_Input._default.inActions.dig.GetStateUp(trackedObj.inputSource)) { TerrainVolumeEditor.SculptTerrainVolume(terrainVolume, pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, brushInnerRadius, brushOuterRadius, brushOpacity * multiplier); dirt.SetActive(false); } //TerrainVolumeEditor.BlurTerrainVolume(terrainVolume, pickResult.volumeSpacePos.x, pickResult.volumeSpacePos.y, pickResult.volumeSpacePos.z, brushInnerRadius, brushOuterRadius, brushOpacity); } } }
void UseAbilite(Vector3 digWhere) { Ray ray = new Ray(Camera.main.transform.position, digWhere - Camera.main.transform.position);//Camera.main.ScreenPointToRay(new Vector3(digWhere.x, digWhere.y, 0)); // Perform the raycasting. PickSurfaceResult pickResult; bool hit = Picking.PickSurface(terrainVolume, ray, 1000.0f, out pickResult); int xPos = (int)pickResult.volumeSpacePos.x; int yPos = (int)pickResult.volumeSpacePos.y; int zPos = (int)pickResult.volumeSpacePos.z; int rangeSquared = range * range; if (hit) { switch (abilite) { //build case 1: for (int z = zPos - range; z < zPos + range; z++) { for (int y = yPos - range; y < yPos + range; y++) { for (int x = xPos - range; x < xPos + range; x++) { int xDistance = x + xPos; int yDistance = y + yPos; int zDistance = z + zPos; float noiseScale = 32.0f; float invNoiseScale = 1.0f / noiseScale; float sampleX = (float)xPos * invNoiseScale; float sampleY = (float)yPos * invNoiseScale; float sampleZ = (float)zPos * invNoiseScale; MaterialSet materialSet = new MaterialSet(); materialSet.weights[0] = (byte)255; int distSquared = xDistance * xDistance + yDistance * yDistance + zDistance * zDistance; if (distSquared > rangeSquared) { terrainVolume.data.SetVoxel(x, y, z, materialSet); } } } } break; case 2: MaterialSet emptyMaterialSet = new MaterialSet(); for (int z = zPos - range; z < zPos + range; z++) { for (int y = yPos - range; y < yPos + range; y++) { for (int x = xPos - range; x < xPos + range; x++) { int xDistance = x - xPos; int yDistance = y - yPos; int zDistance = z - zPos; int distSquared = xDistance * xDistance + yDistance * yDistance + zDistance * zDistance; if (distSquared < rangeSquared) { terrainVolume.data.SetVoxel(x, y, z, emptyMaterialSet); } } } } range += 2; TerrainVolumeEditor.BlurTerrainVolume(terrainVolume, new Region(xPos - range, yPos - range, zPos - range, xPos + range, yPos + range, zPos + range)); range -= 2; break; case 3: Vector3 pos = new Vector3(xPos, yPos, zPos); Vector3 heading = (pos - transform.position); heading = heading.normalized; Quaternion rot = Quaternion.LookRotation(heading); Instantiate(items[0], pos, rot); break; default: Debug.Log("no abilite"); break; } } }