Exemple #1
0
        /// <summary>
        /// Sets up this instance of NoiseSettingsGUI with the specified SerializedObject containing an object reference
        /// to a NoiseSettings instance. GUI will be drawn for this serialized NoiseSettings instance.
        /// </summary>
        /// <param name="serializedNoise"> A SerializedObject instance containing an object reference to a NoiseSettings object </param>
        public void Init(SerializedObject serializedNoise)
        {
            this.serializedNoise = serializedNoise;

            target = this.serializedNoise.targetObject as NoiseSettings;

            // transform settings
            transformSettings = this.serializedNoise.FindProperty("transformSettings");
            translation       = transformSettings.FindPropertyRelative("translation");
            rotation          = transformSettings.FindPropertyRelative("rotation");
            scale             = transformSettings.FindPropertyRelative("scale");
            flipScaleX        = transformSettings.FindPropertyRelative("flipScaleX");
            flipScaleY        = transformSettings.FindPropertyRelative("flipScaleY");
            flipScaleZ        = transformSettings.FindPropertyRelative("flipScaleZ");

            // domain settings
            domainSettings    = this.serializedNoise.FindProperty("domainSettings");
            noiseTypeName     = domainSettings.FindPropertyRelative("noiseTypeName");
            noiseTypeParams   = domainSettings.FindPropertyRelative("noiseTypeParams");
            fractalTypeName   = domainSettings.FindPropertyRelative("fractalTypeName");
            fractalTypeParams = domainSettings.FindPropertyRelative("fractalTypeParams");

            // filter settings
            // filterSettings = serializedNoise.FindProperty( "filterSettings" );
            // m_filterStack = filterSettings.FindPropertyRelative( "filterStack" ).objectReferenceValue as FilterStack;
            // m_serializedFilterStack = new SerializedObject( m_filterStack );
            // m_filterStackView = new FilterStackView( new GUIContent( "Filters" ), m_serializedFilterStack, m_serializedFilterStack.targetObject as FilterStack );
        }
Exemple #2
0
        private static void INTERNAL_Blit2D(NoiseSettings noise, RenderTexture dest, Material mat, int pass)
        {
            noise.SetupMaterial(mat);

            RenderTexture tempRT = RenderTexture.GetTemporary(dest.descriptor);
            RenderTexture prev   = RenderTexture.active;

            RenderTexture.active = tempRT;

            Graphics.Blit(tempRT, mat, pass);

            RenderTexture.active = dest;

            // if(noise.filterSettings.filterStack != null)
            // {
            //     noise.filterSettings.filterStack.Eval(tempRT, dest);
            // }
            // else
            {
                Graphics.Blit(tempRT, dest);
            }

            RenderTexture.active = prev;

            RenderTexture.ReleaseTemporary(tempRT);
        }
Exemple #3
0
        /// <summary>
        /// Copies the runtime information from a provided NoiseSettings instance.
        /// </summary>
        /// <param name="noiseSettings"> The NoiseSettings instance to copy from </param>
        public void Copy(NoiseSettings noiseSettings)
        {
            transformSettings = noiseSettings.transformSettings;
            domainSettings    = noiseSettings.domainSettings;

            // // TODO(wyatt): copy Filter Stack
            // Debug.LogError("TODO(wyatt): copy filter stack");
        }
Exemple #4
0
        /// <summary>
        /// Bakes 3D noise defined by the given NoiseSettings instance into a Texture3D instance and returns
        /// a reference to it.
        /// </summary>
        /// <param name = "noise"> An instance of NoiseSettings defining the type of noise to bake </param>
        /// <param name = "width"> The width of the baked Texture3D </param>
        /// <param name = "height"> The height of the baked Texture3D </param>
        /// <param name = "depth"> The depth of the baked Texture3D </param>
        /// <param name = "format"> The GraphicsFormat for the baked Texture3D. In most cases, you will want to use GraphicsFormat.R16_UNorm </param>
        /// <param name = "flags"> TextureCreation flags for the baked Texture3D. </param>
        /// <returns> A reference to the baked Texture3D instance </returns>
        /// <remarks>
        /// Be careful when specifying TextureCreation flags. If you specify that mipmaps should be generated for
        /// a Texture3D, that will use a lot more memory than if you were generating mipmaps for a Texture2D.
        /// </remarks>
        public static Texture3D BakeToTexture3D(NoiseSettings noise, int width, int height, int depth,
                                                GraphicsFormat format      = GraphicsFormat.R16_UNorm,
                                                TextureCreationFlags flags = TextureCreationFlags.None)
        {
            Material mat = GetDefaultBlitMaterial(noise);

            if (mat == null)
            {
                return(null);
            }

            RenderTexture sliceRT = RenderTexture.GetTemporary(width, height, 0, GraphicsFormat.R16_UNorm);
            Texture2D     slice2D = new Texture2D(width, height, format, flags);

            Color[] colors = new Color[width * height * depth];

            noise.SetupMaterial(mat);
            int pass = NoiseLib.GetNoiseIndex(noise);

            RenderTexture.active = sliceRT;

            List <Color[]> sliceColors = new List <Color[]>(depth);

            for (int i = 0; i < depth; ++i)
            {
                float uvy = ((float)i + 0.5f) / depth;
                mat.SetFloat("_UVY", uvy);

                Graphics.Blit(null, sliceRT, mat, pass * kNumBlitPasses + 1);

                slice2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);

                sliceColors.Add(slice2D.GetPixels(0, 0, width, height));
            }

            int pixPerSlice = width * height;

            for (int sliceID = 0; sliceID < sliceColors.Count; ++sliceID)
            {
                for (int pixelID = 0; pixelID < sliceColors[sliceID].Length; ++pixelID)
                {
                    int pixel = (pixPerSlice * sliceID) + pixelID;
                    colors[pixel] = sliceColors[sliceID][pixelID];
                }
            }

            bool mipChain = ((int)flags & (int)TextureCreationFlags.MipChain) != 0;

            Texture3D texture = new Texture3D(width, height, depth, format, flags);

            texture.SetPixels(colors);
            texture.Apply(mipChain);

            RenderTexture.active = null;
            RenderTexture.ReleaseTemporary(sliceRT);

            return(texture);
        }
Exemple #5
0
        /// <summary>
        /// Returns a Material reference to the default blit material for the given NoiseSettings object.
        /// </summary>
        /// <remarks>
        /// Internally, this uses noise.domainSettings.fractalTypeName to get it's FractalType
        /// </remarks>
        /// <returns> A reference to the default blit Material for the specified NoiseSettings instance </returns>
        public static Material GetDefaultBlitMaterial(NoiseSettings noise)
        {
            IFractalType fractal = NoiseLib.GetFractalTypeInstance(noise.domainSettings.fractalTypeName);

            if (fractal == null)
            {
                return(null);
            }

            return(GetDefaultBlitMaterial(fractal.GetType()));
        }
Exemple #6
0
        public static void ShowWindow(NoiseSettings noise)
        {
            var wnd = ScriptableObject.CreateInstance <ExportNoiseWindow>();

            wnd.titleContent = Styles.title;
            wnd.Init(noise);
            wnd.minSize = new Vector2(400f, 160f);
            wnd.maxSize = new Vector2(400f, 160f);

            wnd.Show();
        }
Exemple #7
0
        /*=========================================================================
        *
        *   Blit
        *       - Blit raw noise data into texture
        *
        *  =========================================================================*/

        /// <summary>
        /// Blits 2D noise defined by the given NoiseSettings instance into the destination RenderTexture.
        /// </summary>
        /// <param name = "noise"> An instance of NoiseSettings defining the type of noise to render </param>
        /// <param name = "dest"> The destination RenderTexture that the noise will be rendered into. </param>
        public static void Blit2D(NoiseSettings noise, RenderTexture dest)
        {
            Material mat = GetDefaultBlitMaterial(noise);

            if (mat == null)
            {
                return;
            }

            Blit2D(noise, dest, mat);
        }
Exemple #8
0
        /// <summary>
        /// Copies the serialized information from a provided NoiseSettings instance
        /// </summary>
        /// <param name="noiseSettings"> The NoiseSettings instance to copy from </param>
        public void CopySerialized(NoiseSettings noiseSettings)
        {
            SerializedObject copy  = new SerializedObject(noiseSettings);
            SerializedObject _this = new SerializedObject(this);

            copy.Update();
            _this.Update();

            _this.CopyFromSerializedProperty(copy.FindProperty("transformSettings"));
            _this.CopyFromSerializedProperty(copy.FindProperty("domainSettings"));
            // _this.CopyFromSerializedProperty(copy.FindProperty("m_filterSettings"));

            _this.ApplyModifiedProperties();
        }
Exemple #9
0
        /// <summary>
        /// Creates a new NoiseSettings Asset in the root Assets folder. This is the function
        /// accessible via the "Assets/Create/Noise Settings" MenuItem
        /// </summary>
        /// <returns> A reference to the newly created NoiseSettings Asset </returns>d
        //[MenuItem("Assets/Create/Noise Settings")]
        //public static NoiseSettings CreateAsset()
        //{
        //    return CreateAsset("Assets/New Noise Settings.asset");
        //}

        /// <summary>
        /// Creates a new NoiseSettings Asset at the specified Asset path
        /// </summary>
        /// <param name="assetPath"> The path in the AssetDatabase where the new NoiseSettings Asset should be saved </param>
        /// <returns> A reference to the newly created NoiseSettings Asset </returns>
        public static NoiseSettings CreateAsset(string assetPath)
        {
            NoiseSettings noiseSettings = ScriptableObject.CreateInstance <NoiseSettings>();

            // FilterStack filterStack = ScriptableObject.CreateInstance<FilterStack>();
            // filterStack.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy | HideFlags.NotEditable;
            // noiseSettings.filterSettings.filterStack = filterStack;

            AssetDatabase.CreateAsset(noiseSettings, assetPath);

            // AssetDatabase.AddObjectToAsset(filterStack, noiseSettings);
            // AssetDatabase.ImportAsset( AssetDatabase.GetAssetPath( filterStack ) );

            AssetDatabase.SaveAssets();

            EditorGUIUtility.PingObject(noiseSettings);

            return(noiseSettings);
        }
Exemple #10
0
        /// <summary>
        /// Bakes 2D noise defined by the given NoiseSettings instance into a Texture2D instance and returns
        /// a reference to it.
        /// </summary>
        /// <param name = "noise"> An instance of NoiseSettings defining the type of noise to bake </param>
        /// <param name = "width"> The width of the baked Texture2D </param>
        /// <param name = "height"> The height of the baked Texture2D </param>
        /// <param name = "format"> The GraphicsFormat for the baked Texture2D. In most cases, you will want to use GraphicsFormat.R16_UNorm </param>
        /// <param name = "flags"> TextureCreation flags for the baked Texture2D </param>
        /// <returns> A reference to the baked Texture2D instance </returns>
        public static Texture2D BakeToTexture2D(NoiseSettings noise, int width, int height,
                                                GraphicsFormat format      = GraphicsFormat.R16_UNorm,
                                                TextureCreationFlags flags = TextureCreationFlags.None)
        {
            RenderTexture rt      = RenderTexture.GetTemporary(width, height, 0, GraphicsFormat.R16_UNorm);
            Texture2D     texture = new Texture2D(width, height, format, flags);

            Blit2D(noise, rt);

            RenderTexture.active = rt;

            bool mipChain = ((int)flags & (int)TextureCreationFlags.MipChain) != 0;

            texture.ReadPixels(new Rect(0, 0, width, height), 0, 0, mipChain);

            RenderTexture.active = null;
            RenderTexture.ReleaseTemporary(rt);

            return(texture);
        }
Exemple #11
0
        /// <summary>
        /// Blits 3D noise defined by the given NoiseSettings instance into the destination RenderTexture.
        /// </summary>
        /// <param name = "noise"> An instance of NoiseSettings defining the type of noise to render </param>
        /// <param name = "dest"> The destination RenderTexture that the noise will be rendered into. </param>
        public static void Blit3D(NoiseSettings noise, RenderTexture dest)
        {
            throw new NotImplementedException("NoiseUtils::Blit3D: Function not implemented yet");

            // Debug.Assert(dest.dimension == UnityEngine.Rendering.TextureDimension.Tex3D,
            //              "NoiseUtils::Blit3D: Provided RenderTexture is not a 3D texture. You have to manually create it as a volume");

            // Material mat = GetDefaultBlitMaterial(noise);

            // if(mat == null)
            // {
            //     return;
            // }

            // int pass = NoiseLib.GetNoiseIndex(noise.domainSettings.noiseTypeName);

            // RenderTexture prev = RenderTexture.active;

            // Graphics.SetRenderTarget(dest, 0, CubemapFace.Unknown, kAllSlices);

            // // Graphics.Blit( dest, mat,  )

            // RenderTexture.active = prev;
        }
Exemple #12
0
 /// <summary>
 /// Returns the global FractalType index associated with provided NoiseSettings instance
 /// </summary>
 /// <param name="noise"> The NoiseSettings instance </param>
 public static int GetFractalIndex(NoiseSettings noise)
 {
     return(GetFractalIndex(noise.domainSettings.fractalTypeName));
 }
Exemple #13
0
 /// <summary>
 /// Returns the global NoiseType index associated with provided NoiseSettings instance
 /// </summary>
 /// <param name="noise"> The NoiseSettings instance </param>
 public static int GetNoiseIndex(NoiseSettings noise)
 {
     return(GetNoiseIndex(noise.domainSettings.noiseTypeName));
 }
Exemple #14
0
 /// <summary>
 /// Initializes the ExportNoiseWindow instance with the given NoiseSettings instance.
 /// </summary>
 /// <param name = "noise"> The NoiseSettings instance to be used with this ExportNoiseWindow instance </param>
 public void Init(NoiseSettings noise)
 {
     m_noise = noise;
 }
Exemple #15
0
 /// <summary>
 /// Sets up this instance of NoiseSettingsGUI with the specified NoiseSettings object.
 /// GUI will be drawn for this NoiseSettings instance.
 /// </summary>
 /// <param name="noiseSettings"> The NoiseSettings instance for which GUI will be drawn </param>
 public void Init(NoiseSettings noiseSettings)
 {
     Init(new SerializedObject(noiseSettings));
 }
Exemple #16
0
        /// <summary>
        /// Renders an interactive Noise Preview along with tooltip icons and an optional Export button that opens a new ExportNoiseWindow.
        /// A background image is also rendered behind the preview that takes up the entire width of the EditorWindow currently being drawn.
        /// </summary>
        /// <param name = "minSize"> Minimum size for the Preview </param>
        /// <param name = "showExportButton"> Whether or not to render the Export button </param>
        public void DrawPreviewTexture(float minSize, ref bool isLocked)
        {
            // Draw label with tooltip
            //GUILayout.Label( Styles.noisePreview );

            float padding     = 4f;
            float iconWidth   = 40f;
            int   size        = (int)Mathf.Min(minSize, EditorGUIUtility.currentViewWidth);
            Rect  currentRect = rect;

            currentRect.y += EditorGUIUtility.singleLineHeight * 2f;
            Rect totalRect = new Rect(currentRect.x, currentRect.y, currentRect.width, size); //GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, size + padding * 2); // extra pixels for highlight border

            Color prev = GUI.color;

            GUI.color = new Color(.1f, .1f, .1f, 1f);
            GUI.DrawTexture(totalRect, Texture2D.whiteTexture, ScaleMode.StretchToFill, false);
            GUI.color = Color.white;

            // draw info icon
            // if(totalRect.Contains(Event.current.mousePosition))
            {
                Rect infoIconRect = new Rect(totalRect.x + padding, totalRect.y + padding, iconWidth, iconWidth);
                GUI.Label(infoIconRect, Styles.infoIcon);
                // GUI.Label( infoIconRect, Styles.noiseTooltip );
            }

            float buttonWidth    = GUI.skin.button.CalcSize(Styles.buttonUnlocked).x;
            float buttonHeight   = EditorGUIUtility.singleLineHeight;
            Rect  lockButtonRect = new Rect(totalRect.x + padding, totalRect.y + padding * 2f + GUI.skin.button.CalcSize(Styles.infoIcon).y, buttonWidth, buttonHeight);


            if (isLocked)
            {
                if (GUI.Button(lockButtonRect, Styles.buttonLocked))
                {
                    isLocked = false;
                }
            }
            else
            {
                if (GUI.Button(lockButtonRect, Styles.buttonUnlocked))
                {
                    isLocked = true;
                }
            }

            m_noisePreviewIsLocked = isLocked;
            // draw export button
            //Rect exportRect = new Rect( totalRect.xMax - buttonWidth - padding, totalRect.yMax - buttonHeight - padding, buttonWidth, buttonHeight );
            //if(GUI.Button(exportRect, Styles.export))
            //{
            //    serializedNoise.ApplyModifiedProperties();
            //    serializedNoise.Update();

            //    ExportNoiseWindow.ShowWindow( serializedNoise.targetObject as NoiseSettings );
            //}

            float safeSpace   = Mathf.Max(iconWidth * 2, buttonWidth * 2) + padding * 4;
            float minWidth    = Mathf.Min(size, totalRect.width - safeSpace);
            Rect  previewRect = new Rect(totalRect.x + totalRect.width / 2 - minWidth / 2, totalRect.y + totalRect.height / 2 - minWidth / 2, minWidth, minWidth);

            EditorGUIUtility.AddCursorRect(previewRect, MouseCursor.Pan);

            if (previewRect.Contains(Event.current.mousePosition) && !isLocked)
            {
                serializedNoise.Update();

                HandlePreviewTextureInput(previewRect);

                serializedNoise.ApplyModifiedProperties();
            }

            if (Event.current.type == EventType.Repaint)
            {
                // create preview RT here and keep until the next Repaint
                if (m_previewRT != null)
                {
                    RenderTexture.ReleaseTemporary(m_previewRT);
                }

                NoiseSettings noiseSettings = serializedNoise.targetObject as NoiseSettings;

                m_previewRT = RenderTexture.GetTemporary(512, 512, 0, RenderTextureFormat.ARGB32);
                RenderTexture tempRT = RenderTexture.GetTemporary(512, 512, 0, RenderTextureFormat.RFloat);

                RenderTexture prevActive = RenderTexture.active;

                NoiseUtils.Blit2D(noiseSettings, tempRT);

                NoiseUtils.BlitPreview2D(tempRT, m_previewRT);

                RenderTexture.active = prevActive;

                GUI.DrawTexture(previewRect, m_previewRT, ScaleMode.ScaleToFit, false);

                RenderTexture.ReleaseTemporary(tempRT);
            }

            GUI.color = prev;
        }
Exemple #17
0
        /// <summary>
        /// Blits 2D noise defined by the given NoiseSettings instance into the destination RenderTexture
        /// using the provided Material.
        /// </summary>
        /// <param name = "noise"> An instance of NoiseSettings defining the type of noise to render </param>
        /// <param name = "dest"> The destination RenderTexture that the noise will be rendered into. </param>
        /// <param name = "mat"> The Material to be used for rendering the noise </param>
        public static void Blit2D(NoiseSettings noise, RenderTexture dest, Material mat)
        {
            int pass = NoiseLib.GetNoiseIndex(noise.domainSettings.noiseTypeName);

            INTERNAL_Blit2D(noise, dest, mat, pass * kNumBlitPasses + 0);
        }