I’m generating custom reflections with Camera.RenderToCubemap() and setting it globally in shaders with Shader.SetGlobalTexture(). My question is can I use the samplerCUBE to make custom reflections behave like Standard Surface reflections in my shader or do I need to write a whole new shader model?
You can either reproduce that in your own code, or call it directly, though to do that you’ll need to use the UNITY_DECLARE_TEXCUBE and UNITY_PASS_TEXCUBE macros as seen in these files:
Presumably this is in a Surface Shader. If that’s the case then no, this is not correct. The UnityGlossyEnvironmentSetup function is expecting the world space view direction and normal. IN.viewDir is in tangent space, if o.Normal is set in the surf function, and o.Normal is also in tangent space. You need to calculate the world space view direction using float3 worldPos; from the Input struct and _WorldSpaceCameraPos.xyz, and transform the o.Normal you’ve set to world space using WorldNormalVector. See the bottom of the Surface Shader documentation on how to use that function.
After that, it still might not work. You mentioned you’re using SetGlobalTexture. But global shader properties only work if you don’t define them as material properties. You didn’t post the rest of your shader, so I don’t know if you’re doing that or not, but it’s a common mistake (I do it frequently too).
Last problem, I use a 3d rendertexture to capture the scene but there not enough mips for smooth transition between different levels of smoothness and get this :
My guess is you’re not rendering to a ARGBHalf cube map, and using the default (aka ARGB32). That’s definitely going to end up a little darker. Unity supports HDR data encoded in an 8 bit per channel texture, but only for baked probes or imported HDR cube maps. Anything generated in real time has to either be a floating point format or just won’t be as bright.
Looks correct to me in terms of the number of mip maps. But you’re not doing any convolution on the cube map you’re generating. This is a whole “thing” that Unity does internally for reflection probes and imported cube maps (if you enable convolution). But there’s no built in utilities for doing it to manually generated cube maps outside of a reflection probe. Plus Unity’s real time convolution is no where near as nice as the baked / imported one. That’s way too expensive to do in real time.
It needs to be run on each face / mip level of the cubemap with different settings for each mip level. Specifically you need to set _Level for the current mip level you’re rendering to, and _Texel which is 1 divided by that mip’s resolution.
You should stop using the term “3D texture” in the context of a cube map. It is not a 3D texture, it is a collection of 6 2D textures. There is an actual 3D texture type that’s completely different.
And GPUs don’t really ever render to anything but a 2D slice of any kind of texture. RenderToCubemap() renders 6 times to each face of a cubemap. Running any shader that also needs to update the faces of a cube map needs to run once per face too.
So that shader is intended to run on and output a single blurred face of a cubemap, for a specific mip level. Though I’m not entirely sure the easiest way to do this since the built in Blit() functions don’t really work correctly with cube maps. It’s probably a custom setup that generates the appropriate UVW quad to render each face with, though it’s possible some secret combination of settings to a normal Blit() call will make it work, I don’t know if anyone has found it.
And it’s not the same cubemap. The one you’re generating manually via RenderToCubemap() isn’t being blurred, and the one the ReflectionProbe component is rendering and then blurring behind the scenes before being passed to the shader on the scene. If it’s a real-time reflection probe, it uses the shader I linked to above to do that blur. If it’s a baked reflection probe it does a much more expensive and thorough blur that doesn’t appear to be exposed anywhere.
Hey, are you there, man, I wanted to use The legacy/reflective/vertex lit shader but just, instead of using a cubemap, I wanted to use here SpecCube i.e the world reflection which get’s baked during lightmapping…could you please reply