Initial commit

This commit is contained in:
gunnbr committed 2021-01-11 22:20:43 -08:00
1 parent ee9615239e
commit 16feac691f
4093 files changed
+1030491

No files matched your search

@@ -0,0 +1,32 @@
namespace Gaia
{
/// <summary>
/// An enum used for defining which n-dimensional spaces a particular
/// NoiseType or FractalType implementation supports
/// </summary>
[System.Serializable]
[System.Flags]
public enum NoiseDimensionFlags
{
/// <summary>
/// No dimensions are supported
/// </summary>
None = 0,
/// <summary>
/// One-dimensional noise is supported
/// </summary>
_1D = ( 1 << 0 ),
/// <summary>
/// Two-dimensional noise is supported
/// </summary>
_2D = ( 1 << 1 ),
/// <summary>
/// Three-dimensional noise is supported
/// </summary>
_3D = ( 1 << 2 ),
/// <summary>
/// Four-dimensional noise is supported
/// </summary>
_4D = ( 1 << 3 ),
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 14213647eda690542800b4e4e82ff4f0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,180 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Gaia
{
/// <summary>
/// Struct that provides a definition for a FractalType implementation. Information in this
/// struct is used during Shader generation and used to determine NoiseType and
/// FractalType compatibilities.
/// </summary>
public struct FractalTypeDescriptor
{
/// <summary>
/// The name to be used for the FractalType
/// </summary>
public string name;
/// <summary>
/// The Asset path to the .noisehlsltemplate file to be used when generating shaders
/// for this FractalType variant
/// </summary>
public string templatePath;
/// <summary>
/// A flag definition the supported number of dimensions that this FractalType implements.
/// This currently is unused.
/// </summary>
public NoiseDimensionFlags supportedDimensions;
/// <summary>
/// List of HlslInput structures defining the HLSL function parameter list for this FractalType
/// </summary>
public List<HlslInput> inputStructDefinition;
/// <summary>
/// Additional include paths to be included in the generated Shader. You can add them to this
/// list or hardcode them somewhere in the Shader itself.
/// </summary>
public List<string> additionalIncludePaths;
// public List<HlslOutput> outputStructDefinition;
}
/// <summary>
/// Interface used for custom FractalType implementations. This should only be used by
/// the FractalType< T > abstract class.
/// </summary>
public interface IFractalType
{
/// <summary>
/// Returns a descriptor struct defining the FractalType
/// </summary>
FractalTypeDescriptor GetDescription();
/// <summary>
/// Returns a string that represents the default data state for the FractalType implementation
/// </summary>
string GetDefaultSerializedString();
/// <summary>
/// Sets up a Material to be used with the FractalType implementation. This function is called
/// from within NoiseSettings.SetupMaterial( Material mat )
/// </summary>
/// <param name="mat"> The Material to setup for use with this FractalType implementation </param>
/// <param name="serializedString">
/// The serialized string representing the FractalType data to
/// be used when setting up the provided Material
/// </param>
void SetupMaterial(Material mat, string serializedString);
/// <summary>
/// Renders the GUI for the FractalType implementation using the data provided by the serialized string.
/// </summary>
/// <param name="serializedString"> A string for the serialized data used by the FractalType implementation </param>
/// <returns>
/// A string for the FractalType implementation's serialized data after having gone through possible
/// changes from user interaction with the GUI
/// </returns>
string DoGUI(string serializedString);
/// <summary>
/// Renders the GUI for the FractalType implementation using the data provided by the serialized string.
/// </summary>
/// <param name="serializedString"> A string for the serialized data used by the FractalType implementation </param>
/// <returns>
/// A string for the FractalType implementation's serialized data after having gone through possible
/// changes from user interaction with the GUI
/// </returns>
string DoGUI(Rect rect, string serializedString);
/// <summary>
/// Converts an object representing the data for a FractalType implementation to a string.
/// This is later serialized and stored in a NoiseSettings Asset.
/// </summary>
/// <param name="target">
/// The data representation for the FractalType implementation to be converted
/// into a string
/// </param>
/// <returns> The string representation of the provided target object </returns>
string ToSerializedString(object target);
/// <summary>
/// Converts a string representing the data for the FractalType implementation into an
/// an actual object reference to be used with said FractalType implementation.
/// </summary>
/// <param name="serializedString">
/// A string representing a serialized object that is
/// used by the FractalType implementation
/// </param>
/// <returns> An object deserialized from the provided string </returns>
object FromSerializedString(string serializedString);
}
/// <summary>
/// A Singleton class used for representing a FractalType implementation. A FractalType class must
/// inherit from this in order to be considered a valid FractalType and included in the noise
/// shader generation and available for use with the various noise tools.
/// </summary>
public abstract class FractalType<T> : ScriptableSingleton<T>, IFractalType where T : FractalType<T>
{
/// <summary>
/// Returns a descriptor struct defining the FractalType
/// </summary>
public abstract FractalTypeDescriptor GetDescription();
/// <summary>
/// When overidden, returns a string that represents the default data state for the FractalType implementation
/// </summary>
public virtual string GetDefaultSerializedString() { return null; }
/// <summary>
/// When overidden, sets up a Material to be used with the FractalType implementation. This function is called
/// from within NoiseSettings.SetupMaterial( Material mat )
/// </summary>
/// <param name="mat"> The Material to setup for use with this FractalType implementation </param>
/// <param name="serializedString">
/// The serialized string representing the FractalType data to
/// be used when setting up the provided Material
/// </param>
public virtual void SetupMaterial(Material mat, string serializedString) { }
/// <summary>
/// When overidden, renders the GUI for the FractalType implementation using the data provided by the serialized string.
/// </summary>
/// <param name="serializedString"> A string for the serialized data used by the FractalType implementation </param>
/// <returns>
/// A string for the FractalType implementation's serialized data after having gone through possible
/// changes from user interaction with the GUI
/// </returns>
public virtual string DoGUI(string serializedString) { return null; }
/// <summary>
/// When overidden, renders the GUI for the FractalType implementation using the data provided by the serialized string.
/// </summary>
/// <param name="serializedString"> A string for the serialized data used by the FractalType implementation </param>
/// <returns>
/// A string for the FractalType implementation's serialized data after having gone through possible
/// changes from user interaction with the GUI
/// </returns>
public virtual string DoGUI(Rect rect, string serializedString) { return null; }
/// <summary>
/// When overidden, converts an object representing the data for a FractalType implementation to a string.
/// This is later serialized and stored in a NoiseSettings Asset.
/// </summary>
/// <param name="target">
/// The data representation for the FractalType implementation to be converted into a string
/// </param>
/// <returns> The string representation of the provided target object </returns>
public virtual string ToSerializedString(object target) { return null; }
/// <summary>
/// When overidden, converts a string representing the data for the FractalType implementation into an
/// an actual object reference to be used with said FractalType implementation.
/// </summary>
/// <param name="serializedString">
/// A string representing a serialized object that is used by the FractalType implementation
/// </param>
/// <returns> An object deserialized from the provided string </returns>
public virtual object FromSerializedString(string serializedString) { return null; }
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 30f8740de321d6044a89ba67e33d3d86
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,216 @@
using UnityEngine;
using UnityEditor;
using System.Text;
using System.Collections.Generic;
namespace Gaia
{
/// <summary>
/// Utility struct for creating all the different shader snippets that are
/// used when generating the shaders for the various NoiseTypes and FractalTypes
/// </summary>
internal struct GeneratedShaderInfo
{
#if UNITY_EDITOR
public FractalTypeDescriptor fractalDesc;
public NoiseTypeDescriptor noiseDesc;
#endif
public string generatedIncludePath { get; private set; }
public string additionalIncludePaths { get; private set; }
public string noiseIncludeStr { get; private set; }
public string variantName { get; private set; }
public string outputDir { get; private set; }
public string noiseStructName {get; private set;}
public string noiseStructDef {get; private set;}
public string fractalStructName {get; private set;}
public string fractalStructDef {get; private set;}
public string fractalParamStr {get; private set;}
public string noiseParamStr {get; private set;}
public string functionInputStr {get; private set;}
public string functionParamStr {get; private set;}
public string getDefaultInputsStr {get; private set;}
public string getInputsStr {get; private set;}
public string getNoiseInputStr {get; private set;}
public string getFractalInputStr {get; private set;}
public string getDefaultFractalInputStr {get; private set;}
public string getDefaultNoiseInputStr {get; private set;}
public string fractalPropertyDefStr {get; private set;}
public int numFractalInputs {get; private set;}
public int numNoiseInputs {get; private set;}
#if UNITY_EDITOR
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);
}
}
#endif
public void ReplaceTags(StringBuilder sb)
{
string fractalMacroDef = fractalStructName.ToUpper() + "_DEF";
string guardedFractalDataDefinitions =
$@"
#ifndef { fractalMacroDef } // [ { fractalMacroDef }
#define { fractalMacroDef }
{fractalStructDef}
{fractalPropertyDefStr}
#endif // ] { fractalMacroDef }
";
#if UNITY_EDITOR
sb.Replace(NoiseLib.Strings.k_tagIncludes, noiseIncludeStr + additionalIncludePaths); // add the noise include
sb.Replace(NoiseLib.Strings.k_tagFractalName, fractalDesc.name); // add fractal name
sb.Replace(NoiseLib.Strings.k_tagNoiseName, noiseDesc.name); // add noise name
sb.Replace(NoiseLib.Strings.k_tagVariantName, variantName); // add combined fractal and noise name
sb.Replace(NoiseLib.Strings.k_tagFractalDataDefinitions, guardedFractalDataDefinitions);
sb.Replace(NoiseLib.Strings.k_tagFunctionParams, functionParamStr);
sb.Replace(NoiseLib.Strings.k_tagFunctionInputs, functionInputStr);
sb.Replace(NoiseLib.Strings.k_tagGetDefaultFractalInput, getDefaultFractalInputStr);
sb.Replace(NoiseLib.Strings.k_tagGetDefaultNoiseInput, getDefaultNoiseInputStr);
sb.Replace(NoiseLib.Strings.k_tagGetDefaultInputs, getDefaultInputsStr);
sb.Replace(NoiseLib.Strings.k_tagGetInputs, getInputsStr);
#endif
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8ac63c2d0bf925e4fba52fa5c5c65736
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,303 @@
using System.Collections.Generic;
using System.Globalization;
namespace Gaia
{
/// <summary>
/// Enum for determining the type of a given HLSL value during shader generation
/// </summary>
public enum HlslValueType
{
Float = 0,
Float2,
Float3,
Float4,
}
/// <summary>
/// Representation for an HLSL float
/// </summary>
public struct HlslFloat
{
/// <summary>
/// The value for the HLSL float
/// </summary>
public float val;
/// <summary>
/// The constructor for an HlslFloat
/// </summary>
/// <param name="val"> The GPU value for this HlslFloat </param>
public HlslFloat(float val)
{
this.val = val;
}
}
/// <summary>
// Representation for an HLSL float2
/// </summary>
public struct HlslFloat2
{
/// <summary>
/// The x-compenent for the HLSL float2
/// </summary>
public float x;
/// <summary>
/// The y-compenent for the HLSL float2
/// </summary>
public float y;
/// <summary>
/// The constructor for an HlslFloat2
/// </summary>
/// <param name="x"> The GPU value of the x-component to be used for this HlslFloat2 </param>
/// <param name="y"> The GPU value of the y-component to be used for this HlslFloat2 </param>
public HlslFloat2(float x, float y)
{
this.x = x;
this.y = y;
}
}
/// <summary>
/// Representation for an HLSL float3
/// </summary>
public struct HlslFloat3
{
/// <summary>
/// The x-compenent for the HLSL float3
/// </summary>
public float x;
/// <summary>
/// The y-compenent for the HLSL float3
/// </summary>
public float y;
/// <summary>
/// The z-compenent for the HLSL float3
/// </summary>
public float z;
/// <summary>
/// The constructor for an HlslFloat3
/// </summary>
/// <param name="x"> The GPU value of the x-component to be used for this HlslFloat3 </param>
/// <param name="y"> The GPU value of the y-component to be used for this HlslFloat3 </param>
/// <param name="z"> The GPU value of the z-component to be used for this HlslFloat3 </param>
public HlslFloat3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
/// <summary>
/// Representation for an HLSL float4
/// </summary>
public struct HlslFloat4
{
/// <summary>
/// The x-compenent for the HLSL float4
/// </summary>
public float x;
/// <summary>
/// The y-compenent for the HLSL float4
/// </summary>
public float y;
/// <summary>
/// The z-compenent for the HLSL float4
/// </summary>
public float z;
/// <summary>
/// The w-compenent for the HLSL float4
/// </summary>
public float w;
/// <summary>
/// The constructor for an HlslFloat4
/// </summary>
/// <param name="x"> The GPU value of the x-component to be used for this HlslFloat4 </param>
/// <param name="y"> The GPU value of the y-component to be used for this HlslFloat4 </param>
/// <param name="z"> The GPU value of the z-component to be used for this HlslFloat4 </param>
/// <param name="w"> The GPU value of the w-component to be used for this HlslFloat4 </param>
public HlslFloat4(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
}
/// <summary>
/// Representation for an HLSL function input parameter
/// </summary>
public struct HlslInput
{
/// <summary>
/// The name of this HLSL function input parameter
/// </summary>
public string name;
/// <summary>
/// Returns the value type for the variable this HlslInput represents
/// </summary>
public HlslValueType valueType
{
get;
private set;
}
private HlslFloat m_floatValue;
/// <summary>
/// Returns the HLSL float value. Sets the HLSL float value and value
/// type to HlslValueType.Float
/// </summary>
public HlslFloat floatValue
{
get { return m_floatValue; }
set
{
valueType = HlslValueType.Float;
m_floatValue = value;
}
}
private HlslFloat2 m_float2Value;
/// <summary>
/// Returns the HLSL float2 value. Sets the HLSL float value and value
/// type to HlslValueType.Float2
/// </summary>
public HlslFloat2 float2Value
{
get { return m_float2Value; }
set
{
valueType = HlslValueType.Float2;
m_float2Value = value;
}
}
private HlslFloat3 m_float3Value;
/// <summary>
/// Returns the HLSL float3 value. Sets the HLSL float value and value
/// type to HlslValueType.Float3
/// </summary>
public HlslFloat3 float3Value
{
get { return m_float3Value; }
set
{
valueType = HlslValueType.Float3;
m_float3Value = value;
}
}
private HlslFloat4 m_float4Value;
/// <summary>
/// Returns the HLSL float4 value. Sets the HLSL float value and value
/// type to HlslValueType.Float4
/// </summary>
public HlslFloat4 float4Value
{
get { return m_float4Value; }
set
{
valueType = HlslValueType.Float4;
m_float4Value = value;
}
}
/// <summary>
/// Returns the string representation of the HlslValueType for this struct
/// </summary>
public string GetHlslValueTypeString()
{
switch(valueType)
{
case HlslValueType.Float:
return "float";
case HlslValueType.Float2:
return "float2";
case HlslValueType.Float3:
return "float3";
case HlslValueType.Float4:
return "float4";
}
return "unsupported_type";
}
/// <summary>
/// Returns the formatted HLSL string for the default value declaration for this struct's HlslValueType
/// </summary>
public string GetDefaultValueString()
{
string valueTypeString = GetHlslValueTypeString();
string constructedValueString = null;
switch(valueType)
{
case HlslValueType.Float:
constructedValueString = floatValue.val.ToString( CultureInfo.InvariantCulture );
break;
case HlslValueType.Float2:
constructedValueString = string.Format( CultureInfo.InvariantCulture,
"{0}({1})", valueTypeString,
float2Value.x.ToString( CultureInfo.InvariantCulture ) + ", " +
float2Value.y.ToString( CultureInfo.InvariantCulture ) );
break;
case HlslValueType.Float3:
constructedValueString = string.Format( CultureInfo.InvariantCulture,
"{0}({1})", valueTypeString,
float3Value.x.ToString( CultureInfo.InvariantCulture ) + ", " +
float3Value.y.ToString( CultureInfo.InvariantCulture ) + ", " +
float3Value.z.ToString( CultureInfo.InvariantCulture ) );
break;
case HlslValueType.Float4:
constructedValueString = string.Format( CultureInfo.InvariantCulture,
"{0}({1})", valueTypeString,
float4Value.x.ToString( CultureInfo.InvariantCulture ) + ", " +
float4Value.y.ToString( CultureInfo.InvariantCulture ) + ", " +
float4Value.z.ToString( CultureInfo.InvariantCulture ) + ", " +
float4Value.w.ToString( CultureInfo.InvariantCulture ) );
break;
default:
return "unsupported_type()";
}
return constructedValueString;
}
}
// public struct HlslOutput
// {
// public string name;
// public HlslValueType valueType;
// public HlslOutput(string name, HlslValueType valueType)
// {
// this.name = name;
// this.valueType = valueType;
// }
// }
// public struct HlslStructDescriptor
// {
// public List<HlslInput> members;
// }
// public struct HlslFunctionDescriptor
// {
// public List<HlslInput> inputs;
// public List<HlslOutput> outputs;
// }
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8ed0c9e52d7bd0f41a31a70e15466f65
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0632acc5c6752b44cb61ea7d5928279f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,64 @@
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
namespace Gaia
{
/// <summary>
/// Struct defining the description for a NoiseShaderGenerator
/// </summary>
public struct ShaderGeneratorDescriptor
{
/// <summary>
/// The name of the NoiseShaderGenerator
/// </summary>
public string name;
/// <summary>
/// The Shader Category for Shaders generated by the NoiseShaderGenerator
/// </summary>
public string shaderCategory;
/// <summary>
/// The path specifying where the Shaders generated by the NoiseShaderGenerator
/// should be written to disk
/// </summary>
public string outputDir;
/// <summary>
/// The path to the ".noisehlsltemplate" file to be used in the NoiseShaderGenerator's
/// Shader generation.
/// </summary>
public string templatePath;
}
/// <summary>
/// Interface for NoiseShaderGenerator. Do not implement this directly. Inherit from the
/// NoiseShaderGenerator< T > generic abstract class, instead.
/// </summary>
public interface INoiseShaderGenerator
{
/// <summary>
/// Returns a description for the NoiseShaderGenerator implementation
/// </summary>
ShaderGeneratorDescriptor GetDescription();
}
/// <summary>
/// Singleton base class for a NoiseShaderGenerator implementation. Inherit from this if you want
/// your shaders to get generated
/// </summary>
public abstract class NoiseShaderGenerator<T> : ScriptableSingleton<T>, INoiseShaderGenerator where T : NoiseShaderGenerator<T>
{
/// <summary>
/// Returns a description for the NoiseShaderGenerator implementation
/// </summary>
public abstract ShaderGeneratorDescriptor GetDescription();
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 687e8089080b8ef4aafcfe30ba68ee6e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,25 @@
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor.Experimental.AssetImporters;
namespace Gaia
{
/// <summary>
/// Asset importer for noise template files. Used to detect when a noise template is imported and checks to see if the shaders need to be regenerated.
/// </summary>
//public class NoiseTemplateImporter : ScriptedImporter
//{
// /// <summary>
// /// Function that is called when an asset with the ".noisehlsltemplate" extension is imported by the AssetDatabase
// /// </summary>
// /// <param name = "ctx"> The context for the imported asset </param>
// public override void OnImportAsset(AssetImportContext ctx)
// {
// //NoiseLib.GenerateHeaderFiles();
// //NoiseLib.GenerateShaders();
// }
//}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a5326fcdff837a045886b69226c13139
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,162 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Gaia
{
/// <summary>
/// Struct used for defining a NoiseType implementation. Information in this struct
/// is used in shader generation and for determining compatabilities with
/// FractalType implementations.
/// </summary>
public struct NoiseTypeDescriptor
{
/// <summary>
/// The name of the NoiseType implementation
/// </summary>
public string name;
/// <summary>
/// The path to the directory where the generated shader files for this NoiseType
/// should be written to disk.
/// </summary>
public string outputDir;
/// <summary>
/// The file path of the HLSL file containing the implementation for this NoiseType
/// </summary>
public string sourcePath;
/// <summary>
/// Flags that represented the dimensions this NoiseType implementation supports
/// </summary>
public NoiseDimensionFlags supportedDimensions;
/// <summary>
/// List of HlslInput structs defining the HLSL function parameter list for this
/// NoiseType implementation. This is used in the noise shader generation.
/// </summary>
public List<HlslInput> inputStructDefinition;
// TODO(wyatt): output structs would be useful for things like curl
// noise and anything requiring derivatives
// public List<HlslOutput> outputStructDefinition;
}
/// <summary>
/// Interface for defining a NoiseType implementation. Don't implement directly from this but
/// instead make your NoiseType implementation inherit from the NoiseType< T > generic abstract class.
/// </summary>
public interface INoiseType
{
/// <summary>
/// Returns a description of the NoiseType implementation
/// </summary>
NoiseTypeDescriptor GetDescription();
/// <summary>
/// Returns a string representing the default state of the data that this NoiseType
/// implementation uses.
/// </summary>
string GetDefaultSerializedString();
/// <summary>
/// Renders the GUI for the NoiseType implementation using the data provided by the serialized string.
/// </summary>
/// <param name="serializedString"> A string for the serialized data used by the NoiseType implementation </param>
/// <returns>
/// A string for the NoiseType implementation's serialized data after having gone through possible
/// changes from user interaction with the GUI
/// </returns>
string DoGUI(string serializedString);
/// <summary>
/// Sets up a Material to be used with the NoiseType implementation. This function is called
/// from within NoiseSettings.SetupMaterial( Material mat )
/// </summary>
/// <param name="mat"> The Material to setup for use with this NoiseType implementation </param>
/// <param name="serializedString">
/// The serialized string representing the NoiseType data to
/// be used when setting up the provided Material
/// </param>
void SetupMaterial(Material mat, string serializedString);
/// <summary>
/// When overidden, converts an object representing the data for a NoiseType implementation to a string.
/// This is later serialized and stored in a NoiseSettings Asset.
/// </summary>
/// <param name="target">
/// The data representation for the NoiseType implementation to be converted
/// into a string
/// </param>
/// <returns> The string representation of the provided target object </returns>
string ToSerializedString(object target);
/// <summary>
/// When overidden, converts a string representing the data for the NoiseType implementation into an
/// an actual object reference to be used with said NoiseType implementation.
/// </summary>
/// <param name="serializedString">
/// A string representing a serialized object that is
/// used by the NoiseType implementation
/// </param>
/// <returns> An object deserialized from the provided string </returns>
object FromSerializedString(string serializedString);
}
#if UNITY_EDITOR
/// <summary>
/// A Singleton class used for representing a NoiseType implementation. A NoiseType class must
/// inherit from this in order to be considered a valid NoiseType and included in the noise
/// shader generation and available for use with the various noise tools.
/// </summary>
public abstract class NoiseType<T> : ScriptableSingleton<T>, INoiseType where T : NoiseType<T>
{
/// <summary>
/// Returns a descriptor struct defining the NoiseType
/// </summary>
public abstract NoiseTypeDescriptor GetDescription();
/// <summary>
/// When overidden, returns a string that represents the default data state for the NoiseType implementation
/// </summary>
public virtual string GetDefaultSerializedString() { return null; }
/// <summary>
/// When overidden, sets up a Material to be used with the NoiseType implementation. This function is called
/// from within NoiseSettings.SetupMaterial( Material mat )
/// </summary>
/// <param name="mat"> The Material to setup for use with this NoiseType implementation </param>
/// <param name="serializedString">
/// The serialized string representing the NoiseType data to
/// be used when setting up the provided Material
/// </param>
public virtual void SetupMaterial(Material mat, string serializedString) { }
/// <summary>
/// When overidden, renders the GUI for the NoiseType implementation using the data provided by the serialized string.
/// </summary>
/// <param name="serializedString"> A string for the serialized data used by the NoiseType implementation </param>
/// <returns>
/// A string for the NoiseType implementation's serialized data after having gone through possible
/// changes from user interaction with the GUI
/// </returns>
public virtual string DoGUI(string serializedString) { return null; }
/// <summary>
/// When overidden, converts an object representing the data for a NoiseType implementation to a string.
/// This is later serialized and stored in a NoiseSettings Asset.
/// </summary>
/// <param name="target">
/// The data representation for the NoiseType implementation to be converted into a string
/// </param>
/// <returns> The string representation of the provided target object </returns>
public virtual string ToSerializedString(object target) { return null; }
/// <summary>
/// When overidden, converts a string representing the data for the NoiseType implementation into an
/// an actual object reference to be used with said NoiseType implementation.
/// </summary>
/// <param name="serializedString">
/// A string representing a serialized object that is used by the NoiseType implementation
/// </param>
/// <returns> An object deserialized from the provided string </returns>
public virtual object FromSerializedString(string serializedString) { return null; }
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c544eb5e7747bd54db98ea4b9de043f3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,329 @@
#if UNITY_EDITOR
using UnityEngine;
using UnityEngine.Rendering;
using UnityEditor;
using System;
using System.Collections.Generic;
using UnityEngine.Experimental.Rendering;
namespace Gaia
{
/// <summary>
/// A utility class for rendering noise defined by NoiseSettings into various Texture types.
/// </summary>
public static class NoiseUtils
{
// private static readonly int kAllSlices = -1;
/// <summary>
/// Number of passes that the builtin noise blit shader contains. The passes include
/// one for blitting 2D noise and one for blitting 3D noise.
/// </summary>
public static readonly int kNumBlitPasses = 2;
private static bool supportsCopyTexture3D
{
get { return (SystemInfo.copyTextureSupport & CopyTextureSupport.Copy3D) != 0; }
}
private static bool supportsCopyTextureRTToTexture
{
get { return (SystemInfo.copyTextureSupport & CopyTextureSupport.RTToTexture) != 0; }
}
static NoiseUtils()
{
}
/*=========================================================================
Material Getter Functions
=========================================================================*/
private static Material s_defaultPreviewMaterial;
private static Material GetDefaultPreviewMaterial()
{
if(s_defaultPreviewMaterial == null)
{
s_defaultPreviewMaterial = new Material(Shader.Find("Hidden/TerrainTools/Noise/Preview"));
}
return s_defaultPreviewMaterial;
}
/// <summary>
/// Returns a Material reference to the default blit material for the given Type of FractalType.
/// </summary>
/// <remarks> Usage is: Material mat = GetDefaultBlitMaterial( typeof(FbmFractalType) ); </remarks>
/// <returns> A reference to the default blit Material for the specified Type of FractalType </return>
/// <param name="fractalType"> The Type for a given FractalType </param>
public static Material GetDefaultBlitMaterial(Type fractalType)
{
return NoiseLib.GetGeneratedMaterial(typeof(NoiseBlitShaderGenerator), fractalType);
}
/// <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() );
}
/*=========================================================================
Blit Noise using Preview shader
=========================================================================*/
/// <summary>
/// Blits the source RenderTexture into the destination RenderTexture using the default Preview Blit Material.
/// This is the blit Material that is used when rendering the NoiseSettings Preview.
/// </summary>
/// <param name = "source"> The source RenderTexture used in the Blit operation </param>
/// <param name = "destination">
/// The destination RenderTexture in the Blit operation
/// This will have the noise preview shader logic applied to it.
/// </param>
public static void BlitPreview2D(RenderTexture source, RenderTexture destination)
{
BlitPreview2D(source, destination, GetDefaultPreviewMaterial());
}
/// <summary>
/// Blits the source RenderTexture into the destination RenderTexture using the specified Material.
/// </summary>
/// <param name = "source"> The source RenderTexture used in the Blit operation </param>
/// <param name = "destination">
/// The destination RenderTexture in the Blit operation
/// This will have the noise preview shader logic applied to it.
/// </param>
/// <param name="mat"> The material to be used in the blit operation </param>
public static void BlitPreview2D(RenderTexture src, RenderTexture dest, Material mat)
{
if(mat == null)
{
Debug.LogError("NoiseUtils::BlitPreview2D: Provided preview material is NULL");
Graphics.Blit(src, dest);
return;
}
Graphics.Blit(src, dest, mat, 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 );
}
/// <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 );
}
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);
}
/// <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;
}
/// <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;
}
/// <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;
}
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 32f30b28138f17a44b58e260d38fb032
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: