protected override void UpdatePosAndTangent() { mCurFrame++; float len = mPathLengthMoved - GetLength(mCurrentWaypointIndex); float lenTotal = GetLength(mCurrentWaypointIndex + 1) - GetLength(mCurrentWaypointIndex); float u = len / lenTotal; Vector3 start = GetWaypoint(mCurrentWaypointIndex); Vector3 end = GetWaypoint(mCurrentWaypointIndex + 1); Vector3 linePos = (1 - u) * start + u * end; #if UNITY_EDITOR mMovedTime += Time.deltaTime; mMovedLength += (linePos - CurInfo.linePos).magnitude; #endif CurInfo.linePos = linePos; CurInfo.curvePos = linePos; if (CurInfo.isDirChanged) { mLast = CurInfo.curveDir; mNext = (end - start).normalized; mCurFrame = 0; } float pro = mCurFrame * mFrameCountInv; if (pro < 1) { CurInfo.curveDir = GeoUtils.Interpolation(mLast, mNext, pro).normalized; } else { CurInfo.curveDir = mNext; } #if UNITY_EDITOR // 记录曲线点和切向,以及线上点和切向 if (trackPos != null) { trackPos.Add(CurInfo.linePos); trackPos.Add(CurInfo.lineDir); trackPos.Add(CurInfo.curvePos); trackPos.Add(CurInfo.curveDir); } #endif }
public bool IsIntersect(ref GeoRay2 dist, ref GeoInsectPointArrayInfo insect) { bool isInsect = GeoRayUtils.IsRayInsectTriangle2(dist.mOrigin, dist.mDirection, mP1, mP2, mP3, ref insect); if (isInsect) { insect.mHitObject2 = this; float min = 1e5f; foreach (Vector3 v in insect.mHitGlobalPoint.mPointArray) { float len = (GeoUtils.ToVector2(v) - dist.mOrigin).magnitude; if (len < min) { min = len; } } insect.mLength = min; } return(isInsect); }
/// <summary> /// 简单多边形或者三角面点数组 /// </summary> /// <param name="tries"></param> /// <param name="isPolygon">是否为简单多边形</param> /// <param name="vert"></param> /// <returns></returns> public static List<List<int>> Decompose(List<Vector2> tries, bool isPolygon, ref List<Vector2> vert) { if (isPolygon) { EarPolygon poly = new EarPolygon(); foreach (Vector2 v in tries) { poly.AddPoint(v.x, v.y); } EarClipping.Clip(poly); List<Vector2> triangles = new List<Vector2>(); GeoUtils.FlatList(poly.mResults, ref triangles); List<int> indices = new List<int>(); GeoUtils.MeshVertexPrimitiveType(triangles, ref vert, ref indices); return HMDecompose(vert, indices); } else { List<int> indices = new List<int>(); GeoUtils.MeshVertexPrimitiveType(tries, ref vert, ref indices); return HMDecompose(vert, indices); } }
public bool GetIntersection(GeoRay2 ray, ref GeoInsectPointArrayInfo intersection, bool occlusion) { if (mFlatTreeList.Count == 0) { return(false); } intersection.mIsIntersect = false; intersection.mLength = 999999999.0f; intersection.mHitObject2 = null; int closer, other; BVHTraversal[] todo = new BVHTraversal[64]; todo[0] = new BVHTraversal(); int stackptr = 0; todo[stackptr].mIndex = 0; todo[stackptr].mLength = -9999999.0f; while (stackptr >= 0) { int ni = todo[stackptr].mIndex; float near = todo[stackptr].mLength; stackptr--; BVHFlatNode2 node = mFlatTreeList[ni]; // 对叶节点做相交测试 if (node.mRightOffset == 0) { bool hit = false; for (int o = 0; o < node.mLeafCount; ++o) { GeoInsectPointArrayInfo current = new GeoInsectPointArrayInfo(); BVHObject2 obj = mBuildPrims[(int)node.mStartIndex + o]; hit = obj.IsIntersect(ref ray, ref current); if (hit) { if (occlusion) { intersection = current; return(true); } if (current.mLength < intersection.mLength) { intersection = current; } } } } else { closer = ni + 1; other = ni + (int)node.mRightOffset; // 对父结点做测试 GeoInsectPointArrayInfo in1 = new GeoInsectPointArrayInfo(); GeoInsectPointArrayInfo in2 = new GeoInsectPointArrayInfo(); bool hitc0 = GeoRayUtils.IsRayInsectAABB2(ray.mOrigin, ray.mDirection, mFlatTreeList[closer].mBox.mMin, mFlatTreeList[closer].mBox.mMax, ref in1); bool hitc1 = GeoRayUtils.IsRayInsectAABB2(ray.mOrigin, ray.mDirection, mFlatTreeList[other].mBox.mMin, mFlatTreeList[other].mBox.mMax, ref in2); if (hitc0 && hitc1) { float l0 = (GeoUtils.ToVector2(in1.mHitGlobalPoint[0]) - ray.mOrigin).magnitude; float l2 = (GeoUtils.ToVector2(in2.mHitGlobalPoint[0]) - ray.mOrigin).magnitude; if (l2 < l0) { float temp = l0; l0 = l2; l2 = temp; int itemp = closer; closer = other; other = itemp; } todo[++stackptr] = new BVHTraversal(other, l2); todo[++stackptr] = new BVHTraversal(closer, l0); } else if (hitc0) { float l0 = (GeoUtils.ToVector2(in1.mHitGlobalPoint[0]) - ray.mOrigin).magnitude; todo[++stackptr] = new BVHTraversal(closer, l0); } else if (hitc1) { float l2 = (GeoUtils.ToVector2(in2.mHitGlobalPoint[0]) - ray.mOrigin).magnitude; todo[++stackptr] = new BVHTraversal(other, l2); } } } if (intersection.mHitObject2 != null) { intersection.mHitGlobalPoint.Clear(); intersection.mHitGlobalPoint.Add(ray.mOrigin + ray.mDirection * intersection.mLength); } return(intersection.mHitObject2 != null); }