public override void Init() { if (!_trailRenderer) { _trailRenderer = UnityEngine.Object.Instantiate <SaberTrailRenderer>(_trailRendererPrefab, new Vector3(0, TRAIL_Y_OFFSET), Quaternion.identity); } if (!leftClawRenderer) { leftClawRenderer = UnityEngine.Object.Instantiate <SaberTrailRenderer>(_trailRendererPrefab, new Vector3(-TRAIL_X_OFFSET, TRAIL_Y_OFFSET), Quaternion.identity); } if (!rightClawRenderer) { rightClawRenderer = UnityEngine.Object.Instantiate <SaberTrailRenderer>(_trailRendererPrefab, new Vector3(TRAIL_X_OFFSET, TRAIL_Y_OFFSET), Quaternion.identity); } _sampleStep = 1f / _samplingFrequency; BladeMovementDataElement lastAddedData = _movementData.lastAddedData; Vector3 bottomPos = lastAddedData.bottomPos; Vector3 topPos = lastAddedData.topPos; _lastTrailElementTime = lastAddedData.time; _trailElementCollection = new TrailElementCollection(Mathf.CeilToInt(_samplingFrequency * _trailDuration) + 3, bottomPos, calcNewTopPos(bottomPos, topPos), _lastTrailElementTime); float trailWidth = GetTrailWidth(lastAddedData); _whiteSectionMaxDuration = Math.Min(_whiteSectionMaxDuration, _trailDuration); _lastZScale = transform.lossyScale.z; _trailRenderer.Init(trailWidth, _trailDuration, _granularity, _whiteSectionMaxDuration); leftClawRenderer.Init(trailWidth, _trailDuration, _granularity, _whiteSectionMaxDuration); rightClawRenderer.Init(trailWidth, _trailDuration, _granularity, _whiteSectionMaxDuration); _inited = true; }
public override void Init() { if (!_trailRenderer) { _trailRenderer = Instantiate(_trailRendererPrefab, new Vector3(0, TrailYOffset), Quaternion.identity); } if (!_leftClawRenderer) { _leftClawRenderer = Instantiate(_trailRendererPrefab, new Vector3(-TrailXOffset, TrailYOffset), Quaternion.identity); } if (!_rightClawRenderer) { _rightClawRenderer = Instantiate(_trailRendererPrefab, new Vector3(TrailXOffset, TrailYOffset), Quaternion.identity); } _sampleStep = 1f / _samplingFrequency; var lastAddedData = _movementData.lastAddedData; var bottomPos = lastAddedData.bottomPos; var topPos = lastAddedData.topPos; _lastTrailElementTime = lastAddedData.time; _trailElementCollection = new TrailElementCollection(Mathf.CeilToInt(_samplingFrequency * _trailDuration) + 3, bottomPos, CalcNewTopPos(bottomPos, topPos), _lastTrailElementTime); var trailWidth = GetTrailWidth(lastAddedData); _whiteSectionMaxDuration = Math.Min(_whiteSectionMaxDuration, _trailDuration); _lastZScale = transform.lossyScale.z; _trailRenderer.Init(trailWidth, _trailDuration, _granularity, _whiteSectionMaxDuration); _leftClawRenderer.Init(trailWidth, _trailDuration * 0.5f, _granularity, _whiteSectionMaxDuration * 0.5f); _rightClawRenderer.Init(trailWidth, _trailDuration * 0.5f, _granularity, _whiteSectionMaxDuration * 0.5f); _inited = true; }
public void LateUpdate() { if (_pointStart == null || _pointEnd == null) { return; } // wait until the fps is stable const int passThroughFrames = 4; if (_framesPassed <= passThroughFrames) { if (_framesPassed == passThroughFrames) { _samplingFrequency = Mathf.RoundToInt(VRDeviceInfo.Instance.refreshRate); /* no need after recent steamvr update for pimax? * if (VRDeviceInfo.Instance.isPimax) * { * _samplingFrequency = _samplingFrequency / 2; * } * else */ { _samplingFrequency = Mathf.RoundToInt((float)_samplingFrequency * Settings.Utilities.PluginConfig.Instance.samplingMultiplier); } if (_samplingFrequency == 0) { _samplingFrequency = 60; } _sampleStep = 1f / (float)_samplingFrequency; int capacity = Mathf.CeilToInt((float)_samplingFrequency * _trailDuration); Logger.log.Debug($"trail samplingFrequency={_samplingFrequency}, duration={_trailDuration}, capacity={capacity}"); _lastTrailElementTime = TimeHelper.time; _trailElementCollection = new TrailElementCollection(capacity, _pointStart.position, _pointEnd.position, _lastTrailElementTime); float trailWidth = (_pointEnd.position - _pointStart.position).magnitude; _whiteSectionMaxDuration = Mathf.Min(_whiteSectionMaxDuration, _trailDuration); _lastZScale = transform.lossyScale.z; _trailRenderer.Init(trailWidth, _trailDuration, (int)_granularity, _whiteSectionMaxDuration); _lastPointStart = _pointStart.position; _lastPointEnd = _pointEnd.position; _inited = true; } _framesPassed++; return; } if (TimeHelper.time <= _lastTrailElementTime) { return; } int num = Mathf.FloorToInt((TimeHelper.time - _lastTrailElementTime) / _sampleStep); float time = TimeHelper.time; float lastTime = _lastTrailElementTime; for (int i = 1; i <= num; i++) { _lastTrailElementTime += _sampleStep; float t = (_lastTrailElementTime - lastTime) / (time - lastTime); _lastPointStart = Vector3.LerpUnclamped(_lastPointStart, _pointStart.position, t); _lastPointEnd = Vector3.LerpUnclamped(_lastPointEnd, _pointEnd.position, t); _trailElementCollection.MoveTailToHead(); _trailElementCollection.head.SetData(_lastPointStart, _lastPointEnd, _lastTrailElementTime); } if (num > 0) { _trailElementCollection.UpdateDistances(); _trailRenderer.UpdateMesh(_trailElementCollection, color); } }