This commit is contained in:
2024-09-20 20:30:10 +02:00
commit 4fabf1a6fd
29169 changed files with 1706941 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
using System;
using UnityEditor.ShaderGraph;
using UnityEngine.Rendering;
namespace UnityEditor.Rendering.CustomRenderTexture.ShaderGraph
{
public static class CustomTextureShaderGraphMenu
{
[MenuItem("Assets/Create/Shader Graph/Custom Render Texture", priority = CoreUtils.Sections.section3 + CoreUtils.Priorities.assetsCreateShaderMenuPriority)]
public static void CreateCustomTextureShaderGraph()
{
var target = (CustomRenderTextureTarget)Activator.CreateInstance(typeof(CustomRenderTextureTarget));
target.TrySetActiveSubTarget(typeof(CustomTextureSubTarget));
var blockDescriptors = new[]
{
BlockFields.SurfaceDescription.BaseColor,
BlockFields.SurfaceDescription.Alpha,
};
GraphUtil.CreateNewGraphWithOutputs(new[] { target }, blockDescriptors);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 592fd794cdd703e4ca365fbf30d4ca8b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,247 @@
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
using UnityEditor;
using UnityEditor.ShaderGraph;
using UnityEditor.UIElements;
using UnityEditor.ShaderGraph.Serialization;
using SubTargetListPool = UnityEngine.Rendering.ListPool<UnityEditor.ShaderGraph.SubTarget>;
using System.Reflection;
namespace UnityEditor.Rendering.CustomRenderTexture.ShaderGraph
{
[GenerateBlocks]
internal struct FullScreenBlocks
{
// TODO: use base color and alpha blocks
public static BlockFieldDescriptor colorBlock = new BlockFieldDescriptor(String.Empty, "Color", "Color",
new ColorRGBAControl(UnityEngine.Color.white), ShaderStage.Fragment);
}
sealed class CustomRenderTextureTarget : Target
{
// Constants
const string kAssetGuid = "a0bae34258e39cd4899b63278c24c086"; // FullscreenPassTarget.cs
// SubTarget
List<SubTarget> m_SubTargets;
List<string> m_SubTargetNames;
int activeSubTargetIndex => m_SubTargets.IndexOf(m_ActiveSubTarget);
// View
PopupField<string> m_SubTargetField;
TextField m_CustomGUIField;
[SerializeField]
JsonData<SubTarget> m_ActiveSubTarget;
[SerializeField]
string m_CustomEditorGUI;
public CustomRenderTextureTarget()
{
displayName = "Custom Render Texture";
isHidden = false;
m_SubTargets = GetSubTargets(this);
m_SubTargetNames = m_SubTargets.Select(x => x.displayName).ToList();
ProcessSubTargetList(ref m_ActiveSubTarget, ref m_SubTargets);
}
public static void ProcessSubTargetList(ref JsonData<SubTarget> activeSubTarget, ref List<SubTarget> subTargets)
{
if (subTargets == null || subTargets.Count == 0)
return;
// assign the initial sub-target, if none is assigned yet
if (activeSubTarget.value == null)
activeSubTarget = subTargets[0];
// Update SubTarget list with active SubTarget
var activeSubTargetType = activeSubTarget.value.GetType();
var activeSubTargetCurrent = subTargets.FirstOrDefault(x => x.GetType() == activeSubTargetType);
var index = subTargets.IndexOf(activeSubTargetCurrent);
subTargets[index] = activeSubTarget;
}
public static List<SubTarget> GetSubTargets<T>(T target) where T : Target
{
// Get Variants
var subTargets = SubTargetListPool.Get();
var typeCollection = TypeCache.GetTypesDerivedFrom<SubTarget>();
foreach (var type in typeCollection)
{
if (type.IsAbstract || !type.IsClass)
continue;
var subTarget = (SubTarget)Activator.CreateInstance(type);
if (!subTarget.isHidden && subTarget.targetType.Equals(typeof(T)))
{
subTarget.target = target;
subTargets.Add(subTarget);
}
}
return subTargets;
}
public SubTarget activeSubTarget
{
get => m_ActiveSubTarget;
set => m_ActiveSubTarget = value;
}
public string customEditorGUI
{
get => m_CustomEditorGUI;
set => m_CustomEditorGUI = value;
}
public override bool IsActive() => activeSubTarget.IsActive();
public override void Setup(ref TargetSetupContext context)
{
// Setup the Target
context.AddAssetDependency(new GUID(kAssetGuid), AssetCollection.Flags.SourceDependency);
// Setup the active SubTarget
ProcessSubTargetList(ref m_ActiveSubTarget, ref m_SubTargets);
m_ActiveSubTarget.value.target = this;
m_ActiveSubTarget.value.Setup(ref context);
// Override EditorGUI
if (!string.IsNullOrEmpty(m_CustomEditorGUI))
{
context.SetDefaultShaderGUI(m_CustomEditorGUI);
}
}
public override void GetFields(ref TargetFieldContext context)
{
var descs = context.blocks.Select(x => x.descriptor);
// Core fields
context.AddField(Fields.GraphVertex, descs.Contains(BlockFields.VertexDescription.Position) ||
descs.Contains(BlockFields.VertexDescription.Normal) ||
descs.Contains(BlockFields.VertexDescription.Tangent));
context.AddField(Fields.GraphPixel);
// SubTarget fields
m_ActiveSubTarget.value.GetFields(ref context);
}
public override void GetActiveBlocks(ref TargetActiveBlockContext context)
{
// SubTarget blocks
m_ActiveSubTarget.value.GetActiveBlocks(ref context);
}
public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)
{
// Core properties
m_SubTargetField = new PopupField<string>(m_SubTargetNames, activeSubTargetIndex);
context.AddProperty("Material", m_SubTargetField, (evt) =>
{
if (Equals(activeSubTargetIndex, m_SubTargetField.index))
return;
registerUndo("Change Material");
m_ActiveSubTarget = m_SubTargets[m_SubTargetField.index];
onChange();
});
// SubTarget properties
m_ActiveSubTarget.value.GetPropertiesGUI(ref context, onChange, registerUndo);
// Custom Editor GUI
// Requires FocusOutEvent
m_CustomGUIField = new TextField("") { value = customEditorGUI };
m_CustomGUIField.RegisterCallback<FocusOutEvent>(s =>
{
if (Equals(customEditorGUI, m_CustomGUIField.value))
return;
registerUndo("Change Custom Editor GUI");
customEditorGUI = m_CustomGUIField.value;
onChange();
});
context.AddProperty("Custom Editor GUI", m_CustomGUIField, (evt) => { });
}
public bool TrySetActiveSubTarget(Type subTargetType)
{
if (!subTargetType.IsSubclassOf(typeof(SubTarget)))
return false;
foreach (var subTarget in m_SubTargets)
{
if (subTarget.GetType().Equals(subTargetType))
{
m_ActiveSubTarget = subTarget;
return true;
}
}
return false;
}
public override bool WorksWithSRP(RenderPipelineAsset scriptableRenderPipeline) => true;
public override bool IsNodeAllowedByTarget(System.Type nodeType)
{
SRPFilterAttribute srpFilter = NodeClassCache.GetAttributeOnNodeType<SRPFilterAttribute>(nodeType);
bool allowed = true;
if (srpFilter != null)
allowed = false;
return allowed;
}
}
static class FullscreePasses
{
public static PassDescriptor CustomRenderTexture = new PassDescriptor
{
// Definition
referenceName = "SHADERPASS_CUSTOM_RENDER_TEXTURE",
useInPreview = true,
// Template
passTemplatePath = AssetDatabase.GUIDToAssetPath("afa536a0de48246de92194c9e987b0b8"), // CustomTextureSubShader.template
// Port Mask
validVertexBlocks = new BlockFieldDescriptor[]
{
BlockFields.VertexDescription.Position,
BlockFields.VertexDescription.Normal,
BlockFields.VertexDescription.Tangent,
},
validPixelBlocks = new BlockFieldDescriptor[]
{
BlockFields.SurfaceDescription.BaseColor,
BlockFields.SurfaceDescription.Alpha,
},
// Fields
structs = new StructCollection
{
{ Structs.Attributes },
{ Structs.SurfaceDescriptionInputs },
{ Structs.VertexDescriptionInputs },
},
requiredFields = new FieldCollection()
{
StructFields.Attributes.color,
StructFields.Attributes.uv0,
StructFields.Varyings.color,
StructFields.Varyings.texCoord0,
},
fieldDependencies = new DependencyCollection()
{
{ FieldDependencies.Default },
},
};
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a0bae34258e39cd4899b63278c24c086
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,279 @@
// Ported from CGinc version
#ifndef UNITY_CUSTOM_TEXTURE_INCLUDED
#define UNITY_CUSTOM_TEXTURE_INCLUDED
#ifndef UNITY_PI
#define UNITY_PI 3.14159265358979323846
#endif
// Keep in sync with CustomRenderTexture.h
#define kCustomTextureBatchSize 16
#define CRT_DIMENSION_2D 0.0
#define CRT_DIMENSION_3D 1.0
#define CRT_DIMENSION_CUBE 2.0
struct appdata_customrendertexture
{
uint vertexID : SV_VertexID;
};
// User facing vertex to fragment shader structure
struct v2f_customrendertexture
{
float4 vertex : SV_POSITION;
float3 localTexcoord : TEXCOORD0; // Texcoord local to the update zone (== globalTexcoord if no partial update zone is specified)
float3 globalTexcoord : TEXCOORD1; // Texcoord relative to the complete custom texture
uint primitiveID : TEXCOORD2; // Index of the update zone (correspond to the index in the updateZones of the Custom Texture)
float3 direction : TEXCOORD3; // For cube textures, direction of the pixel being rendered in the cubemap
};
float2 CustomRenderTextureRotate2D(float2 pos, float angle)
{
float sn = sin(angle);
float cs = cos(angle);
return float2( pos.x * cs - pos.y * sn, pos.x * sn + pos.y * cs);
}
// Built-in unity functions and matrices:
float4 _Time, _SinTime, _CosTime, unity_DeltaTime;
// ================================
// PER FRAME CONSTANTS
// ================================
#if defined(USING_STEREO_MATRICES)
#define glstate_matrix_projection unity_StereoMatrixP[unity_StereoEyeIndex]
#define unity_MatrixV unity_StereoMatrixV[unity_StereoEyeIndex]
#define unity_MatrixInvV unity_StereoMatrixInvV[unity_StereoEyeIndex]
#define unity_MatrixVP unity_StereoMatrixVP[unity_StereoEyeIndex]
#define unity_CameraProjection unity_StereoCameraProjection[unity_StereoEyeIndex]
#define unity_CameraInvProjection unity_StereoCameraInvProjection[unity_StereoEyeIndex]
#define unity_WorldToCamera unity_StereoWorldToCamera[unity_StereoEyeIndex]
#define unity_CameraToWorld unity_StereoCameraToWorld[unity_StereoEyeIndex]
#else
#if !defined(USING_STEREO_MATRICES)
float4x4 glstate_matrix_projection;
float4x4 unity_MatrixV;
float4x4 unity_MatrixInvV;
float4x4 unity_MatrixVP;
float4x4 unity_ObjectToWorld;
float4 unity_StereoScaleOffset;
#endif
#endif
// Internal
float4 CustomRenderTextureCenters[kCustomTextureBatchSize];
float4 CustomRenderTextureSizesAndRotations[kCustomTextureBatchSize];
float CustomRenderTexturePrimitiveIDs[kCustomTextureBatchSize];
float4 CustomRenderTextureParameters;
#define CustomRenderTextureUpdateSpace CustomRenderTextureParameters.x // Normalized(0)/PixelSpace(1)
#define CustomRenderTexture3DTexcoordW CustomRenderTextureParameters.y
#define CustomRenderTextureIs3D (CustomRenderTextureParameters.z == CRT_DIMENSION_3D)
#define CustomRenderTextureDimension CustomRenderTextureParameters.z
// User facing uniform variables
float4 _CustomRenderTextureInfo; // x = width, y = height, z = depth, w = face/3DSlice
// Helpers
#define _CustomRenderTextureWidth _CustomRenderTextureInfo.x
#define _CustomRenderTextureHeight _CustomRenderTextureInfo.y
#define _CustomRenderTextureDepth _CustomRenderTextureInfo.z
// Those two are mutually exclusive so we can use the same slot
#define _CustomRenderTextureCubeFace _CustomRenderTextureInfo.w
#define _CustomRenderTexture3DSlice _CustomRenderTextureInfo.w
float _CustomRenderTextureMipLevel;
TEXTURE2D(_SelfTexture2D);
float4 _SelfTexture2D_TexelSize;
SAMPLER(sampler_SelfTexture2D);
TEXTURECUBE(_SelfTextureCube);
float4 _SelfTextureCube_TexelSize;
SAMPLER(sampler_SelfTextureCube);
TEXTURE3D(_SelfTexture3D);
float4 _SelfTexture3D_TexelSize;
SAMPLER(sampler_SelfTexture3D);
float3 ComputeCubemapDirectionFromUV(float2 uv, int cubeFace)
{
float2 xy = uv * 2.0 - 1.0;
float3 direction;
if(cubeFace == 0.0)
{
direction = normalize(float3(1.0, -xy.y, -xy.x));
}
else if(cubeFace == 1.0)
{
direction = normalize(float3(-1.0, -xy.y, xy.x));
}
else if(cubeFace == 2.0)
{
direction = normalize(float3(xy.x, 1.0, xy.y));
}
else if(cubeFace == 3.0)
{
direction = normalize(float3(xy.x, -1.0, -xy.y));
}
else if(cubeFace == 4.0)
{
direction = normalize(float3(xy.x, -xy.y, 1.0));
}
else if(cubeFace == 5.0)
{
direction = normalize(float3(-xy.x, -xy.y, -1.0));
}
else
{
direction = float3(0, 0, 0);
}
return direction;
}
float3 CustomRenderTextureComputeCubeDirection(float2 globalTexcoord)
{
return ComputeCubemapDirectionFromUV(globalTexcoord, _CustomRenderTextureCubeFace);
}
// standard custom texture vertex shader that should always be used
v2f_customrendertexture CustomRenderTextureVertexShader(appdata_customrendertexture IN)
{
v2f_customrendertexture OUT;
#if UNITY_UV_STARTS_AT_TOP
const float2 vertexPositions[6] =
{
{ -1.0f, 1.0f },
{ -1.0f, -1.0f },
{ 1.0f, -1.0f },
{ 1.0f, 1.0f },
{ -1.0f, 1.0f },
{ 1.0f, -1.0f }
};
const float2 texCoords[6] =
{
{ 0.0f, 0.0f },
{ 0.0f, 1.0f },
{ 1.0f, 1.0f },
{ 1.0f, 0.0f },
{ 0.0f, 0.0f },
{ 1.0f, 1.0f }
};
#else
const float2 vertexPositions[6] =
{
{ 1.0f, 1.0f },
{ -1.0f, -1.0f },
{ -1.0f, 1.0f },
{ -1.0f, -1.0f },
{ 1.0f, 1.0f },
{ 1.0f, -1.0f }
};
const float2 texCoords[6] =
{
{ 1.0f, 1.0f },
{ 0.0f, 0.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 0.0f }
};
#endif
uint primitiveID = IN.vertexID / 6;
uint vertexID = IN.vertexID % 6;
float3 updateZoneCenter = CustomRenderTextureCenters[primitiveID].xyz;
float3 updateZoneSize = CustomRenderTextureSizesAndRotations[primitiveID].xyz;
float rotation = CustomRenderTextureSizesAndRotations[primitiveID].w * UNITY_PI / 180.0f;
#if !UNITY_UV_STARTS_AT_TOP
rotation = -rotation;
#endif
// Normalize rect if needed
if (CustomRenderTextureUpdateSpace > 0.0) // Pixel space
{
// Normalize xy because we need it in clip space.
updateZoneCenter.xy /= _CustomRenderTextureInfo.xy;
updateZoneSize.xy /= _CustomRenderTextureInfo.xy;
}
else // normalized space
{
// Un-normalize depth because we need actual slice index for culling
updateZoneCenter.z *= _CustomRenderTextureInfo.z;
updateZoneSize.z *= _CustomRenderTextureInfo.z;
}
// Compute rotation
// Compute quad vertex position
float2 clipSpaceCenter = updateZoneCenter.xy * 2.0 - 1.0;
float2 pos = vertexPositions[vertexID] * updateZoneSize.xy;
pos = CustomRenderTextureRotate2D(pos, rotation);
pos.x += clipSpaceCenter.x;
#if UNITY_UV_STARTS_AT_TOP
pos.y += clipSpaceCenter.y;
#else
pos.y -= clipSpaceCenter.y;
#endif
// For 3D texture, cull quads outside of the update zone
// This is neeeded in additional to the preliminary minSlice/maxSlice done on the CPU because update zones can be disjointed.
// ie: slices [1..5] and [10..15] for two differents zones so we need to cull out slices 0 and [6..9]
if (CustomRenderTextureIs3D)
{
int minSlice = (int)(updateZoneCenter.z - updateZoneSize.z * 0.5);
int maxSlice = minSlice + (int)updateZoneSize.z;
if (_CustomRenderTexture3DSlice < minSlice || _CustomRenderTexture3DSlice >= maxSlice)
{
pos.xy = float2(1000.0, 1000.0); // Vertex outside of ncs
}
}
OUT.vertex = float4(pos, 0.0, 1.0);
OUT.primitiveID = asuint(CustomRenderTexturePrimitiveIDs[primitiveID]);
OUT.localTexcoord = float3(texCoords[vertexID], CustomRenderTexture3DTexcoordW);
OUT.globalTexcoord = float3(pos.xy * 0.5 + 0.5, CustomRenderTexture3DTexcoordW);
#if UNITY_UV_STARTS_AT_TOP
OUT.globalTexcoord.y = 1.0 - OUT.globalTexcoord.y;
#endif
OUT.direction = CustomRenderTextureComputeCubeDirection(OUT.globalTexcoord.xy);
return OUT;
}
struct appdata_init_customrendertexture
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
// User facing vertex to fragment structure for initialization materials
struct v2f_init_customrendertexture
{
float4 vertex : SV_POSITION;
float3 texcoord : TEXCOORD0;
float3 direction : TEXCOORD1;
};
// standard custom texture vertex shader that should always be used for initialization shaders
v2f_init_customrendertexture InitCustomRenderTextureVertexShader (appdata_init_customrendertexture v)
{
v2f_init_customrendertexture o;
o.vertex = v.vertex;
o.texcoord = float3(v.texcoord.xy, CustomRenderTexture3DTexcoordW);
o.direction = CustomRenderTextureComputeCubeDirection(v.texcoord.xy);
return o;
}
#endif // UNITY_CUSTOM_TEXTURE_INCLUDED

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 8f43a880cf1819049aa196a7696ee562
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
preprocessorOverride: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
#ifndef CUSTOM_TEXTURE_GTRAPH
#define CUSTOM_TEXTURE_GTRAPH
float4 SRGBToLinear( float4 c ) { return c; }
float3 SRGBToLinear( float3 c ) { return c; }
// Unpack normal as DXT5nm (1, y, 1, x) or BC5 (x, y, 0, 1)
// Note neutral texture like "bump" is (0, 0, 1, 1) to work with both plain RGB normal and DXT5nm/BC5
float3 UnpackNormalmapRGorAG(float4 packednormal)
{
// This do the trick
packednormal.x *= packednormal.w;
float3 normal;
normal.xy = packednormal.xy * 2 - 1;
normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
return normal;
}
inline float3 UnpackNormal(float4 packednormal)
{
#if defined(UNITY_NO_DXT5nm)
return packednormal.xyz * 2 - 1;
#else
return UnpackNormalmapRGorAG(packednormal);
#endif
}
#endif // CUSTOM_TEXTURE_GTRAPH

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 96a8b300b2dfb474bb4fd8118749fdec
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
preprocessorOverride: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,69 @@
using UnityEngine;
using UnityEditor.ShaderGraph;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Internal;
namespace UnityEditor.Rendering.CustomRenderTexture.ShaderGraph
{
[Title("Custom Render Texture", "Self")]
[SubTargetFilter(typeof(CustomTextureSubTarget))]
class CustomTextureSelf : AbstractMaterialNode, IGeneratesFunction
{
private const string kOutputSlotSelf2DName = "Self Texture 2D";
private const string kOutputSlotSelfCubeName = "Self Texture Cube";
private const string kOutputSlotSelf3DName = "Self Texture 3D";
public const int OutputSlotSelf2DId = 5;
public const int OutputSlotSelfCubeId = 6;
public const int OutputSlotSelf3DId = 7;
public CustomTextureSelf()
{
name = "Custom Render Texture Self";
UpdateNodeAfterDeserialization();
}
protected int[] validSlots => new[] { OutputSlotSelf2DId, OutputSlotSelfCubeId, OutputSlotSelf3DId };
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Texture2DMaterialSlot(OutputSlotSelf2DId, kOutputSlotSelf2DName, kOutputSlotSelf2DName, SlotType.Output, ShaderStageCapability.Fragment, false) { bareResource = true });
AddSlot(new CubemapMaterialSlot(OutputSlotSelfCubeId, kOutputSlotSelfCubeName, kOutputSlotSelfCubeName, SlotType.Output, ShaderStageCapability.Fragment, false) { bareResource = true });
AddSlot(new Texture3DMaterialSlot(OutputSlotSelf3DId, kOutputSlotSelf3DName, kOutputSlotSelf3DName, SlotType.Output, ShaderStageCapability.Fragment, false) { bareResource = true });
RemoveSlotsNameNotMatching(validSlots);
}
public override string GetVariableNameForSlot(int slotId)
{
switch (slotId)
{
case OutputSlotSelf2DId:
return "UnityBuildTexture2DStructNoScale(_SelfTexture2D)";
case OutputSlotSelfCubeId:
return "UnityBuildTextureCubeStruct(_SelfTextureCube)";
default:
return "UnityBuildTexture3DStruct(_SelfTexture3D)";
}
}
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
// For preview only we declare CRT defines
if (generationMode == GenerationMode.Preview)
{
registry.builder.AppendLine("#if !defined(UNITY_CRT_PREVIEW_TEXTURE) && !defined(UNITY_CUSTOM_TEXTURE_INCLUDED)");
registry.builder.AppendLine("#define UNITY_CRT_PREVIEW_TEXTURE");
registry.builder.AppendLine("TEXTURE2D(_SelfTexture2D);");
registry.builder.AppendLine("SAMPLER(sampler_SelfTexture2D);");
registry.builder.AppendLine("float4 _SelfTexture2D_TexelSize;");
registry.builder.AppendLine("TEXTURECUBE(_SelfTextureCube);");
registry.builder.AppendLine("SAMPLER(sampler_SelfTextureCube);");
registry.builder.AppendLine("float4 _SelfTextureCube_TexelSize;");
registry.builder.AppendLine("TEXTURE3D(_SelfTexture3D);");
registry.builder.AppendLine("SAMPLER(sampler_SelfTexture3D);");
registry.builder.AppendLine("float4 sampler_SelfTexture3D_TexelSize;");
registry.builder.AppendLine("#endif");
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 612128dfda49f7c45861ebe0aaa6dfb6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,66 @@
using UnityEngine;
using UnityEditor.ShaderGraph;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Internal;
namespace UnityEditor.Rendering.CustomRenderTexture.ShaderGraph
{
[Title("Custom Render Texture", "Size")]
[SubTargetFilter(typeof(CustomTextureSubTarget))]
class CustomTextureSize : AbstractMaterialNode, IGeneratesFunction
{
private const string kOutputSlotWidthName = "Texture Width";
private const string kOutputSlotHeightName = "Texture Height";
private const string kOutputSlotDepthName = "Texture Depth";
public const int OutputSlotWidthId = 0;
public const int OutputSlotHeightId = 1;
public const int OutputSlotDepthId = 2;
public CustomTextureSize()
{
name = "Custom Render Texture Size";
UpdateNodeAfterDeserialization();
}
protected int[] validSlots => new[] { OutputSlotWidthId, OutputSlotHeightId, OutputSlotDepthId };
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector1MaterialSlot(OutputSlotWidthId, kOutputSlotWidthName, kOutputSlotWidthName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotHeightId, kOutputSlotHeightName, kOutputSlotHeightName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotDepthId, kOutputSlotDepthName, kOutputSlotDepthName, SlotType.Output, 0));
RemoveSlotsNameNotMatching(validSlots);
}
public override string GetVariableNameForSlot(int slotId)
{
switch (slotId)
{
case OutputSlotHeightId:
return "_CustomRenderTextureHeight";
case OutputSlotDepthId:
return "_CustomRenderTextureDepth";
default:
return "_CustomRenderTextureWidth";
}
}
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
// For preview only we declare CRT defines
if (generationMode == GenerationMode.Preview)
{
registry.builder.AppendLine("#ifndef _CustomRenderTextureHeight");
registry.builder.AppendLine("#define _CustomRenderTextureHeight 1.0");
registry.builder.AppendLine("#endif");
registry.builder.AppendLine("#ifndef _CustomRenderTextureWidth");
registry.builder.AppendLine("#define _CustomRenderTextureWidth 1.0");
registry.builder.AppendLine("#endif");
registry.builder.AppendLine("#ifndef _CustomRenderTextureDepth");
registry.builder.AppendLine("#define _CustomRenderTextureDepth 1.0");
registry.builder.AppendLine("#endif");
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9f969f68890032b439714ed42dc032ff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,55 @@
using UnityEngine;
using UnityEditor.ShaderGraph;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Internal;
namespace UnityEditor.Rendering.CustomRenderTexture.ShaderGraph
{
[Title("Custom Render Texture", "Slice Index / Cubemap Face")]
[SubTargetFilter(typeof(CustomTextureSubTarget))]
class CustomTextureSlice : AbstractMaterialNode, IGeneratesFunction
{
private const string kOutputSlotCubeFaceName = "Texture Cube Face";
private const string kOutputSlot3DSliceName = "Texture Depth Slice";
public const int OutputSlotCubeFaceId = 3;
public const int OutputSlot3DSliceId = 4;
public CustomTextureSlice()
{
name = "Slice Index / Cubemap Face";
UpdateNodeAfterDeserialization();
}
protected int[] validSlots => new[] { OutputSlotCubeFaceId, OutputSlot3DSliceId };
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector1MaterialSlot(OutputSlotCubeFaceId, kOutputSlotCubeFaceName, kOutputSlotCubeFaceName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlot3DSliceId, kOutputSlot3DSliceName, kOutputSlot3DSliceName, SlotType.Output, 0));
RemoveSlotsNameNotMatching(validSlots);
}
public override string GetVariableNameForSlot(int slotId)
{
switch (slotId)
{
case OutputSlotCubeFaceId:
return "_CustomRenderTextureCubeFace";
default:
case OutputSlot3DSliceId:
return "_CustomRenderTexture3DSlice";
}
}
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
// For preview only we declare CRT defines
if (generationMode == GenerationMode.Preview)
{
registry.builder.AppendLine("#define _CustomRenderTextureCubeFace 0.0");
registry.builder.AppendLine("#define _CustomRenderTexture3DSlice 0.0");
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5ae6cbe9ce5f48c499f9f003c826902b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,130 @@
Pass
{
$splice(PassName)
Tags
{
$splice(LightMode)
}
// Debug
$splice(Debug)
// --------------------------------------------------
// Pass
Lighting Off
Blend One Zero
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl"
#include "Packages/com.unity.shadergraph/Editor/Generation/Targets/CustomRenderTexture/CustomTexture.hlsl"
#include "Packages/com.unity.shadergraph/Editor/Generation/Targets/CustomRenderTexture/CustomTextureGraph.hlsl"
#include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl"
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag
#pragma target 3.0
// Pragmas
$splice(PassPragmas)
// Keywords
$splice(PassKeywords)
$splice(GraphKeywords)
struct SurfaceDescriptionInputs
{
// update input values
float4 uv0;
float4 uv1;
uint primitiveID;
float3 direction;
// ShaderGraph accessors:
float3 WorldSpaceViewDirection;
float3 ObjectSpaceViewDirection;
float3 ObjectSpacePosition;
float3 TimeParameters;
float3 WorldSpaceNormal;
float3 ObjectSpaceNormal;
float2 NDCPosition;
float4 ScreenPosition;
};
SurfaceDescriptionInputs ConvertV2FToSurfaceInputs( v2f_customrendertexture IN )
{
SurfaceDescriptionInputs o;
o.uv0 = float4(IN.localTexcoord, 0);
o.uv1 = float4(IN.globalTexcoord, 0);
o.primitiveID = IN.primitiveID;
o.direction = normalize(IN.direction);
// other space of view direction are not supported
$SurfaceDescriptionInputs.WorldSpaceViewDirection: o.WorldSpaceViewDirection = o.direction;
$SurfaceDescriptionInputs.ObjectSpaceViewDirection: o.ObjectSpaceViewDirection = o.direction;
$SurfaceDescriptionInputs.ObjectSpacePosition: o.ObjectSpacePosition = o.direction;
$SurfaceDescriptionInputs.TimeParameters: o.TimeParameters = float3(_Time.y, _SinTime.x, _CosTime.y);
$SurfaceDescriptionInputs.WorldSpaceNormal: o.WorldSpaceNormal = o.direction;
$SurfaceDescriptionInputs.ObjectSpaceNormal o.ObjectSpaceNormal = o.direction;
$SurfaceDescriptionInputs.NDCPosition: o.NDCPosition = o.uv0.xy;
$SurfaceDescriptionInputs.ScreenPosition: o.ScreenPosition = float4(o.uv0.xy, 0, 1);
// Unsupported properties:
$SurfaceDescriptionInputs.WorldSpaceBiTangent: #error 'WorldSpaceBiTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ViewSpaceNormal: #error 'ViewSpaceNormal' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.TangentSpaceNormal: #error 'TangentSpaceNormal' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.WorldSpaceTangent: #error 'WorldSpaceTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.WorldSpaceBiTangent: #error 'WorldSpaceBiTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ObjectSpaceTangent: #error 'ObjectSpaceTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ViewSpaceTangent: #error 'ViewSpaceTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.TangentSpaceTangent: #error 'TangentSpaceTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ObjectSpaceBiTangent: #error 'ObjectSpaceBiTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ViewSpaceBiTangent: #error 'ViewSpaceBiTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.TangentSpaceBiTangent: #error 'TangentSpaceBiTangent' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ViewSpaceViewDirection: #error 'ViewSpaceViewDirection' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.TangentSpaceViewDirection: #error 'TangentSpaceViewDirection' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ViewSpacePosition: #error ViewSpacePosition'' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.TangentSpacePosition: #error 'TangentSpacePosition' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.WorldSpacePositionPredisplacement: #error 'WorldSpacePositionPredisplacement' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ObjectSpacePositionPredisplacement: #error 'ObjectSpacePositionPredisplacement' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ViewSpacePositionPredisplacement: #error 'ViewSpacePositionPredisplacement' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.TangentSpacePositionPredisplacement: #error 'TangentSpacePositionPredisplacement' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.AbsoluteWorldSpacePositionPredisplacement: #error 'AbsoluteWorldSpacePositionPredisplacement' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.uv2: #error 'uv2' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.uv3: #error 'uv3' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.VertexColor: #error 'VertexColor' is not available in Custom Render Textures.
// We can't fake the positions because we can't differentiate Cube and 2D custom render textures
$SurfaceDescriptionInputs.WorldSpacePosition: #error 'WorldSpacePosition' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.ObjectSpacePosition: #error 'ObjectSpacePosition' is not available in Custom Render Textures.
$SurfaceDescriptionInputs.AbsoluteWorldSpacePosition: #error 'AbsoluteWorldSpacePosition' is not available in Custom Render Textures.
return o;
}
// --------------------------------------------------
// Graph
// Graph Properties
$splice(GraphProperties)
// Graph Includes
$splice(GraphIncludes)
// Graph Functions
$splice(GraphFunctions)
// Graph Pixel
$splice(GraphPixel)
float4 frag(v2f_customrendertexture IN) : SV_Target
{
SurfaceDescriptionInputs surfaceInput = ConvertV2FToSurfaceInputs(IN);
SurfaceDescription surface = SurfaceDescriptionFunction(surfaceInput);
return float4(surface.BaseColor, surface.Alpha);
}
ENDHLSL
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: afa536a0de48246de92194c9e987b0b8
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.ShaderGraph;
using UnityEditor.ShaderGraph.Legacy;
namespace UnityEditor.Rendering.CustomRenderTexture.ShaderGraph
{
sealed class CustomTextureSubTarget : SubTarget<CustomRenderTextureTarget>
{
const string kAssetGuid = "5b2d4724a38a5485ba5e7dc2f7d86f1a"; // CustomTextureSubTarget.cs
internal static FieldDescriptor colorField = new FieldDescriptor(String.Empty, "Color", string.Empty);
public CustomTextureSubTarget()
{
isHidden = false;
displayName = "Custom Render Texture";
}
public override bool IsActive() => true;
public override void Setup(ref TargetSetupContext context)
{
context.AddAssetDependency(new GUID(kAssetGuid), AssetCollection.Flags.SourceDependency);
context.AddSubShader(SubShaders.CustomRenderTexture);
}
public override void GetFields(ref TargetFieldContext context)
{
context.AddField(colorField, true);
}
public override void GetActiveBlocks(ref TargetActiveBlockContext context)
{
context.AddBlock(BlockFields.SurfaceDescription.BaseColor);
context.AddBlock(BlockFields.SurfaceDescription.Alpha);
}
public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)
{
}
static class SubShaders
{
public static SubShaderDescriptor CustomRenderTexture = new SubShaderDescriptor()
{
generatesPreview = true,
passes = new PassCollection
{
{ FullscreePasses.CustomRenderTexture },
},
};
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5b2d4724a38a5485ba5e7dc2f7d86f1a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: