Re: Render to texture

#2
Render to texture is not in the engine, this question has been raised many times in the community. The technology is probably not the most complicated, it is present by default in any other engine. But Crytek does not react at all. There is an opinion that maybe the game Hunt: Showdown uses Render to texture, usually the technologies that Crytek use in their games migrate to the national engine, but all this is in fantasy :roll:

Re: Render to texture

#4
This is amazing! I do not know Render to texture or some interpretation of this technology. But I just want to see similar technologies in the engine out of the box. What are some basic technologies that can expand gameplay

Re: Render to texture

#7
the my code use CryEngine V,custom version 5.5.2

Code: Select all

#include "StdAfx.h" #include "Player.h" #include <CryRenderer/IRenderAuxGeom.h> void CPlayerComponent::Initialize() { m_timer = gEnv->pSystem->GetITimer(); // Create the camera component, will automatically update the viewport every frame m_pCameraComponent = m_pEntity->GetOrCreateComponent<Cry::DefaultComponents::CCameraComponent>(); // Get the input component, wraps access to action mapping so we can easily get callbacks when inputs are triggered m_pInputComponent = m_pEntity->GetOrCreateComponent<Cry::DefaultComponents::CInputComponent>(); m_pCameraManager = gEnv->pSystem->GetIPluginManager()->QueryPlugin<IPlugin_CryDefaultEntities>()->GetICameraManager(); // Register an action, and the callback that will be sent when it's triggered m_pInputComponent->RegisterAction("player", "moveleft", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveLeft, activationMode); }); // Bind the 'A' key the "moveleft" action m_pInputComponent->BindAction("player", "moveleft", eAID_KeyboardMouse, EKeyId::eKI_A); m_pInputComponent->RegisterAction("player", "moveright", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveRight, activationMode); }); m_pInputComponent->BindAction("player", "moveright", eAID_KeyboardMouse, EKeyId::eKI_D); m_pInputComponent->RegisterAction("player", "moveforward", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveForward, activationMode); }); m_pInputComponent->BindAction("player", "moveforward", eAID_KeyboardMouse, EKeyId::eKI_W); m_pInputComponent->RegisterAction("player", "moveback", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveBack, activationMode); }); m_pInputComponent->BindAction("player", "moveback", eAID_KeyboardMouse, EKeyId::eKI_S); m_pInputComponent->RegisterAction("player", "mouse_rotateyaw", [this](int activationMode, float value) { m_mouseDeltaRotation.x -= value; }); m_pInputComponent->BindAction("player", "mouse_rotateyaw", eAID_KeyboardMouse, EKeyId::eKI_MouseX); m_pInputComponent->RegisterAction("player", "mouse_rotatepitch", [this](int activationMode, float value) { m_mouseDeltaRotation.y -= value; }); m_pInputComponent->BindAction("player", "mouse_rotatepitch", eAID_KeyboardMouse, EKeyId::eKI_MouseY); m_pInputComponent->RegisterAction("player", "switchCamera", [this](int activationMode, float value) { switchView(); }); m_pInputComponent->BindAction("player", "switchCamera", eAID_KeyboardMouse, EKeyId::eKI_P); Revive(); } uint64 CPlayerComponent::GetEventMask() const { return ENTITY_EVENT_BIT(ENTITY_EVENT_START_GAME) | ENTITY_EVENT_BIT(ENTITY_EVENT_UPDATE)| ENTITY_EVENT_BIT(ENTITY_EVENT_START_LEVEL); } void CPlayerComponent::ProcessEvent(const SEntityEvent& event) { switch (event.event) { case ENTITY_EVENT_START_LEVEL: { IEntity * entity1 = gEnv->pEntitySystem->FindEntityByName("Camera-2"); IEntityClass * camera_class = entity1->GetClass(); Cry::DefaultComponents::CCameraComponent * camera = entity1->GetComponent<Cry::DefaultComponents::CCameraComponent>(); if (camera) { //Quat rotate = entity1->GetWorldRotation(); camera1 = &camera->GetCamera(); camera1->SetMatrix(entity1->GetWorldTM()); //camera1->SetPosition(entity1->GetPos()); //Vec3 dir = camera1->CreateViewdir(Ang3(rotate.GetFwdX(),rotate.GetFwdY(), rotate.GetFwdZ())); } originCamera = gEnv->pSystem->GetViewCamera(); /* IEntity * entity2 = gEnv->pEntitySystem->FindEntityByName("Camera-2"); if (entity2) { camera2 = entity2->GetComponent<Cry::DefaultComponents::CCameraComponent>(); camera1 = &camera2->GetCamera(); }*/ } break; case ENTITY_EVENT_START_GAME: { // Revive the entity when gameplay starts Revive(); } break; case ENTITY_EVENT_UPDATE: { SEntityUpdateContext* pCtx = (SEntityUpdateContext*)event.nParam[0]; const float moveSpeed = 20.5f; Vec3 velocity = ZERO; // Check input to calculate local space velocity if (m_inputFlags & (TInputFlags)EInputFlag::MoveLeft) { velocity.x -= moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveRight) { velocity.x += moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveForward) { velocity.y += moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveBack) { velocity.y -= moveSpeed * pCtx->fFrameTime; } // Update the player's transformation Matrix34 transformation = m_pEntity->GetWorldTM(); transformation.AddTranslation(transformation.TransformVector(velocity)); // Update entity rotation based on latest input Ang3 ypr = CCamera::CreateAnglesYPR(Matrix33(transformation)); const float rotationSpeed = 0.002f; ypr.x += m_mouseDeltaRotation.x * rotationSpeed; ypr.y += m_mouseDeltaRotation.y * rotationSpeed; // Disable roll ypr.z = 0; transformation.SetRotation33(CCamera::CreateOrientationYPR(ypr)); // Reset the mouse delta since we "used" it this frame m_mouseDeltaRotation = ZERO; // Apply set position and rotation to the entity m_pEntity->SetWorldTM(transformation); Update(pCtx->fFrameTime); } break; } } void CPlayerComponent::Revive() { // Set player transformation, but skip this in the Editor if (!gEnv->IsEditor()) { Vec3 playerScale = Vec3(1.f); Quat playerRotation = IDENTITY; // Position the player in the center of the map const float heightOffset = 20.f; float terrainCenter = gEnv->p3DEngine->GetTerrainSize() / 2.f; float height = gEnv->p3DEngine->GetTerrainZ(terrainCenter, terrainCenter); Vec3 playerPosition = Vec3(terrainCenter, terrainCenter, height + heightOffset); m_pEntity->SetWorldTM(Matrix34::Create(playerScale, playerRotation, playerPosition)); } // Unhide the entity in case hidden by the Editor GetEntity()->Hide(false); // Reset input now that the player respawned m_inputFlags = 0; m_mouseDeltaRotation = ZERO; } void CPlayerComponent::RenderCamera1ToCube() { } void CPlayerComponent::HandleInputFlagChange(TInputFlags flags, int activationMode, EInputFlagType type) { switch (type) { case EInputFlagType::Hold: { if (activationMode == eIS_Released) { m_inputFlags &= ~flags; } else { m_inputFlags |= flags; } } break; case EInputFlagType::Toggle: { if (activationMode == eIS_Released) { // Toggle the bit(s) m_inputFlags ^= flags; } } break; } } void CPlayerComponent::switchView() { if (isOriginCamera) switchOtherView(*camera1); else switchOtherView(originCamera); isOriginCamera =!isOriginCamera; } void CPlayerComponent::switchOtherView( CCamera & cam) { gEnv->pSystem->SetViewCamera(cam); } void CPlayerComponent::Update(float frametime) { SDisplayContextKey key = {}; if(camera1){ int width = gEnv->pRenderer->GetWidth(); int height = gEnv->pRenderer->GetHeight(); gEnv->pSystem->RenderBegin(key); uint32 *c = new uint32[width*height]; gEnv->pSystem->SetViewCamera(*camera1); //gEnv->pRenderer->UpdateAuxDefaultCamera(*camera1); //CRenderView * iview = gEnv->pRenderer->GetOrCreateRenderView(IRenderView::eViewType_Recursive); Render(*camera1); //gEnv->p3DEngine->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(*camera1), __FUNCTION__); //gEnv->p3DEngine->RenderWorld(IRenderer::eViewportType_Default,SRenderingPassInfo::CreateGeneralPassRenderingInfo(*gEnv->p3DEngine->GetRenderingPassCamera(*camera1)),"1"); //gEnv->pSystem->GetIProcess()->RenderWorld(805, SRenderingPassInfo::CreateGeneralPassRenderingInfo(*camera1), __FUNCTION__); gEnv->pRenderer->ReadFrameBuffer(c,width,height,false,EReadTextureFormat::RGBA8); //CryLogAlways("%0x",c[0]); if(c[0]!=0){ int textureID = gEnv->pRenderer->UploadToVideoMemory(nullptr, width, height, eTF_R8G8B8A8, eTF_R8G8B8A8, 0, false, FILTER_BILINEAR, 0, "$texture_1", FT_NOMIPS); gEnv->pRenderer->UpdateTextureInVideoMemory(textureID, (uchar*)c, 0, 0, width,height, eTF_R8G8B8A8); } gEnv->pSystem->SetViewCamera(originCamera); //gEnv->pRenderer->UpdateAuxDefaultCamera(originCamera); gEnv->pSystem->RenderBegin(key); delete[]c; } } void CPlayerComponent::Print(char * p, int size) { for (int i = 0; i < size; i++) { printf("%x",p[i]); } //CryLogAlways("%s", p[i]); } void CPlayerComponent::Render(const CCamera& cam) { if (!gEnv->pSystem->GetIProcess()) return; //should never happen SRenderingPassInfo passInfo = SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam); gEnv->p3DEngine->PrepareOcclusion(cam); gEnv->p3DEngine->PreWorldStreamUpdate(cam); if (gEnv->pRenderer) { if (gEnv->pSystem->GetIProcess()->GetFlags() & PROC_3DENGINE) { if (!gEnv->IsEditing()) // Editor calls it's own rendering update { #if !defined(_RELEASE) if (gEnv->pSystem->GetITestSystem()) gEnv->pSystem->GetITestSystem()->BeforeRender(); #endif if (gEnv->p3DEngine && !gEnv->IsFMVPlaying()) { if ((!IsEquivalent(cam.GetPosition(), Vec3(0, 0, 0), VEC_EPSILON) && (!gEnv->pSystem->IsLoading())) || // never pass undefined camera to p3DEngine->RenderWorld() gEnv->IsDedicated() || gEnv->pRenderer->IsPost3DRendererEnabled()) { gEnv->p3DEngine->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam), __FUNCTION__); } else { gEnv->pRenderer->FillFrame(Col_Black); } } } } else { gEnv->pSystem->GetIProcess()->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam), __FUNCTION__); } } gEnv->p3DEngine->WorldStreamUpdate(); }

Re: Render to texture

#8
the my code use CryEngine V,custom version 5.5.2

Code: Select all

#include "StdAfx.h" #include "Player.h" #include <CryRenderer/IRenderAuxGeom.h> void CPlayerComponent::Initialize() { m_timer = gEnv->pSystem->GetITimer(); // Create the camera component, will automatically update the viewport every frame m_pCameraComponent = m_pEntity->GetOrCreateComponent<Cry::DefaultComponents::CCameraComponent>(); // Get the input component, wraps access to action mapping so we can easily get callbacks when inputs are triggered m_pInputComponent = m_pEntity->GetOrCreateComponent<Cry::DefaultComponents::CInputComponent>(); m_pCameraManager = gEnv->pSystem->GetIPluginManager()->QueryPlugin<IPlugin_CryDefaultEntities>()->GetICameraManager(); // Register an action, and the callback that will be sent when it's triggered m_pInputComponent->RegisterAction("player", "moveleft", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveLeft, activationMode); }); // Bind the 'A' key the "moveleft" action m_pInputComponent->BindAction("player", "moveleft", eAID_KeyboardMouse, EKeyId::eKI_A); m_pInputComponent->RegisterAction("player", "moveright", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveRight, activationMode); }); m_pInputComponent->BindAction("player", "moveright", eAID_KeyboardMouse, EKeyId::eKI_D); m_pInputComponent->RegisterAction("player", "moveforward", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveForward, activationMode); }); m_pInputComponent->BindAction("player", "moveforward", eAID_KeyboardMouse, EKeyId::eKI_W); m_pInputComponent->RegisterAction("player", "moveback", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveBack, activationMode); }); m_pInputComponent->BindAction("player", "moveback", eAID_KeyboardMouse, EKeyId::eKI_S); m_pInputComponent->RegisterAction("player", "mouse_rotateyaw", [this](int activationMode, float value) { m_mouseDeltaRotation.x -= value; }); m_pInputComponent->BindAction("player", "mouse_rotateyaw", eAID_KeyboardMouse, EKeyId::eKI_MouseX); m_pInputComponent->RegisterAction("player", "mouse_rotatepitch", [this](int activationMode, float value) { m_mouseDeltaRotation.y -= value; }); m_pInputComponent->BindAction("player", "mouse_rotatepitch", eAID_KeyboardMouse, EKeyId::eKI_MouseY); m_pInputComponent->RegisterAction("player", "switchCamera", [this](int activationMode, float value) { switchView(); }); m_pInputComponent->BindAction("player", "switchCamera", eAID_KeyboardMouse, EKeyId::eKI_P); Revive(); } uint64 CPlayerComponent::GetEventMask() const { return ENTITY_EVENT_BIT(ENTITY_EVENT_START_GAME) | ENTITY_EVENT_BIT(ENTITY_EVENT_UPDATE)| ENTITY_EVENT_BIT(ENTITY_EVENT_START_LEVEL); } void CPlayerComponent::ProcessEvent(const SEntityEvent& event) { switch (event.event) { case ENTITY_EVENT_START_LEVEL: { IEntity * entity1 = gEnv->pEntitySystem->FindEntityByName("Camera-2"); IEntityClass * camera_class = entity1->GetClass(); Cry::DefaultComponents::CCameraComponent * camera = entity1->GetComponent<Cry::DefaultComponents::CCameraComponent>(); if (camera) { //Quat rotate = entity1->GetWorldRotation(); camera1 = &camera->GetCamera(); camera1->SetMatrix(entity1->GetWorldTM()); //camera1->SetPosition(entity1->GetPos()); //Vec3 dir = camera1->CreateViewdir(Ang3(rotate.GetFwdX(),rotate.GetFwdY(), rotate.GetFwdZ())); } originCamera = gEnv->pSystem->GetViewCamera(); /* IEntity * entity2 = gEnv->pEntitySystem->FindEntityByName("Camera-2"); if (entity2) { camera2 = entity2->GetComponent<Cry::DefaultComponents::CCameraComponent>(); camera1 = &camera2->GetCamera(); }*/ } break; case ENTITY_EVENT_START_GAME: { // Revive the entity when gameplay starts Revive(); } break; case ENTITY_EVENT_UPDATE: { SEntityUpdateContext* pCtx = (SEntityUpdateContext*)event.nParam[0]; const float moveSpeed = 20.5f; Vec3 velocity = ZERO; // Check input to calculate local space velocity if (m_inputFlags & (TInputFlags)EInputFlag::MoveLeft) { velocity.x -= moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveRight) { velocity.x += moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveForward) { velocity.y += moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveBack) { velocity.y -= moveSpeed * pCtx->fFrameTime; } // Update the player's transformation Matrix34 transformation = m_pEntity->GetWorldTM(); transformation.AddTranslation(transformation.TransformVector(velocity)); // Update entity rotation based on latest input Ang3 ypr = CCamera::CreateAnglesYPR(Matrix33(transformation)); const float rotationSpeed = 0.002f; ypr.x += m_mouseDeltaRotation.x * rotationSpeed; ypr.y += m_mouseDeltaRotation.y * rotationSpeed; // Disable roll ypr.z = 0; transformation.SetRotation33(CCamera::CreateOrientationYPR(ypr)); // Reset the mouse delta since we "used" it this frame m_mouseDeltaRotation = ZERO; // Apply set position and rotation to the entity m_pEntity->SetWorldTM(transformation); Update(pCtx->fFrameTime); } break; } } void CPlayerComponent::Revive() { // Set player transformation, but skip this in the Editor if (!gEnv->IsEditor()) { Vec3 playerScale = Vec3(1.f); Quat playerRotation = IDENTITY; // Position the player in the center of the map const float heightOffset = 20.f; float terrainCenter = gEnv->p3DEngine->GetTerrainSize() / 2.f; float height = gEnv->p3DEngine->GetTerrainZ(terrainCenter, terrainCenter); Vec3 playerPosition = Vec3(terrainCenter, terrainCenter, height + heightOffset); m_pEntity->SetWorldTM(Matrix34::Create(playerScale, playerRotation, playerPosition)); } // Unhide the entity in case hidden by the Editor GetEntity()->Hide(false); // Reset input now that the player respawned m_inputFlags = 0; m_mouseDeltaRotation = ZERO; } void CPlayerComponent::RenderCamera1ToCube() { } void CPlayerComponent::HandleInputFlagChange(TInputFlags flags, int activationMode, EInputFlagType type) { switch (type) { case EInputFlagType::Hold: { if (activationMode == eIS_Released) { m_inputFlags &= ~flags; } else { m_inputFlags |= flags; } } break; case EInputFlagType::Toggle: { if (activationMode == eIS_Released) { // Toggle the bit(s) m_inputFlags ^= flags; } } break; } } void CPlayerComponent::switchView() { if (isOriginCamera) switchOtherView(*camera1); else switchOtherView(originCamera); isOriginCamera =!isOriginCamera; } void CPlayerComponent::switchOtherView( CCamera & cam) { gEnv->pSystem->SetViewCamera(cam); } void CPlayerComponent::Update(float frametime) { SDisplayContextKey key = {}; if(camera1){ int width = gEnv->pRenderer->GetWidth(); int height = gEnv->pRenderer->GetHeight(); gEnv->pSystem->RenderBegin(key); uint32 *c = new uint32[width*height]; gEnv->pSystem->SetViewCamera(*camera1); //gEnv->pRenderer->UpdateAuxDefaultCamera(*camera1); //CRenderView * iview = gEnv->pRenderer->GetOrCreateRenderView(IRenderView::eViewType_Recursive); Render(*camera1); //gEnv->p3DEngine->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(*camera1), __FUNCTION__); //gEnv->p3DEngine->RenderWorld(IRenderer::eViewportType_Default,SRenderingPassInfo::CreateGeneralPassRenderingInfo(*gEnv->p3DEngine->GetRenderingPassCamera(*camera1)),"1"); //gEnv->pSystem->GetIProcess()->RenderWorld(805, SRenderingPassInfo::CreateGeneralPassRenderingInfo(*camera1), __FUNCTION__); gEnv->pRenderer->ReadFrameBuffer(c,width,height,false,EReadTextureFormat::RGBA8); //CryLogAlways("%0x",c[0]); if(c[0]!=0){ int textureID = gEnv->pRenderer->UploadToVideoMemory(nullptr, width, height, eTF_R8G8B8A8, eTF_R8G8B8A8, 0, false, FILTER_BILINEAR, 0, "$texture_1", FT_NOMIPS); gEnv->pRenderer->UpdateTextureInVideoMemory(textureID, (uchar*)c, 0, 0, width,height, eTF_R8G8B8A8); } gEnv->pSystem->SetViewCamera(originCamera); //gEnv->pRenderer->UpdateAuxDefaultCamera(originCamera); gEnv->pSystem->RenderBegin(key); delete[]c; } } void CPlayerComponent::Print(char * p, int size) { for (int i = 0; i < size; i++) { printf("%x",p[i]); } //CryLogAlways("%s", p[i]); } void CPlayerComponent::Render(const CCamera& cam) { if (!gEnv->pSystem->GetIProcess()) return; //should never happen SRenderingPassInfo passInfo = SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam); gEnv->p3DEngine->PrepareOcclusion(cam); gEnv->p3DEngine->PreWorldStreamUpdate(cam); if (gEnv->pRenderer) { if (gEnv->pSystem->GetIProcess()->GetFlags() & PROC_3DENGINE) { if (!gEnv->IsEditing()) // Editor calls it's own rendering update { #if !defined(_RELEASE) if (gEnv->pSystem->GetITestSystem()) gEnv->pSystem->GetITestSystem()->BeforeRender(); #endif if (gEnv->p3DEngine && !gEnv->IsFMVPlaying()) { if ((!IsEquivalent(cam.GetPosition(), Vec3(0, 0, 0), VEC_EPSILON) && (!gEnv->pSystem->IsLoading())) || // never pass undefined camera to p3DEngine->RenderWorld() gEnv->IsDedicated() || gEnv->pRenderer->IsPost3DRendererEnabled()) { gEnv->p3DEngine->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam), __FUNCTION__); } else { gEnv->pRenderer->FillFrame(Col_Black); } } } } else { gEnv->pSystem->GetIProcess()->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam), __FUNCTION__); } } gEnv->p3DEngine->WorldStreamUpdate(); }
I compile your code , but failed in "$texture_1". can upload sample project???

Re: Render to texture

#10
the my code use CryEngine V,custom version 5.5.2

Code: Select all

#include "StdAfx.h" #include "Player.h" #include <CryRenderer/IRenderAuxGeom.h> void CPlayerComponent::Initialize() { m_timer = gEnv->pSystem->GetITimer(); // Create the camera component, will automatically update the viewport every frame m_pCameraComponent = m_pEntity->GetOrCreateComponent<Cry::DefaultComponents::CCameraComponent>(); // Get the input component, wraps access to action mapping so we can easily get callbacks when inputs are triggered m_pInputComponent = m_pEntity->GetOrCreateComponent<Cry::DefaultComponents::CInputComponent>(); m_pCameraManager = gEnv->pSystem->GetIPluginManager()->QueryPlugin<IPlugin_CryDefaultEntities>()->GetICameraManager(); // Register an action, and the callback that will be sent when it's triggered m_pInputComponent->RegisterAction("player", "moveleft", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveLeft, activationMode); }); // Bind the 'A' key the "moveleft" action m_pInputComponent->BindAction("player", "moveleft", eAID_KeyboardMouse, EKeyId::eKI_A); m_pInputComponent->RegisterAction("player", "moveright", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveRight, activationMode); }); m_pInputComponent->BindAction("player", "moveright", eAID_KeyboardMouse, EKeyId::eKI_D); m_pInputComponent->RegisterAction("player", "moveforward", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveForward, activationMode); }); m_pInputComponent->BindAction("player", "moveforward", eAID_KeyboardMouse, EKeyId::eKI_W); m_pInputComponent->RegisterAction("player", "moveback", [this](int activationMode, float value) { HandleInputFlagChange((TInputFlags)EInputFlag::MoveBack, activationMode); }); m_pInputComponent->BindAction("player", "moveback", eAID_KeyboardMouse, EKeyId::eKI_S); m_pInputComponent->RegisterAction("player", "mouse_rotateyaw", [this](int activationMode, float value) { m_mouseDeltaRotation.x -= value; }); m_pInputComponent->BindAction("player", "mouse_rotateyaw", eAID_KeyboardMouse, EKeyId::eKI_MouseX); m_pInputComponent->RegisterAction("player", "mouse_rotatepitch", [this](int activationMode, float value) { m_mouseDeltaRotation.y -= value; }); m_pInputComponent->BindAction("player", "mouse_rotatepitch", eAID_KeyboardMouse, EKeyId::eKI_MouseY); m_pInputComponent->RegisterAction("player", "switchCamera", [this](int activationMode, float value) { switchView(); }); m_pInputComponent->BindAction("player", "switchCamera", eAID_KeyboardMouse, EKeyId::eKI_P); Revive(); } uint64 CPlayerComponent::GetEventMask() const { return ENTITY_EVENT_BIT(ENTITY_EVENT_START_GAME) | ENTITY_EVENT_BIT(ENTITY_EVENT_UPDATE)| ENTITY_EVENT_BIT(ENTITY_EVENT_START_LEVEL); } void CPlayerComponent::ProcessEvent(const SEntityEvent& event) { switch (event.event) { case ENTITY_EVENT_START_LEVEL: { IEntity * entity1 = gEnv->pEntitySystem->FindEntityByName("Camera-2"); IEntityClass * camera_class = entity1->GetClass(); Cry::DefaultComponents::CCameraComponent * camera = entity1->GetComponent<Cry::DefaultComponents::CCameraComponent>(); if (camera) { //Quat rotate = entity1->GetWorldRotation(); camera1 = &camera->GetCamera(); camera1->SetMatrix(entity1->GetWorldTM()); //camera1->SetPosition(entity1->GetPos()); //Vec3 dir = camera1->CreateViewdir(Ang3(rotate.GetFwdX(),rotate.GetFwdY(), rotate.GetFwdZ())); } originCamera = gEnv->pSystem->GetViewCamera(); /* IEntity * entity2 = gEnv->pEntitySystem->FindEntityByName("Camera-2"); if (entity2) { camera2 = entity2->GetComponent<Cry::DefaultComponents::CCameraComponent>(); camera1 = &camera2->GetCamera(); }*/ } break; case ENTITY_EVENT_START_GAME: { // Revive the entity when gameplay starts Revive(); } break; case ENTITY_EVENT_UPDATE: { SEntityUpdateContext* pCtx = (SEntityUpdateContext*)event.nParam[0]; const float moveSpeed = 20.5f; Vec3 velocity = ZERO; // Check input to calculate local space velocity if (m_inputFlags & (TInputFlags)EInputFlag::MoveLeft) { velocity.x -= moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveRight) { velocity.x += moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveForward) { velocity.y += moveSpeed * pCtx->fFrameTime; } if (m_inputFlags & (TInputFlags)EInputFlag::MoveBack) { velocity.y -= moveSpeed * pCtx->fFrameTime; } // Update the player's transformation Matrix34 transformation = m_pEntity->GetWorldTM(); transformation.AddTranslation(transformation.TransformVector(velocity)); // Update entity rotation based on latest input Ang3 ypr = CCamera::CreateAnglesYPR(Matrix33(transformation)); const float rotationSpeed = 0.002f; ypr.x += m_mouseDeltaRotation.x * rotationSpeed; ypr.y += m_mouseDeltaRotation.y * rotationSpeed; // Disable roll ypr.z = 0; transformation.SetRotation33(CCamera::CreateOrientationYPR(ypr)); // Reset the mouse delta since we "used" it this frame m_mouseDeltaRotation = ZERO; // Apply set position and rotation to the entity m_pEntity->SetWorldTM(transformation); Update(pCtx->fFrameTime); } break; } } void CPlayerComponent::Revive() { // Set player transformation, but skip this in the Editor if (!gEnv->IsEditor()) { Vec3 playerScale = Vec3(1.f); Quat playerRotation = IDENTITY; // Position the player in the center of the map const float heightOffset = 20.f; float terrainCenter = gEnv->p3DEngine->GetTerrainSize() / 2.f; float height = gEnv->p3DEngine->GetTerrainZ(terrainCenter, terrainCenter); Vec3 playerPosition = Vec3(terrainCenter, terrainCenter, height + heightOffset); m_pEntity->SetWorldTM(Matrix34::Create(playerScale, playerRotation, playerPosition)); } // Unhide the entity in case hidden by the Editor GetEntity()->Hide(false); // Reset input now that the player respawned m_inputFlags = 0; m_mouseDeltaRotation = ZERO; } void CPlayerComponent::RenderCamera1ToCube() { } void CPlayerComponent::HandleInputFlagChange(TInputFlags flags, int activationMode, EInputFlagType type) { switch (type) { case EInputFlagType::Hold: { if (activationMode == eIS_Released) { m_inputFlags &= ~flags; } else { m_inputFlags |= flags; } } break; case EInputFlagType::Toggle: { if (activationMode == eIS_Released) { // Toggle the bit(s) m_inputFlags ^= flags; } } break; } } void CPlayerComponent::switchView() { if (isOriginCamera) switchOtherView(*camera1); else switchOtherView(originCamera); isOriginCamera =!isOriginCamera; } void CPlayerComponent::switchOtherView( CCamera & cam) { gEnv->pSystem->SetViewCamera(cam); } void CPlayerComponent::Update(float frametime) { SDisplayContextKey key = {}; if(camera1){ int width = gEnv->pRenderer->GetWidth(); int height = gEnv->pRenderer->GetHeight(); gEnv->pSystem->RenderBegin(key); uint32 *c = new uint32[width*height]; gEnv->pSystem->SetViewCamera(*camera1); //gEnv->pRenderer->UpdateAuxDefaultCamera(*camera1); //CRenderView * iview = gEnv->pRenderer->GetOrCreateRenderView(IRenderView::eViewType_Recursive); Render(*camera1); //gEnv->p3DEngine->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(*camera1), __FUNCTION__); //gEnv->p3DEngine->RenderWorld(IRenderer::eViewportType_Default,SRenderingPassInfo::CreateGeneralPassRenderingInfo(*gEnv->p3DEngine->GetRenderingPassCamera(*camera1)),"1"); //gEnv->pSystem->GetIProcess()->RenderWorld(805, SRenderingPassInfo::CreateGeneralPassRenderingInfo(*camera1), __FUNCTION__); gEnv->pRenderer->ReadFrameBuffer(c,width,height,false,EReadTextureFormat::RGBA8); //CryLogAlways("%0x",c[0]); if(c[0]!=0){ int textureID = gEnv->pRenderer->UploadToVideoMemory(nullptr, width, height, eTF_R8G8B8A8, eTF_R8G8B8A8, 0, false, FILTER_BILINEAR, 0, "$texture_1", FT_NOMIPS); gEnv->pRenderer->UpdateTextureInVideoMemory(textureID, (uchar*)c, 0, 0, width,height, eTF_R8G8B8A8); } gEnv->pSystem->SetViewCamera(originCamera); //gEnv->pRenderer->UpdateAuxDefaultCamera(originCamera); gEnv->pSystem->RenderBegin(key); delete[]c; } } void CPlayerComponent::Print(char * p, int size) { for (int i = 0; i < size; i++) { printf("%x",p[i]); } //CryLogAlways("%s", p[i]); } void CPlayerComponent::Render(const CCamera& cam) { if (!gEnv->pSystem->GetIProcess()) return; //should never happen SRenderingPassInfo passInfo = SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam); gEnv->p3DEngine->PrepareOcclusion(cam); gEnv->p3DEngine->PreWorldStreamUpdate(cam); if (gEnv->pRenderer) { if (gEnv->pSystem->GetIProcess()->GetFlags() & PROC_3DENGINE) { if (!gEnv->IsEditing()) // Editor calls it's own rendering update { #if !defined(_RELEASE) if (gEnv->pSystem->GetITestSystem()) gEnv->pSystem->GetITestSystem()->BeforeRender(); #endif if (gEnv->p3DEngine && !gEnv->IsFMVPlaying()) { if ((!IsEquivalent(cam.GetPosition(), Vec3(0, 0, 0), VEC_EPSILON) && (!gEnv->pSystem->IsLoading())) || // never pass undefined camera to p3DEngine->RenderWorld() gEnv->IsDedicated() || gEnv->pRenderer->IsPost3DRendererEnabled()) { gEnv->p3DEngine->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam), __FUNCTION__); } else { gEnv->pRenderer->FillFrame(Col_Black); } } } } else { gEnv->pSystem->GetIProcess()->RenderWorld(SHDF_ALLOW_WATER | SHDF_ALLOWPOSTPROCESS | SHDF_ALLOWHDR | SHDF_ZPASS | SHDF_ALLOW_AO, SRenderingPassInfo::CreateGeneralPassRenderingInfo(cam), __FUNCTION__); } } gEnv->p3DEngine->WorldStreamUpdate(); }
I compile your code , but failed in "$texture_1". can upload sample project???
geometry mesh object materials,diffuse filename is $texture_1

Who is online

Users browsing this forum: ik-interactive and 0 guests