Esempio n. 1
0
    /// <summary>
    /// Normalize the specified spline.
    /// </summary>

    public static SplineV Normalize(SplineV initial, int subdivisions)
    {
        float key    = 0.0f;
        float start  = initial.startTime;
        float length = initial.endTime - start;

        // Add the first key
        SplineV spline = new SplineV();

        spline.AddKey(key, initial.start);

        // Add remaining values using the traveled distance as keys
        for (int i = 1; i < subdivisions; ++i)
        {
            float f0 = (float)(i - 1) / subdivisions;
            float f1 = (float)(i) / subdivisions;

            Vector3 v0 = initial.Sample(start + f0 * length, SplineV.SampleType.Spline);
            Vector3 v1 = initial.Sample(start + f1 * length, SplineV.SampleType.Spline);

            key += (v1 - v0).magnitude;
            spline.AddKey(key, v1);
        }
        return(spline);
    }
Esempio n. 2
0
    /// <summary>
    /// Copies the trade route path from the specified trade route.
    /// </summary>

    public void CopyPath(SplineV sp)
    {
        mRebuild = true;
        mOriginal.Clear();
        foreach (SplineV.CtrlPoint cp in sp.list)
        {
            mOriginal.AddKey(cp.mTime, cp.mVal);
        }
    }
Esempio n. 3
0
    public void Restart()
    {
        if (controlPoints != null)
        {
            mPos = new SplineV();
            mRot = new SplineQ();

            float time = Time.time;

            foreach (Transform t in controlPoints)
            {
                mPos.AddKey(time, t.position);
                mRot.AddKey(time, t.rotation);
                time += timePerPoint;
            }
        }
    }
Esempio n. 4
0
    public void Restart()
    {
        if (controlPoints != null)
        {
            mPos = new SplineV();
            mRot = new SplineQ();

            float time = Time.time;

            foreach (Transform t in controlPoints)
            {
                mPos.AddKey(time, t.position);
                mRot.AddKey(time, t.rotation);
                time += timePerPoint;
            }
        }
    }
Esempio n. 5
0
    /// <summary>
    /// Creates a trade route mesh.
    /// </summary>

    void CreateMesh(SplineV initial, Vector3 offset, float width, int subdivisions)
    {
        mMesh.Clear();
        if (initial.list.Count < 2)
        {
            return;
        }

        SplineV spline = SplineV.Normalize(initial, subdivisions * 4);

        float start  = spline.startTime;
        float length = spline.endTime - start;

        // We will need the spline's center for tooltip purposes
        mTooltipPos = spline.Sample(start + length * 0.5f, SplineV.SampleType.Spline);

        List <Vector3> v     = new List <Vector3>();
        List <Vector3> n     = new List <Vector3>();
        List <Vector2> uv    = new List <Vector2>();
        List <int>     faces = new List <int>();

        ++subdivisions;

        for (int i = 0; i < subdivisions; ++i)
        {
            float f0 = (float)(i - 1) / subdivisions;
            float f1 = (float)(i) / subdivisions;
            float f2 = (float)(i + 1) / subdivisions;
            float f3 = (float)(i + 2) / subdivisions;

            Vector3 s0 = spline.Sample(start + f0 * length, SplineV.SampleType.Linear);
            Vector3 s1 = spline.Sample(start + f1 * length, SplineV.SampleType.Linear);
            Vector3 s2 = spline.Sample(start + f2 * length, SplineV.SampleType.Linear);
            Vector3 s3 = spline.Sample(start + f3 * length, SplineV.SampleType.Linear);

            Vector3 dir0 = (s2 - s0).normalized;
            Vector3 dir1 = (s3 - s1).normalized;

            // Cross(dir, up)
            Vector3 tan0 = new Vector3(-dir0.z, 0f, dir0.x);
            Vector3 tan1 = new Vector3(-dir1.z, 0f, dir1.x);

            tan0 *= width;
            tan1 *= width;

            Vector3 v0 = s1 - tan0;
            Vector3 v1 = s2 - tan1;
            Vector3 v2 = s2 + tan1;
            Vector3 v3 = s1 + tan0;

            v.Add(offset + v1);
            n.Add(Vector3.up);
            uv.Add(new Vector2(1.0f, f2));

            v.Add(offset + v0);
            n.Add(Vector3.up);
            uv.Add(new Vector2(1.0f, f1));

            v.Add(offset + v3);
            n.Add(Vector3.up);
            uv.Add(new Vector2(0.0f, f1));

            v.Add(offset + v2);
            n.Add(Vector3.up);
            uv.Add(new Vector2(0.0f, f2));
        }

        for (int i = 0; i < v.Count; i += 4)
        {
            faces.Add(i);
            faces.Add(i + 1);
            faces.Add(i + 2);
            faces.Add(i + 2);
            faces.Add(i + 3);
            faces.Add(i);
        }

        // Assign the mesh data
        mMesh.vertices  = v.ToArray();
        mMesh.normals   = n.ToArray();
        mMesh.uv        = uv.ToArray();
        mMesh.triangles = faces.ToArray();
    }
Esempio n. 6
0
    /// <summary>
    /// Update this instance.
    /// </summary>

    void Update()
    {
        bool  wasVisible = mAlpha > 0.001f;
        float factor     = Mathf.Min(1f, Time.deltaTime * 5f);

        mAlpha = Mathf.Lerp(mAlpha, targetAlpha * globalAlpha, factor);

        if (mAlpha > 0.001f)
        {
            if (mRebuild)
            {
                if (mOriginal.isValid)
                {
                    mRebuild = false;

                    if (mMesh == null)
                    {
                        mMesh      = new Mesh();
                        mMesh.name = "Trade Route " + GetInstanceID();
                    }

                    if (mFilter == null)
                    {
                        mFilter      = gameObject.AddComponent <MeshFilter>();
                        mFilter.mesh = mMesh;
                    }

                    if (mMat == null)
                    {
                        Shader shader = Shader.Find("Transparent/Diffuse");
                        mMat             = new Material(shader);
                        mMat.name        = "Trade Route";
                        mMat.mainTexture = texture;
                    }

                    if (mRen == null)
                    {
                        mRen                = gameObject.AddComponent <MeshRenderer>();
                        mRen.material       = mMat;
                        mRen.castShadows    = false;
                        mRen.receiveShadows = false;
                    }

                    // Find the center of the spline
                    Vector3 center = Vector3.zero;
                    foreach (SplineV.CtrlPoint cp in mOriginal.list)
                    {
                        center += cp.mVal;
                    }
                    center *= 1.0f / mOriginal.list.Count;

                    // Reposition the trade route
                    transform.position = center;

                    // Re-create the mesh
                    mLength = mOriginal.GetMagnitude();
                    int subdivisions = Mathf.RoundToInt(mLength);
                    CreateMesh(mOriginal, -center, 0.15f, subdivisions);

                    // Create the normalized path that will be used for sampling
                    mNormalized = SplineV.Normalize(mOriginal, subdivisions);
                    mLength     = mNormalized.GetMagnitude();
                }
                else if (mMesh != null)
                {
                    mMesh.Clear();
                    if (mRen != null)
                    {
                        mRen.enabled = false;
                    }
                }
            }

            // Update the material color
            if (mRen != null)
            {
                mRen.enabled        = true;
                mRen.material.color = isValid ? new Color(1f, 1f, 1f, mAlpha * 0.5f) : new Color(1f, 1f, 1f, mAlpha * 0.25f);
            }
        }
        else if (wasVisible)
        {
            mAlpha       = 0f;
            mRen.enabled = false;
        }
    }
Esempio n. 7
0
    /// <summary>
    /// Update this instance.
    /// </summary>
    void Update()
    {
        bool wasVisible = mAlpha > 0.001f;
        float factor = Mathf.Min(1f, Time.deltaTime * 5f);
        mAlpha = Mathf.Lerp(mAlpha, targetAlpha * globalAlpha, factor);

        if (mAlpha > 0.001f)
        {
            if (mRebuild)
            {
                if (mOriginal.isValid)
                {
                    mRebuild = false;

                    if (mMesh == null)
                    {
                        mMesh = new Mesh();
                        mMesh.name = "Trade Route " + GetInstanceID();
                    }

                    if (mFilter == null)
                    {
                        mFilter = gameObject.AddComponent<MeshFilter>();
                        mFilter.mesh = mMesh;
                    }

                    if (mMat == null)
                    {
                        Shader shader = Shader.Find("Transparent/Diffuse");
                        mMat = new Material(shader);
                        mMat.name = "Trade Route";
                        mMat.mainTexture = texture;
                    }

                    if (mRen == null)
                    {
                        mRen = gameObject.AddComponent<MeshRenderer>();
                        mRen.material = mMat;
                        mRen.castShadows = false;
                        mRen.receiveShadows = false;
                    }

                    // Find the center of the spline
                    Vector3 center = Vector3.zero;
                    foreach (SplineV.CtrlPoint cp in mOriginal.list) center += cp.mVal;
                    center *= 1.0f / mOriginal.list.Count;

                    // Reposition the trade route
                    transform.position = center;

                    // Re-create the mesh
                    mLength = mOriginal.GetMagnitude();
                    int subdivisions = Mathf.RoundToInt(mLength);
                    CreateMesh(mOriginal, -center, 0.15f, subdivisions);

                    // Create the normalized path that will be used for sampling
                    mNormalized = SplineV.Normalize(mOriginal, subdivisions);
                    mLength = mNormalized.GetMagnitude();
                }
                else if (mMesh != null)
                {
                    mMesh.Clear();
                    if (mRen != null) mRen.enabled = false;
                }
            }

            // Update the material color
            if (mRen != null)
            {
                mRen.enabled = true;
                mRen.material.color = isValid ? new Color(1f, 1f, 1f, mAlpha * 0.5f) : new Color(1f, 1f, 1f, mAlpha * 0.25f);
            }
        }
        else if (wasVisible)
        {
            mAlpha = 0f;
            mRen.enabled = false;
        }
    }
Esempio n. 8
0
    /// <summary>
    /// Creates a trade route mesh.
    /// </summary>
    void CreateMesh(SplineV initial, Vector3 offset, float width, int subdivisions)
    {
        mMesh.Clear();
        if (initial.list.Count < 2) return;

        SplineV spline = SplineV.Normalize(initial, subdivisions * 4);

        float start = spline.startTime;
        float length = spline.endTime - start;

        // We will need the spline's center for tooltip purposes
        mTooltipPos = spline.Sample(start + length * 0.5f, SplineV.SampleType.Spline);

        List<Vector3> v = new List<Vector3>();
        List<Vector3> n = new List<Vector3>();
        List<Vector2> uv = new List<Vector2>();
        List<int> faces = new List<int>();

        ++subdivisions;

        for (int i = 0; i < subdivisions; ++i)
        {
            float f0 = (float)(i - 1) / subdivisions;
            float f1 = (float)(i    ) / subdivisions;
            float f2 = (float)(i + 1) / subdivisions;
            float f3 = (float)(i + 2) / subdivisions;

            Vector3 s0 = spline.Sample(start + f0 * length, SplineV.SampleType.Linear);
            Vector3 s1 = spline.Sample(start + f1 * length, SplineV.SampleType.Linear);
            Vector3 s2 = spline.Sample(start + f2 * length, SplineV.SampleType.Linear);
            Vector3 s3 = spline.Sample(start + f3 * length, SplineV.SampleType.Linear);

            Vector3 dir0 = (s2 - s0).normalized;
            Vector3 dir1 = (s3 - s1).normalized;

            // Cross(dir, up)
            Vector3 tan0 = new Vector3(-dir0.z, 0f, dir0.x);
            Vector3 tan1 = new Vector3(-dir1.z, 0f, dir1.x);

            tan0 *= width;
            tan1 *= width;

            Vector3 v0 = s1 - tan0;
            Vector3 v1 = s2 - tan1;
            Vector3 v2 = s2 + tan1;
            Vector3 v3 = s1 + tan0;

            v.Add(offset + v1);
            n.Add(Vector3.up);
            uv.Add(new Vector2(1.0f, f2));

            v.Add(offset + v0);
            n.Add(Vector3.up);
            uv.Add(new Vector2(1.0f, f1));

            v.Add(offset + v3);
            n.Add(Vector3.up);
            uv.Add(new Vector2(0.0f, f1));

            v.Add(offset + v2);
            n.Add(Vector3.up);
            uv.Add(new Vector2(0.0f, f2));
        }

        for (int i = 0; i < v.Count; i += 4)
        {
            faces.Add(i);
            faces.Add(i+1);
            faces.Add(i+2);
            faces.Add(i+2);
            faces.Add(i+3);
            faces.Add(i);
        }

        // Assign the mesh data
        mMesh.vertices = v.ToArray();
        mMesh.normals = n.ToArray();
        mMesh.uv = uv.ToArray();
        mMesh.triangles = faces.ToArray();
    }
Esempio n. 9
0
 /// <summary>
 /// Copies the trade route path from the specified trade route.
 /// </summary>
 public void CopyPath(SplineV sp)
 {
     mRebuild = true;
     mOriginal.Clear();
     foreach (SplineV.CtrlPoint cp in sp.list) mOriginal.AddKey(cp.mTime, cp.mVal);
 }
Esempio n. 10
0
    /// <summary>
    /// Update this instance.
    /// </summary>

    public void Update()
    {
#if UNITY_EDITOR
        if (isEditMode && !Application.isPlaying)
        {
            if (KeyTradeRouteNodes != null)
            {
                for (int i = 0; i < KeyTradeRouteNodes.Count - 1; i++)
                {
                    Debug.DrawLine(KeyTradeRouteNodes[i], KeyTradeRouteNodes[i + 1]);
                }
            }
        }
        else
        {
#endif
        if (!Application.isPlaying)
        {
            return;
        }
        bool wasVisible = targetAlpha > 0.001f;
        float factor    = Mathf.Min(1f, Time.deltaTime * 5f);
        //mAlpha = Mathf.Lerp (mAlpha, targetAlpha * globalAlpha, factor);
        if (targetAlpha > 0.001f)
        {
            if (mRebuild)
            {
                if (mOriginal.isValid)
                {
                    mRebuild = false;
                    if (isNotRender)
                    {
                        Vector3 centerPos = Vector3.zero;
                        foreach (SplineV.CtrlPoint cp in mOriginal.list)
                        {
                            centerPos += cp.mVal;
                        }
                        centerPos  *= 1.0f / mOriginal.list.Count;
                        centerPos.y = 3f;
                        mLength     = mOriginal.GetMagnitude();
                        int divisions = Mathf.RoundToInt(mLength);
                        CreateMesh(mOriginal, -centerPos, tradeSize, divisions);

                        // Create the normalized path that will be used for sampling
                        mNormalized = SplineV.Normalize(mOriginal, divisions);
                        mLength     = mNormalized.GetMagnitude();
                        return;
                    }
                    if (mMesh == null)
                    {
                        mMesh      = new Mesh();
                        mMesh.name = "Trade Route " + GetInstanceID();
                    }

                    if (mFilter == null)
                    {
                        mFilter      = gameObject.AddComponent <MeshFilter> ();
                        mFilter.mesh = mMesh;
                    }

                    if (mMat == null)
                    {
//							Shader shader = Shader.Find ("Transparent/Diffuse");
                        Shader shader = Shader.Find("Mobile/Transparent/Vertex Color");

                        //						Shader shader = Shader.Find ("Toon/Basic");
                        mMat      = new Material(shader);
                        mMat.name = "Trade Route" + mMat.GetInstanceID();
                        //mMat.color = mColor;
                        mMat.mainTexture = texture;
                    }

                    if (mRen == null)
                    {
                        mRen                = gameObject.AddComponent <MeshRenderer> ();
                        mRen.material       = mMat;
                        mRen.castShadows    = false;
                        mRen.receiveShadows = false;
                    }
                    // Find the center of the spline
                    Vector3 center = Vector3.zero;
                    foreach (SplineV.CtrlPoint cp in mOriginal.list)
                    {
                        center += cp.mVal;
                    }
                    center  *= 1.0f / mOriginal.list.Count;
                    center.y = 3f;
                    // Reposition the trade route
                    //transform.position = center;
                    transform.localPosition = center;

                    // Re-create the mesh
                    mLength = mOriginal.GetMagnitude();
                    int subdivisions = Mathf.RoundToInt(mLength);
                    CreateMesh(mOriginal, -center, tradeSize, subdivisions);

                    // Create the normalized path that will be used for sampling
                    mNormalized = SplineV.Normalize(mOriginal, subdivisions);
                    mLength     = mNormalized.GetMagnitude();
                }
                else if (mMesh != null)
                {
                    mMesh.Clear();
                    if (mRen != null)
                    {
                        mRen.enabled = false;
                    }
                }
                // Update the material color
                if (mRen != null)
                {
                    mRen.enabled = true;
                    Color newColor = mColor;
//					if (isValid) {
//						newColor.a = 0.5f;
//					} else {
//						newColor.a = 0.25f;
//					}
                    //newColor.a = targetAlpha;
                    mRen.material.color = newColor;
                }
            }
        }
        else if (wasVisible)
        {
            mAlpha       = 0f;
            mRen.enabled = false;
        }
#if UNITY_EDITOR
    }
#endif
    }
Esempio n. 11
0
	/// <summary>
	/// Normalize the specified spline.
	/// </summary>
	
	public static SplineV Normalize (SplineV initial, int subdivisions)
	{
		float key = 0.0f;
		float start = initial.startTime;
		float length = initial.endTime - start;
		
		// Add the first key
		SplineV spline = new SplineV();
		spline.AddKey(key, initial.start);
		
		// Add remaining values using the traveled distance as keys
		for (int i = 1; i < subdivisions; ++i)
		{
			float f0 = (float)(i - 1) / subdivisions;
			float f1 = (float)(i    ) / subdivisions;
			
			Vector3 v0 = initial.Sample(start + f0 * length, SplineV.SampleType.Spline);
			Vector3 v1 = initial.Sample(start + f1 * length, SplineV.SampleType.Spline);
			
			key += (v1 - v0).magnitude;
			spline.AddKey(key, v1);
		}
		return spline;
	}