1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #include <Urho3D/Core/CoreEvents.h>
24 #include <Urho3D/Engine/Engine.h>
25 #include <Urho3D/Graphics/AnimatedModel.h>
26 #include <Urho3D/Graphics/AnimationController.h>
27 #include <Urho3D/Graphics/Camera.h>
28 #include <Urho3D/Graphics/Graphics.h>
29 #include <Urho3D/Graphics/Material.h>
30 #include <Urho3D/Graphics/Octree.h>
31 #include <Urho3D/Graphics/Renderer.h>
32 #include <Urho3D/Graphics/RibbonTrail.h>
33 #include <Urho3D/Input/Input.h>
34 #include <Urho3D/Resource/ResourceCache.h>
35 #include <Urho3D/Scene/Scene.h>
36 #include <Urho3D/UI/Font.h>
37 #include <Urho3D/UI/Text.h>
38 #include <Urho3D/UI/UI.h>
39 #include <Urho3D/UI/Text3D.h>
40 
41 #include "RibbonTrailDemo.h"
42 
43 #include <Urho3D/DebugNew.h>
44 
URHO3D_DEFINE_APPLICATION_MAIN(RibbonTrailDemo)45 URHO3D_DEFINE_APPLICATION_MAIN(RibbonTrailDemo)
46 
47 RibbonTrailDemo::RibbonTrailDemo(Context* context) :
48     Sample(context),
49     swordTrailStartTime_(0.2f),
50     swordTrailEndTime_(0.46f),
51     timeStepSum_(0.0f)
52 {
53 }
54 
Start()55 void RibbonTrailDemo::Start()
56 {
57     // Execute base class startup
58     Sample::Start();
59 
60     // Create the scene content
61     CreateScene();
62 
63     // Create the UI content
64     CreateInstructions();
65 
66     // Setup the viewport for displaying the scene
67     SetupViewport();
68 
69     // Hook up to the frame update events
70     SubscribeToEvents();
71 
72     // Set the mouse mode to use in the sample
73     Sample::InitMouseMode(MM_RELATIVE);
74 }
75 
CreateScene()76 void RibbonTrailDemo::CreateScene()
77 {
78     ResourceCache* cache = GetSubsystem<ResourceCache>();
79 
80     scene_ = new Scene(context_);
81 
82     // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
83     scene_->CreateComponent<Octree>();
84 
85     // Create scene node & StaticModel component for showing a static plane
86     Node* planeNode = scene_->CreateChild("Plane");
87     planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f));
88     StaticModel* planeObject = planeNode->CreateComponent<StaticModel>();
89     planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
90     planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
91 
92     // Create a directional light to the world.
93     Node* lightNode = scene_->CreateChild("DirectionalLight");
94     lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); // The direction vector does not need to be normalized
95     Light* light = lightNode->CreateComponent<Light>();
96     light->SetLightType(LIGHT_DIRECTIONAL);
97     light->SetCastShadows(true);
98     light->SetShadowBias(BiasParameters(0.00005f, 0.5f));
99     // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
100     light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f));
101 
102     // Create first box for face camera trail demo with 1 column.
103     boxNode1_ = scene_->CreateChild("Box1");
104     StaticModel* box1 = boxNode1_->CreateComponent<StaticModel>();
105     box1->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
106     box1->SetCastShadows(true);
107     RibbonTrail* boxTrail1 = boxNode1_->CreateComponent<RibbonTrail>();
108     boxTrail1->SetMaterial(cache->GetResource<Material>("Materials/RibbonTrail.xml"));
109     boxTrail1->SetStartColor(Color(1.0f, 0.5f, 0.0f, 1.0f));
110     boxTrail1->SetEndColor(Color(1.0f, 1.0f, 0.0f, 0.0f));
111     boxTrail1->SetWidth(0.5f);
112     boxTrail1->SetUpdateInvisible(true);
113 
114     // Create second box for face camera trail demo with 4 column.
115     // This will produce less distortion than first trail.
116     boxNode2_ = scene_->CreateChild("Box2");
117     StaticModel* box2 = boxNode2_->CreateComponent<StaticModel>();
118     box2->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
119     box2->SetCastShadows(true);
120     RibbonTrail* boxTrail2 = boxNode2_->CreateComponent<RibbonTrail>();
121     boxTrail2->SetMaterial(cache->GetResource<Material>("Materials/RibbonTrail.xml"));
122     boxTrail2->SetStartColor(Color(1.0f, 0.5f, 0.0f, 1.0f));
123     boxTrail2->SetEndColor(Color(1.0f, 1.0f, 0.0f, 0.0f));
124     boxTrail2->SetWidth(0.5f);
125     boxTrail2->SetTailColumn(4);
126     boxTrail2->SetUpdateInvisible(true);
127 
128     // Load ninja animated model for bone trail demo.
129     Node* ninjaNode = scene_->CreateChild("Ninja");
130     ninjaNode->SetPosition(Vector3(5.0f, 0.0f, 0.0f));
131     ninjaNode->SetRotation(Quaternion(0.0f, 180.0f, 0.0f));
132     AnimatedModel* ninja = ninjaNode->CreateComponent<AnimatedModel>();
133     ninja->SetModel(cache->GetResource<Model>("Models/NinjaSnowWar/Ninja.mdl"));
134     ninja->SetMaterial(cache->GetResource<Material>("Materials/NinjaSnowWar/Ninja.xml"));
135     ninja->SetCastShadows(true);
136 
137     // Create animation controller and play attack animation.
138     ninjaAnimCtrl_ = ninjaNode->CreateComponent<AnimationController>();
139     ninjaAnimCtrl_->PlayExclusive("Models/NinjaSnowWar/Ninja_Attack3.ani", 0, true, 0.0f);
140 
141     // Add ribbon trail to tip of sword.
142     Node* swordTip = ninjaNode->GetChild("Joint29", true);
143     swordTrail_ = swordTip->CreateComponent<RibbonTrail>();
144 
145     // Set sword trail type to bone and set other parameters.
146     swordTrail_->SetTrailType(TT_BONE);
147     swordTrail_->SetMaterial(cache->GetResource<Material>("Materials/SlashTrail.xml"));
148     swordTrail_->SetLifetime(0.22f);
149     swordTrail_->SetStartColor(Color(1.0f, 1.0f, 1.0f, 0.75f));
150     swordTrail_->SetEndColor(Color(0.2f, 0.5f, 1.0f, 0.0f));
151     swordTrail_->SetTailColumn(4);
152     swordTrail_->SetUpdateInvisible(true);
153 
154     // Add floating text for info.
155     Node* boxTextNode1 = scene_->CreateChild("BoxText1");
156     boxTextNode1->SetPosition(Vector3(-1.0f, 2.0f, 0.0f));
157     Text3D* boxText1 = boxTextNode1->CreateComponent<Text3D>();
158     boxText1->SetText(String("Face Camera Trail (4 Column)"));
159     boxText1->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.sdf"), 24);
160 
161     Node* boxTextNode2 = scene_->CreateChild("BoxText2");
162     boxTextNode2->SetPosition(Vector3(-6.0f, 2.0f, 0.0f));
163     Text3D* boxText2 = boxTextNode2->CreateComponent<Text3D>();
164     boxText2->SetText(String("Face Camera Trail (1 Column)"));
165     boxText2->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.sdf"), 24);
166 
167     Node* ninjaTextNode2 = scene_->CreateChild("NinjaText");
168     ninjaTextNode2->SetPosition(Vector3(4.0f, 2.5f, 0.0f));
169     Text3D* ninjaText = ninjaTextNode2->CreateComponent<Text3D>();
170     ninjaText->SetText(String("Bone Trail (4 Column)"));
171     ninjaText->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.sdf"), 24);
172 
173     // Create the camera.
174     cameraNode_ = scene_->CreateChild("Camera");
175     cameraNode_->CreateComponent<Camera>();
176 
177     // Set an initial position for the camera scene node above the plane
178     cameraNode_->SetPosition(Vector3(0.0f, 2.0f, -14.0f));
179 }
180 
CreateInstructions()181 void RibbonTrailDemo::CreateInstructions()
182 {
183     ResourceCache* cache = GetSubsystem<ResourceCache>();
184     UI* ui = GetSubsystem<UI>();
185 
186     // Construct new Text object, set string to display and font to use
187     Text* instructionText = ui->GetRoot()->CreateChild<Text>();
188     instructionText->SetText("Use WASD keys and mouse/touch to move");
189     instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
190 
191     // Position the text relative to the screen center
192     instructionText->SetHorizontalAlignment(HA_CENTER);
193     instructionText->SetVerticalAlignment(VA_CENTER);
194     instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
195 }
196 
SetupViewport()197 void RibbonTrailDemo::SetupViewport()
198 {
199     Renderer* renderer = GetSubsystem<Renderer>();
200 
201     // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
202     // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
203     // use, but now we just use full screen and default render path configured in the engine command line options
204     SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
205     renderer->SetViewport(0, viewport);
206 }
207 
MoveCamera(float timeStep)208 void RibbonTrailDemo::MoveCamera(float timeStep)
209 {
210     // Do not move if the UI has a focused element (the console)
211     if (GetSubsystem<UI>()->GetFocusElement())
212         return;
213 
214     Input* input = GetSubsystem<Input>();
215 
216     // Movement speed as world units per second
217     const float MOVE_SPEED = 20.0f;
218     // Mouse sensitivity as degrees per pixel
219     const float MOUSE_SENSITIVITY = 0.1f;
220 
221     // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
222     IntVector2 mouseMove = input->GetMouseMove();
223     yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
224     pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
225     pitch_ = Clamp(pitch_, -90.0f, 90.0f);
226 
227     // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
228     cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
229 
230     // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
231     // Use the Translate() function (default local space) to move relative to the node's orientation.
232     if (input->GetKeyDown(KEY_W))
233         cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
234     if (input->GetKeyDown(KEY_S))
235         cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
236     if (input->GetKeyDown(KEY_A))
237         cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
238     if (input->GetKeyDown(KEY_D))
239         cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
240 }
241 
SubscribeToEvents()242 void RibbonTrailDemo::SubscribeToEvents()
243 {
244     // Subscribe HandleUpdate() function for processing update events
245     SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(RibbonTrailDemo, HandleUpdate));
246 }
247 
HandleUpdate(StringHash eventType,VariantMap & eventData)248 void RibbonTrailDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
249 {
250     using namespace Update;
251 
252     // Take the frame time step, which is stored as a float
253     float timeStep = eventData[P_TIMESTEP].GetFloat();
254 
255     // Move the camera, scale movement with time step
256     MoveCamera(timeStep);
257 
258     // Sum of timesteps.
259     timeStepSum_ += timeStep;
260 
261     // Move first box with pattern.
262     boxNode1_->SetTransform(Vector3(-4.0f + 3.0f * Cos(100.0f * timeStepSum_), 0.5f, -2.0f * Cos(400.0f * timeStepSum_)),
263         Quaternion());
264 
265     // Move second box with pattern.
266     boxNode2_->SetTransform(Vector3(0.0f + 3.0f * Cos(100.0f * timeStepSum_), 0.5f, -2.0f * Cos(400.0f * timeStepSum_)),
267         Quaternion());
268 
269     // Get elapsed attack animation time.
270     float swordAnimTime = ninjaAnimCtrl_->GetAnimationState(String("Models/NinjaSnowWar/Ninja_Attack3.ani"))->GetTime();
271 
272     // Stop emitting trail when sword is finished slashing.
273     if (!swordTrail_->IsEmitting() && swordAnimTime > swordTrailStartTime_ && swordAnimTime < swordTrailEndTime_)
274         swordTrail_->SetEmitting(true);
275     else if (swordTrail_->IsEmitting() && swordAnimTime >= swordTrailEndTime_)
276         swordTrail_->SetEmitting(false);
277 }
278