void CCoherentUISystem::ChangeEntityDiffuseTextureForMaterial( CCoherentViewListener* pViewListener, const char* entityName, const char* materialName ) { IMaterial* pMaterial = gEnv->p3DEngine->GetMaterialManager()->FindMaterial( materialName ); IEntity* pEntity = gEnv->pEntitySystem->FindEntityByName( entityName ); if ( pEntity && !pMaterial ) { pMaterial = pEntity->GetMaterial(); } if ( pMaterial ) { STexSamplerRT& sampler = pMaterial->GetShaderItem().m_pShaderResources->GetTexture( EFTT_DIFFUSE )->m_Sampler; // Create a new texture and scrap the old one void* pD3DTextureDst = NULL; ITexture* pCryTex = gD3DSystem->CreateTexture( &pD3DTextureDst, sampler.m_pITex->GetWidth(), sampler.m_pITex->GetHeight(), 1, eTF_A8R8G8B8, FT_USAGE_DYNAMIC ); int oldTextureID = sampler.m_pITex->GetTextureID(); gEnv->pRenderer->RemoveTexture( oldTextureID ); sampler.m_pITex = pCryTex; pCryTex->AddRef(); pViewListener->SetTexture( pD3DTextureDst, pCryTex->GetTextureID() ); } }
void CUIEntityDynTexTag::OnAddTaggedEntity(EntityId entityId, const char *uiElementName, const char *entityClass, const char *materialTemplate, const Vec3 &offset, const char *idx) { OnRemoveTaggedEntity(entityId, idx); IEntityClass *pEntClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(entityClass); if(pEntClass) { SEntitySpawnParams params; params.nFlags = ENTITY_FLAG_CLIENT_ONLY; params.pClass = pEntClass; IEntity *pTagEntity = gEnv->pEntitySystem->SpawnEntity(params); IUIElement *pElement = gEnv->pFlashUI->GetUIElement(uiElementName); if(pTagEntity && pElement) { IMaterial *pMatTemplate = gEnv->p3DEngine->GetMaterialManager()->LoadMaterial(materialTemplate, false); if(pMatTemplate && pMatTemplate->GetShaderItem().m_pShaderResources->GetTexture(0)) { pMatTemplate->GetShaderItem().m_pShaderResources->GetTexture(0)->m_Name.Format("%s@%d.ui", uiElementName, entityId); IMaterial *pMat = gEnv->p3DEngine->GetMaterialManager()->CloneMaterial(pMatTemplate); pTagEntity->SetMaterial(pMat); } IEntityRenderProxy *pRenderProxy = (IEntityRenderProxy *) pTagEntity->GetProxy(ENTITY_PROXY_RENDER); if(pRenderProxy) { IRenderNode *pRenderNode = pRenderProxy->GetRenderNode(); if(pRenderNode) { pRenderNode->SetViewDistRatio(256); } } pElement->RemoveEventListener(this); // first remove to avoid assert if already registered! pElement->AddEventListener(this, "CUIEntityDynTexTag"); gEnv->pEntitySystem->AddEntityEventListener(entityId, ENTITY_EVENT_DONE, this); m_Tags.push_back(STagInfo(entityId, pTagEntity->GetId(), idx, offset, pElement->GetInstance((uint)entityId))); } } }
void CGameVolume_Water::SetupVolume() { IGameVolumes::VolumeInfo volumeInfo; if (GetVolumeInfoForEntity(GetEntityId(), volumeInfo) == false) return; if (volumeInfo.verticesCount < 3) return; WaterProperties waterProperties(GetEntity()); if(waterProperties.isRiver) { if(volumeInfo.verticesCount < 4 || volumeInfo.verticesCount % 2 != 0) return; int numSegments = (volumeInfo.verticesCount / 2) - 1; m_segments.resize(numSegments); for(int i = 0; i < numSegments; ++i) { SetupVolumeSegment(waterProperties, i, &volumeInfo.pVertices[0], volumeInfo.verticesCount); } } else { SetupVolumeSegment(waterProperties, 0, &volumeInfo.pVertices[0], volumeInfo.verticesCount); } #if CRY_PLATFORM_WINDOWS && !defined(_RELEASE) { if (gEnv->IsEditor()) { IEntity* pEnt = GetEntity(); IMaterial* pMat = pEnt ? pEnt->GetMaterial() : 0; if (pMat) { const SShaderItem& si = pMat->GetShaderItem(); if (si.m_pShader && si.m_pShader->GetShaderType() != eST_Water) { CryWarning(VALIDATOR_MODULE_3DENGINE, VALIDATOR_ERROR, "Incorrect shader set for water / water fog volume \"%s\"!", pEnt->GetName()); } } } } #endif m_baseMatrix = GetEntity()->GetWorldTM(); m_initialMatrix = m_baseMatrix; m_volumeDepth = waterProperties.depth; m_streamSpeed = waterProperties.streamSpeed; m_awakeAreaWhenMoving = waterProperties.awakeAreaWhenMoving; m_isRiver = waterProperties.isRiver; }
//------------------------------------------------------------------------ bool CVehiclePartTread::Init(IVehicle* pVehicle, const CVehicleParams& table, IVehiclePart* parent, CVehicle::SPartInitInfo& initInfo, int partType) { if (!CVehiclePartBase::Init(pVehicle, table, parent, initInfo, eVPT_Tread)) return false; CVehicleParams subTable = table.findChild("Tread"); // Tread subtable if (!subTable) return false; string filename = subTable.getAttr("filename"); if (filename.empty()) return false; subTable.getAttr("uvSpeedMultiplier", m_uvSpeedMultiplier); if (table.haveAttr("component")) { if (CVehicleComponent* pComponent = static_cast<CVehicleComponent*>(m_pVehicle->GetComponent(table.getAttr("component")))) pComponent->AddPart(this); } m_slot = GetEntity()->LoadCharacter(m_slot, filename); m_pCharInstance = GetEntity()->GetCharacter(m_slot); if (!m_pCharInstance) return false; if (subTable.haveAttr("materialName")) { string materialName = subTable.getAttr("materialName"); materialName.MakeLower(); IMaterial* pMaterial = 0; const char* subMtlName = 0; int subMtlSlot = -1; // find tread material IEntityRenderProxy *pRenderProxy = (IEntityRenderProxy*)GetEntity()->GetProxy(ENTITY_PROXY_RENDER); if (pRenderProxy) { pMaterial = pRenderProxy->GetRenderMaterial(m_slot); if (pMaterial) { // if matname doesn't fit, look in submaterials if (string(pMaterial->GetName()).MakeLower().find(materialName) == string::npos) { for (int i=0; i < pMaterial->GetSubMtlCount(); ++i) { if (string(pMaterial->GetSubMtl(i)->GetName()).MakeLower().find(materialName) != string::npos) { subMtlName = pMaterial->GetSubMtl(i)->GetName(); subMtlSlot = i; break; } } } } } if (pMaterial) { // clone IMaterial *pCloned = pMaterial->GetMaterialManager()->CloneMultiMaterial(pMaterial, subMtlName); if (pCloned) { pRenderProxy->SetSlotMaterial(m_slot, pCloned); pMaterial = pCloned; m_pMaterial = pMaterial; } if (subMtlSlot > -1) m_pShaderResources = pMaterial->GetShaderItem(subMtlSlot).m_pShaderResources; else m_pShaderResources = pMaterial->GetShaderItem().m_pShaderResources; } if (m_pShaderResources) { for (int i = 0; i < EFTT_MAX; ++i) { if (!m_pShaderResources->GetTexture(i)) continue; SEfTexModificator& modif = *m_pShaderResources->GetTexture(i)->AddModificator(); modif.SetMember("m_eMoveType[0]", 1.0f); // ETMM_Fixed: u = m_OscRate[0] + sourceU modif.SetMember("m_OscRate[0]", 0.0f); } } } char wheelName[256]; IDefaultSkeleton& rIDefaultSkeleton = m_pCharInstance->GetIDefaultSkeleton(); for (int i=0; i<rIDefaultSkeleton.GetJointCount(); ++i) { const char* boneName = rIDefaultSkeleton.GetJointNameByID(i); if (0 != boneName) { // extract wheel name const size_t len = strcspn(boneName, "_"); if (len < strlen(boneName) && len < sizeof(wheelName)) { cry_strcpy(wheelName, boneName, len); CVehiclePartSubPartWheel* pWheel = (CVehiclePartSubPartWheel*)m_pVehicle->GetPart(wheelName); if (pWheel) { SWheelInfo wheelInfo; wheelInfo.slot = pWheel->m_slot; wheelInfo.jointId = i; wheelInfo.pWheel = pWheel; m_wheels.push_back(wheelInfo); m_lastWheelIndex = pWheel->GetWheelIndex(); } } } } m_pVehicle->SetObjectUpdate(this, IVehicle::eVOU_AlwaysUpdate); m_state = eVGS_Default; return true; }
virtual void ProcessEvent( EFlowEvent event, SActivationInfo *pActInfo ) { switch (event) { case eFE_Initialize: break; case eFE_Activate: IGameFramework* pGameFramework = gEnv->pGame->GetIGameFramework(); if(IsPortActive(pActInfo, EIP_Cast)) { // setup ray + optionally skip 1 entity ray_hit rayHit; static const float maxRayDist = 100.f; const unsigned int flags = rwi_stop_at_pierceable|rwi_colltype_any; IPhysicalEntity *skipList[1]; int skipCount = 0; IEntity* skipEntity = gEnv->pEntitySystem->GetEntity(GetPortEntityId(pActInfo, EIP_SkipEntity)); if(skipEntity) { skipList[0] = skipEntity->GetPhysics(); skipCount = 1; } Vec3 rayPos = GetPortVec3(pActInfo, EIP_RayPos); Vec3 rayDir = GetPortVec3(pActInfo, EIP_RayDir); // Check if the ray hits an entity if(gEnv->pSystem->GetIPhysicalWorld()->RayWorldIntersection(rayPos, rayDir * 100, ent_all, flags, &rayHit, 1, skipList, skipCount)) { int type = rayHit.pCollider->GetiForeignData(); if (type == PHYS_FOREIGN_ID_ENTITY) { IEntity* pEntity = (IEntity*)rayHit.pCollider->GetForeignData(PHYS_FOREIGN_ID_ENTITY); IEntityRenderProxy* pRenderProxy = pEntity ? (IEntityRenderProxy*)pEntity->GetProxy(ENTITY_PROXY_RENDER) : 0; // Get the renderproxy, and use it to check if the material is a DynTex, and get the UIElement if so if(pRenderProxy) { IRenderNode *pRenderNode = pRenderProxy->GetRenderNode(); IMaterial* pMaterial = pRenderProxy->GetRenderMaterial(); SEfResTexture* texture = 0; if(pMaterial && pMaterial->GetShaderItem().m_pShaderResources) texture= pMaterial->GetShaderItem().m_pShaderResources->GetTexture(EFTT_DIFFUSE); IUIElement* pElement = texture ? gEnv->pFlashUI->GetUIElementByInstanceStr(texture->m_Name) : 0; if(pElement && pRenderNode) { int m_dynTexGeomSlot = 0; IStatObj* pObj = pRenderNode->GetEntityStatObj(m_dynTexGeomSlot); // result bool hasHit = false; Vec2 uv0, uv1, uv2; Vec3 p0, p1, p2; Vec3 hitpos; // calculate ray dir CCamera cam = gEnv->pRenderer->GetCamera(); if (pEntity->GetSlotFlags(m_dynTexGeomSlot) & ENTITY_SLOT_RENDER_NEAREST) { ICVar *r_drawnearfov = gEnv->pConsole->GetCVar("r_DrawNearFoV"); assert(r_drawnearfov); cam.SetFrustum(cam.GetViewSurfaceX(),cam.GetViewSurfaceZ(),DEG2RAD(r_drawnearfov->GetFVal()),cam.GetNearPlane(),cam.GetFarPlane(), cam.GetPixelAspectRatio()); } Vec3 vPos0 = rayPos; Vec3 vPos1 = rayPos + rayDir; // translate into object space const Matrix34 m = pEntity->GetWorldTM().GetInverted(); vPos0 = m * vPos0; vPos1 = m * vPos1; // walk through all sub objects const int objCount = pObj->GetSubObjectCount(); for (int obj = 0; obj <= objCount && !hasHit; ++obj) { Vec3 vP0, vP1; IStatObj* pSubObj = NULL; if (obj == objCount) { vP0 = vPos0; vP1 = vPos1; pSubObj = pObj; } else { IStatObj::SSubObject* pSub = pObj->GetSubObject(obj); const Matrix34 mm = pSub->tm.GetInverted(); vP0 = mm * vPos0; vP1 = mm * vPos1; pSubObj = pSub->pStatObj; } IRenderMesh* pMesh = pSubObj ? pSubObj->GetRenderMesh() : NULL; if (pMesh) { const Ray ray(vP0, (vP1-vP0).GetNormalized() * maxRayDist); hasHit = RayIntersectMesh(pMesh, pMaterial, pElement, ray, hitpos, p0, p1, p2, uv0, uv1, uv2); } } // skip if not hit if (!hasHit) { ActivateOutput(pActInfo, EOP_Failed, 1); return; } // calculate vectors from hitpos to vertices p0, p1 and p2: const Vec3 v0 = p0-hitpos; const Vec3 v1 = p1-hitpos; const Vec3 v2 = p2-hitpos; // calculate factors const float h = (p0-p1).Cross(p0-p2).GetLength(); const float f0 = v1.Cross(v2).GetLength() / h; const float f1 = v2.Cross(v0).GetLength() / h; const float f2 = v0.Cross(v1).GetLength() / h; // find the uv corresponding to hitpos Vec3 uv = uv0 * f0 + uv1 * f1 + uv2 * f2; // translate to flash space int x, y, width, height; float aspect; pElement->GetFlashPlayer()->GetViewport(x, y, width, height, aspect); int iX = int_round(uv.x * (float)width); int iY = int_round(uv.y * (float)height); // call the function provided if it is present in the UIElement description string funcName = GetPortString(pActInfo, EIP_CallFunction); const SUIEventDesc* eventDesc = pElement->GetFunctionDesc(funcName); if(eventDesc) { SUIArguments arg; arg.AddArgument(iX); arg.AddArgument(iY); pElement->CallFunction(eventDesc->sName, arg); } ActivateOutput(pActInfo, EOP_Success, 1); } } } } ActivateOutput(pActInfo, EOP_Failed, 1); } break; } }