void World::CreateNamedRT(String worldName) { Graphics* g = context->GetSubsystem<Graphics>(); int w = g->GetWidth(); int h = g->GetHeight(); renderTexture = SharedPtr<Texture2D>(new Texture2D(context)); if (renderTexture) { renderTexture->SetSize(w, h, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET); renderTexture->SetFilterMode(FILTER_NEAREST); renderTexture->SetName(worldName); ResourceCache* cache = context->GetSubsystem<ResourceCache>(); cache->AddManualResource(renderTexture); RenderSurface* surface = renderTexture->GetRenderSurface(); surface->SetUpdateMode(RenderSurfaceUpdateMode::SURFACE_UPDATEALWAYS); surface->SetViewport(0, camera.vp); } }
void RTTScene::CreateScene() { rttScene_ = new Scene(context_); rttScene_->CreateComponent<Octree>(); Node* zoneNode = rttScene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); //用来指定ViewPort时的定位 rttCameraNode_ = rttScene_->CreateChild("Camera"); Camera* camera = rttCameraNode_->CreateComponent<Camera>(); Light* light = rttCameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); renderTexture = new Texture2D(context_); //设置当前场景的ViewPort renderTexture->SetSize(_width,_height,Graphics::GetRGBFormat(),TEXTURE_RENDERTARGET); RenderSurface* surface = renderTexture->GetRenderSurface(); renderTexture->SetFilterMode(FILTER_BILINEAR); Viewport* rttViewPort = new Viewport(context_,rttScene_,camera); surface->SetViewport(0,rttViewPort); }
void Water::SetupViewport() { Graphics* graphics = GetContext()->m_Graphics.get(); Renderer* renderer = GetContext()->m_Renderer.get(); ResourceCache* cache = GetContext()->m_ResourceCache.get(); // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen SharedPtr<Viewport> viewport(new Viewport(GetContext(), scene_, cameraNode_->GetComponent<Camera>())); renderer->SetViewport(0, viewport); // Create a mathematical plane to represent the water in calculations waterPlane_ = Plane(waterNode_->GetWorldRotation() * Vector3(0.0f, 1.0f, 0.0f), waterNode_->GetWorldPosition()); // Create a downward biased plane for reflection view clipping. Biasing is necessary to avoid too aggressive clipping waterClipPlane_ = Plane(waterNode_->GetWorldRotation() * Vector3(0.0f, 1.0f, 0.0f), waterNode_->GetWorldPosition() - Vector3(0.0f, 0.1f, 0.0f)); // Create camera for water reflection // It will have the same farclip and position as the main viewport camera, but uses a reflection plane to modify // its position when rendering reflectionCameraNode_ = cameraNode_->CreateChild(); Camera* reflectionCamera = reflectionCameraNode_->CreateComponent<Camera>(); reflectionCamera->setFarClipDistance(750.0); reflectionCamera->SetViewMask(0x7fffffff); // Hide objects with only bit 31 in the viewmask (the water plane) reflectionCamera->SetAutoAspectRatio(false); reflectionCamera->SetUseReflection(true); reflectionCamera->SetReflectionPlane(waterPlane_); reflectionCamera->SetUseClipping(true); // Enable clipping of geometry behind water plane reflectionCamera->SetClipPlane(waterClipPlane_); // The water reflection texture is rectangular. Set reflection camera aspect ratio to match reflectionCamera->SetAspectRatio((float)graphics->GetWidth() / (float)graphics->GetHeight()); // View override flags could be used to optimize reflection rendering. For example disable shadows //reflectionCamera->SetViewOverrideFlags(VO_DISABLE_SHADOWS); // Create a texture and setup viewport for water reflection. Assign the reflection texture to the diffuse // texture unit of the water material int texSize = 1024; SharedPtr<Texture2D> renderTexture(new Texture2D(GetContext())); renderTexture->SetSize(texSize, texSize, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET); renderTexture->SetFilterMode(FILTER_BILINEAR); RenderSurface* surface = renderTexture->GetRenderSurface(); SharedPtr<Viewport> rttViewport(new Viewport(GetContext(), scene_, reflectionCamera)); surface->SetViewport(0, rttViewport); Material* waterMat = cache->GetResource<Material>("Materials/Water.xml"); waterMat->SetTexture(TU_DIFFUSE, renderTexture); }
void UISceneView::OnResize(const IntVector2 &newSize) { if (newSize.x_ == size_.x_ && newSize.y_ == size_.y_) return; int width = newSize.x_; int height = newSize.y_; if (width > 0 && height > 0) { viewport_->SetRect(IntRect(0, 0, width, height)); renderTexture_->SetSize(width, height, rttFormat_, TEXTURE_RENDERTARGET); depthTexture_->SetSize(width, height, Graphics::GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL); RenderSurface* surface = renderTexture_->GetRenderSurface(); surface->SetViewport(0, viewport_); surface->SetUpdateMode(autoUpdate_ ? SURFACE_UPDATEALWAYS : SURFACE_MANUALUPDATE); surface->SetLinkedDepthStencil(depthTexture_->GetRenderSurface()); size_ = newSize; } }
void GameMain::WaterSetup(Urho3D::String waterNodeName) { ResourceCache* cache = GetSubsystem<ResourceCache>(); Graphics* graphics = GetSubsystem<Graphics>(); Node* waterNode_ = scene_->GetChild("waterNode",true); StaticModel* waterMesh = waterNode_->GetComponent<StaticModel>(); waterMesh->SetViewMask(0x80000000); world.camera.waterPlane_ = Plane(waterNode_->GetWorldRotation() * Vector3(0.0f, 1.0f, 0.0f), waterNode_->GetWorldPosition()); world.camera.waterClipPlane_ = Plane(waterNode_->GetWorldRotation() * Vector3(0.0f, 1.0f, 0.0f), waterNode_->GetWorldPosition() - Vector3(0.0f, 0.1f, 0.0f)); world.camera.reflectionCameraNode_ = world.camera.node_->CreateChild(); Camera* reflectionCamera = world.camera.reflectionCameraNode_->CreateComponent<Camera>(); reflectionCamera->SetFarClip(1000.0); reflectionCamera->SetFov(75.0f); reflectionCamera->SetViewMask(0x7fffffff); // Hide objects with only bit 31 in the viewmask (the water plane) reflectionCamera->SetAutoAspectRatio(false); reflectionCamera->SetUseReflection(true); reflectionCamera->SetReflectionPlane(world.camera.waterPlane_); reflectionCamera->SetUseClipping(true); // Enable clipping of geometry behind water plane reflectionCamera->SetClipPlane(world.camera.waterClipPlane_); reflectionCamera->SetAspectRatio((float)graphics->GetWidth() / (float)graphics->GetHeight()); int texSize = 1024; SharedPtr<Texture2D> renderTexture(new Texture2D(context_)); renderTexture->SetSize(texSize, texSize, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET); renderTexture->SetFilterMode(FILTER_BILINEAR); RenderSurface* surface = renderTexture->GetRenderSurface(); SharedPtr<Viewport> rttViewport(new Viewport(context_, scene_, reflectionCamera)); surface->SetViewport(0, rttViewport); Material* waterMat = cache->GetResource<Material>("Materials/Water.xml"); waterMat->SetTexture(TU_DIFFUSE, renderTexture); }
void RenderToTexture::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); { // Create the scene which will be rendered to a texture rttScene_ = new Scene(context_); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) rttScene_->CreateComponent<Octree>(); // Create a Zone for ambient light & fog control Node* zoneNode = rttScene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); // Set same volume as the Octree, set a close bluish fog and some ambient light zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); // Create randomly positioned and oriented box StaticModels in the scene const unsigned NUM_OBJECTS = 2000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = rttScene_->CreateChild("Box"); boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f)); // Orient using random pitch, yaw and roll Euler angles boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event. // Simply set same rotation speed for all objects Rotator* rotator = boxNode->CreateComponent<Rotator>(); rotator->SetRotationSpeed(Vector3(10.0f, 20.0f, 30.0f)); } // Create a camera for the render-to-texture scene. Simply leave it at the world origin and let it observe the scene rttCameraNode_ = rttScene_->CreateChild("Camera"); Camera* camera = rttCameraNode_->CreateComponent<Camera>(); camera->SetFarClip(100.0f); // Create a point light to the camera scene node Light* light = rttCameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); light->SetRange(30.0f); } { // Create the scene in which we move around scene_ = new Scene(context_); // Create octree, use also default volume (-1000, -1000, -1000) to (1000, 1000, 1000) scene_->CreateComponent<Octree>(); // Create a Zone component for ambient lighting & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.1f, 0.1f, 0.1f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light without shadows Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.5f, -1.0f, 0.5f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetColor(Color(0.2f, 0.2f, 0.2f)); light->SetSpecularIntensity(1.0f); // Create a "floor" consisting of several tiles for (int y = -5; y <= 5; ++y) { for (int x = -5; x <= 5; ++x) { Node* floorNode = scene_->CreateChild("FloorTile"); floorNode->SetPosition(Vector3(x * 20.5f, -0.5f, y * 20.5f)); floorNode->SetScale(Vector3(20.0f, 1.0f, 20.f)); StaticModel* floorObject = floorNode->CreateComponent<StaticModel>(); floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); floorObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); } } // Create a "screen" like object for viewing the second scene. Construct it from two StaticModels, a box for the frame // and a plane for the actual view { Node* boxNode = scene_->CreateChild("ScreenBox"); boxNode->SetPosition(Vector3(0.0f, 10.0f, 0.0f)); boxNode->SetScale(Vector3(21.0f, 16.0f, 0.5f)); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); Node* screenNode = scene_->CreateChild("Screen"); screenNode->SetPosition(Vector3(0.0f, 10.0f, -0.27f)); screenNode->SetRotation(Quaternion(-90.0f, 0.0f, 0.0f)); screenNode->SetScale(Vector3(20.0f, 0.0f, 15.0f)); StaticModel* screenObject = screenNode->CreateComponent<StaticModel>(); screenObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); // Create a renderable texture (1024x768, RGB format), enable bilinear filtering on it SharedPtr<Texture2D> renderTexture(new Texture2D(context_)); renderTexture->SetSize(1024, 768, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET); renderTexture->SetFilterMode(FILTER_BILINEAR); // Create a new material from scratch, use the diffuse unlit technique, assign the render texture // as its diffuse texture, then assign the material to the screen plane object SharedPtr<Material> renderMaterial(new Material(context_)); renderMaterial->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml")); renderMaterial->SetTexture(TU_DIFFUSE, renderTexture); screenObject->SetMaterial(renderMaterial); // Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode) // and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined // to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible // in the main view RenderSurface* surface = renderTexture->GetRenderSurface(); SharedPtr<Viewport> rttViewport(new Viewport(context_, rttScene_, rttCameraNode_->GetComponent<Camera>())); surface->SetViewport(0, rttViewport); } // Create the camera which we will move around. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 7.0f, -30.0f)); } }