// static exchange function
VDecorationDensityChannel *VDecorationDensityChannel::ChunkFileExchange(VChunkFile &file, VDecorationDensityChannel *pChannel, VEditableTerrain *pTerrain)
{
  const VTerrainConfig &cfg(pTerrain->m_Config);
  int iVersion = 8;
  char szModelFilename[FS_MAX_PATH];
  if (file.IsLoading())
  {
    int iSizeX,iSizeY;
    IVTerrainDecorationModel::ModelProperties_t props;

    file.ReadInt(iVersion);
    IVTerrainDecorationModel::ModelType_e eForcedType = IVTerrainDecorationModel::AnyType;
    if (iVersion>=6)
      file.ReadInt((int &)eForcedType);
    file.ReadString(szModelFilename,FS_MAX_PATH);
    props.ChunkFileExchange(file,iVersion);
    file.ReadInt(iSizeX);
    file.ReadInt(iSizeY);

    IVTerrainDecorationModel *pRes = VTerrainDecorationModelManager::GlobalManager().CreateModel(szModelFilename,props,true,eForcedType);
    if (!pRes || !pRes->IsValid()) // discard this one, but keep the file order intact
    {
      file.SkipBytes(iSizeX*iSizeY);
      return NULL;
    }
    pChannel = new VDecorationDensityChannel(pRes,cfg);
    pChannel->ReadScaled(file,iSizeX,iSizeY); // considers and fixes different scaling
    pChannel->SetDirtyFlag();
    return pChannel;
  }

  // saving:
  VASSERT(pChannel && pChannel->m_spSource);
  file.WriteInt(iVersion);
  file.WriteInt((int)pChannel->m_spSource->GetDecorationType()); // version 6
  file.WriteString(pChannel->m_spSource->GetFilename());
  pChannel->m_spSource->m_Properties.ChunkFileExchange(file,iVersion);
  file.WriteInt(pChannel->m_iSizeX);
  file.WriteInt(pChannel->m_iSizeY);
  file.Write(pChannel->GetValuePtr(0,0),pChannel->GetSampleCount());

  return pChannel;
}
 // called by sectors:
 inline void AddVisibleDecorationInstance(VTerrainDecorationInstance *pInstance)
 {
   IVTerrainDecorationModel *pModel = pInstance->m_spModel;
   VASSERT(pModel!=NULL);
   if (VISION_UNLIKELY(!pModel->IsLoaded()))
   {
     if (pModel->IsMissing())
       return;
     pModel->EnsureLoaded();
     if (pModel->IsMissing() || !pModel->IsValid())
       return;
   }
   pModel->UpdateTimeStamp(); // for resource management
   m_VisibleDecoration[m_iVisibleDecorationCount++] = pInstance;
 }