Beispiel #1
0
void
USDVMP::setup(FnKat::ViewerModifierInput& input)
{
    TF_DEBUG(KATANA_DEBUG_VMP_USD).Msg("%s @ %p : %s\n",
            TF_FUNC_NAME().c_str(), this, input.getFullName().c_str());

    // The multi-threaded Usd Op may be loading or unloading models on the stage
    // we need, so we grab the global lock in reader mode.
    boost::shared_lock<boost::upgrade_mutex> readerLock(UsdKatanaGetStageLock());

    // Open stage if necessary.
    if (not _stage) {

        // Get usd file, node path, and current time, 
        // needed to call TidSceneRenderer
        FnKat::StringAttribute usdFileAttr = 
                input.getAttribute("fileName");
        FnKat::StringAttribute usdRootLocationAttr = 
                input.getAttribute("rootLocation");
        FnKat::StringAttribute usdReferencePathAttr = 
                input.getAttribute("referencePath");
        FnKat::StringAttribute variantStringAttr =
                input.getAttribute("variants");
        FnKat::StringAttribute ignoreLayerAttr=
                input.getAttribute("ignoreLayerRegex");
        FnKat::FloatAttribute forcePopulateAttr = 
                input.getAttribute("forcePopulateUsdStage");

        std::string usdFile = usdFileAttr.getValue("", false);
        std::string usdRootLocation = usdRootLocationAttr.getValue("", false);
        std::string usdReferencePath = usdReferencePathAttr.getValue("",false);
        std::string variantString = variantStringAttr.getValue("",false);
        std::string ignoreLayerRegex = ignoreLayerAttr.getValue("$^", false);
        bool forcePopulate = forcePopulateAttr.getValue((float)true,false);

        if (usdFile.empty())
            return;

        _stage = UsdKatanaCache::GetInstance().GetStage(usdFile, 
                                                        variantString,
                                                        ignoreLayerRegex,
                                                        forcePopulate);

        if (not _stage) {
            TF_DEBUG(KATANA_DEBUG_VMP_USD).Msg(
                "Cannot resolve path %s", usdFile.c_str());
            return;
        } 

        if (usdReferencePath == "")
            _prim = _stage->GetPseudoRoot();
        else
            _prim = _stage->GetPrimAtPath(SdfPath(usdReferencePath));

        if (not _prim)
            FnLogWarn(std::string("Cannot compose ") + 
                _prim.GetPath().GetString());

        _params.cullStyle = UsdImagingEngine::CULL_STYLE_BACK_UNLESS_DOUBLE_SIDED;

        _renderer = UsdKatanaCache::GetInstance()
                                  .GetRenderer(_stage, _prim, variantString);
    }
    
    // always update frame time
    FnKat::DoubleAttribute currentTimeAttr = input.getAttribute("currentTime");
    double currentTime = currentTimeAttr.getValue(0.0, false);
    _params.frame = currentTime;
    
    
    
}
int ImagineRender::queueDataUpdates(FnKat::GroupAttribute updateAttribute)
{
//	fprintf(stderr, "Queue updates...\n");
	
	m_liveRenderState.lock();
	if (!m_pRaytracer)
	{
		m_liveRenderState.unlock();
		return 0;
	}

	m_liveRenderState.unlock();

	unsigned int numItems = updateAttribute.getNumberOfChildren();

	for (unsigned int i = 0; i < numItems; i++)
	{
		FnKat::GroupAttribute dataUpdateItemAttribute = updateAttribute.getChildByIndex(i);

		if (!dataUpdateItemAttribute.isValid())
			continue;
		
//		fprintf(stderr, "\n\n%s\n\n", dataUpdateItemAttribute.getXML().c_str());

		FnKat::StringAttribute typeAttribute = dataUpdateItemAttribute.getChildByName("type");

		if (!typeAttribute.isValid())
			continue;

		FnKat::StringAttribute locationAttribute = dataUpdateItemAttribute.getChildByName("location");
		FnKat::GroupAttribute attributesAttribute = dataUpdateItemAttribute.getChildByName("attributes");
		
//		fprintf(stderr, "\n\n%s\n\n", attributesAttribute.getXML().c_str());
		
		bool partialUpdate = false;

		FnKat::StringAttribute partialUpdateAttribute = attributesAttribute.getChildByName("partialUpdate");
		if (partialUpdateAttribute.isValid() && partialUpdateAttribute.getValue("", false) == "True")
		{
			partialUpdate = true;
		}

		std::string type = typeAttribute.getValue("", false);
		std::string location = locationAttribute.getValue("", false);

		if (type == "camera" && location == m_renderCameraLocation)
		{
			KatanaUpdateItem newUpdate(KatanaUpdateItem::eTypeCamera, KatanaUpdateItem::eLocCamera, location);
			
			FnKat::GroupAttribute xformAttribute = attributesAttribute.getChildByName("xform");
			if (xformAttribute.isValid())
			{
				LiveRenderHelpers::setUpdateXFormFromAttribute(xformAttribute, newUpdate);
			}
			
			FnKat::GroupAttribute geometryAttribute = attributesAttribute.getChildByName("geometry");
			if (geometryAttribute.isValid())
			{
				FnKat::DoubleAttribute fovAttribute = geometryAttribute.getChildByName("fov");
				if (fovAttribute.isValid())
				{
					double fovValue = fovAttribute.getValue(70.0, false);
					newUpdate.extra.add("fov", (float)fovValue);
				}
				
				FnKat::DoubleAttribute nearClipAttribute = geometryAttribute.getChildByName("near");
				if (nearClipAttribute.isValid())
				{
					double nearClipValue = nearClipAttribute.getValue(0.1, false);
					newUpdate.extra.add("nearClip", (float)nearClipValue);
				}
			}
			
			m_liveRenderState.addUpdate(newUpdate);
		}
		else if (type == "geoMaterial")
		{
			FnKat::GroupAttribute materialAttribute = attributesAttribute.getChildByName("material");
			
			if (!materialAttribute.isValid())
				continue;
			
			FnKat::StringAttribute shaderAttribute = materialAttribute.getChildByName("imagineSurfaceShader");
			if (shaderAttribute.isValid())
			{
				// we have a none-network material
				
				FnKat::GroupAttribute shaderParams = materialAttribute.getChildByName("imagineSurfaceParams");
				// if all shader params are default, there won't be a group, so shaderParams will be invalid, but we can pass
				// this down anyway. So we don't need to check for the validity of shaderParams, as its possible non-existance
				// is okay.
				
				// extremely hacky for the moment...
				KatanaUpdateItem newUpdate(KatanaUpdateItem::eTypeObjectMaterial, KatanaUpdateItem::eLocObject, location);
				
				std::string shaderType = shaderAttribute.getValue("", false);
				
				Material* pNewMaterial = MaterialHelper::createNewMaterialStandAlone(shaderType, shaderParams);
				
				if (pNewMaterial)
				{
					newUpdate.pMaterial = pNewMaterial;
					
					m_liveRenderState.addUpdate(newUpdate);
				}
				
				continue;
			}
			
			FnKat::GroupAttribute nodesAttribute = materialAttribute.getChildByName("nodes");
			if (nodesAttribute.isValid())
			{
				MaterialHelper materialHelper(m_logger);
				// we have a network material
				
				Material* pNewMaterial = materialHelper.createNetworkMaterial(materialAttribute, false);
				if (pNewMaterial)
				{
					// extremely hacky for the moment...
					KatanaUpdateItem newUpdate(KatanaUpdateItem::eTypeObjectMaterial, KatanaUpdateItem::eLocObject, location);
					
					newUpdate.pMaterial = pNewMaterial;
					
					m_liveRenderState.addUpdate(newUpdate);
				}
			}
		}
		else if (type == "geo")
		{
			KatanaUpdateItem newUpdate(KatanaUpdateItem::eTypeObject, KatanaUpdateItem::eLocObject, location);
			
			bool changed = false;
			
			FnKat::GroupAttribute xformAttribute = attributesAttribute.getChildByName("xform");
			if (xformAttribute.isValid())
			{
				LiveRenderHelpers::setUpdateXFormFromAttribute(xformAttribute, newUpdate);
				changed = true;
			}
			
			FnKat::IntAttribute deletedAttribute = attributesAttribute.getChildByName("deleted");
			if (deletedAttribute.isValid())
			{
				int deletedValue = deletedAttribute.getValue(0, false);
				if (deletedValue == 1)
				{
					newUpdate.extra.add("deleted", true);
					changed = true;
				}
			}
			
			if (changed)
			{
				m_liveRenderState.addUpdate(newUpdate);
			}
		}
		else if (type == "light")
		{
//			fprintf(stderr, "\n\n%s\n\n", attributesAttribute.getXML().c_str());
			
			KatanaUpdateItem newUpdate(KatanaUpdateItem::eTypeLight, KatanaUpdateItem::eLocLight, location);
			
			FnKat::GroupAttribute xformAttribute = attributesAttribute.getChildByName("xform");
			if (xformAttribute.isValid())
			{
				LiveRenderHelpers::setUpdateXFormFromAttribute(xformAttribute, newUpdate);
			}
			
			FnKat::GroupAttribute materialAttribute = attributesAttribute.getChildByName("material");
			if (materialAttribute.isValid())
			{
				FnKat::GroupAttribute shaderParamsAttribute = materialAttribute.getChildByName("imagineLightParams");
				if (shaderParamsAttribute.isValid())
				{
					KatanaAttributeHelper helper(shaderParamsAttribute);
					
					float intensity = helper.getFloatParam("intensity", 1.0f);
					newUpdate.extra.add("intensity", intensity);
					
					float exposure = helper.getFloatParam("exposure", 1.0f);
					newUpdate.extra.add("exposure", exposure);
				}
			}
			
			FnKat::IntAttribute muteAttribute = attributesAttribute.getChildByName("mute");
			if (muteAttribute.isValid())
			{
				int muteValue = muteAttribute.getValue(0, false);
				newUpdate.extra.add("muted", (bool)muteValue);
			}
			
			m_liveRenderState.addUpdate(newUpdate);
		}
	}

	return 0;
}