Exemple #1
0
    void OnPostRender()
    {
        if (!settings)
        {
            return;
        }

        if (draw)
        {
            CreateLineMaterial();
            lineMaterial.SetPass(0);

            AdvectedScalesFlatland[] ars = GetComponents <AdvectedScalesFlatland>();
            if (ars.Length == 0)
            {
                return;
            }

            // insertion sort
            for (int i = 1; i < ars.Length; i++)
            {
                for (int j = i - 1; j >= 0; j--)
                {
                    if (ars[j].m_radiusIndex <= ars[j + 1].m_radiusIndex)
                    {
                        break;
                    }

                    AdvectedScalesFlatland temp = ars[j + 1];
                    ars[j + 1] = ars[j];
                    ars[j]     = temp;
                }
            }

            Quaternion q;

            GL.PushMatrix();
            GL.LoadProjectionMatrix(GetComponent <Camera> ().projectionMatrix);

            float depth  = 10f + 5f / GetComponent <Camera> ().aspect;
            float height = -5f;

            Vector3 p1 = transform.position;
            p1 = transform.TransformPoint(Quaternion.Euler(-90f, 0f, 0f) * (transform.InverseTransformPoint(p1) * 0.4f)) + depth * transform.forward + height * transform.up;

            Vector3 p2 = transform.position + Quaternion.AngleAxis(getTheta(0) * Mathf.Rad2Deg, -Vector3.up) * transform.right * (figureRadius + 2f) * ars[1].scales_norm[0] * settings.debugDrawScale;
            p2 = transform.TransformPoint(Quaternion.Euler(-90f, 0f, 0f) * (transform.InverseTransformPoint(p2) * 0.4f)) + depth * transform.forward + height * transform.up;

            Vector3 p3 = transform.position + Quaternion.AngleAxis(getTheta(settings.scaleCount - 1) * Mathf.Rad2Deg, -Vector3.up) * transform.right * (figureRadius + 2f) * ars[1].scales_norm[settings.scaleCount - 1] * settings.debugDrawScale;
            p3 = transform.TransformPoint(Quaternion.Euler(-90f, 0f, 0f) * (transform.InverseTransformPoint(p3) * 0.4f)) + depth * transform.forward + height * transform.up;


            GL.Begin(GL.LINES);
            GL.Color(Color.white);
            GL.Vertex(p1);
            GL.Vertex(p2);
            GL.Vertex(p1);
            GL.Vertex(p3);

            for (int i = 1; i < settings.scaleCount; i++)
            {
                float prevTheta = getTheta(i - 1);
                float thisTheta = getTheta(i);

                q = Quaternion.AngleAxis(prevTheta * Mathf.Rad2Deg, -Vector3.up);
                Vector3 prevRd = q * transform.right;

                q = Quaternion.AngleAxis(thisTheta * Mathf.Rad2Deg, -Vector3.up);
                Vector3 thisRd = q * transform.right;

                float scale = settings.debugDrawScale * 1.0f;

                // ray march
                float t;
                float Dt;
                float wt;
                bool  even;
                FirstT(out t, out Dt, out wt, out even);

                float fadeDistance = 6f;

                for ( ; t <= figureRadius + fadeDistance;)
                {
                    // get current ray scale
                    float prevRayScale = Mathf.Lerp(ars[0].scales_norm[i - 1], ars[1].scales_norm[i - 1], t / figureRadius);
                    float curRayScale  = Mathf.Lerp(ars[0].scales_norm[i], ars[1].scales_norm[i], t / figureRadius);

                    // get scaled position
                    p1 = transform.position + prevRd * t * prevRayScale * scale;
                    p2 = transform.position + thisRd * t * curRayScale * scale;

                    p1 = transform.TransformPoint(Quaternion.Euler(-90f, 0f, 0f) * (transform.InverseTransformPoint(p1) * 0.4f)) + depth * transform.forward + height * transform.up;
                    p2 = transform.TransformPoint(Quaternion.Euler(-90f, 0f, 0f) * (transform.InverseTransformPoint(p2) * 0.4f)) + depth * transform.forward + height * transform.up;

                    Color col = Color.white;
                    if (adaptive)
                    {
                        col.a = Mathf.Clamp01(even ? (2.0f - wt) : wt);
                    }

                    if (t > fadeDistance)
                    {
                        col.a *= 1f - (t - figureRadius) / fadeDistance;
                    }

                    GL.Color(col);
                    GL.Vertex(p1);
                    GL.Vertex(p2);

                    NextT(ref t, ref Dt, ref wt, ref even);
                }
            }
            GL.End();
            GL.PopMatrix();
        }
    }
Exemple #2
0
    void LateUpdate()
    {
        AdvectedScalesFlatland[] ars = viewer.GetComponents <AdvectedScalesFlatland> ();
        if (ars.Length == 0)
        {
            return;
        }

        // insertion sort
        for (int i = 1; i < ars.Length; i++)
        {
            for (int j = i - 1; j >= 0; j--)
            {
                if (ars[j].m_radiusIndex <= ars[j + 1].m_radiusIndex)
                {
                    break;
                }

                AdvectedScalesFlatland temp = ars[j + 1];
                ars[j + 1] = ars[j];
                ars[j]     = temp;
            }
        }

        MeshFilter[] meshes = GetComponentsInChildren <MeshFilter>();

        if (meshInstances == null || meshInstances.Length != meshes.Length)
        {
            meshInstances = new Mesh[meshes.Length];

            int i = 0;
            foreach (MeshFilter mf in meshes)
            {
                // this is to avoid the error about causing the mesh to instantiate in the editor - manually
                // instance the mesh in this case.
                                #if UNITY_EDITOR
                if (!UnityEditor.EditorApplication.isPlaying)
                {
                    meshInstances[i++] = mf.mesh = Mesh.Instantiate(mf.sharedMesh) as Mesh;
                }
                else
                                #endif
                meshInstances[i++] = mf.mesh;
            }
        }

        int k = 0;
        foreach (Mesh mesh in meshInstances)
        {
            Vector3[] verts = mesh.vertices;
            Vector2[] uv    = mesh.uv;

            for (int i = 0; i < uv.Length; i++)
            {
                float r0 = -1;

                Vector3 pos   = meshes[k].transform.TransformPoint(verts[i]);
                float   theta = -CloudsBase.halfFov_horiz_rad * pos.x / 5.0f + Mathf.PI / 2.0f;

                r0 = ars[0].sampleR(theta) / ars[0].m_radius;

                float r1;

                if (ars.Length > 1 && AdvectedScalesSettings.instance.twoRSolution)
                {
                    r1 = ars[1].sampleR(theta) / ars[1].m_radius;
                }
                else
                {
                    r1 = r0;
                }

                uv[i] = new Vector2(r0, r1);
            }

            mesh.uv = uv;
            k++;
        }
    }