1
\$\begingroup\$

I'm quite sure that I'm missing something very simple here, but I'm a complete Unity noob and I absolutely can't figure out what I'm seeing.

I'm trying to light a sphere from both sides, perpendicular to the camera angle, to create an effect where each side of the sphere is illuminated but there's a strip of darkness down the center. Instead, I'm getting really weird angles from the two lights.

My setup (I've quintuple checked all numbers)...

  • A single sphere (textured like Mars) at position (0, 0, 0).
  • An orthographic camera pointed at it from position (0, 0, -20) and rotation (0, 0, 0).
  • Two Point lights at (-5, 0, 0) and (5, 0, 0), with the same intensity and all other light settings.

By my understanding, that should position each light equidistant from the sphere, exactly on either side from the camera's POV. Sort of like a capital " T " configuration. In theory their shine on the sphere should be mirrored on each side, complimentary angles toward the sphere from exactly horizontal. Instead, I'm getting this:

Left Light... Left Light

Right Light... Right Light

So, instead of being horizontal and mirrored, it appears as though the Right Light is significantly closer to the camera than the Left Light along the Z axis, and they both seem to be somewhat below the vertical center of the sphere (despite all elements having a Y value of zero).

Even weirder, viewing it in the Scene panel from above, there's a spiral pattern of darkness when both lights are on, rather than a single dark band between them... enter image description here

Can someone help me out here? Does Unity handle perspective in a way I'm not getting, or have I messed up my arrangement of elements? And aside from eyeballing it, what positions should I use to create the effect I'm going for?

\$\endgroup\$
  • \$\begingroup\$ It looks to me like there might be a problem with the normals on your object - either the sphere mesh, or its normal map texture, or both. This can make the surface react to light as though it's facing a different direction than the polygon is actually oriented. If you show your material settings we may be able to help you narrow down the cause. \$\endgroup\$ – DMGregory May 5 '17 at 2:22
  • \$\begingroup\$ @DMGregory Huh, you're right, if I just create a white sphere with no textures the lighting works fine. The thing is, I'm not doing anything weird with the normals in my custom surface shader, just getting them from the input (half4 n = tex2D (_BumpMap , IN.uv_BumpMap);) and then passing them to the output (o.Normal = n;). It looks fine in the render, as far as the shadows lining up with the diffuse texture, there's just this weird lighting thing. I'm a complete Unity noob, so is there anywhere else I should look that could be influencing this? \$\endgroup\$ – Nerrolken May 5 '17 at 20:29
  • \$\begingroup\$ Are you doing anything to unpack your texture's normal from the 0...1 range of a texture colour sample to the -1...1 range of a 3D direction vector? \$\endgroup\$ – DMGregory May 5 '17 at 20:36
  • \$\begingroup\$ I am, in fact, not doing that. I had no idea what that is. I've taken a look at that link you included, and it kind of made my eyes blur a little bit. But I'll see if I can decipher it, and work it into my shader. If you have any advice feel free to share it, and otherwise thanks for pointing me in this direction! \$\endgroup\$ – Nerrolken May 5 '17 at 22:33
  • 1
    \$\begingroup\$ I'm at a jam all weekend, so feel free to write up the answer yourself and mark it accepted. :) \$\endgroup\$ – DMGregory May 5 '17 at 22:49
1
\$\begingroup\$

So, thanks to DMGregory in the comments, I learned about the need to "unpack" the normal data from the texture file in my custom shader. It was a super-easy fix, though. In my custom surface shader, I got the normal data for the vertex and passed it straight through...

half4 n = tex2D (_BumpMap , IN.uv_BumpMap);
o.Normal = n;

When really what I needed to be doing was unpacking that data before passing it to the output. Simply changing the above code to...

half4 n = tex2D (_BumpMap , IN.uv_BumpMap);
o.Normal = UnpackNormal(n);

...seemed to do the trick!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.