small OBJ Loader bug fixes

@aaro4130 - I have a few fixes for your OBJ loader. I’m not sure where to post, so I’ll try here…

  1. The custom float parser ReadFloat() in ObjLoader.cs only handles “-” exponent polarity, but some float writers use “+” also… I ran into this with the Hallwyl Museum photogrammetry models.

A small fix makes it work:

public float ReadFloat() {
         bool isNegative = this.currentChar == '-';
         if (isNegative) {
             this.MoveNext();
         } else if (this.currentChar == '+') {
             this.MoveNext();
         }
  1. setting currentMaterial.EnableKeyword("_EMISSION"); isn’t enough to make emissive materials draw. We also need to turn on currentMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive; I also find that turning on emissive just because of a “Ka” line isn’t really what we want. “Ka” originaly meant “ambient color” which would be multiplied by your scene’s ambient strength so many files write out “Ka 1.0” by default even with no “map_Ka”, and if we turn on emission because of that everything turns out bright white. So I changed it so we only turn on emissive if there is actually a “map_Ka” texture. A slightly more advanced version would turn on emissive only if the map_Ka texture is actually found and loaded, but I didn’t bother.
//emission color
      if (splitLine[0] == "Ka" || splitLine[0] == "ka")
      {             
        currentMaterial.SetColor("_EmissionColor", OBJLoaderHelper.ColorFromStrArray(splitLine));          
        // don't turn on emission unless there is an actual emission map...
        continue;
      }

      //emission map
      if (splitLine[0] == "map_Ka" || splitLine[0] == "map_ka")
      {
        string texturePath = GetTexPathFromMapStatement(processedLine, splitLine);
        if (texturePath == null)
        {
          continue; //invalid args or sth
        }
        currentMaterial.EnableKeyword("_EMISSION");     
        currentMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;

        // if there is no default emission color, set a reasonable default
        var emissionColor = currentMaterial.GetColor("_EmissionColor");
        if (emissionColor.r == 0.0 && emissionColor.g == 0.0 && emissionColor.b == 0.0) {
            currentMaterial.SetColor("_EmissionColor", new Color(1.0f,1.0f,1.0f));
        }

        // setup the emissive texture map 
        currentMaterial.SetTexture("_EmissionMap", TryLoadTexture(texturePath));     
        continue;
      }
1 Like

This sounds useful enough I’d toss it in my toolkit… any chance you can post the entire thing?