Пример #1
0
    void LateUpdate()
    {
        if (AdvectedScalesSettings.instance.integrateForwardsMotion)
        {
            // this is a little bit awkward. we need to compute dist travelled based on the ray (radius) scale, otherwise it is meaningless.
            // however different rays have different scales. in this case we just pick the radius at the center. its possible that this could
            // be the average radius, i dont recall if i tried this.

            AdvectedScales[] ars = GetComponents <AdvectedScales>();
            if (ars.Length == 0)
            {
                ars = GetComponentsInChildren <AdvectedScales>();
            }
            AdvectedScales rVals = null;
            for (int i = 0; i < ars.Length; i++)
            {
                if (ars[i].m_radiusIndex == 0)
                {
                    rVals = ars[i];
                    break;
                }
            }

            if (rVals != null)
            {
                // need to know how fast samples will move forwards/backwards under pinning, so that we can hold them stationary.
                // we take the middle scale. this is assumed in the advection code as well, and the advection will then compensate
                // for non-uniform scale for different rays.
                m_forwardPinScale = rVals.MiddleScaleValue / rVals.m_radius;

                m_distTravelledForward += Vector3.Dot(transform.position - lastPos, transform.forward) / m_forwardPinScale;

                // this is actually a bit of a fudge for an issue in the shader where the OnBoundary() function seems to fail
                // when the integrator is negative (?), so this keeps it positive
                m_distTravelledForward = Mathf.Repeat(m_distTravelledForward, m_largestPosRayStep);
            }
        }

        // heuristic to compute curvature, based on trading error of translational motion vs rotational motion
        //float r = 5.0f; // radius at which to measure rot error
        //float theta = Mathf.PI/4.0f;
        //targetCurv = 1.0f/(vx*vx/(omega*omega*r*r*Mathf.Tan(theta)*Mathf.Tan(theta)) + 1.0f);

        lastPos = transform.position;
    }
Пример #2
0
    void OnPostRender()
    {
        if (!settings)
        {
            return;
        }

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

            AdvectedScales[] ars = GetComponents <AdvectedScales> ();
            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].radiusIndex <= ars[j + 1].radiusIndex)
                    {
                        break;
                    }

                    AdvectedScales 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();
        }
    }
Пример #3
0
    void LateUpdate()
    {
        AdvectedScales[] ars = viewer.GetComponents <AdvectedScales> ();
        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].radiusIndex <= ars[j + 1].radiusIndex)
                {
                    break;
                }

                AdvectedScales 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].radius;

                float r1;

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

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

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