public Material GetLightMappedMaterial(Material baseMaterial, LightMapReference lightMapRef) { var flags = LightMappingFlags.Lightmapped; if (lightMapRef.LightMaps.hasDirections) { flags |= LightMappingFlags.Directional; } if (lightMapRef.LightMaps.hasShadowMask) { flags |= LightMappingFlags.ShadowMask; } var key = new MaterialLookupKey { BaseMaterial = baseMaterial, LightMaps = lightMapRef.LightMaps, Flags = flags }; if (m_LightMappedMaterialCache.TryGetValue(key, out var lightMappedMaterial)) { ++m_NumLightMappedMaterialCacheHits; return(lightMappedMaterial); } else { ++m_NumLightMappedMaterialCacheMisses; lightMappedMaterial = CreateLightMappedMaterial(baseMaterial, lightMapRef.LightMaps); m_LightMappedMaterialCache[key] = lightMappedMaterial; return(lightMappedMaterial); } }
// Check all light maps referenced within the current batch of converted Renderers. // Any references to light maps that have already been inserted into a LightMaps array // will be implemented by reusing the existing LightMaps object. Any leftover previously // unseen (or changed = content hash changed) light maps are combined into a new LightMaps array. public void ProcessLightMapsForConversion() { var lightmaps = LightmapSettings.lightmaps; var uniqueIndices = m_UsedLightmapIndices .Distinct() .OrderBy(x => x) .Where(x => x >= 0 && x != 65534 && x < lightmaps.Length) .ToArray(); var colors = new List <Texture2D>(); var directions = new List <Texture2D>(); var shadowMasks = new List <Texture2D>(); var lightMapIndices = new List <int>(); // Each light map reference is converted into a LightMapKey which identifies the light map // using the content hashes regardless of the index number. Previously encountered light maps // should be found from the cache even if their index number has changed. New or changed // light maps are placed into a new array. for (var i = 0; i < uniqueIndices.Length; i++) { var index = uniqueIndices[i]; var lightmapData = lightmaps[index]; var key = new LightMapKey(lightmapData); if (m_LightMapArrayCache.TryGetValue(key, out var lightMapRef)) { m_LightMapReferences[index] = lightMapRef; ++m_NumLightMapCacheHits; } else { colors.Add(lightmapData.lightmapColor); directions.Add(lightmapData.lightmapDir); shadowMasks.Add(lightmapData.shadowMask); lightMapIndices.Add(index); ++m_NumLightMapCacheMisses; } } if (lightMapIndices.Count > 0) { #if DEBUG_LOG_LIGHT_MAP_CONVERSION Debug.Log($"Creating new DOTS light map array from {lightMapIndices.Count} light maps."); #endif var lightMapArray = LightMaps.ConstructLightMaps(colors, directions, shadowMasks); for (int i = 0; i < lightMapIndices.Count; ++i) { var lightMapRef = new LightMapReference { LightMaps = lightMapArray, LightMapIndex = i, }; m_LightMapReferences[lightMapIndices[i]] = lightMapRef; m_LightMapArrayCache[new LightMapKey(colors[i], directions[i], shadowMasks[i])] = lightMapRef; } } }