Example #1
0
        public void Reset()
        {
            colorMode      = Consts.ColorModeDefault;
            color          = Consts.FlatColor;
            colorFromLight = true;

            intensityFromLight    = true;
            intensityModeAdvanced = false;
            intensityInside       = Consts.IntensityDefault;
            intensityOutside      = Consts.IntensityDefault;

            blendingMode   = Consts.BlendingModeDefault;
            shaderAccuracy = Consts.ShaderAccuracyDefault;

            spotAngleFromLight = true;
            spotAngle          = Consts.SpotAngleDefault;

            coneRadiusStart    = Consts.ConeRadiusStart;
            geomMeshType       = Consts.GeomMeshType;
            geomCustomSides    = Consts.GeomSidesDefault;
            geomCustomSegments = Consts.GeomSegmentsDefault;
            geomCap            = Consts.GeomCap;

            attenuationEquation       = Consts.AttenuationEquationDefault;
            attenuationCustomBlending = Consts.AttenuationCustomBlending;

            fallOffEndFromLight = true;
            fallOffStart        = Consts.FallOffStart;
            fallOffEnd          = Consts.FallOffEnd;

            depthBlendDistance     = Consts.DepthBlendDistance;
            cameraClippingDistance = Consts.CameraClippingDistance;

            glareFrontal = Consts.GlareFrontal;
            glareBehind  = Consts.GlareBehind;

            fresnelPow = Consts.FresnelPow;

            noiseMode              = Consts.NoiseModeDefault;
            noiseIntensity         = Consts.NoiseIntensityDefault;
            noiseScaleUseGlobal    = true;
            noiseScaleLocal        = Consts.NoiseScaleDefault;
            noiseVelocityUseGlobal = true;
            noiseVelocityLocal     = Consts.NoiseVelocityDefault;

            sortingLayerID = 0;
            sortingOrder   = 0;

            fadeOutBegin = Consts.FadeOutBeginDefault;
            fadeOutEnd   = Consts.FadeOutEndDefault;

            dimensions = Consts.DimensionsDefault;
            tiltFactor = Consts.TiltDefault;
            skewingLocalForwardDirection = Consts.SkewingLocalForwardDirectionDefault;
            clippingPlaneTransform       = Consts.ClippingPlaneTransformDefault;

            trackChangesDuringPlaytime = false;

            m_EditorDirtyFlags = EditorDirtyFlags.Everything;
        }
    /// <summary>
    /// Render a single 3D noise slice.
    /// </summary>
    /// <param name="rt">The RenderTexture to blit the noise to.</param>
    /// <param name="z">The z coordiante of the slice.</param>
    public static void GetSlice(ref RenderTexture rt, float z, NoiseMode noiseMode = NoiseMode.Mix)
    {
        var wasActive = RenderTexture.active;

        UpdateGenerator();
        generatorMat.SetFloat("_Slice", z);
        generatorMat.DisableKeyword("MIX");
        generatorMat.DisableKeyword("PERLIN");
        generatorMat.DisableKeyword("WORLEY");
        switch (noiseMode)
        {
        case NoiseMode.Mix:
            generatorMat.EnableKeyword("MIX");
            break;

        case NoiseMode.PerlinOnly:
            generatorMat.EnableKeyword("PERLIN");
            break;

        case NoiseMode.WorleyOnly:
            generatorMat.EnableKeyword("WORLEY");
            break;
        }
        Graphics.Blit(null, rt, generatorMat);
        RenderTexture.active = wasActive;
    }
Example #3
0
    /// <summary>
    /// Render a single 3D noise slice.
    /// </summary>
    /// <param name="rt">The RenderTexture to blit the noise to.</param>
    /// <param name="z">The z coordiante of the slice.</param>
    public static void GetSlice(ref RenderTexture rt, float z, NoiseMode noiseMode = NoiseMode.Mix)
    {
        var wasActive = RenderTexture.active;

        UpdateGenerator();
        generatorMat.SetFloat("_Slice", z);
        generatorMat.SetInt("_Mode", (int)noiseMode);
        Graphics.Blit(null, rt, generatorMat);
        RenderTexture.active = wasActive;
    }
Example #4
0
        void HandleBackwardCompatibility(int serializedVersion, int newVersion)
        {
            if (serializedVersion == -1)
            {
                return;                                     // freshly new spawned entity: nothing to do
            }
            if (serializedVersion == newVersion)
            {
                return;                                     // same version: nothing to do
            }
            if (serializedVersion < 1301)
            {
                // quadratic attenuation is a new feature of 1.3
                attenuationEquation = AttenuationEquation.Linear;
            }

            if (serializedVersion < 1501)
            {
                // custom mesh is a new feature of 1.5
                geomMeshType       = MeshType.Custom;
                geomCustomSegments = 5;
            }

            if (serializedVersion < 1610)
            {
                // intensity global/advanced mode is a feature of 1.61
                intensityFromLight    = false;
                intensityModeAdvanced = !Mathf.Approximately(intensityInside, intensityOutside);
            }

            if (serializedVersion < 1710)
            {
                // noiseMode is a feature of 1.71
                noiseMode = _DEPRECATED_NoiseEnabled ? NoiseMode.WorldSpace : NoiseMode.Disabled;
            }

            Utils.MarkCurrentSceneDirty();
        }
Example #5
0
 /// <summary>
 /// Adds noise to the local scale of the specified Transforms
 /// </summary>
 public static void AddSclNoise(float min, float max, UnityEngine.Transform[] toNoisify, NoiseMode noiseMode = NoiseMode.Additive)
 {
     AddXSclNoise(min, max, toNoisify, noiseMode);
     AddYSclNoise(min, max, toNoisify, noiseMode);
     AddZSclNoise(min, max, toNoisify, noiseMode);
 }
Example #6
0
        /// <summary>
        /// Adds noise to the local Z scale of the specified Transforms
        /// </summary>
        public static void AddZSclNoise(float min, float max, UnityEngine.Transform[] toNoisify, NoiseMode noiseMode = NoiseMode.Additive)
        {
            if (toNoisify.Length <= 0)
            {
                throw new System.Exception("Noisify array is empty");
            }

            float amount;

            foreach (UnityEngine.Transform t in toNoisify)
            {
                amount = Random.Range(min, max);

                switch (noiseMode)
                {
                case NoiseMode.Absolute:

                    t.SetLocalZScl(amount);
                    break;

                case NoiseMode.Additive:

                    t.SetLocalZScl(t.localScale.z + amount);
                    break;

                default:
                    break;
                }
            }
        }
Example #7
0
        /// <summary>
        /// Adds noise to the local Y position of the specified Transforms
        /// </summary>
        public static void AddYPosNoise(float min, float max, UnityEngine.Transform[] toNoisify, NoiseMode noiseMode = NoiseMode.Additive)
        {
            if (toNoisify.Length <= 0)
            {
                throw new System.Exception("Noisify array is empty");
            }

            float amount;

            foreach (UnityEngine.Transform t in toNoisify)
            {
                amount = Random.Range(min, max);

                switch (noiseMode)
                {
                case NoiseMode.Absolute:

                    t.SetLocalYPos(amount);
                    break;

                case NoiseMode.Additive:

                    t.SetLocalYPos(t.localPosition.y + amount);
                    break;

                default:
                    throw new System.Exception("Add Noise got a wrong NoiseMode type");
                }
            }
        }
Example #8
0
        /// <summary>
        /// Load or generate 3D noise texture from/in 'Resources/3DNoise/folderName'
        /// </summary>
        /// <param name="noiseTexture">The Texture3D to fill with the result.</param>
        /// <param name="folderName">The folder name to look for/put the noise in. Relateive to 'Resources/3DNoise'.</param>
        /// <param name="resolution">The resolution of the 3D noise texture (^3).</param>
        /// <param name="mode">The load/generation mode.</param>
        /// /// <returns>True if successful.</returns>
        public static bool InitializeNoise(ref Texture3D noiseTexture, string folderName, int resolution, Mode mode = Mode.LoadAvailableElseGenerate, NoiseMode noiseMode = NoiseMode.Mix)
        {
            noiseTexture = null;

                        #if !UNITY_EDITOR
            // We cannot generate new textures in stand alone player, so we have to force this behaviour
            mode = Mode.LoadAvailableElseAbort;
                        #endif

            bool noiseLoaded = false;

            // Attempt to load noise
            if (mode != Mode.ForceGenerate)
            {
                noiseLoaded = LoadNoise(ref noiseTexture, folderName, resolution);
            }

                        #if UNITY_EDITOR // Noise textures can only be generated in the editor
            if ((!noiseLoaded && mode == Mode.LoadAvailableElseGenerate) || mode == Mode.ForceGenerate)
            {
                // Generate new noise
                GenerateNoise(folderName, resolution, noiseMode);

                // Attempt to load noise
                noiseLoaded = LoadNoise(ref noiseTexture, folderName, resolution);

                // If we failed to load noise right after generating it, something has gone seriously wrong
                if (!noiseLoaded)
                {
                    Debug.LogError("Fatal error: Unable to load noise after generating.");
                }
            }
                        #endif

            return(noiseLoaded);
        }
Example #9
0
        /// <summary>
        /// Generate 3D noise texture in 'Resources/3DNoise/folderName'
        /// </summary>
        /// <param name="folderName">The folder name to look for/put the noise in. Relateive to 'Resources/3DNoise'.</param>
        /// <param name="resolution">The resolution of the 3D noise texture (^3).</param>
        public static void GenerateNoise(string folderName, int resolution, NoiseMode noiseMode = NoiseMode.Mix)
        {
            // Delete existing noise textures
            if (AssetDatabase.IsValidFolder("Assets/Resources/CloudRendering/" + folderName))
            {
                FileUtil.DeleteFileOrDirectory("Assets/Resources/CloudRendering/" + folderName);
            }
            else
            {
                if (!AssetDatabase.IsValidFolder("Assets/Resources"))
                {
                    AssetDatabase.CreateFolder("Assets", "Resources");
                }
                if (!AssetDatabase.IsValidFolder("Assets/Resources/CloudRendering"))
                {
                    AssetDatabase.CreateFolder("Assets/Resources", "CloudRendering");
                }
            }
            AssetDatabase.CreateFolder("Assets/Resources/CloudRendering", folderName);

            RenderTexture rt = new RenderTexture(resolution, resolution, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);

            rt.filterMode = FilterMode.Point;
            rt.Create();

            Texture2D slice = new Texture2D(resolution, resolution, TextureFormat.ARGB32, false, true);

            slice.filterMode = FilterMode.Point;

            for (int u = 0; u < resolution; u++)
            {
                // Generate z slice
                GetSlice(ref rt, ((float)u + 0.5f) / (float)resolution, noiseMode);

                // Transfer to temporary texture
                var rtPrev = RenderTexture.active;
                RenderTexture.active = rt;

                slice.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
                slice.Apply();
                RenderTexture.active = rtPrev;

                // Store slice
                var    bytes = slice.EncodeToPNG();
                string fileNumber;
                if (u < 10)
                {
                    fileNumber = "000" + u;
                }
                else if (u < 100)
                {
                    fileNumber = "00" + u;
                }
                else if (u < 1000)
                {
                    fileNumber = "0" + u;
                }
                else
                {
                    fileNumber = u.ToString();
                }
                var assetPath = "/Resources/CloudRendering/" + folderName + "/ZSlice_" + fileNumber + ".png";
                File.WriteAllBytes(Application.dataPath + assetPath, bytes);
            }

            rt.DiscardContents();
            rt = null;

            if (Application.isPlaying)
            {
                Object.Destroy(slice);
            }
            else
            {
                Object.DestroyImmediate(slice);
            }

            // Need to refresh the asset DB so the new textures are detected
            AssetDatabase.Refresh();

            // Texture settings need to be applied on newly generated textures
            for (int u = 0; u < resolution; u++)
            {
                // Get slice name
                string fileNumber;
                if (u < 10)
                {
                    fileNumber = "000" + u;
                }
                else if (u < 100)
                {
                    fileNumber = "00" + u;
                }
                else if (u < 1000)
                {
                    fileNumber = "0" + u;
                }
                else
                {
                    fileNumber = u.ToString();
                }
                var assetPath = "/Resources/CloudRendering/" + folderName + "/ZSlice_" + fileNumber + ".png";

                var tImporter = AssetImporter.GetAtPath("Assets" + assetPath) as TextureImporter;
                if (tImporter)
                {
                    bool changed = false;
                    if (tImporter.textureCompression != TextureImporterCompression.Uncompressed)
                    {
                        tImporter.textureCompression = TextureImporterCompression.Uncompressed;
                        changed = true;
                    }
                    if (tImporter.sRGBTexture != false)
                    {
                        tImporter.sRGBTexture = false;
                        changed = true;
                    }
                    if (tImporter.isReadable != true)
                    {
                        tImporter.isReadable = true;
                        changed = true;
                    }
                    if (changed)
                    {
                        EditorUtility.DisplayProgressBar(
                            "CloudNoiseGen",
                            "Setting texture import settings for slice (" + (u + 1) + "/" + resolution + ")",
                            (float)(u + 1) / (float)resolution
                            );
                        AssetDatabase.ImportAsset("Assets" + assetPath);
                    }
                    else
                    {
                        EditorUtility.ClearProgressBar();
                    }
                }
            }
            EditorUtility.ClearProgressBar();

            AssetDatabase.Refresh();
        }