Ok, I picked this again, but no success, I will put all the code I have, may someone can help a little bit. The result is just weird, and again trying to set the RenderTexture (generate at runtime) at runtime, just doesn’t work.
Code to set the RenderTexture at Runtime (snippet only):
HDRCreatorSky sky = null;
var found = false;
var volumes = FindObjectsOfType<Volume>();
foreach (var volume in volumes)
{
if (volume.sharedProfile.Has<HDRCreatorSky>())
{
found = volume.sharedProfile.TryGet(out sky);
break;
}
}
sky.skyRenderTexture.value = _cacheBackground;
Sky Settings
using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
namespace HeadlessStudio.HDRCreator
{
[VolumeComponentMenu("Sky/HDR Creator Sky")]
// Use 99 for the sky id.
[SkyUniqueID(99)]
public class HDRCreatorSky : SkySettings
{
[Tooltip("Specify the latlong texturemap HDRP uses to render the sky.")]
public RenderTextureParameter skyRenderTexture = new RenderTextureParameter(null);
public override int GetHashCode()
{
int hash = base.GetHashCode();
unchecked
{
hash = skyRenderTexture.value != null ? hash * 23 + skyRenderTexture.GetHashCode() : hash;
}
return hash;
}
public override Type GetSkyRendererType() => typeof(HDRCreatorSkyRenderer);
}
}
SkyRenderer (adapt parts from HDRISky
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
namespace HeadlessStudio.HDRCreator
{
public static class HelperSky
{
public static RenderTexture _sky;
}
public class HDRCreatorSkyRenderer : SkyRenderer
{
Material m_SkyHDRIMaterial; // Renders a cubemap into a render texture (can be a 3D cube or 2D)
MaterialPropertyBlock m_PropertyBlock = new MaterialPropertyBlock();
public HDRCreatorSkyRenderer()
{
}
public override void Build()
{
m_SkyHDRIMaterial = CoreUtils.CreateEngineMaterial(Shader.Find("Hidden/HDRCreator/SkyPreview"));
}
public override void Cleanup()
{
CoreUtils.Destroy(m_SkyHDRIMaterial);
}
protected override bool Update(BuiltinSkyParameters builtinParams)
{
return base.Update(builtinParams);
}
public override void RenderSky(BuiltinSkyParameters builtinParams, bool renderForCubemap, bool renderSunDisk)
{
var hdriSky = (HDRCreatorSky)builtinParams.skySettings;
if (HelperSky._sky != null)
{
//m_PropertyBlock.SetTexture("_MainTex", HelperSky._sky);
m_PropertyBlock.SetTexture("_MainTex", hdriSky.skyRenderTexture.value);
m_PropertyBlock.SetFloat("_Exposure", hdriSky.exposure.value);
m_PropertyBlock.SetFloat("_Rotation", -Mathf.Deg2Rad * hdriSky.rotation.value);
}
m_PropertyBlock.SetMatrix("_PixelCoordToViewDirWS", builtinParams.pixelCoordToViewDirMatrix);
CoreUtils.DrawFullScreen(builtinParams.commandBuffer, m_SkyHDRIMaterial, m_PropertyBlock, renderForCubemap ? 0 : 1);
}
}
}
And the shader based on panoramic shader
Shader "Hidden/HDRCreator/SkyPreview"
{
HLSLINCLUDE
#pragma vertex Vert
#pragma editor_sync_compilation
#pragma target 4.5
#pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
#define LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
#pragma multi_compile _ DEBUG_DISPLAY
#pragma multi_compile SHADOW_LOW SHADOW_MEDIUM SHADOW_HIGH
#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
#define ATTRIBUTES_NEED_NORMAL
#define ATTRIBUTES_NEED_TANGENT
#define VARYINGS_NEED_POSITION_WS
#define VARYINGS_NEED_TANGENT_TO_WORLD
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl"
#define SHADERPASS SHADERPASS_FORWARD_UNLIT
#define HAS_LIGHTLOOP
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonLighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SDF2D.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl"
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
half _Exposure;
float _Rotation;
inline float2 ToRadialCoords(float3 coords)
{
float3 normalizedCoords = normalize(coords);
float latitude = acos(normalizedCoords.y);
float longitude = atan2(normalizedCoords.z, normalizedCoords.x);
float2 sphereCoords = float2(longitude, latitude) * float2(0.5 / PI, 1.0 / PI);
return float2(0.5, 1.0) - sphereCoords;
}
float3 RotateAroundYInDegrees(float3 vertex, float degrees)
{
float alpha = degrees * PI / 180.0;
float sina, cosa;
sincos(alpha, sina, cosa);
float2x2 m = float2x2(cosa, -sina, sina, cosa);
return float3(mul(m, vertex.xz), vertex.y).xzy;
}
struct appdata_t
{
float4 vertex: POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex: SV_POSITION;
float3 texcoord: TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f Vert(appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
o.vertex = TransformWorldToHClip(rotated);
o.texcoord = v.vertex.xyz;
return o;
}
float4 RenderSky(v2f i, float exposure)
{
float2 tc = ToRadialCoords(i.texcoord);
if (tc.x > 1.0)
return float4(0, 0, 0, 1);
tc.x = fmod(tc.x, 1);
float4 c = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, tc);
c *= exposure;
return float4(c.rgb, 1);
}
float4 FragBaking(v2f i): SV_Target
{
return RenderSky(i, 1.0);
}
float4 FragRender(v2f i): SV_Target
{
return RenderSky(i, _Exposure);
}
ENDHLSL
SubShader
{
Pass
{
ZWrite Off
ZTest Always
Blend Off
Cull Off
HLSLPROGRAM
#pragma fragment FragBaking
ENDHLSL
}
Pass
{
ZWrite Off
ZTest LEqual
Blend Off
Cull Off
HLSLPROGRAM
#pragma fragment FragRender
ENDHLSL
}
}
Fallback Off
}