Ejemplo n.º 1
0
        /// <summary>
        /// Sets up the provided Material to be used with this NoiseSettings instance. Some assumptions are made
        /// here as far as definitions and variable declarations in the Material's Shader go.
        /// float4 _NoiseTranslation, float4 _NoiseRotation, float4 _NoiseScale, and float4x4 _NoiseTransform are
        /// assumed to be declared. Sets up the Material using the NoiseType and FractalType SetupMaterial
        /// functions.
        /// </summary>
        /// <param name="mat"> The Material to set up for use with this NoiseSettings instance </param>
        public void SetupMaterial(Material mat)
        {
            INoiseType   noiseType   = NoiseLib.GetNoiseTypeInstance(domainSettings.noiseTypeName);
            IFractalType fractalType = NoiseLib.GetFractalTypeInstance(domainSettings.fractalTypeName);

            // set individual transform info
            mat.SetVector(ShaderStrings.translation, transformSettings.translation);
            mat.SetVector(ShaderStrings.rotation, transformSettings.rotation);
            mat.SetVector(ShaderStrings.scale, transformSettings.scale);

            // set full transform matrix
            mat.SetMatrix(ShaderStrings.transform, trs);

            noiseType?.SetupMaterial(mat, domainSettings.noiseTypeParams);
            fractalType?.SetupMaterial(mat, domainSettings.fractalTypeParams);

            if (useTextureForPositions)
            {
                mat.EnableKeyword("USE_NOISE_TEXTURE");
                mat.SetTexture("_NoiseTex", positionTexture);
            }
            else
            {
                mat.DisableKeyword("USE_NOISE_TEXTURE");
            }
        }
Ejemplo n.º 2
0
        private void DomainSettingsGUI()
        {
            noiseTypeName.stringValue = NoiseLib.NoiseTypePopup(Styles.noiseType, noiseTypeName.stringValue);
            INoiseType noiseType = NoiseLib.GetNoiseTypeInstance(noiseTypeName.stringValue);

            noiseTypeParams.stringValue = noiseType?.DoGUI(noiseTypeParams.stringValue);

            fractalTypeName.stringValue = NoiseLib.FractalTypePopup(Styles.fractalType, fractalTypeName.stringValue);
            IFractalType fractalType = NoiseLib.GetFractalTypeInstance(fractalTypeName.stringValue);

            fractalTypeParams.stringValue = fractalType?.DoGUI(fractalTypeParams.stringValue);
        }
Ejemplo n.º 3
0
        private static void GatherNoiseTypes()
        {
            List <INoiseType> instances = new List <INoiseType>();
            List <string>     names     = new List <string>();

            List <Type> types = new List <Type>();

            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                Type[] assemblyTypes = null;

                try
                {
                    assemblyTypes = asm.GetTypes();
                }
                catch (Exception)
                {
                    Debug.Log("NoiseLib::GatherNoiseTypes: Failed to get types from assembly: " + asm);
                    assemblyTypes = null;
                }

                if (assemblyTypes != null)
                {
                    types.AddRange(GetSubclassesOfGenericType(assemblyTypes, typeof(NoiseType <>)));
                }
            }

            foreach (Type t in types)
            {
                PropertyInfo propertyInfo = t.GetProperty("instance", s_bindingFlags);
                MethodInfo   methodInfo   = propertyInfo.GetGetMethod();
                INoiseType   instance     = (INoiseType)methodInfo.Invoke(null, null);

                NoiseTypeDescriptor desc = instance.GetDescription();

                if (string.IsNullOrEmpty(desc.name))
                {
                    Debug.LogError("NoiseType name cannot be null or empty! Skipping noise type: " + t);
                    continue;
                }

                instances.Add(instance);
                names.Add(desc.name);
            }

            s_noiseTypes = instances.ToArray();
            s_noiseNames = names.ToArray();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets up the provided Material to be used with this NoiseSettings instance. Some assumptions are made
        /// here as far as definitions and variable declarations in the Material's Shader go.
        /// float4 _NoiseTranslation, float4 _NoiseRotation, float4 _NoiseScale, and float4x4 _NoiseTransform are
        /// assumed to be declared. Sets up the Material using the NoiseType and FractalType SetupMaterial
        /// functions.
        /// </summary>
        /// <param name="mat"> The Material to set up for use with this NoiseSettings instance </param>
        public void SetupMaterial(Material mat)
        {
            INoiseType   noiseType   = NoiseLib.GetNoiseTypeInstance(domainSettings.noiseTypeName);
            IFractalType fractalType = NoiseLib.GetFractalTypeInstance(domainSettings.fractalTypeName);

            // set individual transform info
            mat.SetVector(ShaderStrings.translation, transformSettings.translation);
            mat.SetVector(ShaderStrings.rotation, transformSettings.rotation);
            mat.SetVector(ShaderStrings.scale, transformSettings.scale);

            // set full transform matrix
            mat.SetMatrix(ShaderStrings.transform, trs);

            noiseType?.SetupMaterial(mat, domainSettings.noiseTypeParams);
            fractalType?.SetupMaterial(mat, domainSettings.fractalTypeParams);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns the Singleton instance for the specified NoiseType
        /// </summary>
        /// <param name="t"> The Type for the NoiseType implementation </param>
        public static INoiseType GetNoiseTypeInstance(Type t)
        {
            INoiseType[] instances = s_noiseTypes;

            for (int i = 0; i < instances.Length; ++i)
            {
                INoiseType noiseType = instances[i];

                if (noiseType.GetType() == t)
                {
                    return(instances[i]);
                }
            }

            return(null);
        }
        public GeneratedShaderInfo(IFractalType fractalType, INoiseType noiseType)
        {
            this.fractalDesc = fractalType.GetDescription();
            this.noiseDesc   = noiseType.GetDescription();

            this.noiseIncludeStr = string.Format("#include \"{0}\"", noiseDesc.sourcePath);

            if (!string.IsNullOrEmpty(fractalDesc.name))
            {
                this.variantName = string.Format("{0}{1}", fractalDesc.name, noiseDesc.name);
            }
            else
            {
                this.variantName = noiseDesc.name;
            }

            // set the path of the generated file. this will be used when writing the file
            // to disk and when adding the include in any generated shaders that use this
            // fractal and noise type variant
            this.generatedIncludePath = string.Format("{0}/{1}/{2}.hlsl", noiseDesc.outputDir,
                                                      fractalDesc.name,
                                                      noiseDesc.name);
            this.outputDir = string.Format("{0}/{1}", noiseDesc.outputDir, fractalDesc.name);

            fractalStructName = string.Format("{0}FractalInput", fractalDesc.name);
            noiseStructName   = string.Format("{0}NoiseInput", noiseDesc.name);
            numFractalInputs  = fractalDesc.inputStructDefinition == null ? 0 : fractalDesc.inputStructDefinition.Count;
            numNoiseInputs    = noiseDesc.inputStructDefinition == null ? 0 : noiseDesc.inputStructDefinition.Count;
            fractalParamStr   = null;
            noiseParamStr     = null;
            functionInputStr  = "";

            // construct include paths string
            additionalIncludePaths = "\n";

            for (int i = 0; i < fractalDesc.additionalIncludePaths.Count; ++i)
            {
                additionalIncludePaths += $"#include \"{ fractalDesc.additionalIncludePaths[ i ] }\"\n";
            }

            additionalIncludePaths += "\n";

            // generate the string for the fractal type structure as it would appear as a parameter
            // in an HLSL function declaration
            if (numFractalInputs > 0)
            {
                fractalParamStr = string.Format("{0} {1}", fractalStructName, "fractalInput");
            }

            // generate the string for the noise type structure as it would appear as a parameter
            // in an HLSL function declaration
            if (numNoiseInputs > 0)
            {
                noiseParamStr = string.Format("{0} {1}", noiseStructName, "noiseInput");
            }

            // generate the argument string for an HLSL function declaration that would be
            // using this combination of noise and fractal type structure definitions
            functionParamStr = "";

            if (fractalParamStr != null)
            {
                functionParamStr += fractalParamStr;
                functionInputStr += "fractalInput";
            }

            if (fractalParamStr != null && noiseParamStr != null)
            {
                functionParamStr += ", ";
                functionInputStr += ", ";
            }

            if (noiseParamStr != null)
            {
                functionParamStr += noiseParamStr;
                functionInputStr += "noiseInput";
            }

            fractalStructDef = "";

            if (numFractalInputs > 0)
            {
                fractalStructDef = NoiseLib.BuildStructString(fractalStructName, fractalDesc.inputStructDefinition);

                string getDefaultFuncStr = NoiseLib.GetDefaultFunctionString(fractalStructName, fractalDesc.inputStructDefinition);
                fractalStructDef += $"\n\n{ getDefaultFuncStr }\n\n";
            }

            noiseStructDef = "";

            if (numNoiseInputs > 0)
            {
                noiseStructDef = NoiseLib.BuildStructString(noiseStructName, noiseDesc.inputStructDefinition);
            }

            // get input str construction
            getInputsStr       = "";
            getFractalInputStr = NoiseLib.GetInputFunctionCallString(fractalStructName);
            getNoiseInputStr   = NoiseLib.GetInputFunctionCallString(fractalStructName);

            if (numFractalInputs > 0)
            {
                getInputsStr += getFractalInputStr;
            }

            if (numFractalInputs > 0 && numNoiseInputs > 0)
            {
                getInputsStr += ", ";
            }

            if (numNoiseInputs > 0)
            {
                getInputsStr += getNoiseInputStr;
            }

            // get default input str construction
            getDefaultInputsStr       = "";
            getDefaultFractalInputStr = NoiseLib.GetDefaultInputFunctionCallString(fractalStructName);
            getDefaultNoiseInputStr   = NoiseLib.GetDefaultInputFunctionCallString(noiseStructName);

            if (numFractalInputs > 0)
            {
                getDefaultInputsStr += getDefaultFractalInputStr;
            }

            if (numFractalInputs > 0 && numNoiseInputs > 0)
            {
                getDefaultInputsStr += ", ";
            }

            if (numNoiseInputs > 0)
            {
                getDefaultInputsStr += getDefaultNoiseInputStr;
            }

            fractalPropertyDefStr = "";

            if (fractalDesc.inputStructDefinition != null &&
                fractalDesc.inputStructDefinition.Count > 0)
            {
                fractalPropertyDefStr  = NoiseLib.GetPropertyDefinitionStr(fractalDesc.name, fractalDesc.inputStructDefinition);
                fractalPropertyDefStr += "\n" + NoiseLib.GetPropertyFunctionStr(fractalStructName, fractalDesc.name, fractalDesc.inputStructDefinition);
            }
        }
Ejemplo n.º 7
0
        /*==========================================================================================
        *
        *   Generate HLSL
        *
        *  ==========================================================================================*/

        /// <summary>
        /// Forces generation of the NoiseType and FractalType variant HLSL header files
        /// </summary>
        public static void GenerateHeaderFiles()
        {
            System.Globalization.CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            GatherNoiseTypes();
            GatherFractalTypes();

            INoiseType[]   noiseTypes   = s_noiseTypes;
            IFractalType[] fractalTypes = s_fractalTypes;

            string[] fractalContents = LoadFractalSource(fractalTypes);
            string[] noiseContents   = LoadNoiseSource(noiseTypes);

            for (int f = 0; f < fractalTypes.Length; ++f)
            {
                string fractalStr = fractalContents[f];

                // dont generate for this fractal type if the source could not be found
                if (fractalStr == null)
                {
                    continue;
                }

                IFractalType fractal = fractalTypes[f];

                for (int n = 0; n < noiseTypes.Length; ++n)
                {
                    string noiseStr = noiseContents[n];

                    // dont generate for this noise type if the source could not be found
                    if (noiseStr == null)
                    {
                        continue;
                    }

                    INoiseType          noise = noiseTypes[n];
                    GeneratedShaderInfo info  = new GeneratedShaderInfo(fractal, noise);

                    StringBuilder sb = new StringBuilder();

                    sb.Append(Strings.k_warningHeader);                             // add the DO NOT EDIT warning
                    sb.Append(fractalStr);                                          // add the fractal template

                    info.ReplaceTags(sb);

                    string newContents = sb.ToString();

                    // do some code cleanup
                    newContents = Regex.Replace(newContents, Strings.k_regexDupCommas, ", ");
                    newContents = Regex.Replace(newContents, Strings.k_emptyArgsRight, " )");
                    newContents = Regex.Replace(newContents, Strings.k_emptyArgsLeft, "( ");

                    newContents = NormalizeLineEndings(newContents);

                    string outputDir = info.outputDir;

                    // TODO(wyatt): need to verify this is actually a directory and not a file
                    if (!Directory.Exists(outputDir))
                    {
                        Directory.CreateDirectory(outputDir);
                    }

                    string oldContents = null;

                    FileInfo fi = new FileInfo(info.generatedIncludePath);

                    if (File.Exists(info.generatedIncludePath))
                    {
                        using (StreamReader sr = new StreamReader(info.generatedIncludePath))
                        {
                            oldContents = sr.ReadToEnd();
                            oldContents = NormalizeLineEndings(oldContents);
                        }
                    }

                    if (!fi.IsReadOnly)
                    {
                        if (oldContents == null || newContents.CompareTo(oldContents) != 0)
                        {
                            try
                            {
                                using (StreamWriter sw = new StreamWriter(info.generatedIncludePath))
                                {
                                    sw.Write(newContents);
                                }
                            }
                            catch (Exception)
                            {
                                // restore previous cultureinfo
                                System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo;
                            }
                        }
                    }
                }
            }

            // restore previous cultureinfo
            System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo;

            //UnityEditor.AssetDatabase.Refresh();
        }