Search Unity

small OBJ Loader bug fixes

Discussion in 'Scripting' started by dave_sf_42, Jul 8, 2020.

  1. dave_sf_42

    dave_sf_42

    Joined:
    May 28, 2019
    Posts:
    41
    @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:

    Code (CSharp):
    1. public float ReadFloat() {
    2.          bool isNegative = this.currentChar == '-';
    3.          if (isNegative) {
    4.              this.MoveNext();
    5.          } else if (this.currentChar == '+') {
    6.              this.MoveNext();
    7.          }
    2) 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.

    Code (CSharp):
    1. //emission color
    2.       if (splitLine[0] == "Ka" || splitLine[0] == "ka")
    3.       {            
    4.         currentMaterial.SetColor("_EmissionColor", OBJLoaderHelper.ColorFromStrArray(splitLine));          
    5.         // don't turn on emission unless there is an actual emission map...
    6.         continue;
    7.       }
    8.  
    9.       //emission map
    10.       if (splitLine[0] == "map_Ka" || splitLine[0] == "map_ka")
    11.       {
    12.         string texturePath = GetTexPathFromMapStatement(processedLine, splitLine);
    13.         if (texturePath == null)
    14.         {
    15.           continue; //invalid args or sth
    16.         }
    17.         currentMaterial.EnableKeyword("_EMISSION");    
    18.         currentMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
    19.  
    20.         // if there is no default emission color, set a reasonable default
    21.         var emissionColor = currentMaterial.GetColor("_EmissionColor");
    22.         if (emissionColor.r == 0.0 && emissionColor.g == 0.0 && emissionColor.b == 0.0) {
    23.             currentMaterial.SetColor("_EmissionColor", new Color(1.0f,1.0f,1.0f));
    24.         }
    25.  
    26.         // setup the emissive texture map
    27.         currentMaterial.SetTexture("_EmissionMap", TryLoadTexture(texturePath));    
    28.         continue;
    29.       }
     
    Kurt-Dekker likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    This sounds useful enough I'd toss it in my toolkit... any chance you can post the entire thing?