mirror of
https://github.com/maxartz15/SceneDebugViewer.git
synced 2025-06-27 04:06:07 +02:00
Testing multiple MipMap/TexelDensity solutions
This commit is contained in:
75
Runtime/Shaders/MipMaps.shader
Normal file
75
Runtime/Shaders/MipMaps.shader
Normal file
@ -0,0 +1,75 @@
|
||||
// Ref: https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-28-mipmap-level-measurement
|
||||
// https://github.com/jintiao/MipmapLevel/blob/master/Assets/MipmapColor.shader
|
||||
|
||||
Shader "Hidden/RS/MipMaps"
|
||||
{
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType" = "Opaque" }
|
||||
LOD 100
|
||||
|
||||
Pass
|
||||
{
|
||||
CGPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
#include "RSUtilsCG.cginc"
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float2 uv : TEXCOORD0;
|
||||
float2 uvmip : TEXCOORD1;
|
||||
float4 vertex : SV_POSITION;
|
||||
};
|
||||
|
||||
sampler2D _MainTex;
|
||||
float4 _MainTex_ST;
|
||||
float4 _MainTex_TexelSize;
|
||||
float4 _MipMapColors[15]; // max mipmaps = 1 + floor(log2(maxTexSize))
|
||||
int _Max;
|
||||
|
||||
v2f vert(appdata v)
|
||||
{
|
||||
v2f o;
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
||||
|
||||
int mips = RS_MipCount(_MainTex_TexelSize);
|
||||
float2 mipuv = v.uv * (_MainTex_TexelSize.zw / mips);
|
||||
o.uvmip = TRANSFORM_TEX(mipuv, _MainTex);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
int mipLevels = 1 + floor(log2(max(_MainTex_TexelSize.z, _MainTex_TexelSize.w)));
|
||||
|
||||
// int m = RS_MipMap(i.uvmip, _MainTex_TexelSize);
|
||||
int m = RS_M(i.uvmip, _MainTex_TexelSize);
|
||||
// int m = RS_Mip(i.uvmip, _MainTex_TexelSize, mipLevels);
|
||||
// m = clamp(m, 0, _Max);
|
||||
|
||||
// m = RS_Remap(m, 0, 15, 0, _Max);
|
||||
|
||||
fixed4 mip = _MipMapColors[m];
|
||||
|
||||
fixed4 col = tex2D(_MainTex, i.uv);
|
||||
fixed4 res;
|
||||
res.rgb = lerp(col.rgb, mip.rgb, mip.a);
|
||||
res.a = col.a;
|
||||
|
||||
return res;
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
10
Runtime/Shaders/MipMaps.shader.meta
Normal file
10
Runtime/Shaders/MipMaps.shader.meta
Normal file
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 19d63524eb012af4e8e086121f546128
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
preprocessorOverride: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -2,7 +2,7 @@
|
||||
#define RS_UTILS_CG_INCLUDED
|
||||
|
||||
// https://www.ronja-tutorials.com/post/010-triplanar-mapping/
|
||||
fixed4 SampleTriPlanar(sampler2D tex, float4 tex_ST, float3 position, float3 normal, float sharpness)
|
||||
fixed4 RS_TriPlanar(sampler2D tex, float4 tex_ST, float3 position, float3 normal, float sharpness)
|
||||
{
|
||||
//calculate UV coordinates for three projections
|
||||
float2 uv_front = TRANSFORM_TEX(position.xy, tex);
|
||||
@ -34,4 +34,65 @@ fixed4 SampleTriPlanar(sampler2D tex, float4 tex_ST, float3 position, float3 nor
|
||||
return col;
|
||||
}
|
||||
|
||||
float RS_Remap(float value, float oldMin, float oldMax, float newMin, float newMax)
|
||||
{
|
||||
return (newMin + (value - oldMin) * (newMax - newMin) / (oldMax - oldMin));
|
||||
}
|
||||
|
||||
int RS_MipCount(float4 texelSize)
|
||||
{
|
||||
int m = max(texelSize.z, texelSize.w);
|
||||
int mip = 1 + floor(log2(m));
|
||||
|
||||
return mip;
|
||||
}
|
||||
|
||||
// https://github.com/jintiao/MipmapLevel/blob/master/Assets/MipmapColor.shader
|
||||
int RS_MipMap(float2 uv, float4 texelSize)
|
||||
{
|
||||
// Mipmap calculation.
|
||||
float2 muv = uv * texelSize.zw;
|
||||
float2 dx = ddx(muv);
|
||||
float2 dy = ddy(muv);
|
||||
|
||||
//#if 0
|
||||
// float rho = max(sqrt(dot(dx, dx)), sqrt(dot(dy, dy)));
|
||||
// float lambda = log2(rho);
|
||||
//#else
|
||||
float rho = max(dot(dx, dx), dot(dy, dy));
|
||||
float lambda = 0.5 * log2(rho);
|
||||
//#endif
|
||||
|
||||
return max(int(lambda + 0.5), 0);
|
||||
}
|
||||
|
||||
float RS_Mip(float2 uv, float4 texelSize, int mipLevels)
|
||||
{
|
||||
float2 muv = uv * texelSize.zw;
|
||||
|
||||
float2 derivX = ddx(muv);
|
||||
float2 derivY = ddy(muv);
|
||||
|
||||
float delta_max_sqr = max(dot(derivX, derivX), dot(derivY, derivY));
|
||||
float mip = 0.5 * log2(delta_max_sqr) * mipLevels;
|
||||
|
||||
return mip;
|
||||
}
|
||||
|
||||
// https://gamedev.stackexchange.com/questions/28401/detect-mip-mapping-level-in-the-shader
|
||||
float RS_M(float2 uv, float4 texelSize)
|
||||
{
|
||||
float2 dx = ddx(uv * texelSize.zw);
|
||||
float2 dy = ddy(uv * texelSize.zw);
|
||||
float d = max(dot(dx, dx), dot(dy, dy));
|
||||
|
||||
// Clamp the value to the max mip level counts
|
||||
const float rangeClamp = pow(2.0, (RS_MipCount(texelSize) - 1) * 2.0);
|
||||
d = clamp(d, 1.0, rangeClamp);
|
||||
|
||||
float mipLevel = 0.5 * log2(d);
|
||||
mipLevel = floor(mipLevel);
|
||||
|
||||
return mipLevel;
|
||||
}
|
||||
#endif
|
64
Runtime/Shaders/TexelDensityChecker.shader
Normal file
64
Runtime/Shaders/TexelDensityChecker.shader
Normal file
@ -0,0 +1,64 @@
|
||||
// Ref: https://aras-p.info/blog/2011/05/03/a-way-to-visualize-mip-levels/
|
||||
|
||||
Shader "Hidden/RS/TexelDensityChecker"
|
||||
{
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType"="Opaque" }
|
||||
LOD 100
|
||||
|
||||
Pass
|
||||
{
|
||||
CGPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
#include "RSPropertiesCG.cginc"
|
||||
#include "RSUtilsCG.cginc"
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float2 uv : TEXCOORD0;
|
||||
float2 uvmip : TEXCOORD1;
|
||||
float4 vertex : SV_POSITION;
|
||||
};
|
||||
|
||||
sampler2D _MainTex;
|
||||
float4 _MainTex_ST;
|
||||
float4 _MainTex_TexelSize;
|
||||
|
||||
v2f vert (appdata v)
|
||||
{
|
||||
v2f o;
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
||||
|
||||
int mips = RS_MipCount(_MainTex_TexelSize);
|
||||
float2 mipuv = v.uv * (_MainTex_TexelSize.zw / mips);
|
||||
o.uvmip = TRANSFORM_TEX(mipuv, _MainTex);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
fixed4 col = tex2D(_MainTex, i.uv);
|
||||
fixed4 mip = tex2D(_RS_Texture, i.uvmip);
|
||||
|
||||
fixed4 res;
|
||||
res.rgb = lerp(col.rgb, mip.rgb, mip.a);
|
||||
res.a = col.a;
|
||||
|
||||
return res;
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
10
Runtime/Shaders/TexelDensityChecker.shader.meta
Normal file
10
Runtime/Shaders/TexelDensityChecker.shader.meta
Normal file
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 83a3ee0da908c7542be7a089c047d28f
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
preprocessorOverride: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -23,7 +23,7 @@ Shader "Hidden/RS/TriPlanarLit"
|
||||
|
||||
void surf (Input IN, inout SurfaceOutputStandard o)
|
||||
{
|
||||
fixed4 c = SampleTriPlanar(_RS_Texture, _RS_Texture_ST, IN.worldPos, IN.worldNormal, _RS_Sharpness);
|
||||
fixed4 c = RS_TriPlanar(_RS_Texture, _RS_Texture_ST, IN.worldPos, IN.worldNormal, _RS_Sharpness);
|
||||
|
||||
o.Albedo = c * _RS_Color;
|
||||
o.Metallic = _RS_Metallic;
|
||||
|
@ -38,7 +38,7 @@ Shader "Hidden/RS/TriPlanarUnlit"
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
fixed4 c = SampleTriPlanar(_RS_Texture, _RS_Texture_ST, i.worldPos, i.normal, _RS_Sharpness);
|
||||
fixed4 c = RS_TriPlanar(_RS_Texture, _RS_Texture_ST, i.worldPos, i.normal, _RS_Sharpness);
|
||||
c *= _RS_Color;
|
||||
return c;
|
||||
}
|
||||
|
Reference in New Issue
Block a user