Пример #1
0
    // Inspector GUI function for polishable probes.
    void ShowPolisherGUI(LightProbes probes)
    {
        ProbePolisher.ResetPolisherIfNeeded(probes);

        var coeffs = probes.coefficients;

        // Retrieve the optional information from the second probe.
        var baseIntensity = coeffs[28];
        var skyIntensity  = coeffs[29];
        var skyColor1     = new Color(coeffs[30], coeffs[31], coeffs[32]);
        var skyColor2     = new Color(coeffs[33], coeffs[34], coeffs[35]);
        var yaw           = coeffs[36];

        EditorGUI.BeginChangeCheck();

        // Show the controls.
        baseIntensity = EditorGUILayout.Slider("Base Intensity", baseIntensity, 0.0f, 10.0f);
        skyIntensity  = EditorGUILayout.Slider("Sky Intensity", skyIntensity, 0.0f, 10.0f);
        skyColor1     = EditorGUILayout.ColorField("Sky Color Top", skyColor1);
        skyColor2     = EditorGUILayout.ColorField("Sky Color Bottom", skyColor2);
        yaw           = EditorGUILayout.Slider("Rotation (Y-Axis)", yaw, -180.0f, 180.0f);

        if (EditorGUI.EndChangeCheck())
        {
            // Update the optional information.
            coeffs[28] = baseIntensity;
            coeffs[29] = skyIntensity;
            coeffs[30] = skyColor1.r;
            coeffs[31] = skyColor1.g;
            coeffs[32] = skyColor1.b;
            coeffs[33] = skyColor2.r;
            coeffs[34] = skyColor2.g;
            coeffs[35] = skyColor2.b;
            coeffs[36] = yaw;

            // Retrieve the SH coefficients from the first probe.
            var sh = ProbePolisher.NewVectorArrayFromCoeffs(coeffs, 0);

            // Apply the intensity value.
            for (var i = 0; i < 9; i++)
            {
                sh[i] *= baseIntensity;
            }

            // Apply y-axis rotation.
            sh = ProbePolisher.RotateSHCoeffs(sh, yaw);

            // Add the skylight.
            var sky1 = ProbePolisher.ColorToVector3(skyColor1) * skyIntensity;
            var sky2 = ProbePolisher.ColorToVector3(skyColor2) * skyIntensity;
            sh[0] += (sky1 + sky2) * 0.5f;
            sh[1] += (sky2 - sky1) * 0.5f;

            // Copy the coefficients to the all probes.
            ProbePolisher.UpdateCoeffsWithVectorArray(coeffs, sh);

            // Update the asset.
            probes.coefficients = coeffs;
        }

        EditorGUILayout.Space();

        // Skybox generator button.
        if (GUILayout.Button("Make/Update Skybox"))
        {
            // Look for the asset of the material.
            var probePath = AssetDatabase.GetAssetPath(target);
            var assetPath = probePath.Substring(0, probePath.Length - 6) + " skybox.mat";
            var material  = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Material)) as Material;

            // If there is no asset, make a new one.
            if (material == null)
            {
                material = new Material(Shader.Find("ProbePolisher/SH Skybox"));
                AssetDatabase.CreateAsset(material, assetPath);
            }

            // Update the material.
            ProbePolisher.UpdateSkyboxMaterial(material, ProbePolisher.NewVectorArrayFromCoeffs(coeffs, 27 * 2));
        }
    }
    void Update()
    {
        // Do nothing if nothing was changed.
        if (mix == prevMix && intensity == prevIntensity && skyboxIntensity == prevSkyboxIntensity)
        {
            return;
        }

        if (probe == null)
        {
            // Clone the first probe and set it to the scene.
            probe = Instantiate(sourceProbes[0]) as LightProbes;
            LightmapSettings.lightProbes = probe;
        }

        if (updateSkybox && skybox == null)
        {
            // Make a empty skybox and set it to the scene.
            skybox = ProbePolisher.NewSkyboxMaterial(null);
            RenderSettings.skybox = skybox;
        }

        // Clamp the mixing position.
        mix = Mathf.Clamp(mix, 0.0f, 1.0f * (sourceProbes.Length - 1));

        // Choose two probes to mix.
        var mixIndex = Mathf.FloorToInt(mix);

        if (mixIndex == sourceProbes.Length - 1)
        {
            mixIndex--;
        }

        var source1 = ProbePolisher.NewVectorArrayFromCoeffs(sourceProbes[mixIndex + 0].coefficients, 27 * 2);
        var source2 = ProbePolisher.NewVectorArrayFromCoeffs(sourceProbes[mixIndex + 1].coefficients, 27 * 2);

        // Mix the two probes.
        var coeffs  = new Vector3[9];
        var mixRate = mix - mixIndex;

        for (var i = 0; i < 9; i++)
        {
            coeffs[i] = Vector3.Lerp(source1[i], source2[i], mixRate) * intensity;
        }

        // Update the probe with the mixed coefficients.
        var temp = probe.coefficients;

        ProbePolisher.UpdateCoeffsWithVectorArray(temp, coeffs);
        probe.coefficients = temp;

        // Update the skybox if needed.
        if (updateSkybox && skybox != null)
        {
            ProbePolisher.UpdateSkyboxMaterial(skybox, coeffs);
            skybox.SetFloat("_Intensity", skyboxIntensity);
        }

        prevMix             = mix;
        prevIntensity       = intensity;
        prevSkyboxIntensity = skyboxIntensity;
    }