One of my shaders needs to draw textures from a custom texture atlas, hence I need to handle mipmapping "manually". Does anyone know of a good way to do this?
Thank you for the link, but my shader needs to use a custom mipmap and read in pixels from a texture atlas, so tex2Dlod won't work.
You lost me. What do you mean by "handling mipmapping manually" if not "choosing the mipmap level explicitly"?
I'm making my own mipmaps for my own texture atlas, which the shader reads by calculating the correct UV values, I need a way to determine which mipmap level should be used based on the distance to the camera. I've seen one method which uses ddx/ddy, but I don't know if Unity supports that.
Is there another way to determine the distance to the camera within a fragment shader (or rather a combination of vertex and fragment shaders of course)?
What is the first way? Determining this distance from ddx/ddy is not particularly easy. Usually you would use something like this: In the vertex position, you transform the vertex position to world coordinates and hand it to the fragment shader (or use the worldPos in the input structure of a SurfaceShader) and then compute the distance to the camera position: _WorldSpaceCameraPos ( http://unity3d.com/support/documentation/Components/SL-BuiltinValues.html )
Just for future reference, another way of clamping mip-levels is to use texturesampling with explicit gradients, while clamping ddx/ddy. The function is called textureGrad in GLSL, and is an overload of tex2D in Cg. Code (csharp): float2 uv_dx = clamp( ddx( uv ), minval, maxval ); float2 uv_dy = clamp( ddy( uv ), minval, maxval ); half4 texsample = tex2D( _MainTex, uv0, uv_dx, uv_dy );
Do you really need a "custom" mipmap? A texture atlas is a common texture and works with mipmapping quite well, you only could face problems at the edges with low resolutions, but you could limit that, too. What do you want to achieve?