// Use this for initialization void Start () { // We'll store a reference to the colored cubes volume so we can interact with it later. terrainVolume = gameObject.GetComponent<TerrainVolume>(); if(terrainVolume == null) { Debug.LogError("This 'ClickToCarveTerrainVolume' script should be attached to a game object with a TerrainVolume component"); } }
public void OnEnable() { terrainVolume = target as TerrainVolume; brushTextures = new Texture[NoOfBrushes]; brushTextures[0] = Resources.Load("Icons/SoftBrush") as Texture; brushTextures[1] = Resources.Load("Icons/MediumSoftBrush") as Texture; brushTextures[2] = Resources.Load("Icons/MediumBrush") as Texture; brushTextures[3] = Resources.Load("Icons/MediumHardBrush") as Texture; brushTextures[4] = Resources.Load("Icons/HardBrush") as Texture; }
static void CreateTerrainVolume() { int width = 128; int height = 32; int depth = 128; TerrainVolumeData data = VolumeDataAsset.CreateEmptyVolumeData <TerrainVolumeData>(new Region(0, 0, 0, width - 1, height - 1, depth - 1)); // Create some ground in the terrain so it shows up in the editor. // Soil as a base (mat 1) and then a couple of layers of grass (mat 2). TerrainVolumeGenerator.GenerateFloor(data, 6, (uint)1, 8, (uint)2); // Now create the terrain game object from the data. GameObject terrain = TerrainVolume.CreateGameObject(data, true, true); // And select it, so the user can get straight on with editing. Selection.activeGameObject = terrain; }
/// Picks a point on the surface of the terrain volume. /** * Note that the result may be slightly inaccurate due to the fixed step size. */ public static bool PickSurface(TerrainVolume volume, Vector3 origin, Vector3 direction, float distance, out PickSurfaceResult pickResult) { validateDistance(distance); // This 'out' value needs to be initialised even if we don't hit // anything (in which case it will be left at it's default value). pickResult = new PickSurfaceResult(); // Can't hit it the volume if there's no data. if ((volume.data == null) || (volume.data.volumeHandle == null)) { return(false); } // Cubiquity's picking code works in volume space whereas we expose an interface that works in world // space (for consistancy with other Unity functions). Therefore we apply the inverse of the volume's // volume-to-world transform to the ray, to bring it from world space into volume space. // // Note that we do this by transforming the start and end points of the ray (rather than the direction // of the ray) as Unity's Transform.InverseTransformDirection method does not handle scaling. Vector3 target = origin + direction * distance; origin = volume.transform.InverseTransformPoint(origin); target = volume.transform.InverseTransformPoint(target); direction = target - origin; // Now call through to the Cubiquity dll to do the actual picking. uint hit = CubiquityDLL.PickTerrainSurface((uint)volume.data.volumeHandle, origin.x, origin.y, origin.z, direction.x, direction.y, direction.z, out pickResult.volumeSpacePos.x, out pickResult.volumeSpacePos.y, out pickResult.volumeSpacePos.z); // The result is in volume space, but again it is more convienient for Unity users to have the result // in world space. Therefore we apply the volume's volume-to-world transform to the volume space position. pickResult.worldSpacePos = volume.transform.TransformPoint(pickResult.volumeSpacePos); // Return true if we hit a surface. return(hit == 1); }
/// Picks a point on the surface of the terrain volume. /** * Note that the result may be slightly inaccurate due to the fixed step size. */ public static bool PickSurface(TerrainVolume volume, Vector3 origin, Vector3 direction, float distance, out PickSurfaceResult pickResult) { validateDistance(distance); // This 'out' value needs to be initialised even if we don't hit // anything (in which case it will be left at it's default value). pickResult = new PickSurfaceResult(); // Can't hit it the volume if there's no data. if((volume.data == null) || (volume.data.volumeHandle == null)) { return false; } // Cubiquity's picking code works in volume space whereas we expose an interface that works in world // space (for consistancy with other Unity functions). Therefore we apply the inverse of the volume's // volume-to-world transform to the ray, to bring it from world space into volume space. // // Note that we do this by transforming the start and end points of the ray (rather than the direction // of the ray) as Unity's Transform.InverseTransformDirection method does not handle scaling. Vector3 target = origin + direction * distance; origin = volume.transform.InverseTransformPoint(origin); target = volume.transform.InverseTransformPoint(target); direction = target - origin; // Now call through to the Cubiquity dll to do the actual picking. uint hit = CubiquityDLL.PickTerrainSurface((uint)volume.data.volumeHandle, origin.x, origin.y, origin.z, direction.x, direction.y, direction.z, out pickResult.volumeSpacePos.x, out pickResult.volumeSpacePos.y, out pickResult.volumeSpacePos.z); // The result is in volume space, but again it is more convienient for Unity users to have the result // in world space. Therefore we apply the volume's volume-to-world transform to the volume space position. pickResult.worldSpacePos = volume.transform.TransformPoint(pickResult.volumeSpacePos); // Return true if we hit a surface. return hit == 1; }
/// Convinience method for creating a GameObject with a set of terrain components attached. /** * Adding a volume to a scene requires creating a GameObject and then attching the required Cubiquity components such a renderer and a * collider. This method simply automates the process and also attaches the provided volume data. * * \param data The volume data which should be attached to the construced volume. * \param addRenderer Specifies whether a renderer component should be added so that the volume is displayed. * \param addCollider Specifies whether a collider component should be added so that the volume can participate in collisions. */ public static GameObject CreateGameObject(TerrainVolumeData data, bool addRenderer, bool addCollider) { // Create our main game object representing the volume. GameObject terrainVolumeGameObject = new GameObject("Terrain Volume"); //Add the required volume component. TerrainVolume terrainVolume = terrainVolumeGameObject.GetOrAddComponent <TerrainVolume>(); // Set the provided data. terrainVolume.data = data; // Add the renderer and collider if desired. if (addRenderer) { terrainVolumeGameObject.AddComponent <TerrainVolumeRenderer>(); } if (addCollider) { terrainVolumeGameObject.AddComponent <TerrainVolumeCollider>(); } // Return the created object return(terrainVolumeGameObject); }
public static void PaintTerrainVolume(TerrainVolume volume, float centerX, float centerY, float centerZ, float brushInnerRadius, float brushOuterRadius, float amount, uint materialIndex) { CubiquityDLL.PaintTerrainVolume((uint)volume.data.volumeHandle, centerX, centerY, centerZ, brushInnerRadius, brushOuterRadius, amount, materialIndex); }
public static void BlurTerrainVolume(TerrainVolume volume, Region region) { CubiquityDLL.BlurTerrainVolumeRegion((uint)volume.data.volumeHandle, region.lowerCorner.x, region.lowerCorner.y, region.lowerCorner.z, region.upperCorner.x, region.upperCorner.y, region.upperCorner.z); }
public static void BlurTerrainVolume(TerrainVolume volume, float centerX, float centerY, float centerZ, float brushInnerRadius, float brushOuterRadius, float amount) { CubiquityDLL.BlurTerrainVolume((uint)volume.data.volumeHandle, centerX, centerY, centerZ, brushInnerRadius, brushOuterRadius, amount); }
public static void SculptTerrainVolume(TerrainVolume volume, float centerX, float centerY, float centerZ, float brushInnerRadius, float brushOuterRadius, float amount) { CubiquityDLL.SculptTerrainVolume((uint)volume.data.volumeHandle, centerX, centerY, centerZ, brushInnerRadius, brushOuterRadius, amount); }
/// Picks a point on the surface of the terrain volume. /** * Note that the result may be slightly inaccurate due to the fixed step size. */ public static bool PickSurface(TerrainVolume volume, Ray ray, float distance, out PickSurfaceResult pickResult) { return(PickSurface(volume, ray.origin, ray.direction, distance, out pickResult)); }
/// Picks a point on the surface of the terrain volume. /** * Note that the result may be slightly inaccurate due to the fixed step size. */ public static bool PickSurface(TerrainVolume volume, Ray ray, float distance, out PickSurfaceResult pickResult) { return PickSurface(volume, ray.origin, ray.direction, distance, out pickResult); }