void Awake() { //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< // 序列帧 float us = 1.0f / Tiles.x; float vs = 1.0f / Tiles.y; float fu = 0.0f; float fv = 0.0f; runTime = playSpeed; for (int y = 0; y < Tiles.y; ++y) { fu = 0.0f; for (int x = 0; x < Tiles.x; ++x) { TrailUVTile uvt = new TrailUVTile(); uvt.uvMin = new Vector2(fu, fv); uvt.uvMax = new Vector2(fu + us, fv + vs); uvAry.Add(uvt); fu += us; } fv += vs; } curUVTile = uvAry[curFrame]; //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MeshFilter meshF = GetComponent(typeof(MeshFilter)) as MeshFilter; mesh = meshF.mesh; meshRenderer = GetComponent(typeof(MeshRenderer)) as MeshRenderer; trailMaterial = meshRenderer.material; }
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< // 序列帧 TrailUVTile UpdateUVTile(float fTM) { if (uvAry.Count > 1) { runTime -= fTM; if (runTime <= 0.0f) { ++curFrame; if (curFrame >= uvAry.Count) { curFrame = 0;// uvAry.Count - 1; } runTime += playSpeed; curUVTile = uvAry[curFrame]; } } return(curUVTile); }
public void UpdateTrail(float currentTime, float deltaTime) // ** call once a frame ** // Rebuild the mesh { mesh.Clear(); // // Remove old sections while (sections.Count > 0 && currentTime > sections[sections.Count - 1].time + time) { sections.RemoveAt(sections.Count - 1); } // We need at least 2 sections to create the line if (sections.Count < 2) { return; } // vertices = new Vector3[sections.Count * 2]; colors = new Color[sections.Count * 2]; uv = new Vector2[sections.Count * 2]; // currentSection = sections[0]; // // Use matrix instead of transform.TransformPoint for performance reasons localSpaceTransform = transform.worldToLocalMatrix; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< // 序列帧 TrailUVTile uvt = UpdateUVTile(deltaTime); float uMax = uvt.uvMax.x - uvt.uvMin.x; float uMin = uvt.uvMin.x; float vMax = uvt.uvMax.y; // 1.0f float vMin = uvt.uvMin.y; // 0.0f // Generate vertex, uv and colors for (var i = 0; i < sections.Count; i++) { // currentSection = sections[i]; // Calculate u for texture uv and color interpolation float u = 0.0f; if (i != 0) { u = Mathf.Clamp01((currentTime - currentSection.time) / time); } // // Calculate upwards direction Vector3 upDir = currentSection.upDir; // Generate vertices vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point); vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + upDir * height); //uv[i * 2 + 0] = new Vector2(u, 0); //uv[i * 2 + 1] = new Vector2(u, 1); float fu = uMin + uMax * u; uv[i * 2 + 0] = new Vector2(fu, vMin); uv[i * 2 + 1] = new Vector2(fu, vMax); // fade colors out over time Color interpolatedColor = Color.Lerp(startColor, endColor, u); colors[i * 2 + 0] = interpolatedColor; colors[i * 2 + 1] = interpolatedColor; } // Generate triangles indices int[] triangles = new int[(sections.Count - 1) * 2 * 3]; for (int i = 0; i < triangles.Length / 6; i++) { 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; } // Assign to mesh mesh.vertices = vertices; mesh.colors = colors; mesh.uv = uv; mesh.triangles = triangles; // // Tween to the desired time // if (time > desiredTime) { time -= deltaTime * timeTransitionSpeed; if (time <= desiredTime) { time = desiredTime; } } else if (time < desiredTime) { time += deltaTime * timeTransitionSpeed; if (time >= desiredTime) { time = desiredTime; } } }