// ===================================================================================== // Tidal 2 - Water System | Scripting API usage (v2.2.0, Unity 6000.0+ URP) // // Everything a game script normally calls, as working examples. This whole file is // valid C#: rename it to .cs to drop it into a project. All of it runs on the MAIN // THREAD, in Play mode (the water is simulated on the GPU; queries read a baked copy). // ===================================================================================== using System.Collections.Generic; using Tidal.Core; using UnityEngine; public class Tidal2ApiExamples : MonoBehaviour { // --------------------------------------------------------------------------------- // 1. FINDING THE WATER // --------------------------------------------------------------------------------- void FindingTheWater() { // The body whose water covers a point (null when no body does). TidalWaterBody body = TidalWaterBody.FindBodyAt(transform.position); // Every active body in the loaded scenes. foreach (TidalWaterBody b in TidalWaterBody.AllBodies) Debug.Log(b.name); // Which body a camera is in, and the water height at the camera. if (TidalManager.Instance != null && TidalManager.Instance.TryGetCameraWaterInfo(Camera.main, out TidalWaterBody camBody, out float waterY)) { bool cameraUnderwater = Camera.main.transform.position.y < waterY; } } // --------------------------------------------------------------------------------- // 2. READING THE SURFACE (height, normal, current, submersion) // --------------------------------------------------------------------------------- void ReadingTheSurface(TidalWaterBody body) { Vector3 p = transform.position; // Surface height (world Y). float.MinValue = no water here (outside a lake's // bounds, a clipped volume, or the simulation not ready yet) - always check it. float h = body.GetWaterHeight(p); if (h > float.MinValue) { bool under = p.y < h; float depth = h - p.y; // > 0 when the point is underwater } // Height and normal in one call; false where there is no water. if (body.TryGetWaterSurface(p, out float height, out Vector3 normal)) { transform.up = normal; } // Surface normal on its own (ignore a list of volumes, sample spacing in metres). Vector3 n = body.GetWaterNormal(p, null, 0.5f); // The current: world XZ, metres per second (zones, whirlpools, propellers). Vector2 flow = body.GetWaterFlow(p); Vector3 flow3 = new Vector3(flow.x, 0f, flow.y); // How much of a sphere is under the water, 0..1. float submerged = body.GetSubmersion01(p, 0.5f); // Ignore volumes that carve water out of your own hull. List myHull = new List(); float hIgnoringHull = body.GetWaterHeight(p, myHull); } // --------------------------------------------------------------------------------- // 3. BREAKING WAVES (several surfaces on one vertical line under a curl) // --------------------------------------------------------------------------------- void UnderACurl(TidalWaterBody body) { Vector3 p = transform.position; // GetWaterHeight reports the TOP surface - under a barrel that is the lip above you. // The column reports every surface on the line. if (body.TryGetWaterColumn(p, out TidalWaterColumn col)) { bool inWater = col.Submerged; // in water (not in the air pocket) float signed = col.Depth; // + under water, - in air float top = col.TopSurface; int surfaces = col.SurfaceCount; // > 1 = the wave folds over here bool overhang = col.Overhang; if (col.HasSurfaceBelow) { float below = col.SurfaceBelow; Vector3 nb = col.NormalBelow; } if (col.HasSurfaceAbove) { float above = col.SurfaceAbove; Vector3 na = col.NormalAbove; } if (col.HasWaterVelocity) { Vector3 v = col.WaterVelocity; } // the wave's water } // Shortcut: the nearest surface at or below the point (the face under a barrel). if (body.TryGetWaterSurfaceBelow(p, out float floorY, out Vector3 floorN)) { } } // --------------------------------------------------------------------------------- // 4. FLOATING OBJECTS (TidalBuoyancy - no code needed, but all of it is scriptable) // --------------------------------------------------------------------------------- void MakeItFloat(GameObject boat, TidalWaterBody body) { var rb = boat.GetComponent(); var buoy = boat.AddComponent(); buoy.ConnectedRigid = rb; buoy.UsingTidalBody = body; // null = first body found // Float points: 4+ children at the hull's bottom corners (more = better pitch/roll). buoy.FloatPoints = new Transform[4]; Vector3[] corners = { new Vector3(-1, -0.5f, -2), new Vector3(1, -0.5f, -2), new Vector3(-1, -0.5f, 2), new Vector3(1, -0.5f, 2) }; for (int i = 0; i < 4; i++) { var fp = new GameObject("Float Point " + i).transform; fp.SetParent(boat.transform, false); fp.localPosition = corners[i]; buoy.FloatPoints[i] = fp; } // BuoyancyFactor is a FORCE per point (x 9.81 N), not scaled by mass: // about mass / points x 2 rides half submerged. buoy.BuoyancyFactor = rb.mass / buoy.FloatPoints.Length * 2f; buoy.CenterOfMassOffset = new Vector3(0f, -0.5f, 0f); // lower = harder to capsize buoy.FlowFollow = 1f; // drift at the current's speed (0 = still-water drag) buoy.KeelEffect = 0.7f; // boats: resist sliding sideways (0 for crates) buoy.IgnoreOwnWake = true; // don't sink into the hull's own wake dimple buoy.RideUnderCurls = false; // true: float on the face under a breaking curl buoy.IgnoreVolumes = new List(); // e.g. the hull's own Exclude volume bool afloat = buoy.IsInWater; } // --------------------------------------------------------------------------------- // 5. PROPELLERS (BETA), WAKES AND FOAM // TidalPropulsion is in BETA in 2.2: its settings and behaviour may change in a // later release. // --------------------------------------------------------------------------------- void BoatEffects(GameObject boat, float throttleInput) { // Propeller / jet: pushes water backwards, churns foam and a wake. // Put it at the stern, Z axis pointing forward. var prop = boat.GetComponentInChildren(); prop.Throttle = throttleInput; // -1..1, drive it from your controller prop.PushRigidbody = false; // true = it also moves the boat itself float thrust = prop.Thrust; // newtons actually applied float inWater = prop.Submersion; // 0..1 // A wake trail (displaces the water) and a foam-only trail. Size and strength are // set in the Inspector; the emitters follow their GameObject on their own. boat.AddComponent(); boat.AddComponent(); } // --------------------------------------------------------------------------------- // 6. IMPACTS AND BODY-WIDE SETTINGS // --------------------------------------------------------------------------------- void Impacts(TidalWaterBody body, Vector3 hitPoint) { // A crater pushed into the water then released: rings that rock floating objects. // radius (m) ~ wavelength, depth (m) ~ height (max 40), duration (s), foam (s). body.AddImpact(hitPoint, 6f, 3f, 0.4f, 4f); // Wake physics: Realistic (deep-water Kelvin wake) or Fast (cheaper cone). body.WakeModel = TidalWakeModel.Fast; // How big this body's waves are, 0..1 - also on a simulation shared with others. body.WaveStrength = 0.33f; } // --------------------------------------------------------------------------------- // 7. WAVE BOXES (big individual / breaking / triggered waves) // --------------------------------------------------------------------------------- void WaveBoxes(TidalWaveBox box) { box.Mode = TidalWaveBoxMode.SingleWave; // or Consecutive (endless set) box.Play(); // start (Single Wave: one wave crosses the box) box.Pause(); // hold where it is box.Stop(); // back to the start, flat box.Playhead = 0.5f; // jump to a point along the wave's run, 0..1 bool rolling = box.IsPlaying; float seconds = box.Duration; // Is it steep enough to curl? Q*k*A above 1 overturns into a barrel. bool curls = box.SteepnessProduct > 1f; bool lipOk = box.LipWithinWavelength; // lip does not reach the next wave float breakAt = box.BreakPoint; // < 1 = breaks inside the box // Inside the wave? (0 = outside, 1 = fully inside) float w = box.WeightAtXZ(new Vector2(transform.position.x, transform.position.z)); bool inBarrelAir = box.InAirPocket(transform.position); bool cameraInBarrel = TidalWaveBox.ViewerInAirPocket; } // --------------------------------------------------------------------------------- // 8. WHIRLPOOLS (read-only at runtime; tune them in the Inspector) // --------------------------------------------------------------------------------- void Whirlpools(TidalWhirlpool pool) { float radius = pool.Radius; // influence radius (m) float eye = pool.CoreRadius; float funnel = pool.FunnelDepthMetres; // how deep the pit is float peakFlow = pool.FlowStrength; // peak current speed (m/s) bool clockwise = pool.Clockwise; } // --------------------------------------------------------------------------------- // 9. VOLUMES (cut water out of a hull, keep it inside a pool, or calm it) // --------------------------------------------------------------------------------- void Volumes(GameObject hull, TidalWaterBody body) { var vol = hull.AddComponent(); vol.AnalyticType = TidalVolumeAnalyticTypes.Cube; // size = the transform's scale vol.Flags = TidalVolumeFlags.EXCLUDE; // INCLUDE, DAMPEN, NONE vol.Damping = 0.25f; // DAMPEN only: 1 = untouched vol.ClipVisual = TidalClipVisuals.CROSS_SECTION; // show the water column on cuts // Scoping: a NON-EMPTY list is exclusive - only these bodies are affected. vol.AffectedBodies = new List { body }; bool hits = vol.Affects(body); } // --------------------------------------------------------------------------------- // 10. ZONES (painted areas: calm, height, current, foam) // --------------------------------------------------------------------------------- void Zones(TidalZone zone) { Vector3 p = transform.position; if (zone.IsInside(p)) { Vector2 current = zone.SampleFlowWorld(p); // Flow zones if (zone.SampleHeightWorld(p, out float extraHeight)) { } // Height zones if (zone.SampleDampingWorld(p, out float calm)) { } // Damping zones float raw = zone.Sample01(p, 1f); } // After changing the zone texture's pixels from script: zone.MarkTextureDirty(); } // --------------------------------------------------------------------------------- // 11. WAVE SIMULATION (FFT or lightweight Gerstner) // --------------------------------------------------------------------------------- void Simulation(TidalWaterBody body, TidalProfileGerstner gerstnerProfile) { TidalMeshSharedSettings settings = body.GetTidalMeshSettings(); // Switch a body to Gerstner waves (lower-cost alternative; mobile is untested). var gerstner = body.gameObject.AddComponent(); gerstner.ActiveGerstnerProfile = gerstnerProfile; // Assets > Create > Tidal 2 > Water Profile (Gerstner) gerstner.Size = ComputeTidalSize.W256; // 256-512 for Gerstner settings.TCompute = gerstner; // Or the FFT ocean: swap its profile at runtime. var fft = body.GetComponent(); if (fft != null) { TidalProfile current = fft.ActiveTidalProfile; } // Per body, on the mesh settings. settings.Tessellation = true; // smooth curls / whirlpools (GPU cost) settings.ViewerCam = Camera.main; // the camera the water is built around float maxWave = settings.MaxWaveHeightEstimate; } }