Example #1
0
    //~CLutGenerator()
    //{
    //    #if UNITY_EDITOR
    //        Texture2D.DestroyImmediate(m_LutTex);
    //    #else
    //        Texture2D.Destroy(m_LutTex);
    //    #endif
    //}
    // maximum of 4 layers supported
    // layer 0 goes into color.r
    // layer 1 goes into color.g
    // layer 2 goes into color.b
    // layer 3 goes into color.a
    public void UpdateLutTex(CLutLayer[] lutLayers)
    {
        float step = 1.0f / 256;
        Color[] pixels = new Color[256 * 256];

        int layerCount = (lutLayers.Length >= 4 ? 4 : lutLayers.Length);

        for (int i = 0; i < layerCount; i++)
        {
            float fy = 0.0f; // the height in 0..1 range

            for (int height = 0; height < 256; height++)
            {
                if (fy >= lutLayers[i].minh && fy <= lutLayers[i].maxh)
                {
                    float fx = 0.0f; // the slope in 0..1 range
                    for (int slope = 0; slope < 256; slope++)
                    {
                        float slopeDiff = Mathf.Abs(lutLayers[i].slope - fx);
                        float value;

                        if (slopeDiff <= lutLayers[i].aperture)
                        {
                            float slopeFade = 1.0f - (slopeDiff / lutLayers[i].aperture);
                            float heightFade = 1.0f -(fy - lutLayers[i].minh) / (lutLayers[i].maxh - lutLayers[i].minh);
                            value = Mathf.Min(1.0f, Mathf.Max(0.0f, slopeFade * heightFade));
                        }
                        else
                        {
                            value = 0;
                        }

                        switch (i)
                        {
                            case 0:
                                pixels[height * 256 + slope].r = value;
                                break;

                            case 1:
                                pixels[height * 256 + slope].g = value;
                                break;

                            case 2:
                                pixels[height * 256 + slope].b = value;
                                break;

                            case 3:
                                pixels[height * 256 + slope].a = value;
                                break;
                        }

                        fx += step;
                    }
                }

                fy += step;
            }
        }

        m_LutTex.anisoLevel = 0;
        m_LutTex.filterMode = FilterMode.Bilinear;
        m_LutTex.wrapMode = TextureWrapMode.Clamp;

        // upload pixels
        m_LutTex.SetPixels(pixels);
        m_LutTex.Apply(false);

        //// ### SAVE TO DISK
        //byte[] bytes = m_LutTex.EncodeToPNG();
        //Debug.Log(bytes + " bytes in PNG");
        //string str = "d:/temp/lutTex.png";
        //if (System.IO.File.Exists(str)) System.IO.File.Delete(str);
        //System.IO.FileStream fs = new System.IO.FileStream(str, System.IO.FileMode.CreateNew);
        //System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
        //w.Write(bytes);
        //w.Close();
        //fs.Close();
    }
Example #2
0
    public void Rebuild()
    {
        CGlobal global = CGlobal.GetInstance();

        m_LutGen = new CLutGenerator();
        m_LutLayers[0] = new CLutLayer(m_Diffuse1LutLayer.minh, m_Diffuse1LutLayer.maxh, m_Diffuse1LutLayer.slope, m_Diffuse1LutLayer.aperture);
        m_LutLayers[1] = new CLutLayer(m_Diffuse2LutLayer.minh, m_Diffuse2LutLayer.maxh, m_Diffuse2LutLayer.slope, m_Diffuse2LutLayer.aperture);
        m_LutLayers[2] = new CLutLayer(m_Diffuse3LutLayer.minh, m_Diffuse3LutLayer.maxh, m_Diffuse3LutLayer.slope, m_Diffuse3LutLayer.aperture);
        m_LutLayers[3] = new CLutLayer(m_Diffuse4LutLayer.minh, m_Diffuse4LutLayer.maxh, m_Diffuse4LutLayer.slope, m_Diffuse4LutLayer.aperture);
        m_LutGen.UpdateLutTex(m_LutLayers);

        m_PatchConfig = new CPatchConfig(m_PatchQuality, m_NormalQuality, m_FilteredHeights);
        m_PatchManager = new CPatchManager(m_PatchConfig, this);
        global.Setup();

        // ---

        // destroy the previous planet tree
        DestroyPlanet();

        //
        // rebuild patch tree
        //

        m_Quadtrees.Add(new CQuadtree(new Vector3(0, 1, 0), new Vector3(0, 0, -1), this, m_ShapeTop));		// 0: top
        m_Quadtrees.Add(new CQuadtree(new Vector3(0, -1, 0), new Vector3(0, 0, 1), this, m_ShapeBottom));		// 1: bottom

        m_Quadtrees.Add(new CQuadtree(new Vector3(-1, 0, 0), new Vector3(0, 1, 0), this, m_ShapeLeft));		// 2: left
        m_Quadtrees.Add(new CQuadtree(new Vector3(1, 0, 0), new Vector3(0, 1, 0), this, m_ShapeRight));		// 3: right

        m_Quadtrees.Add(new CQuadtree(new Vector3(0, 0, 1), new Vector3(0, 1, 0), this, m_ShapeFront));		// 4: front
        m_Quadtrees.Add(new CQuadtree(new Vector3(0, 0, -1), new Vector3(0, 1, 0), this, m_ShapeBack));		// 5: back

        // link neighbors of TOP quadtree
        m_Quadtrees[0].SetNeighbor(en_NeighborDirection.NB_Top, m_Quadtrees[5], en_NeighborDirection.NB_Top); // back
        m_Quadtrees[0].SetNeighbor(en_NeighborDirection.NB_Bottom, m_Quadtrees[4], en_NeighborDirection.NB_Top); // front
        m_Quadtrees[0].SetNeighbor(en_NeighborDirection.NB_Left, m_Quadtrees[2], en_NeighborDirection.NB_Top); // left
        m_Quadtrees[0].SetNeighbor(en_NeighborDirection.NB_Right, m_Quadtrees[3], en_NeighborDirection.NB_Top); // right

        // link neighbors of BOTTOM quadtree
        m_Quadtrees[1].SetNeighbor(en_NeighborDirection.NB_Top, m_Quadtrees[4], en_NeighborDirection.NB_Bottom); // front
        m_Quadtrees[1].SetNeighbor(en_NeighborDirection.NB_Bottom, m_Quadtrees[5], en_NeighborDirection.NB_Bottom); // back
        m_Quadtrees[1].SetNeighbor(en_NeighborDirection.NB_Left, m_Quadtrees[2], en_NeighborDirection.NB_Bottom); // left
        m_Quadtrees[1].SetNeighbor(en_NeighborDirection.NB_Right, m_Quadtrees[3], en_NeighborDirection.NB_Bottom); // right

        // link neighbors of LEFT quadtree
        m_Quadtrees[2].SetNeighbor(en_NeighborDirection.NB_Top, m_Quadtrees[0], en_NeighborDirection.NB_Left); // top
        m_Quadtrees[2].SetNeighbor(en_NeighborDirection.NB_Bottom, m_Quadtrees[1], en_NeighborDirection.NB_Left); // bottom
        m_Quadtrees[2].SetNeighbor(en_NeighborDirection.NB_Left, m_Quadtrees[5], en_NeighborDirection.NB_Right); // back
        m_Quadtrees[2].SetNeighbor(en_NeighborDirection.NB_Right, m_Quadtrees[4], en_NeighborDirection.NB_Left); // front

        // link neighbors of RIGHT quadtree
        m_Quadtrees[3].SetNeighbor(en_NeighborDirection.NB_Top, m_Quadtrees[0], en_NeighborDirection.NB_Right); // top
        m_Quadtrees[3].SetNeighbor(en_NeighborDirection.NB_Bottom, m_Quadtrees[1], en_NeighborDirection.NB_Right); // bottom
        m_Quadtrees[3].SetNeighbor(en_NeighborDirection.NB_Left, m_Quadtrees[4], en_NeighborDirection.NB_Right); // front
        m_Quadtrees[3].SetNeighbor(en_NeighborDirection.NB_Right, m_Quadtrees[5], en_NeighborDirection.NB_Left); // back

        // link neighbors of FRONT quadtree
        m_Quadtrees[4].SetNeighbor(en_NeighborDirection.NB_Top, m_Quadtrees[0], en_NeighborDirection.NB_Bottom); // top
        m_Quadtrees[4].SetNeighbor(en_NeighborDirection.NB_Bottom, m_Quadtrees[1], en_NeighborDirection.NB_Top); // bottom
        m_Quadtrees[4].SetNeighbor(en_NeighborDirection.NB_Left, m_Quadtrees[2], en_NeighborDirection.NB_Right); // left
        m_Quadtrees[4].SetNeighbor(en_NeighborDirection.NB_Right, m_Quadtrees[3], en_NeighborDirection.NB_Left); // right

        // link neighbors of BACK quadtree
        m_Quadtrees[5].SetNeighbor(en_NeighborDirection.NB_Top, m_Quadtrees[0], en_NeighborDirection.NB_Top); // top
        m_Quadtrees[5].SetNeighbor(en_NeighborDirection.NB_Bottom, m_Quadtrees[1], en_NeighborDirection.NB_Bottom); // bottom
        m_Quadtrees[5].SetNeighbor(en_NeighborDirection.NB_Left, m_Quadtrees[3], en_NeighborDirection.NB_Right); // right
        m_Quadtrees[5].SetNeighbor(en_NeighborDirection.NB_Right, m_Quadtrees[2], en_NeighborDirection.NB_Left); // left

        // atmosphere
        m_AtmosRadius = m_Radius + m_HeightScale * m_AtmosScale;
        m_LodSpheres = new CLodSphere[]
        {
            new CLodSphere(true, this),		// outer sphere
            new CLodSphere(false, this)		// inner sphere
        };

        if (m_Camera == null)
        {
            Debug.Log("[ETHEREA1] Planet " + this.name + " had no associated Camera. Main Camera is being selected as the Camera for this Planet.");
            m_Camera = Camera.main;
        }

        if (m_SunTransform == null)
        {
            Debug.Log("[ETHEREA1] Planet " + this.name + " has no Sun. Please select a Light as the Sun for this Planet.");
        }
    }