Exemplo n.º 1
0
    void RenderToBRDFMap()
    {
        if (File.Exists(mOutputPath + "brdfLUT.png"))
        {
            return;
        }
        RenderTexture mBRDFMap = new RenderTexture(512, 512, 0, RenderTextureFormat.ARGBFloat);


        if (mRenderToBRDFMaterial == null)
        {
            Shader shader = Shader.Find("liangairan/pbr/brdf_lut");
            mRenderToBRDFMaterial = new Material(shader);
        }

        if (mQuad == null)
        {
            mQuad = new FullScreenQuad();
            GameObject quadObj = mQuad.GetGameObject();
            quadObj.GetComponent <MeshRenderer>().sharedMaterial = mRenderToBRDFMaterial;
            quadObj.layer = LayerMask.NameToLayer("RenderToCubemap");
            quadObj.transform.position = mCamera.transform.position + mCamera.transform.forward * 5.0f;
        }

        mCamera.targetTexture = mBRDFMap;
        mCamera.Render();
        RenderTexture.active = mBRDFMap;
        Texture2D brdfLUT = new Texture2D(mBRDFMap.width, mBRDFMap.height, TextureFormat.RGBAFloat, false);

        brdfLUT.ReadPixels(new Rect(0, 0, mBRDFMap.width, mBRDFMap.height), 0, 0);
        brdfLUT.Apply();
        byte[] bytes = brdfLUT.EncodeToPNG();

        File.WriteAllBytes(mOutputPath + "brdfLUT.png", bytes);
        DestroyImmediate(brdfLUT);
        brdfLUT = null;
        mCamera.targetTexture = null;
        DestroyImmediate(mBRDFMap);
        AssetDatabase.Refresh();

        TextureImporter textureImporter = AssetImporter.GetAtPath(mOutputPath + "brdfLUT.png") as TextureImporter;

        textureImporter.textureType    = TextureImporterType.Default;
        textureImporter.mipmapEnabled  = false;
        textureImporter.sRGBTexture    = true;
        textureImporter.filterMode     = FilterMode.Bilinear;
        textureImporter.wrapMode       = TextureWrapMode.Clamp;
        textureImporter.maxTextureSize = Mathf.Max(512, 512);
        AssetDatabase.ImportAsset(mOutputPath + "brdfLUT.png", ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
    }