Have transparency support in the shader
benrajsdKiksARVR opened this issue · 21 comments
Hi, Is it possible to have base texture as transparent one, so it will blend with background color, like floor/plane edges will be blended with background color due to the smooth transparency in the edge of texture/base map.
you can try changing blend from one zero to SrcAlpha OneMinusSrcAlpha, and renderqueue to 3000
renderqueue to 3000 - done, it shows as transparent in material, what about other parameters, in .shader file it is not available
try add a line
blend SrcAlpha OneMinusSrcAlpha in .shader
try turn on ApplyFillHoleFix in renderer feature
try add a line
blend SrcAlpha OneMinusSrcAlpha in .shader
try turn on ApplyFillHoleFix in renderer feature
Working to a extent
Getting this error
Shader error in 'MobileSSPR/ExampleShader': unrecognized identifier 'Blend' at line 52 (on d3d11)
Also shader become pink due to issue/un recoganized
`//see README here: https://github.com/ColinLeung-NiloCat/UnityURP-MobileScreenSpacePlanarReflection
//just a simple example shader to show how to use SSPR's result texture
Shader "MobileSSPR/ExampleShader"
{
Properties
{
[MainColor] _BaseColor("BaseColor", Color) = (1,1,1,1)
[MainTexture] _BaseMap("BaseMap", 2D) = "black" {}
_Roughness("_Roughness", range(0,1)) = 0.25
[NoScaleOffset]_SSPR_UVNoiseTex("_SSPR_UVNoiseTex", 2D) = "gray" {}
_SSPR_NoiseIntensity("_SSPR_NoiseIntensity", range(-0.2,0.2)) = 0.0
_UV_MoveSpeed("_UV_MoveSpeed (xy only)(for things like water flow)", Vector) = (0,0,0,0)
[NoScaleOffset]_ReflectionAreaTex("_ReflectionArea", 2D) = "white" {}
}
SubShader
{
Pass
{
//================================================================================================
//if "LightMode"="MobileSSPR", this shader will only draw if MobileSSPRRendererFeature is on
Tags { "LightMode"="MobileSSPR" }
//================================================================================================
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
//================================================================================================
#include "MobileSSPRInclude.hlsl"
#pragma multi_compile _ _MobileSSPR
//================================================================================================
Blend SrcAlpha OneMinusSrcAlpha
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float2 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
float3 posWS : TEXCOORD2;
float4 positionHCS : SV_POSITION;
};
//textures
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
TEXTURE2D(_SSPR_UVNoiseTex);
SAMPLER(sampler_SSPR_UVNoiseTex);
TEXTURE2D(_ReflectionAreaTex);
SAMPLER(sampler_ReflectionAreaTex);
//cbuffer
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
half4 _BaseColor;
half _SSPR_NoiseIntensity;
float2 _UV_MoveSpeed;
half _Roughness;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap) + _Time.y*_UV_MoveSpeed;
OUT.screenPos = ComputeScreenPos(OUT.positionHCS);
OUT.posWS = TransformObjectToWorld(IN.positionOS.xyz);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
//base color
half3 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * _BaseColor.rgb;
//noise texture
float2 noise = SAMPLE_TEXTURE2D(_SSPR_UVNoiseTex,sampler_SSPR_UVNoiseTex, IN.uv);
noise = noise *2-1;
noise.y = -abs(noise); //hide missing data, only allow offset to valid location
noise.x *= 0.25;
noise *= _SSPR_NoiseIntensity;
//================================================================================================
//GetResultReflection from SSPR
ReflectionInput reflectionData;
reflectionData.posWS = IN.posWS;
reflectionData.screenPos = IN.screenPos;
reflectionData.screenSpaceNoise = noise;
reflectionData.roughness = _Roughness;
reflectionData.SSPR_Usage = _BaseColor.a;
half3 resultReflection = GetResultReflection(reflectionData);
//================================================================================================
//decide show reflection area
half reflectionArea = SAMPLE_TEXTURE2D(_ReflectionAreaTex,sampler_ReflectionAreaTex, IN.uv);
half3 finalRGB = lerp(baseColor,resultReflection,reflectionArea);
return half4(finalRGB,1);
}
ENDHLSL
}
}
}
`
add this line
Blend SrcAlpha OneMinusSrcAlpha
under tag{}
SubShader
{
Pass
{
Tags { "LightMode"="MobileSSPR", "Blend SrcAlpha OneMinusSrcAlpha"}
Is it like this - still getting error
Tags{}
Blend SrcAlpha OneMinusSrcAlpha
SubShader
{
Pass
{
//================================================================================================
//if "LightMode"="MobileSSPR", this shader will only draw if MobileSSPRRendererFeature is on
Tags { "LightMode"="MobileSSPR" }
Blend SrcAlpha OneMinusSrcAlpha
//================================================================================================
Added, no errors, but transparent base map texture is not working, still opaque only
in the fragment shader's return value alpha, you need to return baseColor's alpha .
Can you help on it where to update
return half4(finalRGB, _BaseColor.a); ? instead of return half4(finalRGB, 1);
Something like this:
half4 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
baseColor.rgb *= _BaseColor.rgb;
return half4(finalRGB,baseColor.a);
baseColor
This is working, entire plane alpha is based on base color alpha, but still texture alpha it is not taking, so edges is still solid only, blending will come when texture alpha applied only
show me your current code
`//see README here: https://github.com/ColinLeung-NiloCat/UnityURP-MobileScreenSpacePlanarReflection
//just a simple example shader to show how to use SSPR's result texture
Shader "MobileSSPR/ExampleShader"
{
Properties
{
[MainColor] _BaseColor("BaseColor", Color) = (1,1,1,1)
[MainTexture] _BaseMap("BaseMap", 2D) = "black" {}
_Roughness("_Roughness", range(0,1)) = 0.25
[NoScaleOffset]_SSPR_UVNoiseTex("_SSPR_UVNoiseTex", 2D) = "gray" {}
_SSPR_NoiseIntensity("_SSPR_NoiseIntensity", range(-0.2,0.2)) = 0.0
_UV_MoveSpeed("_UV_MoveSpeed (xy only)(for things like water flow)", Vector) = (0,0,0,0)
[NoScaleOffset]_ReflectionAreaTex("_ReflectionArea", 2D) = "white" {}
}
SubShader
{
Pass
{
//================================================================================================
//if "LightMode"="MobileSSPR", this shader will only draw if MobileSSPRRendererFeature is on
Tags { "LightMode"="MobileSSPR" }
Blend SrcAlpha OneMinusSrcAlpha
//================================================================================================
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
//================================================================================================
#include "MobileSSPRInclude.hlsl"
#pragma multi_compile _ _MobileSSPR
//================================================================================================
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float2 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
float3 posWS : TEXCOORD2;
float4 positionHCS : SV_POSITION;
};
//textures
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
TEXTURE2D(_SSPR_UVNoiseTex);
SAMPLER(sampler_SSPR_UVNoiseTex);
TEXTURE2D(_ReflectionAreaTex);
SAMPLER(sampler_ReflectionAreaTex);
//cbuffer
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
half4 _BaseColor;
half _SSPR_NoiseIntensity;
float2 _UV_MoveSpeed;
half _Roughness;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap) + _Time.y*_UV_MoveSpeed;
OUT.screenPos = ComputeScreenPos(OUT.positionHCS);
OUT.posWS = TransformObjectToWorld(IN.positionOS.xyz);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
//base color
half3 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * _BaseColor.rgb;
//noise texture
float2 noise = SAMPLE_TEXTURE2D(_SSPR_UVNoiseTex,sampler_SSPR_UVNoiseTex, IN.uv);
noise = noise *2-1;
noise.y = -abs(noise); //hide missing data, only allow offset to valid location
noise.x *= 0.25;
noise *= _SSPR_NoiseIntensity;
//================================================================================================
//GetResultReflection from SSPR
ReflectionInput reflectionData;
reflectionData.posWS = IN.posWS;
reflectionData.screenPos = IN.screenPos;
reflectionData.screenSpaceNoise = noise;
reflectionData.roughness = _Roughness;
reflectionData.SSPR_Usage = _BaseColor.a;
half3 resultReflection = GetResultReflection(reflectionData);
//================================================================================================
//decide show reflection area
half reflectionArea = SAMPLE_TEXTURE2D(_ReflectionAreaTex,sampler_ReflectionAreaTex, IN.uv);
half3 finalRGB = lerp(baseColor,resultReflection,reflectionArea);
return half4(finalRGB, _BaseColor.a);
}
ENDHLSL
}
}
}
`
// in your current shader,
// replace this line
half3 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * _BaseColor.rgb;
// to these 2 lines
half4 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
baseColor.rgb *= _BaseColor.rgb;
/ / then,
// replace this line
return half4(finalRGB, _BaseColor.a);
// to this line
return half4(finalRGB,baseColor.a);
Working. Thank you very much
`//see README here: https://github.com/ColinLeung-NiloCat/UnityURP-MobileScreenSpacePlanarReflection
//just a simple example shader to show how to use SSPR's result texture
Shader "MobileSSPR/ExampleShader"
{
Properties
{
[MainColor] _BaseColor("BaseColor", Color) = (1,1,1,1)
[MainTexture] _BaseMap("BaseMap", 2D) = "black" {}
_Roughness("_Roughness", range(0,1)) = 0.25
[NoScaleOffset]_SSPR_UVNoiseTex("_SSPR_UVNoiseTex", 2D) = "gray" {}
_SSPR_NoiseIntensity("_SSPR_NoiseIntensity", range(-0.2,0.2)) = 0.0
_UV_MoveSpeed("_UV_MoveSpeed (xy only)(for things like water flow)", Vector) = (0,0,0,0)
[NoScaleOffset]_ReflectionAreaTex("_ReflectionArea", 2D) = "white" {}
}
SubShader
{
Pass
{
//================================================================================================
//if "LightMode"="MobileSSPR", this shader will only draw if MobileSSPRRendererFeature is on
Tags { "LightMode"="MobileSSPR" }
Blend SrcAlpha OneMinusSrcAlpha
//================================================================================================
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
//================================================================================================
#include "MobileSSPRInclude.hlsl"
#pragma multi_compile _ _MobileSSPR
//================================================================================================
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float2 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
float3 posWS : TEXCOORD2;
float4 positionHCS : SV_POSITION;
};
//textures
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
TEXTURE2D(_SSPR_UVNoiseTex);
SAMPLER(sampler_SSPR_UVNoiseTex);
TEXTURE2D(_ReflectionAreaTex);
SAMPLER(sampler_ReflectionAreaTex);
//cbuffer
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
half4 _BaseColor;
half _SSPR_NoiseIntensity;
float2 _UV_MoveSpeed;
half _Roughness;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap) + _Time.y*_UV_MoveSpeed;
OUT.screenPos = ComputeScreenPos(OUT.positionHCS);
OUT.posWS = TransformObjectToWorld(IN.positionOS.xyz);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
//base color
//half3 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * _BaseColor.rgb;
half4 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
baseColor.rgb *= _BaseColor.rgb;
//noise texture
float2 noise = SAMPLE_TEXTURE2D(_SSPR_UVNoiseTex,sampler_SSPR_UVNoiseTex, IN.uv);
noise = noise *2-1;
noise.y = -abs(noise); //hide missing data, only allow offset to valid location
noise.x *= 0.25;
noise *= _SSPR_NoiseIntensity;
//================================================================================================
//GetResultReflection from SSPR
ReflectionInput reflectionData;
reflectionData.posWS = IN.posWS;
reflectionData.screenPos = IN.screenPos;
reflectionData.screenSpaceNoise = noise;
reflectionData.roughness = _Roughness;
reflectionData.SSPR_Usage = _BaseColor.a;
half3 resultReflection = GetResultReflection(reflectionData);
//================================================================================================
//decide show reflection area
half reflectionArea = SAMPLE_TEXTURE2D(_ReflectionAreaTex,sampler_ReflectionAreaTex, IN.uv);
half3 finalRGB = lerp(baseColor,resultReflection,reflectionArea);
//return half4(finalRGB, _BaseColor.a);
return half4(finalRGB, baseColor.a);
}
ENDHLSL
}
}
}
`