Example #1
0
    public void Itterate(float itterateTime)   // ** call everytime you sample animation **
    {
        position = transform.position;
        now      = itterateTime;

        // Add a new trail section
        if (sections.Count == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
        {
            TronTrailSection section = new TronTrailSection();
            section.point = position;
            if (alwaysUp)
            {
                section.upDir = Vector3.up;
            }
            else if (Vector3.zero != rotateTrail)
            {
                section.upDir = transform.TransformDirection(rotateTrail);
            }
            else
            {
                section.upDir = transform.TransformDirection(Vector3.up);
            }

            section.time = now;
            sections.Insert(0, section);
        }
    }
Example #2
0
    //首先计算当前位置
    //接着插值 三个点给trail position scale 和 rotation
    public void AddRefPoint(float t)
    {
        var diff = Time.deltaTime;

        position = transform.position;
        Vector3 upDir = transform.TransformDirection(Vector3.up);

        now = t;
        if (sections.Count == 0)
        {
            TronTrailSection section = new TronTrailSection();
            section.point = position;
            if (alwaysUp)
            {
                section.upDir = Vector3.up;
            }
            else
            {
                section.upDir = upDir;
            }

            section.time = now;
            sections.Insert(0, section);
        }
        else if ((sections [0].point - position).sqrMagnitude > minDistance * minDistance)
        {
            var lastPos = sections[0].point;
            var lastUp  = sections[0].upDir;
            for (int i = 1; i <= 3; i++)
            {
                var newPos  = Vector3.Lerp(lastPos, position, i * 1.0f / 3);
                var newUp   = Vector3.Slerp(lastUp, upDir, i * 1.0f / 3);
                var curTime = now + diff * (i - 1) / 2.0f;

                TronTrailSection section = new TronTrailSection();
                section.point = newPos;
                if (alwaysUp)
                {
                    section.upDir = Vector3.up;
                }
                else
                {
                    //section.upDir = transform.TransformDirection (Vector3.up);
                    section.upDir = newUp;
                }

                section.time = curTime;
                sections.Insert(0, section);
            }
        }
    }
Example #3
0
    public void Itterate(float itterateTime)
    {
        TronTrailSection section = new TronTrailSection();

        //这里都预先分配出来
        section.points = new Vector3[sectionData.points.Length];
        for (int i = 0; i < sectionData.points.Length; i++)
        {
            section.points[i] = transform.TransformPoint(sectionData.points[i]);
        }

        section.time = itterateTime;
        sections.Insert(0, section);
    }
Example #4
0
    public void Itterate(float itterateTime)   // ** call everytime you sample animation **
    {
        position = transform.position;
        now      = itterateTime;
        if (currentlife < 0.0f)
        {
            return;
        }

        if (time < 0.0001f)
        {
            return;
        }
        Vector3 up = Vector3.up;

        if (!alwaysUp)
        {
            up = Vector3.Normalize(transform.TransformDirection(Vector3.up));
        }


        float lastDistance = 0;                           //

        if (sections.Count > 0)
        {
            Vector3 vNear = position - sections[0].point;
            Vector3 vFar  = (up - sections[0].upDir) * height + vNear;
            lastDistance = Vector3.Distance(vNear, Vector3.zero) + Vector3.Distance(vFar, Vector3.zero);
        }

        // Add a new trail section
        if (sections.Count == 0 || lastDistance > minDistance)
        {
            TronTrailSection section = new TronTrailSection();
            section.point = position;
            section.upDir = up;
            section.time  = now;

            if (sections.Count > 0)
            {
                section.length = lastDistance;
            }
            sections.Insert(0, section);
        }
    }
Example #5
0
    public void Itterate(float itterateTime)
    {
        if (mCurrentStartPos != transform.position)
        {
            if (this.time <= 0)
            {
                ClearTrail();
            }

            currentposition = transform.position;
            Quaternion currentrotation = transform.rotation;

            int stepNum = 10;

            for (int i = 0; i < stepNum; i++)
            {
                float stepP = i / (stepNum - 1f);
                transform.position = Vector3.Lerp(Vector3.Lerp(mLastFramePos, mCurrentStartPos, stepP), currentposition, stepP);
                transform.rotation = Quaternion.Lerp(Quaternion.Lerp(mLastFrameRot, mCurrentStartRot, stepP), currentrotation, stepP);

                now = Time.time + (i / (stepNum - 1f) - 1) * Time.deltaTime;
//		        if (sections.Count == 0 || (sections[0].point - transform.position).sqrMagnitude > minDistance * minDistance)
                {        //if (sections.Count == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance) {
                    //just add
                    TronTrailSection section = new TronTrailSection();
                    section.point = transform.position;
                    if (alwaysUp)
                    {
                        section.upDir = Vector3.up;
                    }
                    else
                    {
                        section.upDir = transform.TransformDirection(Vector3.up);
                    }

                    section.time = now;
                    sections.Insert(0, section);
                }
            }
            mLastFramePos = transform.position;
            mLastFrameRot = transform.rotation;
        }
    }
Example #6
0
 public void Itterate(float itterateTime)
 {
     this.position = base.transform.position;
     this.now      = itterateTime;
     if (this.sections.Count == 0 || (this.sections[0].point - this.position).sqrMagnitude > this.minDistance * this.minDistance)
     {
         TronTrailSection tronTrailSection = new TronTrailSection();
         tronTrailSection.point = this.position;
         if (this.alwaysUp)
         {
             tronTrailSection.upDir = Vector3.up;
         }
         else
         {
             tronTrailSection.upDir = base.transform.TransformDirection(Vector3.up);
         }
         tronTrailSection.time = this.now;
         this.sections.Insert(0, tronTrailSection);
     }
 }
    public void Itterate(float itterateTime)
    {
        // ** call everytime you sample animation **

        position = transform.position;
        now = itterateTime;

        // Add a new trail section
        if (sections.Count == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance) {
            TronTrailSection section = new TronTrailSection();
            section.point = position;
            if (alwaysUp)
                section.upDir = Vector3.up;
            else
                section.upDir = transform.TransformDirection(Vector3.up);

            section.time = now;
            sections.Insert(0, section);

        }
    }
Example #8
0
    public void Itterate(float itterateTime)
    {
        position = transform.position;
        now      = itterateTime;

        if (sections.Count == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
        {
            TronTrailSection section = new TronTrailSection();
            section.point = position;
            if (alwaysUp)
            {
                section.upDir = Vector3.up;
            }
            else
            {
                section.upDir = transform.TransformDirection(Vector3.up);
            }

            section.time = now;
            sections.Insert(0, section);
        }
    }
Example #9
0
    public void UpdateTrail(float currentTime)
    {
        //20150520谭志恒:有镜头则更新层,防QTE状态镜头的层有变
        UpdateLayer();

        // 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;
        }

        int vertexCountPerSection = sectionData.points.Length;

        vertices = new Vector3[sections.Count * vertexCountPerSection];
        uv       = new Vector2[sections.Count * vertexCountPerSection];
        //
        TronTrailSection currentSection = sections[0];
        //
        // Use matrix instead of transform.TransformPoint for performance reasons
        Matrix4x4 localSpaceTransform = transform.worldToLocalMatrix;

        // 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);
            }

            for (int j = 0; j < vertexCountPerSection; j++)
            {
                vertices[i * vertexCountPerSection + j] = localSpaceTransform.MultiplyPoint(currentSection.points[j]);
                uv[i * vertexCountPerSection + j]       = new Vector2(u, (float)j / (vertexCountPerSection - 1));
            }
        }

        int[] triangles = new int[(sections.Count - 1) * (vertexCountPerSection - 1) * 2 * 3];

        for (int i = 0; i < sections.Count - 1; i++)
        {
            for (int j = 0; j < vertexCountPerSection - 1; j++)
            {
                int lbIdx = i * vertexCountPerSection + j;
                int ltIdx = (i + 1) * vertexCountPerSection + j;

                int startTriIdx = (i * (vertexCountPerSection - 1) + j) * 6;
                triangles[startTriIdx + 0] = lbIdx;
                triangles[startTriIdx + 1] = lbIdx + 1;
                triangles[startTriIdx + 2] = ltIdx;

                triangles[startTriIdx + 3] = ltIdx;
                triangles[startTriIdx + 4] = lbIdx + 1;
                triangles[startTriIdx + 5] = ltIdx + 1;
            }
        }

        mesh.vertices  = vertices;
        mesh.uv        = uv;
        mesh.triangles = triangles;
    }
Example #10
0
 public void UpdateTrail(float currentTime, float deltaTime)
 {
     this.mesh.Clear();
     while (this.sections.Count > 0 && currentTime > this.sections[this.sections.Count - 1].time + this.time)
     {
         this.sections.RemoveAt(this.sections.Count - 1);
     }
     if (this.sections.Count < 2)
     {
         return;
     }
     this.vertices            = new Vector3[this.sections.Count * 2];
     this.colors              = new Color[this.sections.Count * 2];
     this.uv                  = new Vector2[this.sections.Count * 2];
     this.currentSection      = this.sections[0];
     this.localSpaceTransform = base.transform.worldToLocalMatrix;
     for (int i = 0; i < this.sections.Count; i++)
     {
         this.currentSection = this.sections[i];
         float num = 0f;
         if (i != 0)
         {
             num = Mathf.Clamp01((currentTime - this.currentSection.time) / this.time);
         }
         Vector3 upDir = this.currentSection.upDir;
         this.vertices[i * 2]     = this.localSpaceTransform.MultiplyPoint(this.currentSection.point);
         this.vertices[i * 2 + 1] = this.localSpaceTransform.MultiplyPoint(this.currentSection.point + upDir * this.height);
         this.uv[i * 2]           = new Vector2(num, 0f);
         this.uv[i * 2 + 1]       = new Vector2(num, 1f);
         Color color = Color.Lerp(this.startColor, this.endColor, num);
         this.colors[i * 2]     = color;
         this.colors[i * 2 + 1] = color;
     }
     int[] array = new int[(this.sections.Count - 1) * 2 * 3];
     for (int j = 0; j < array.Length / 6; j++)
     {
         array[j * 6]     = j * 2;
         array[j * 6 + 1] = j * 2 + 1;
         array[j * 6 + 2] = j * 2 + 2;
         array[j * 6 + 3] = j * 2 + 2;
         array[j * 6 + 4] = j * 2 + 1;
         array[j * 6 + 5] = j * 2 + 3;
     }
     this.mesh.vertices  = this.vertices;
     this.mesh.colors    = this.colors;
     this.mesh.uv        = this.uv;
     this.mesh.triangles = array;
     if (this.time > this.desiredTime)
     {
         this.time -= deltaTime * this.timeTransitionSpeed;
         if (this.time <= this.desiredTime)
         {
             this.time = this.desiredTime;
         }
     }
     else if (this.time < this.desiredTime)
     {
         this.time += deltaTime * this.timeTransitionSpeed;
         if (this.time >= this.desiredTime)
         {
             this.time = this.desiredTime;
         }
     }
 }
    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;

        // 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);

            // 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;
        }
    }
Example #12
0
    public void UpdateTrail(float deltaTime)    // ** call once a frame **
    {
        if (time < 0.0001f)
        {
            return;
        }
        if (life > currentlife)
        {
            currentlife += deltaTime;
            if (currentlife < 0)
            {
                return;
            }
        }
        else
        {
            ClearTrail();
            return;
        }
        // Rebuild the mesh
        mesh.Clear();
        //
        // Remove old sections
        while (sections.Count > 0 && currentlife > 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;
        }
        //
        int secCount = Mathf.Max(sections.Count, 4);

        vertices  = new Vector3[secCount];
        vertices2 = new Vector3[secCount];

        /*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;

        // 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 /*)*/;
            vertices2[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);
             *
             * // fade colors out over time
             * Color interpolatedColor = Color.Lerp(startColor, endColor, u);
             * colors[i * 2 + 0] = interpolatedColor;
             * colors[i * 2 + 1] = interpolatedColor;*/
        }

        for (int i = sections.Count; i < 4; i++)
        {
            vertices[i]  = vertices[sections.Count - 1];
            vertices2[i] = vertices2[sections.Count - 1];
        }

        float totalLen = 0.0f;

        for (int i = 0; i < sections.Count - 1; i++)
        {
            totalLen += sections[i].length;
        }

        int lNum = (int)(totalLen / maxDistance);

        if (lNum < secCount - 1)
        {
            lNum = secCount - 1;
        }

        if (lNum > 256)
        {
            lNum = 256;
        }

        int   k1    = 4;
        int   num   = secCount;
        float delta = 1.0f;
        float tv    = 0.0f;

        float[] bspT = new float[num + k1];

        for (int i = k1 - 1; i < num + k1 - 3; i++)
        {
            bspT[i] = tv;
            tv     += delta;
        }
        for (int i = 0; i < k1 - 1; i++)
        {
            bspT[i] = bspT[3];
        }
        for (int i = num + k1 - 3; i < num + k1; i++)
        {
            bspT[i] = bspT[num];
        }

        Vector3[] pointBuffer = new Vector3[lNum + 1];
        bspLine(ref vertices, ref bspT, num - 1, k1, lNum, ref pointBuffer);
        Vector3[] pointBuffer2 = new Vector3[lNum + 1];
        bspLine(ref vertices2, ref bspT, num - 1, k1, lNum, ref pointBuffer2);

        vertices = new Vector3[lNum * 2 + 2];

        colors = new Color[lNum * 2 + 2];
        uv     = new Vector2[lNum * 2 + 2];

        float maxU = 0.0f;

        if (sections.Count > 0)
        {
            maxU = Mathf.Clamp01((currentlife - sections[sections.Count - 1].time) / time);
        }

        for (int i = 0; i < lNum + 1; i++)
        {
            vertices[i * 2]     = localSpaceTransform.MultiplyPoint(pointBuffer[i]);
            vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(pointBuffer2[i]);

            float u = (i / ((float)lNum)) * maxU;
            uv[i * 2 + 0] = new Vector2(u, 0);
            uv[i * 2 + 1] = new Vector2(u, 1);

            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*/ lNum) * 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;
         * }*/
    }
Example #13
0
    public void UpdateTrail(float currentTime, float deltaTime)
    {
        mesh.Clear();

        while (sections.Count > 0 && currentTime > sections[sections.Count - 1].time + time)
        {
            sections.RemoveAt(sections.Count - 1);
        }

        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];
        localSpaceTransform = transform.worldToLocalMatrix;


        for (var i = 0; i < sections.Count; i++)
        {
            currentSection = sections[i];

            float u = 0.0f;
            if (i != 0)
            {
                u = Mathf.Clamp01((currentTime - currentSection.time) / time);
            }
            Vector3 upDir = currentSection.upDir;


            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);

            Color interpolatedColor = Color.Lerp(startColor, endColor, u);
            colors[i * 2 + 0] = interpolatedColor;
            colors[i * 2 + 1] = interpolatedColor;
        }

        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;
        }

        mesh.vertices  = vertices;
        mesh.colors    = colors;
        mesh.uv        = uv;
        mesh.triangles = triangles;

        if (time > desiredTime)
        {
            time -= deltaTime * timeTransitionSpeed;
            if (time <= desiredTime)
            {
                time = desiredTime;
            }
        }
        else if (time < desiredTime)
        {
            time += deltaTime * timeTransitionSpeed;
            if (time >= desiredTime)
            {
                time = desiredTime;
            }
        }
    }
Example #14
0
    void LateUpdate()
    {
        Vector3 position = transform.position;
        float now = Time.time;

        // Remove old sections
        while (sections.Count > 0 && now > sections[sections.Count - 1].time + time) {
            sections.RemoveAt(sections.Count-1);
        }

        // Add a new trail section
        if (sections.Count == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
        {
            TronTrailSection section = new TronTrailSection();
            section.point = position;
            if (alwaysUp)
                section.upDir = Vector3.up;
            else
                section.upDir = transform.TransformDirection(Vector3.up);
            section.time = now;
            sections.Insert(0,section);
        }

        // Rebuild the mesh
        Mesh mesh = GetComponent<MeshFilter>().mesh;
        mesh.Clear();

        // We need at least 2 sections to create the line
        if (sections.Count < 2)
            return;

        Vector3[] vertices = new Vector3[sections.Count * 2];
        Color[] colors = new Color[sections.Count * 2];
        Vector2[] uv = new Vector2[sections.Count * 2];

        TronTrailSection previousSection = sections[0];
        TronTrailSection currentSection = sections[0];

        // Use matrix instead of transform.TransformPoint for performance reasons
        Matrix4x4 localSpaceTransform = transform.worldToLocalMatrix;

        // Generate vertex, uv and colors
        for (int i=0;i<sections.Count;i++)
        {
            previousSection = currentSection;
            currentSection = sections[i];
            // Calculate u for texture uv and color interpolation
            float u = 0.0f;
            if (i != 0)
                u = Mathf.Clamp01 ((Time.time - 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);

            // 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;
    }
Example #15
0
    private void LateUpdate()
    {
        Vector3 position = transform.position;
        float   now      = Time.time;

        // Remove old sections
        while (sections.Count > 0 && now > sections[sections.Count - 1].time + time)
        {
            sections.RemoveAt(sections.Count - 1);
        }

        // Add a new trail section
        if (sections.Count == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
        {
            TronTrailSection section = new TronTrailSection();
            section.point = position;
            if (alwaysUp)
            {
                section.upDir = Vector3.up;
            }
            else
            {
                section.upDir = transform.TransformDirection(Vector3.up);
            }

            section.time = now;

            sections.Insert(0, section);
        }

        // Rebuild the mesh
        Mesh mesh = GetComponent <MeshFilter>().mesh;

        mesh.Clear();

        // We need at least 2 sections to create the line
        if (sections.Count < 2)
        {
            return;
        }

        Vector3[] vertices = new Vector3[sections.Count * 2];
        Color[]   colors   = new Color[sections.Count * 2];
        Vector2[] uv       = new Vector2[sections.Count * 2];

        TronTrailSection previousSection = sections[0];
        TronTrailSection currentSection  = sections[0];

        // Use matrix instead of transform.TransformPoint for performance reasons
        Matrix4x4 localSpaceTransform = transform.worldToLocalMatrix;

        // Generate vertex, uv and colors
        for (int i = 0; i < sections.Count; i++)
        {
            previousSection = currentSection;
            currentSection  = sections[i];
            // Calculate u for texture uv and color interpolation
            float u = 0.0f;
            if (i != 0)
            {
                u = Mathf.Clamp01((Time.time - 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);

            // 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;
    }
Example #16
0
    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;

        // 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);

            // 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;
            }
        }
    }
Example #17
0
 public void Start()
 {
     sections = new TronTrailSection[sectionCount];
     for (int i = 0; i < sectionCount; i++){
         sections[i] = new TronTrailSection();
     }
     vertices = new Vector3[sectionCount * 2];
     colors = new Color[sectionCount * 2];
     uv = new Vector2[sectionCount * 2];
     triangles = new int[(sectionCount - 1) * 2 * 3];
 }
Example #18
0
    public void LateUpdate()
    {
        if (candraw) {
            //transform.position = new Vector3(Input.mousePosition.x,Input.mousePosition.y,20);
            StartCoroutine(ScreenCoordinates());
        }
        ticker += Time.deltaTime;
        if (ticker > time){
            ticker -= time;
            sections[head].point = transform.position;
            if (alwaysUp)
                sections[head].upDir = Vector3.left;
            else
                sections[head].upDir = transform.TransformDirection(Vector3.left);

            head++;
            if (head >= sectionCount){
                head = 0;
            }
        }

        // Rebuild the mesh
        Mesh mesh = (GetComponent(typeof(MeshFilter)) as MeshFilter).mesh;
        mesh.Clear();

        // Use matrix instead of transform.TransformPoint for performance reasons
        Matrix4x4 localSpaceTransform = transform.worldToLocalMatrix;

        int j = head-1;
        if (j < 0)
            j = sectionCount - 1;

        for(int i=0;i<sectionCount;i++){
        //foreach (TronTrailSection currentSection in sections){
            // Calculate u for texture uv and color interpolation
            currentSection = sections[j];

            float u = (float)i / (float)sectionCount;// 0.0f;

            // 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].x = u;
            uv[i * 2 + 0].y = 0;
            uv[i * 2 + 1].x = u;
            uv[i * 2 + 1].y = 1.0f;

            // fade colors out over time
            Color interpolatedColor = Color.Lerp(startColor, endColor, u);
            colors[i * 2 + 0] = interpolatedColor;
            colors[i * 2 + 1] = interpolatedColor;

            j--;
            if (j < 0)
                j = sectionCount - 1;
        }

        // Generate triangles indices
        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;
    }
Example #19
0
    void Update()
    {
        if (isOn)
        {
            sections.AddLast(new TronTrailSection(startPoint.position, endPoint.position, Time.time));
        }

        while (sections.Count > 0 && (Time.time - sections.First.Value.time) > lifeTime)
        {
            sections.RemoveFirst();
        }

        if (sections.Count < 2)
        {
            meshRenderer.enabled = false;
            return;
        }
        else
        {
            meshRenderer.enabled = true;
        }

        //
        // Use matrix instead of transform.TransformPoint for performance reasons
        Matrix4x4 localSpaceTransform = transform.worldToLocalMatrix;

        var itr = sections.First;

        for (var i = 0; i < sections.Count; i++)
        {
            TronTrailSection currentSection = itr.Value;

            float u = Mathf.Clamp01((Time.time - currentSection.time) / lifeTime);

            vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.startP);
            vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.endP);

            uv[i * 2 + 0] = new Vector2(u, 0);
            uv[i * 2 + 1] = new Vector2(u, 1);

            itr = itr.Next;
        }

        int activeTriangleCount = sections.Count - 1;

        for (int i = 0; i < activeTriangleCount; 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;
        }
        int lastActiveTriIdx = (sections.Count - 1) * 6 - 1;

        for (int i = lastActiveTriIdx + 1; i < triangles.Length; ++i) // hide spare trail spaces
        {
            triangles[i] = triangles[lastActiveTriIdx];
        }

        // Assign to mesh
        mesh.vertices  = vertices;
        mesh.colors    = colors;
        mesh.uv        = uv;
        mesh.triangles = triangles;
    }