bool InsertSample(Vector3 pos, Vector3 forward, Vector3 right, float timer) { if (_samples.Count > 0 && Vector3.Distance(pos, _samples[0].pos) < _minSampleDistance) { // 过滤距离过近的点 return(false); } if (Time.timeSinceLevelLoad > timer + sampleLife) { // 过滤超时的样品 return(false); } // 创建mesh样本 SamplePointData newSample = SamplePointData.pool.Get(); newSample.pos = pos; newSample.forward = forward; newSample.right = right; newSample.sampleTimer = timer; // 插入样本 _samples.Insert(0, newSample); return(true); }
/// <summary> /// 依据clip中固定在武器上的trail对象的移动轨迹获得一系列采样点 /// </summary> void RawSample(AnimProxy animProxy, TrailMgr.InterpolationMode interpolationMode) { // 动画的播放进度 float normalizedTime = animProxy.GetNormalizedTime(clip); // 遍历设定的每个动画采样时段(注意:时段之间不能有重叠) foreach (var clipNormalizedTime in clipNormalizedTimes) { if (normalizedTime < clipNormalizedTime.startNormalizedTime) { // 本时段尚未开始 continue; } if (normalizedTime < clipNormalizedTime.stopNormalizedTime) { // 进入指定动画进度 if (clipNormalizedTime.sampling == false) { // 开始本时段采样 clipNormalizedTime.sampling = true; for (int i = 0; i < _rawSamples.Count; ++i) { SamplePointData.pool.Collect(_rawSamples[i]); } _rawSamples.Clear(); } } else { // 离开指定动画进度 if (clipNormalizedTime.sampling) { // 停止采样 clipNormalizedTime.sampling = false; } continue; } // 添加原始样本 SamplePointData raw = SamplePointData.pool.Get(); raw.pos = trail.transform.position; raw.forward = trail.transform.forward; raw.right = trail.transform.right; raw.sampleTimer = Time.timeSinceLevelLoad; _rawSamples.Add(raw); // 平滑插值处理 MakeSmooth(/*Mathf.InverseLerp(clipNormalizedTime.startNormalizedTime, * clipNormalizedTime.stopNormalizedTime, normalizedTime)*/interpolationMode); } }
/// <summary> /// 依据采样点构建trail对象的mesh /// </summary> void BuildTrailMesh() { if (_samples.Count == 0) { return; } // 移除超期样本 while (_samples.Count > 0 && Time.timeSinceLevelLoad > _samples[_samples.Count - 1].sampleTimer + sampleLife) { SamplePointData.pool.Collect(_samples[_samples.Count - 1]); _samples.RemoveAt(_samples.Count - 1); } if (_samples.Count <= 1) { _meshFilter.mesh.Clear(); return; } Vector3[] vertices = new Vector3[_samples.Count * (isStab ? 3 : 2)]; Vector2[] uv = new Vector2[_samples.Count * (isStab ? 3 : 2)]; // Use matrix instead of transform.TransformPoint for performance reasons Matrix4x4 localSpaceTransform = trail.transform.worldToLocalMatrix; // 创建顶点、UV for (var i = 0; i < _samples.Count; i++) { SamplePointData sample = _samples[i]; if (isStab) { // 一个样本创建三个顶点: 刃面中心,刃宽左侧,刃宽右侧 vertices[i * 3 + 0] = localSpaceTransform.MultiplyPoint(sample.pos); vertices[i * 3 + 1] = localSpaceTransform.MultiplyPoint(sample.pos - sample.right * trailWidth * 0.5f); vertices[i * 3 + 2] = localSpaceTransform.MultiplyPoint(sample.pos + sample.right * trailWidth * 0.5f); // 一个样本创建三个UV float u = 0.0f; if (i != 0) { u = Mathf.Clamp01((Time.timeSinceLevelLoad - sample.sampleTimer) / sampleLife); } uv[i * 3 + 0] = new Vector2(u, 0); uv[i * 3 + 1] = new Vector2(u, 1); uv[i * 3 + 2] = new Vector2(u, 1); } else { // 一个样本创建两个顶点: 刃面中心,刃长底端 vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(sample.pos); vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(sample.pos - sample.forward * trailWidth); // 一个样本创建两个UV float u = 0.0f; if (i != 0) { u = Mathf.Clamp01((Time.timeSinceLevelLoad - sample.sampleTimer) / sampleLife); } uv[i * 2 + 0] = new Vector2(u, 0); uv[i * 2 + 1] = new Vector2(u, 1); } } // 创建三角形 int triangleCount = (_samples.Count - 1) * 2 * (isStab ? 2 : 1); // 三角形数量 int trianglesVerticeCount = triangleCount * 3; // 三角形顶点数量 int[] triangles = new int[trianglesVerticeCount]; int groupCount = triangles.Length / (6 * (isStab ? 2 : 1)); for (int i = 0; i < groupCount; i++) { if (isStab) { triangles[i * 12 + 0] = i * 3; triangles[i * 12 + 1] = i * 3 + 1; triangles[i * 12 + 2] = i * 3 + 3; triangles[i * 12 + 3] = i * 3; triangles[i * 12 + 4] = i * 3 + 3; triangles[i * 12 + 5] = i * 3 + 2; triangles[i * 12 + 6] = i * 3 + 3; triangles[i * 12 + 7] = i * 3 + 1; triangles[i * 12 + 8] = i * 3 + 4; triangles[i * 12 + 9] = i * 3 + 3; triangles[i * 12 + 10] = i * 3 + 5; triangles[i * 12 + 11] = i * 3 + 1; } else { triangles[i * 6 + 0] = i * 2; triangles[i * 6 + 1] = i * 2 + 1; triangles[i * 6 + 2] = i * 2 + 2; triangles[i * 6 + 3] = i * 2 + 2; triangles[i * 6 + 4] = i * 2 + 1; triangles[i * 6 + 5] = i * 2 + 3; } } MeshTools.Instance.MakeMesh(_meshFilter, vertices, triangles, uv); }
public static void Reset(SamplePointData data) { data.Reset(); }