// Calculate the closest point of approach for line-segment vs line-segment. public static bool SegmentSegmentCPA(out Unity.Mathematics.float3 c0, out Unity.Mathematics.float3 c1, Unity.Mathematics.float3 p0, Unity.Mathematics.float3 p1, Unity.Mathematics.float3 q0, Unity.Mathematics.float3 q1) { var u = p1 - p0; var v = q1 - q0; var w0 = p0 - q0; float a = Unity.Mathematics.math.dot(u, u); float b = Unity.Mathematics.math.dot(u, v); float c = Unity.Mathematics.math.dot(v, v); float d = Unity.Mathematics.math.dot(u, w0); float e = Unity.Mathematics.math.dot(v, w0); var den = a * c - b * b; float sc, tc; if (den == 0) { sc = 0; tc = d / b; // todo: handle b = 0 (=> a and/or c is 0) } else { sc = (b * e - c * d) / (a * c - b * b); tc = (a * e - b * d) / (a * c - b * b); } c0 = Unity.Mathematics.math.lerp(p0, p1, sc); c1 = Unity.Mathematics.math.lerp(q0, q1, tc); return(den != 0); }
protected override void OnUpdate() { var random = new Unity.Mathematics.Random((uint)Random.Range(1, 100000)); var buffer = PostAttackEntityBuffer.CreateCommandBuffer().AsParallelWriter(); Dependency = Entities.ForEach( (Entity e, int entityInQueryIndex, in Attack attack, in Instigator attacker, in Target target, in BeamEffectStyle style, in HitLocation hitLoc, in SourceLocation sourceLoc) => { var laserEffect = new BeamEffect() { start = sourceLoc.Position, end = hitLoc.Position, lifetime = 0.2f }; if (attack.Result == Attack.eResult.Miss) { var delta = new Unity.Mathematics.float3(random.NextFloat() - 0.5f, 0f, random.NextFloat() - 0.5f); laserEffect.end = laserEffect.end + 6f * delta; } Entity effect = buffer.CreateEntity(entityInQueryIndex); buffer.AddComponent(entityInQueryIndex, effect, laserEffect); buffer.AddComponent(entityInQueryIndex, effect, style); buffer.AddComponent(entityInQueryIndex, effect, attacker); buffer.AddComponent(entityInQueryIndex, effect, target); }
public void TestAabb() { float3 v0 = float3(100, 200, 300); float3 v1 = float3(200, 300, 400); float3 v2 = float3(50, 100, 350); Aabb a0; a0.Min = float3.zero; a0.Max = v0; Aabb a1; a1.Min = float3.zero; a1.Max = v1; Aabb a2; a2.Min = v2; a2.Max = v1; Aabb a3; a3.Min = v2; a3.Max = v0; Assert.IsTrue(a0.IsValid); Assert.IsTrue(a1.IsValid); Assert.IsTrue(a2.IsValid); Assert.IsFalse(a3.IsValid); Assert.IsTrue(a1.Contains(a0)); Assert.IsFalse(a0.Contains(a1)); Assert.IsTrue(a1.Contains(a2)); Assert.IsFalse(a2.Contains(a1)); Assert.IsFalse(a0.Contains(a2)); Assert.IsFalse(a2.Contains(a0)); // Test Expand / Contains { Aabb a5; a5.Min = v2; a5.Max = v1; float3 testPoint = float3(v2.x - 1.0f, v1.y + 1.0f, .5f * (v2.z + v1.z)); Assert.IsFalse(a5.Contains(testPoint)); a5.Expand(1.5f); Assert.IsTrue(a5.Contains(testPoint)); } // Test transform { Aabb ut; ut.Min = v0; ut.Max = v1; // Identity transform should not modify aabb Aabb outAabb = Unity.Physics.Math.TransformAabb(RigidTransform.identity, ut); TestUtils.AreEqual(ut.Min, outAabb.Min, 1e-3f); // Test translation outAabb = Unity.Physics.Math.TransformAabb(new RigidTransform(quaternion.identity, float3(100.0f, 0.0f, 0.0f)), ut); Assert.AreEqual(outAabb.Min.x, 200); Assert.AreEqual(outAabb.Min.y, 200); Assert.AreEqual(outAabb.Max.x, 300); Assert.AreEqual(outAabb.Max.z, 400); // Test rotation quaternion rot = quaternion.EulerXYZ(0.0f, 0.0f, k_pi2); outAabb = Unity.Physics.Math.TransformAabb(new RigidTransform(rot, float3.zero), ut); TestUtils.AreEqual(outAabb.Min, float3(-300.0f, 100.0f, 300.0f), 1e-3f); TestUtils.AreEqual(outAabb.Max, float3(-200.0f, 200.0f, 400.0f), 1e-3f); TestUtils.AreEqual(outAabb.SurfaceArea, ut.SurfaceArea, 1e-2f); } }
// Update is called once per frame private void Update() { Unity.Mathematics.float3 zero = IMRE.HandWaver.ScaleDimension.MeshOperations.projectPosition(Unity.Mathematics.float4.zero); for (int i = 0; i < 4; i++) { axes[i].SetPosition(0, scale * zero); axes[i].SetPosition(1, scale * IMRE.HandWaver.ScaleDimension.MeshOperations.projectPosition(endpoints[i])); } }
protected override void OnEnableFunc() { Awake(); if (!pcgRes) { enabled = false; } PCGNodeBase.initRes = pcgRes; localToWorldMatrix = Matrix4x4.identity; boundingBoxExtents = new Unity.Mathematics.float3(float.MaxValue, float.MaxValue, float.MaxValue); }
public void TestAabbTransform() { Random rnd = new Random(0x12345678); for (int i = 0; i < 100; i++) { quaternion r = rnd.NextQuaternionRotation(); float3 t = rnd.NextFloat3(); Aabb orig = new Aabb(); orig.Include(rnd.NextFloat3()); orig.Include(rnd.NextFloat3()); Aabb outAabb1 = Unity.Physics.Math.TransformAabb(new RigidTransform(r, t), orig); Physics.Math.MTransform bFromA = new Physics.Math.MTransform(r, t); Aabb outAabb2 = Unity.Physics.Math.TransformAabb(bFromA, orig); TestUtils.AreEqual(outAabb1.Min, outAabb2.Min, 1e-3f); TestUtils.AreEqual(outAabb1.Max, outAabb2.Max, 1e-3f); } }
/// <summary> /// method for rendering a sphere /// by taking a centerpoint, radius, mesh, and n vertices /// </summary> /// <param name="crossSectionRadius"></param> /// <param name="center"></param> /// <param name="crossSectionRenderer"></param> /// <param name="n"></param> public static void RenderSphere(float crossSectionRadius, Unity.Mathematics.float3 center, UnityEngine.Mesh crossSectionRenderer, int n) { crossSectionRenderer.Clear(); int nbLong = n; int nbLat = n; #region Vertices UnityEngine.Vector3[] vertices = new UnityEngine.Vector3[(nbLong + 1) * nbLat + 2]; float pi = UnityEngine.Mathf.PI; float _2pi = pi * 2f; vertices[0] = UnityEngine.Vector3.up * crossSectionRadius; for (int lat = 0; lat < nbLat; lat++) { float a1 = pi * (lat + 1) / (nbLat + 1); float sin1 = UnityEngine.Mathf.Sin(a1); float cos1 = UnityEngine.Mathf.Cos(a1); for (int lon = 0; lon <= nbLong; lon++) { float a2 = _2pi * (lon == nbLong ? 0 : lon) / nbLong; float sin2 = UnityEngine.Mathf.Sin(a2); float cos2 = UnityEngine.Mathf.Cos(a2); vertices[lon + lat * (nbLong + 1) + 1] = new UnityEngine.Vector3(sin1 * cos2, cos1, sin1 * sin2) * crossSectionRadius; } } vertices[vertices.Length - 1] = UnityEngine.Vector3.up * -crossSectionRadius; #endregion #region Normals UnityEngine.Vector3[] normals = new UnityEngine.Vector3[vertices.Length]; for (int j = 0; j < vertices.Length; j++) { normals[j] = vertices[j].normalized; } #endregion #region UVs UnityEngine.Vector2[] uvs = new UnityEngine.Vector2[vertices.Length]; uvs[0] = UnityEngine.Vector2.up; uvs[uvs.Length - 1] = UnityEngine.Vector2.zero; for (int lat = 0; lat < nbLat; lat++) { for (int lon = 0; lon <= nbLong; lon++) { uvs[lon + lat * (nbLong + 1) + 1] = new UnityEngine.Vector2((float)lon / nbLong, 1f - (float)(lat + 1) / (nbLat + 1)); } } #endregion #region Triangles int nbFaces = vertices.Length; int nbTriangles = nbFaces * 2; int nbIndexes = nbTriangles * 3; int[] triangles = new int[nbIndexes]; //Top Cap int i = 0; for (int lon = 0; lon < nbLong; lon++) { triangles[i++] = lon + 2; triangles[i++] = lon + 1; triangles[i++] = 0; } //Middle for (int lat = 0; lat < nbLat - 1; lat++) { for (int lon = 0; lon < nbLong; lon++) { int current = lon + lat * (nbLong + 1) + 1; int next = current + nbLong + 1; triangles[i++] = current; triangles[i++] = current + 1; triangles[i++] = next + 1; triangles[i++] = current; triangles[i++] = next + 1; triangles[i++] = next; } } //Bottom Cap for (int lon = 0; lon < nbLong; lon++) { triangles[i++] = vertices.Length - 1; triangles[i++] = vertices.Length - (lon + 2) - 1; triangles[i++] = vertices.Length - (lon + 1) - 1; } #endregion crossSectionRenderer.vertices = vertices; crossSectionRenderer.normals = normals; crossSectionRenderer.uv = uvs; crossSectionRenderer.triangles = triangles; crossSectionRenderer.RecalculateBounds(); }
/// <summary> /// Finds the point of intersection between a line and a plane /// </summary> /// <param name="linePos">A point on the line</param> /// <param name="lineDir">The normalDirection of the line</param> /// <param name="planePos">A point on the plane</param> /// <param name="planeNorm">The normal normalDirection of the plane</param> /// <returns></returns> internal static Unity.Mathematics.float3 SegmentPlaneIntersection(Unity.Mathematics.float3 segmentA, Unity.Mathematics.float3 segmentB, Unity.Mathematics.float3 planePos, Unity.Mathematics.float3 planeNorm) { // 0 = disjoint (no intersection) // 1 = intersection in the unique point *I0 // 2 = the segment lies in the plane int type = 0; Unity.Mathematics.float3 result; float tolerance = .00001f; //segmenta and segmentb are endpoints of a segment Unity.Mathematics.float3 u = segmentB - segmentA; Unity.Mathematics.float3 w = segmentA - planePos; float D = Unity.Mathematics.math.dot(planeNorm, u); float N = -Unity.Mathematics.math.dot(planeNorm, w); if (UnityEngine.Mathf.Abs(D) < tolerance) { // segment is parallel to plane if (N == 0f) { // segment lies in plane type = 2; result = new Unity.Mathematics.float3(UnityEngine.Mathf.Infinity, UnityEngine.Mathf.Infinity, UnityEngine.Mathf.Infinity); } else { type = 0; // no intersection result = new Unity.Mathematics.float3(UnityEngine.Mathf.Infinity, UnityEngine.Mathf.Infinity, UnityEngine.Mathf.Infinity); } } else { // they are not parallel // compute intersect param float sI = N / D; if (sI < 0 || sI > 1) { type = 0; // no intersection result = new Unity.Mathematics.float3(UnityEngine.Mathf.Infinity, UnityEngine.Mathf.Infinity, UnityEngine.Mathf.Infinity); } else { result = segmentA + sI * u; // compute segment intersect point type = 1; } } /* if (type != 1) * { * Debug.Log(type); * }*/ return(result); }
public static float Angle(Unity.Mathematics.float3 from, Unity.Mathematics.float3 to) { return(Unity.Mathematics.math.acos(Unity.Mathematics.math.dot(Unity.Mathematics.math.normalize(from), Unity.Mathematics.math.normalize(to))) * UnityEngine.Mathf.Rad2Deg); }
public static float magnitude(Unity.Mathematics.float3 v) { return(Unity.Mathematics.math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)); }
private void setCrossSections(Unity.Mathematics.float3 pos, Unity.Mathematics.float3 normal) { _crosssections.ForEach(xc => xc.planePos = pos); _crosssections.ForEach(xc => xc.planeNormal = normal); }
private void updateCrossSections(Valve.VR.SteamVR_Behaviour_Pose arg0, Valve.VR.SteamVR_Input_Sources arg1) { pos = pose.origin.position; normal = pose.origin.up; setCrossSections(pose.origin.position, pose.origin.up); }
public T Add <T>(float3 position = default) where T : Element => context.Add <T>(position);