Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/unity-2023-fixes' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Apr 13, 2024
2 parents d0cfc9d + f7237b4 commit 0eb8256
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 21 deletions.
11 changes: 10 additions & 1 deletion Editor/Scripts/GLTFImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Object = UnityEngine.Object;
using UnityGLTF.Loader;
using GLTF;
using UnityEditor.Build;
using UnityGLTF.Extensions;
using UnityGLTF.Plugins;
#if UNITY_2020_2_OR_NEWER
Expand Down Expand Up @@ -835,7 +836,15 @@ string GetUniqueName(string desiredName)
private static void UpdateCustomDependencies()
{
AssetDatabase.RegisterCustomDependency(ColorSpaceDependency, Hash128.Compute((int) PlayerSettings.colorSpace));
AssetDatabase.RegisterCustomDependency(NormalMapEncodingDependency, Hash128.Compute((int) PlayerSettings.GetNormalMapEncoding(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget))));

BuildTargetGroup activeTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
#if UNITY_2023_1_OR_NEWER
var normalEncoding = PlayerSettings.GetNormalMapEncoding(NamedBuildTarget.FromBuildTargetGroup(activeTargetGroup));
#else
var normalEncoding = PlayerSettings.GetNormalMapEncoding(activeTargetGroup);
#endif

AssetDatabase.RegisterCustomDependency(NormalMapEncodingDependency, Hash128.Compute((int) normalEncoding));
}

#if UNITY_2021_3_OR_NEWER
Expand Down
5 changes: 4 additions & 1 deletion Runtime/Scripts/Plugins/Experimental/BakeParticleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public override void BeforeNodeExport(GLTFSceneExporter exporter, GLTFRoot gltfR
var previousSortMode = p.sortMode;
if (p.sortMode == ParticleSystemSortMode.None)
p.sortMode = ParticleSystemSortMode.Distance;

#if UNITY_2023_1_OR_NEWER
p.BakeMesh(m, Camera.main, ParticleSystemBakeMeshOptions.Default);
#else
p.BakeMesh(m, Camera.main, true);
#endif
mf.sharedMesh = m;
mr.sharedMaterial = p.sharedMaterial;
p.sortMode = previousSortMode;
Expand Down
155 changes: 137 additions & 18 deletions Runtime/Scripts/RenderPipelines/RoughRefractionFeature.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#if HAVE_URP_12_OR_NEWER || HAVE_URP_10_OR_NEWER

using System;
using UnityEngine;
using UnityEngine.Rendering;
#if UNITY_2023_3_OR_NEWER
using UnityEngine.Rendering.RenderGraphModule;
#endif
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.Universal.Internal;

Expand Down Expand Up @@ -35,15 +39,21 @@ class CustomRenderPass : CopyColorPass
{ }

#if UNITY_2022_3_OR_NEWER
#pragma warning disable 672
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
#pragma warning restore 672
{
if (renderingData.cameraData.isPreviewCamera)
return;

if (m_source == null || m_destination == null || !m_source.rt || !m_destination.rt)
return;

#pragma warning disable 672
#pragma warning disable 618
base.Execute(context, ref renderingData);
#pragma warning restore 672
#pragma warning restore 618
}

public void Setup(RTHandle source, Downsampling downsampling)
Expand All @@ -65,7 +75,10 @@ public new void Setup(RenderTargetIdentifier source, RenderTargetHandle destinat
this.m_DownsamplingMethod = downsampling;
}
#endif
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)

#pragma warning disable 672
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
#pragma warning restore 672
{
var desc = renderingData.cameraData.cameraTargetDescriptor;
desc.useMipMap = true;
Expand Down Expand Up @@ -97,57 +110,163 @@ public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderin
}
}

CustomRenderPass m_ScriptablePass;
private CustomRenderPass m_RoughRefractionPassNonRG;
#if UNITY_2023_3_OR_NEWER
private bool usingRenderGraph = false;
#endif

#if !UNITY_2022_3_OR_NEWER
RenderTargetHandle m_OpaqueColor;
#endif

/// <inheritdoc/>
/// <inheritdoc/>
public override void Create()
{
#if UNITY_2022_3_OR_NEWER
if (m_ScriptablePass == null)
#if UNITY_2023_3_OR_NEWER
var renderGraphSettings = GraphicsSettings.GetRenderPipelineSettings<RenderGraphSettings>();
usingRenderGraph = !renderGraphSettings.enableRenderCompatibilityMode;
if (!usingRenderGraph)
{
m_ScriptablePass = new CustomRenderPass(RenderPassEvent.AfterRenderingSkybox);
}
#endif
#if UNITY_2022_3_OR_NEWER
if (m_RoughRefractionPassNonRG == null)
{
m_RoughRefractionPassNonRG = new CustomRenderPass(RenderPassEvent.AfterRenderingSkybox);
}
#else
m_OpaqueColor.Init(CAMERA_OPAQUE_TEXTURENAME);
m_OpaqueColor.Init(CAMERA_OPAQUE_TEXTURENAME);
#endif

#if UNITY_2023_3_OR_NEWER
}
else
{
if (m_RoughRefractionPassRG == null)
{
m_RoughRefractionPassRG = new RoughRefractionPassRG();
m_RoughRefractionPassRG.renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
}
}
#endif
}

// Here you can inject one or multiple render passes in the renderer.
// This method is called when setting up the renderer once per-camera.
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{

if (renderingData.cameraData.cameraType != CameraType.Game && renderingData.cameraData.cameraType != CameraType.SceneView)
return;

#if UNITY_2022_3_OR_NEWER
renderer.EnqueuePass(m_ScriptablePass);
if (!usingRenderGraph && m_RoughRefractionPassNonRG != null)
{
renderer.EnqueuePass(m_RoughRefractionPassNonRG);
}
#if UNITY_2023_3_OR_NEWER
else
if (usingRenderGraph && m_RoughRefractionPassRG != null)
{
renderer.EnqueuePass(m_RoughRefractionPassRG);
}
#endif
#else
if (m_ScriptablePass == null)
if (m_RoughRefractionPassNonRG == null)
{
m_ScriptablePass = new CustomRenderPass(RenderPassEvent.AfterRenderingSkybox);
m_RoughRefractionPassNonRG = new CustomRenderPass(RenderPassEvent.AfterRenderingSkybox);
}

#if UNITY_2022_3_OR_NEWER
var identifier = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);
m_ScriptablePass.Setup(identifier, m_OpaqueColor, downsampling);
m_RoughRefractionPassNonRG.Setup(identifier, m_OpaqueColor, downsampling);
#else
m_ScriptablePass.Setup(renderer.cameraColorTarget, m_OpaqueColor, downsampling);
m_RoughRefractionPassNonRG.Setup(renderer.cameraColorTarget, m_OpaqueColor, downsampling);
#endif
renderer.EnqueuePass(m_ScriptablePass);
renderer.EnqueuePass(m_RoughRefractionPassNonRG);
#endif
}

#if UNITY_2022_3_OR_NEWER
public override void SetupRenderPasses(ScriptableRenderer renderer, in RenderingData renderingData)
{
m_ScriptablePass.Setup(renderer.cameraColorTargetHandle, downsampling);
#pragma warning disable 618
m_RoughRefractionPassNonRG.Setup(renderer.cameraColorTargetHandle, downsampling);
#pragma warning restore 618
}

public void OnDestroy()
{
m_ScriptablePass?.Dispose();
m_RoughRefractionPassNonRG?.Dispose();
}
#endif

#if UNITY_2023_3_OR_NEWER
private RoughRefractionPassRG m_RoughRefractionPassRG;

// ######### RenderGraph Version #########
class RoughRefractionPassRG : ScriptableRenderPass
{
// This class stores the data that the render pass needs. The RecordRenderGraph method populates the data and the render graph passes it as a parameter to the rendering function.
class PassData
{
internal TextureHandle activeColorTexture;
internal TextureHandle destinationTexture;
}

// Rendering function that generates the rendering commands for the render pass.
// The RecordRenderGraph method instructs the render graph to use it with the SetRenderFunc method.
static void ExecutePass(PassData data, RasterGraphContext context)
{
Blitter.BlitTexture(context.cmd, data.activeColorTexture, new Vector4(1, 1, 0, 0), 0, false);
}

// This method adds and configures one or more render passes in the render graph.
// This process includes declaring their inputs and outputs, but does not include adding commands to command buffers.
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
{
string passName = "Rough Refraction Pass";

// Add a raster render pass to the render graph. The PassData type parameter determines the type of the passData out variable
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData))
{
// UniversalResourceData contains all the texture handles used by URP, including the active color and depth textures of the camera

UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();

// Populate passData with the data needed by the rendering function of the render pass

// Use the camera’s active color texture as the source texture for the copy
passData.activeColorTexture = resourceData.activeColorTexture;

UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();

TextureDesc rgDesc = new TextureDesc(cameraData.cameraTargetDescriptor.width, cameraData.cameraTargetDescriptor.height);
rgDesc.name = "_CameraOpaqueTexture";
rgDesc.dimension = cameraData.cameraTargetDescriptor.dimension;
rgDesc.clearBuffer = false;
rgDesc.autoGenerateMips = true;
rgDesc.useMipMap = true;
rgDesc.msaaSamples = MSAASamples.None;
rgDesc.filterMode = FilterMode.Bilinear;
rgDesc.wrapMode = TextureWrapMode.Clamp;

rgDesc.bindTextureMS = cameraData.cameraTargetDescriptor.bindMS;
rgDesc.colorFormat = cameraData.cameraTargetDescriptor.graphicsFormat;
rgDesc.depthBufferBits = 0;
rgDesc.isShadowMap = false;
rgDesc.vrUsage = cameraData.cameraTargetDescriptor.vrUsage;

//TextureHandle destination = UniversalRenderer.CreateRenderGraphTexture(renderGraph, desc, "_CameraOpaqueTexture", false);

passData.destinationTexture = renderGraph.CreateTexture(rgDesc);;

builder.UseTexture(passData.activeColorTexture);

builder.SetRenderAttachment(passData.destinationTexture, 0, AccessFlags.Write);
builder.SetGlobalTextureAfterPass(passData.destinationTexture, Shader.PropertyToID("_CameraOpaqueTexture"));

builder.SetRenderFunc((RoughRefractionFeature.RoughRefractionPassRG.PassData data, RasterGraphContext context) => ExecutePass(data, context));
}
}
}
#endif

Expand Down
9 changes: 8 additions & 1 deletion Runtime/Scripts/SceneImporter/ImporterTextures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
using GLTF.Schema;
using GLTF.Utilities;
using Unity.Collections;
using UnityEditor;
using UnityEngine;
using UnityGLTF.Cache;
using UnityGLTF.Extensions;
using UnityGLTF.Loader;
using UnityGLTF.Plugins;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Build;
#endif

namespace UnityGLTF
{
Expand Down Expand Up @@ -207,7 +210,11 @@ protected virtual async Task ConstructUnityTexture(Stream stream, bool markGpuOn
if (isNormal && Context.SourceImporter != null)
{
BuildTargetGroup activeTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
#if UNITY_2023_1_OR_NEWER
if (PlayerSettings.GetNormalMapEncoding(NamedBuildTarget.FromBuildTargetGroup(activeTargetGroup)) == NormalMapEncoding.DXT5nm)
#else
if (PlayerSettings.GetNormalMapEncoding(activeTargetGroup) == NormalMapEncoding.DXT5nm)
#endif
{
convertToDxt5nmFormat = true;
}
Expand Down

0 comments on commit 0eb8256

Please sign in to comment.