private static void AllocateBuilder(TrackMarksBuilderComponent builder, TrackMarksComponent component, TrackMarksRenderComponent renderer) { builder.leftWheels = new Transform[3]; builder.rightWheels = new Transform[3]; builder.prevLeftWheelsData = new WheelData[3]; builder.prevRightWheelsData = new WheelData[3]; builder.tempLeftWheelsData = new WheelData[3]; builder.tempRightWheelsData = new WheelData[3]; builder.currentLeftWheelsData = new WheelData[3]; builder.currentRightWheelsData = new WheelData[3]; builder.positions = new Vector3[2]; builder.nextPositions = new Vector3[2]; builder.normals = new Vector3[2]; builder.nextNormals = new Vector3[2]; builder.directions = new Vector3[2]; builder.contiguous = new bool[2]; builder.prevHits = new bool[2]; builder.remaingDistance = new float[2]; builder.resetWheels = new bool[2]; builder.side = new float[] { -1f, 1f }; for (int i = 0; i < 2; i++) { builder.contiguous[i] = false; builder.prevHits[i] = false; } builder.moveStep = component.markWidth / ((float)component.parts); }
public void InitTracks(InitTrackRendererEvent e, TrackMarksInitNode node) { Transform parent = node.assembledTank.AssemblyRoot.transform; TrackMarksComponent trackMarks = node.trackMarks; TrackMarksRenderComponent render = new TrackMarksRenderComponent { mesh = new Mesh() }; render.mesh.MarkDynamic(); GameObject obj2 = new GameObject("Track"); obj2.transform.SetParent(parent); obj2.AddComponent <MeshFilter>().mesh = render.mesh; trackMarks.material = new Material(trackMarks.material); MeshRenderer renderer = obj2.AddComponent <MeshRenderer>(); renderer.material = trackMarks.material; foreach (ParticleSystem system in trackMarks.particleSystems) { ParticleSystem.ShapeModule shape = system.shape; shape.enabled = true; shape.shapeType = ParticleSystemShapeType.MeshRenderer; shape.meshRenderer = renderer; } this.InitTrackMarksRender(render, trackMarks); node.Entity.AddComponent(render); base.NewEvent <InitTrackBuilderEvent>().Attach(node).ScheduleDelayed(0.3f); }
public void OnUpdate(UpdateEvent evt, TrackMarkUpdateNode node, [JoinAll] CameraNode cameraNode) { if (cameraNode.camera.Enabled) { int num = 6 - GraphicsSettings.INSTANCE.CurrentQualityLevel; if (num > 0) { long num2; node.trackMarks.tick = num2 = node.trackMarks.tick + 1L; if ((num2 % ((long)num)) != 0L) { return; } } node.trackMarks.tick = 0L; TrackMarksRenderComponent trackMarksRender = node.trackMarksRender; TrackMarksBuilderComponent trackMarksBuilder = node.trackMarksBuilder; ChassisAnimationComponent chassisAnimation = node.chassisAnimation; TrackMarksComponent trackMarks = node.trackMarks; trackMarksBuilder.rigidbody = node.rigidbody.Rigidbody; if (this.NeedUpdateMarks(trackMarksBuilder, trackMarks)) { this.UpdateSingleTrack(trackMarksBuilder, chassisAnimation, trackMarksRender, trackMarks, 0, trackMarksBuilder.leftWheels, trackMarksBuilder.prevLeftWheelsData, trackMarksBuilder.currentLeftWheelsData, trackMarksBuilder.tempLeftWheelsData); this.UpdateSingleTrack(trackMarksBuilder, chassisAnimation, trackMarksRender, trackMarks, 1, trackMarksBuilder.rightWheels, trackMarksBuilder.prevRightWheelsData, trackMarksBuilder.currentRightWheelsData, trackMarksBuilder.tempRightWheelsData); } if (trackMarksRender.dirty) { this.UpdateMesh(trackMarksRender); trackMarksRender.dirty = false; } } }
private void ResetTrack(TrackMarksBuilderComponent builder, TrackMarksComponent trackMarks, int i, ref RaycastHit hitData) { builder.directions[i] = trackMarks.transform.forward; builder.nextPositions[i] = builder.positions[i] = hitData.point; builder.nextNormals[i] = builder.normals[i] = hitData.normal; builder.contiguous[i] = false; builder.remaingDistance[i] = 0f; }
private bool CheckDirectionChange(TrackMarksBuilderComponent builder, TrackMarksComponent trackMarks, int i, ref RaycastHit hitData) { if (Mathf.Abs(Vector3.Dot(this.GetVelocity(builder).normalized, builder.directions[i])) >= 0.75f) { return(true); } this.ResetTrack(builder, trackMarks, i, ref hitData); return(false); }
public void InitBuilder(InitTrackBuilderEvent e, TrackMarksInitNode node, SingleNode <TrackMarksRenderComponent> renderer) { TrackMarksComponent trackMarks = node.trackMarks; TrackMarksBuilderComponent builder = new TrackMarksBuilderComponent(); AllocateBuilder(builder, trackMarks, renderer.component); this.FindWheels(builder, node.chassisAnimation, trackMarks); node.Entity.AddComponent(builder); }
private bool CheckNormalChange(TrackMarksBuilderComponent builder, TrackMarksComponent trackMarks, int i, ref RaycastHit hitData) { if (Vector3.Dot(builder.normals[i], hitData.normal) >= 0.95f) { return(true); } this.ResetTrack(builder, trackMarks, i, ref hitData); return(false); }
private void InitTrackMarksRender(TrackMarksRenderComponent render, TrackMarksComponent trackMarks) { int num = (2 * trackMarks.maxSectorsPerTrack) * 4; int num2 = (2 * trackMarks.maxSectorsPerTrack) * 6; render.meshColors = new Color[num]; render.meshNormals = new Vector3[num]; render.meshPositions = new Vector3[num]; render.meshUVs = new Vector2[num]; render.meshTris = new int[num2]; render.trackRenderDatas = new TrackRenderData[2]; for (int i = 0; i < 2; i++) { render.trackRenderDatas[i] = new TrackRenderData(trackMarks.maxSectorsPerTrack, trackMarks.hideSectorsFrom, trackMarks.baseAlpha, trackMarks.parts); } }
private bool IsHit(TrackMarksBuilderComponent builder, TrackMarksComponent trackMarks, WheelData[] wheels, float hitDistance, out RaycastHit hit) { WheelData data = wheels[wheels.Length / 2]; bool flag = Physics.Raycast(data.position, -trackMarks.transform.up, out hit, hitDistance * 2f, TRACK_LAYER_MASK); if (flag) { RaycastHit hit2; if ((hit.distance >= (hitDistance * 1.5f)) && !this.CheckExtraContacts(builder, trackMarks, wheels, hitDistance)) { return(false); } float maxDistance = hit.distance + 0.1f; flag &= Physics.Raycast(data.position + (data.right * trackMarks.markTestShift), -trackMarks.transform.up, out hit2, maxDistance, TRACK_LAYER_MASK); if (flag) { RaycastHit hit3; flag &= Physics.Raycast(data.position - (data.right * trackMarks.markTestShift), -trackMarks.transform.up, out hit3, maxDistance, TRACK_LAYER_MASK); hit.normal = (hit3.normal + hit3.normal) / 2f; hit.point = (hit3.point + hit2.point) / 2f; } } return(flag); }
private bool CheckExtraContacts(TrackMarksBuilderComponent builder, TrackMarksComponent trackMarks, WheelData[] wheels, float hitDistance) { float maxDistance = hitDistance + 0.2f; return(Physics.Raycast(wheels[0].position, -trackMarks.transform.up, maxDistance, TRACK_LAYER_MASK) || !Physics.Raycast(wheels[wheels.Length - 1].position, -trackMarks.transform.up, maxDistance, TRACK_LAYER_MASK)); }
private unsafe void UpdateWheels(TrackMarksBuilderComponent builder, ChassisAnimationComponent chassis, TrackMarksRenderComponent render, TrackMarksComponent trackMarks, int track, WheelData[] currentWheelsPositions, WheelData[] prevWheelsPosition, WheelData[] temp) { float highDistance = chassis.highDistance; WheelData data = prevWheelsPosition[prevWheelsPosition.Length / 2]; WheelData data2 = currentWheelsPositions[currentWheelsPositions.Length / 2]; float num2 = (data.position - data2.position).magnitude + builder.remaingDistance[track]; int num3 = (int)(num2 / builder.moveStep); Vector3 lhs = Vector3.Cross(trackMarks.transform.up, this.GetVelocity(builder) * Time.smoothDeltaTime).normalized *builder.side[track]; float b = Mathf.Max(Mathf.Abs(Vector3.Dot(lhs, this.ComputeTrackDiagonal(builder, trackMarks, currentWheelsPositions, ref lhs))) / 2f, trackMarks.markWidth); float a = Mathf.Max(Mathf.Abs(Vector3.Dot(lhs, this.ComputeTrackDiagonal(builder, trackMarks, prevWheelsPosition, ref lhs))) / 2f, trackMarks.markWidth); bool flag = false; RaycastHit hit = new RaycastHit(); for (int i = 0; i < num3; i++) { float t = ((float)i) / ((float)num3); float width = Mathf.Lerp(a, b, t); float num9 = Mathf.Min((float)(width / trackMarks.markWidth), (float)2f); this.InterpolateWheelsPosition(t, prevWheelsPosition, currentWheelsPositions, temp); flag = this.IsHit(builder, trackMarks, temp, highDistance, out hit); RaycastHit *hitPtr1 = &hit; hitPtr1.point -= (builder.side[track] * trackMarks.transform.right) * trackMarks.shiftToCenter; this.UpdateTrack(builder, render, trackMarks, track, flag, ref hit, width, num9 - 1f); num2 -= builder.moveStep; } builder.remaingDistance[track] = num2; }
private void UpdateTrack(TrackMarksBuilderComponent builder, TrackMarksRenderComponent render, TrackMarksComponent trackMarks, int track, bool hit, ref RaycastHit hitData, float width, float shiftToBack) { if (!hit) { this.ResetTrack(builder, trackMarks, track, ref hitData); } else if (!builder.prevHits[track]) { this.ResetTrack(builder, trackMarks, track, ref hitData); } else { Vector3 normalized = this.GetVelocity(builder).normalized; builder.nextPositions[track] = (hitData.point - (((trackMarks.transform.forward * shiftToBack) * trackMarks.markWidth) / 2f)) + ((normalized * shiftToBack) * trackMarks.markWidth); builder.nextNormals[track] = hitData.normal; if (this.CheckEnoughMove(builder, track, ref hitData) && (this.CheckDirectionChange(builder, trackMarks, track, ref hitData) && this.CheckNormalChange(builder, trackMarks, track, ref hitData))) { float rotationCos = Vector3.Dot(trackMarks.transform.forward, normalized); if (rotationCos > 0.95f) { rotationCos = 1f; } else if (rotationCos < 0.8f) { rotationCos = 0f; } this.MakeTracks(render, track, builder.positions[track], builder.directions[track], builder.nextPositions[track], normalized, hitData.normal, width, width / trackMarks.markWidth, rotationCos, builder.contiguous[track]); builder.directions[track] = normalized; builder.positions[track] = builder.nextPositions[track]; builder.normals[track] = builder.nextNormals[track]; builder.contiguous[track] = true; } } builder.prevHits[track] = hit; }
private void UpdateSingleTrack(TrackMarksBuilderComponent builder, ChassisAnimationComponent chassis, TrackMarksRenderComponent render, TrackMarksComponent trackMarks, int track, Transform[] wheels, WheelData[] prevWheelsData, WheelData[] currentWheelsData, WheelData[] tempWheelsData) { this.CheckResetWheels(builder, track, wheels, prevWheelsData); this.CopyWheelsPositionFromTransforms(wheels, currentWheelsData); this.UpdateWheels(builder, chassis, render, trackMarks, track, currentWheelsData, prevWheelsData, tempWheelsData); Array.Copy(currentWheelsData, prevWheelsData, currentWheelsData.Length); }
private bool NeedUpdateMarks(TrackMarksBuilderComponent builder, TrackMarksComponent trackMarks) => (builder.CachedCamera.transform.position - trackMarks.transform.position).magnitude <= trackMarks.maxDistance;
private void FindWheels(TrackMarksBuilderComponent builder, ChassisAnimationComponent chassis, TrackMarksComponent trackMarks) { List <Transform> list = this.FindAllNodes(trackMarks.transform, chassis.movingWheelName, null); List <Transform> list2 = new List <Transform>(); List <Transform> list3 = new List <Transform>(); for (int i = 0; i < list.Count; i++) { Transform item = list[i]; if (item.name.Contains("left_")) { list2.Add(item); } if (item.name.Contains("right_")) { list3.Add(item); } } ZSorter comparer = new ZSorter(); list2.Sort(comparer); list3.Sort(comparer); builder.leftWheels = list2.ToArray(); builder.rightWheels = list3.ToArray(); }
private Vector3 ComputeTrackDiagonal(TrackMarksBuilderComponent builder, TrackMarksComponent trackMarks, WheelData[] data, ref Vector3 left) => (data[data.Length - 1].position + ((data[data.Length - 1].right * trackMarks.markWidth) / 2f)) - (data[0].position - ((data[0].right * trackMarks.markWidth) / 2f));