- Home /
Shader blending 2 cubemaps?
I am not very experienced in shader coding so thats why I ask.
I have a idea for a cubemap blending system because I dont like the easy way with just replace a cubemap if close enough because this looks like it pops right in.
but for my idea I would need 2 cubemaps in the shader with a lerp slider... If I would generate every frame for all moving objects new cubemaps that would be too expensive for the pc.
can someone please tell me how to code this shader and tell me whats going on in the shader code? because I think cubemaps blending is a bit complicated because its like you blend 6 + 6 textures together...
btw I manipulated shader before so I am not an absolute beginner in shader coding.
I know nothing about shader coding so this may be irrelevant, but there's an example of a 6+6 blend shader here
Answer by Harry64 · Feb 22, 2014 at 11:54 PM
It was very easy... I can really just lerp them.
float4 env1 = texCUBE (_Cube, WorldReflectionVector (IN, o.Normal));
float4 env2 = texCUBE (_Cube2, WorldReflectionVector (IN, o.Normal));
float4 env = lerp( env2, env1, _BlendCubemaps );
Could you please paste the whole shader if you still have it ? I need this too be can't understand anything just from these lines.
All it does is create 2 reflection cubemap values for that pixel, then blends between them on a scale of 0-1. The only difference the rest of the shader would make is if you knew nothing about shaders. In which case, the Unity docs is a good place to find a base to fit this into.
http://docs.unity3d.com/$$anonymous$$anual/SL-SurfaceShaderExamples.html
Hi @Harry64,
I am also looking into this solution, as @$$anonymous$$andra was.
$$anonymous$$ay I please ask to see the rest of the shader, if you still have access to it?
Answer by solarbabies · Apr 13, 2017 at 06:50 AM
For anyone still interested ( @Mandra, @Mirk_Andy ), here's a shader I wrote to accomplish this, using @Harry64's answer, combined with the code in this question.
It allows you to blend between both cubemaps and colors simultaneously or independently. Also supports rotation, exposure, and color transparency.
Shader "Skybox/CubemapSkyboxBlend" {
Properties {
_Tint ("Tint Color", Color) = (.5, .5, .5, 1)
_Tint2 ("Tint Color 2", Color) = (.5, .5, .5, 1)
[Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
_Rotation ("Rotation", Range(0, 360)) = 0
_BlendCubemaps ("Blend Cubemaps", Range(0, 1)) = 0.5
[NoScaleOffset] _Tex ("Cubemap (HDR)", Cube) = "grey" {}
[NoScaleOffset] _Tex2 ("Cubemap (HDR) 2", Cube) = "grey" {}
}
SubShader {
Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
Cull Off ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
samplerCUBE _Tex;
samplerCUBE _Tex2;
float _BlendCubemaps;
half4 _Tex_HDR;
half4 _Tint;
half4 _Tint2;
half _Exposure;
float _Rotation;
float4 RotateAroundYInDegrees (float4 vertex, float degrees)
{
float alpha = degrees * UNITY_PI / 180.0;
float sina, cosa;
sincos(alpha, sina, cosa);
float2x2 m = float2x2(cosa, -sina, sina, cosa);
return float4(mul(m, vertex.xz), vertex.yw).xzyw;
}
struct appdata_t {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 vertex : SV_POSITION;
float3 texcoord : TEXCOORD0;
};
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, RotateAroundYInDegrees(v.vertex, _Rotation));
o.texcoord = v.vertex;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float4 env1 = texCUBE (_Tex, i.texcoord);
float4 env2 = texCUBE (_Tex2, i.texcoord);
float4 env = lerp( env2, env1, _BlendCubemaps );
float4 tint = lerp( _Tint, _Tint2, _BlendCubemaps );
half3 c = DecodeHDR (env, _Tex_HDR);
c = c * tint.rgb * unity_ColorSpaceDouble;
c *= _Exposure;
return half4(c, tint.a);
}
ENDCG
}
}
Fallback Off
}
Ha... came here to share the shader I'd written to solve this and it's exactly like yours @solarbabies ! Thanks.