Jump to content

Diffuse lighting not working on added mesh


Recommended Posts

I am trying to add a feature to RasterPropMonitor that allows me to have config file configurable text.  I'm using the Unity TextMesh object, and I can replace the built-in shader with one that respects the depth buffer, which is fine for emissive text.  However, I want to add support for text that is lit using environmental lighting.  So far, I have not been able to get the surface shaders to work correctly - I can make the text visible, but it behaves like some parameters are not updating correctly (not reacting to light intensity changes, reacting to shadows like the normal is pointed the wrong way).

I assume it has to do with me adding a new transform to the model programmatically, and I haven't hooked something up correctly when I added the TextMesh.  Here's the code I use to set up the new object:

Transform textObjTransform = internalProp.FindModelTransform(transformName);

Transform offsetTransform = new GameObject().transform;
offsetTransform.gameObject.layer = textObjTransform.gameObject.layer;
offsetTransform.SetParent(textObjTransform, false);
offsetTransform.Translate(transformOffset.x, transformOffset.y, 0.0f);
textObj = offsetTransform.gameObject.AddComponent<TextMesh>();

font = JUtil.LoadOSFont(fontName, fontQuality, out destroyFontOnExit);
textObj.font = font;
  
Renderer r = textObj.GetComponent<Renderer>();
overrideMaterial = new Material(JUtil.LoadInternalShader("RPM/JSILabel"));
overrideMaterial.mainTexture = font.material.mainTexture;
r.material = overrideMaterial;

I'm adding offsetTransform to allow the TextMesh to be displaced from the anchor transform.

RPM/JSILabel is based on KSP Alpha / Transparent, which is one of Squad's customized Unity surface shaders.  I've made changes to the shader to work with the TextMesh, but I don't think that's the problem, since if I write values to the output Emission field, it shows up just fine (but glowing).  If I use Squad's diffuse lighting, the text is unresponsive to lights in the IVA.  Is there something else I need to do to get those values updated automatically?  Or am I not adding the text to the hierarchy correctly?  Or do I need to update the shader values myself in OnUpdate?  If it's the latter, how do I find those values?

Link to comment
Share on other sites

1 hour ago, sarbian said:

From what I found it seems TextMesh do not have any normals at all. Have a look at this and see if it still work for U5

Yes, they have no normals (they are abnormal? :) ).  I am setting normals to (0, 0, 1) in the surface shader, which is what the KSP / Alpha Specular shader does.

Changing the lighting in the IVA view (adjusting the brightness of the light sources) does not affect the color of the text that uses this shader - it's like the shader is not being run through all of the appropriate lighting/shadow passes, or the parameters used for the BlinnPhongSmooth surface shader (KSP's surface shader) aren't being set up.

Link to comment
Share on other sites

Not sure if it's anything to do with this in particular, but I've found reference to a bug where normals get un-normalized somewhere between the shader surface function & the lighting function ( in my case I was trying to fix fireflies in a specular shader ). You might try a custom lighting function?

Edited by Van Disaster
Link to comment
Share on other sites

Digging around some more and looking at the shader code that Unity spews, it looks like the missing normals in the mesh are the problem.  Some of the lighting is computed per-vertex, and apparently the surface shader's normals don't apply at that stage.  I'll have to see if I can add normals to the mesh, since i don't relish the thought of having to roll an entire shader for this if I don't have to.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...