void Update() { if (loaded)// && nextUpdateTime < Time.time) { //nextUpdateTime = Time.time + 0.2f; lastUpdateTime = Time.time; if (DrivingSimulatorProxy.DataReady()) { notReadyCounter = 0; IntPtr ptr; DrivingSimulatorProxy.GetTrafficVehicles(out totalVehicleCount, out ptr); if (totalVehicleCount > 0) { updatedVehicles.Clear(); long iptr = ptr.ToInt64(); long iCurOffset = 0; for (int i = 0; i < totalVehicleCount; i++, iCurOffset += Driver_Veh_Data_Size) { VehicleData v = (VehicleData)Marshal.PtrToStructure(new IntPtr(iptr + iCurOffset), typeof(VehicleData)); updatedVehicles.Add(v.VehicleID, v); } int numNew = 0, numMoved = 0, numDeleted = 0; IntPtr pNewIds, pNewVehTypes, pMovedIds, pDeletedIds; DrivingSimulatorProxy.GetVehicleLists(out numNew, out pNewIds, out pNewVehTypes, out numMoved, out pMovedIds, out numDeleted, out pDeletedIds); int[] newIds = new int[numNew]; int[] newVehTypes = new int[numNew]; int[] movedIds = new int[numMoved]; int[] deletedIds = new int[numDeleted]; Marshal.Copy(pNewIds, newIds, 0, numNew); Marshal.Copy(pNewVehTypes, newVehTypes, 0, numNew); Marshal.Copy(pMovedIds, movedIds, 0, numMoved); Marshal.Copy(pDeletedIds, deletedIds, 0, numDeleted); for (int i = 0; i < numNew; i++) { int vehicleId = newIds[i]; GameObject go = trafficAssets.Instantiate((byte)newVehTypes[i], transform); vehicles.Add(vehicleId, go); go.GetComponent <SimulationElement>().Initialize(vehicleId); } for (int i = 0; i < numMoved; i++) { int vehicleId = movedIds[i]; GameObject go = vehicles[vehicleId]; VehicleData v = updatedVehicles[vehicleId]; go.transform.localPosition = new Vector3((float)v.Position_X, (float)v.Position_Z, (float)v.Position_Y); go.transform.localRotation = Quaternion.Euler(0f, 90f - (float)v.Orient_Heading * Mathf.Rad2Deg, 0f); } for (int i = 0; i < numDeleted; i++) { int vehicleId = deletedIds[i]; GameObject go; if (vehicles.TryGetValue(vehicleId, out go)) { go.SetActive(false); go.transform.parent = null; Destroy(go); vehicles.Remove(vehicleId); } } } Vector3 driverPosition = driver.localPosition + driver.forward * 0.5f; //+ Replace this magic number with vehicle's half length driverData[0].Position_X = driverPosition.x; driverData[0].Position_Y = driverPosition.z; driverData[0].Orient_Heading = (90f - driver.rotation.eulerAngles.y) * Mathf.Deg2Rad; if (calculateSpeed) { driverData[0].Speed = (driver.position - lastUpdatePosition).magnitude / (Time.time - lastUpdateTime); lastUpdatePosition = driver.position; lastUpdateTime = Time.time; } DrivingSimulatorProxy.SetDriverVehicles(1, driverData); simulationUpdateCounter++; } else { if (useDeadReckoning) { foreach (var vehicle in updatedVehicles.Values) { int vehicleId = vehicle.VehicleID; float speed = (float)vehicle.Speed; GameObject go = vehicles[vehicleId]; go.transform.localPosition += go.transform.forward * speed * Time.deltaTime; } } notReadyCounter++; if (notReadyCounter > 1000) { Debug.LogWarning("VISSIM wasn't ready for too long!"); Destroy(this); } } } }
void Update(float time) { float frame = time * TIME_TO_FRAME; int lowframe = Mathf.FloorToInt(frame); int highframe = Mathf.CeilToInt(frame); // Interpolate between low and high frame float lerp = frame - lowframe; //- Mathf.InverseLerp(lowframe, highframe, frame); int offset = data.frameOffsets[lowframe]; int nextOffset = data.frameOffsets[lowframe + 1]; SimulationElement simElement; if (currentLowFrame == lowframe && currentHighFrame == highframe) { for (; offset < nextOffset; offset++) { simElement = activeSimElements[data.keyframes[offset].vehicleID]; SimulationKeyframe from = data.keyframes[simElement.fromKeyframe]; SimulationKeyframe to = data.keyframes[simElement.toKeyframe]; simElement.targetPosition = Vector3.Lerp(from.position, to.position, lerp); simElement.targetRotation = Quaternion.Slerp(from.rotation, to.rotation, lerp); simElement.MoveToTarget(); } } else { currentLowFrame = lowframe; currentHighFrame = highframe; var temp = previousSimElements; previousSimElements = activeSimElements; activeSimElements = temp; // Update existing elements and create new ones for (; offset < nextOffset; offset++) { SimulationKeyframe keyframe = data.keyframes[offset]; if (previousSimElements.TryGetValue(keyframe.vehicleID, out simElement)) { previousSimElements.Remove(keyframe.vehicleID); } else { byte assetType = data.assetTypes[keyframe.vehicleID]; GameObject go = trafficAssets.Instantiate(assetType, transform); go.transform.localPosition = keyframe.position; go.transform.localRotation = keyframe.rotation; simElement = go.GetComponent <SimulationElement>(); simElement.Initialize(keyframe.vehicleID); } simElement.fromKeyframe = offset; simElement.targetPosition = keyframe.position; simElement.targetRotation = keyframe.rotation; activeSimElements.Add(keyframe.vehicleID, simElement); } // Remove elements that have ceased to exist foreach (var previousChild in previousSimElements.Values) { previousChild.gameObject.SetActive(false); previousChild.transform.parent = null; Destroy(previousChild.gameObject); } previousSimElements.Clear(); offset = nextOffset; nextOffset = data.frameOffsets[highframe + 1]; // Find the next frame position/rotation and interpolate for (; offset < nextOffset; offset++) { SimulationKeyframe keyframe = data.keyframes[offset]; if (activeSimElements.TryGetValue(keyframe.vehicleID, out simElement)) { simElement.toKeyframe = offset; simElement.SetSpeed(Vector3.Distance(simElement.targetPosition, keyframe.position) * TIME_TO_FRAME); simElement.targetPosition = Vector3.Lerp(simElement.targetPosition, keyframe.position, lerp); simElement.targetRotation = Quaternion.Slerp(simElement.targetRotation, keyframe.rotation, lerp); simElement.MoveToTarget(); //var audioSource = simElement.GetComponent<AudioSource>(); //float sqrDst = (simElement.targetPosition - Camera.main.transform.position).sqrMagnitude; //if (audioSource) //{ // audioSource.priority = (int)Mathf.Lerp(50, 250, Mathf.Clamp01(sqrDst * 0.00005f)); //} } } // Random honk/bell //if (random.Next() > 1000) //{ // offset = data.frameOffsets[lowframe]; // nextOffset = data.frameOffsets[lowframe + 1]; // vvv OUT OF RANGE EXCEPTION vvv // simElement = activeSimElements[data.keyframes[random.Next(offset, nextOffset)].vehicleID]; // simElement.SoundSignal(); //} } }