/
outline.cs
385 lines (323 loc) · 16.2 KB
/
outline.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright (c) 2020-2021 Eli Aloni (a.k.a elix22)
// Copyright (c) 2008-2015 the Urho3D project.
// Copyright (c) 2015 Xamarin Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using UrhoNetSamples;
using Urho;
using Urho.Gui;
namespace Decals
{
public class Decals : Sample
{
bool drawDebug;
Camera camera;
Camera outelinecamera;
protected Scene outlineScene = null;
protected Node outlineCameraNode { get; set; }
protected Node outlineNode { get; set; }
[Preserve]
public Decals() : base(new ApplicationOptions(assetsFolder: "Data;CoreData")) { }
protected override void Start()
{
base.Start();
Graphics.WindowTitle = "Decals";
CreateScene();
CreateUI();
SetupViewport();
SubscribeToEvents();
}
protected override void Stop()
{
UnSubscribeFromEvents();
UI.Cursor.Visible = false;
base.Stop();
}
// Resize cursor based on Screen size
private void ResizeCursor(Cursor cursor)
{
int size = Graphics.Width / 60;
cursor.Size = new IntVector2(size, size);
}
void CreateUI()
{
var cache = ResourceCache;
var ui = UI;
var graphics = Graphics;
var style = cache.GetXmlFile("UI/DefaultStyle.xml");
var cursor = new Cursor();
cursor.SetStyleAuto(style);
ResizeCursor(cursor);
ui.Cursor = cursor;
cursor.SetPosition(graphics.Width / 2, graphics.Height / 2);
if (IsMobile)
{
SimpleCreateInstructionsWithWasd(
"Touch screen to move cursor\nPaint to paint decals at cursor position\n" +
"Debug to toggle debug geometry\nTouch screen to rotate view");
}
else
{
SimpleCreateInstructionsWithWasd(
"LMB to paint decals, RMB to rotate view\n" +
"Space to toggle debug geometry");
}
}
void SubscribeToEvents()
{
Engine.PostRenderUpdate += OnPostRenderUpdate;
}
void UnSubscribeFromEvents()
{
Engine.PostRenderUpdate -= OnPostRenderUpdate;
}
private void OnPostRenderUpdate(PostRenderUpdateEventArgs obj)
{
// If draw debug mode is enabled, draw viewport debug geometry, which will show eg. drawable bounding boxes and skeleton
// bones. Note that debug geometry has to be separately requested each frame. Disable depth test so that we can see the
// bones properly
if (drawDebug)
Renderer.DrawDebugGeometry(drawDebug);
}
protected override void OnUpdate(float timeStep)
{
var cache = ResourceCache;
outlineCameraNode.Position = CameraNode.Position;
outlineCameraNode.Rotation = CameraNode.Rotation;
outlineCameraNode.Scale = CameraNode.Scale;
if (outlineNode != null)
{
outlineNode.Remove();
}
var lifanode = scene.GetChild("MushroomMask");
{
outlineNode = outlineScene.CreateChild("MushroomMask111");
outlineNode.Position = lifanode.Position;
outlineNode.Rotation = lifanode.Rotation;
outlineNode.Scale = lifanode.Scale;
var mushroomObject = outlineNode.CreateComponent<StaticModel>();
mushroomObject.Model = cache.GetModel("Models/Mushroom.mdl");
mushroomObject.SetMaterial(cache.GetMaterial("Materials/White.xml"));
mushroomObject.CastShadows = true;
}
UI ui = UI;
var input = Input;
ui.Cursor.Visible = !input.GetMouseButtonDown(MouseButton.Right);
const float mouseSensitivity = .1f;
const float moveSpeed = 40f;
if (UI.FocusElement != null)
return;
if (!ui.Cursor.Visible)
{
var mouseMove = input.MouseMove;
Yaw += mouseSensitivity * mouseMove.X;
Pitch += mouseSensitivity * mouseMove.Y;
Pitch = MathHelper.Clamp(Pitch, -90, 90);
}
CameraNode.Rotation = new Quaternion(Pitch, Yaw, 0);
if (input.GetKeyDown(Key.W))
CameraNode.Translate(Vector3.UnitZ * moveSpeed * timeStep);
if (input.GetKeyDown(Key.S))
CameraNode.Translate(-Vector3.UnitZ * moveSpeed * timeStep);
if (input.GetKeyDown(Key.A))
CameraNode.Translate(-Vector3.UnitX * moveSpeed * timeStep);
if (input.GetKeyDown(Key.D))
CameraNode.Translate(Vector3.UnitX * moveSpeed * timeStep);
if (Input.GetKeyPress(Key.Space))
drawDebug = !drawDebug;
if (UI.Cursor.Visible && Input.GetMouseButtonPress(MouseButton.Left))
PaintDecal();
base.OnUpdate(timeStep);
}
void SetupViewport()
{
var cache = ResourceCache;
var g = Graphics;
var renderer = Renderer;
var viewport = new Viewport(Context, scene, CameraNode.GetComponent<Camera>());
renderer.SetViewport(0, viewport);
RenderPath effectRenderPath = viewport.RenderPath.Clone();
effectRenderPath.Append(cache.GetXmlFile("PostProcess/Outline.xml"));
effectRenderPath.Append(cache.GetXmlFile("PostProcess/FXAA3.xml"));
viewport.RenderPath = effectRenderPath;
int w = g.Width;
int h = g.Height;
var renderTexture = new Urho.Urho2D.Texture2D(Context);
renderTexture.SetSize(w, h, Graphics.RGBFormat, TextureUsage.Rendertarget);
renderTexture.FilterMode = TextureFilterMode.Nearest;
renderTexture.Name = "OutlineMask";
cache.AddManualResource(renderTexture);
var surface = renderTexture.RenderSurface;
surface.UpdateMode = RenderSurfaceUpdateMode.Updatealways;
var outlineViewport = new Viewport(Context, outlineScene, outlineCameraNode.GetComponent<Camera>());
surface.SetViewport(0, outlineViewport);
}
void CreateScene()
{
var cache = ResourceCache;
scene = new Scene();
// Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
// Also create a DebugRenderer component so that we can draw debug geometry
scene.CreateComponent<Octree>();
scene.CreateComponent<DebugRenderer>();
// Create scene node & StaticModel component for showing a static plane
var planeNode = scene.CreateChild("Plane");
planeNode.Scale = new Vector3(100.0f, 1.0f, 100.0f);
var planeObject = planeNode.CreateComponent<StaticModel>();
planeObject.Model = cache.GetModel("Models/Plane.mdl");
planeObject.SetMaterial(cache.GetMaterial("Materials/StoneTiled.xml"));
// Create a Zone component for ambient lighting & fog control
var zoneNode = scene.CreateChild("Zone");
var zone = zoneNode.CreateComponent<Zone>();
zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
zone.AmbientColor = new Color(0.15f, 0.15f, 0.15f);
zone.FogColor = new Color(0.5f, 0.5f, 0.7f);
zone.FogStart = 100.0f;
zone.FogEnd = 300.0f;
// Create a directional light to the world. Enable cascaded shadows on it
var lightNode = scene.CreateChild("DirectionalLight");
lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
var light = lightNode.CreateComponent<Light>();
// light.Color=new Color(0.6f,0.5f,0.2f);
light.LightType = LightType.Directional;
light.CastShadows = true;
light.ShadowBias = new BiasParameters(0.00025f, 0.5f);
// Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
light.ShadowCascade = new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);
// Create some mushrooms
const uint numMushrooms = 0;
for (uint i = 0; i < numMushrooms; ++i)
{
var mushroomNode = scene.CreateChild("Mushroom");
mushroomNode.Position = new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f);
mushroomNode.Rotation = new Quaternion(0.0f, NextRandom(360.0f), 0.0f);
mushroomNode.SetScale(0.5f + NextRandom(2.0f));
var mushroomObject = mushroomNode.CreateComponent<StaticModel>();
mushroomObject.Model = cache.GetModel("Models/Mushroom.mdl");
mushroomObject.SetMaterial(cache.GetMaterial("Materials/Mushroom.xml"));
mushroomObject.CastShadows = true;
}
{
var mushroomNode = scene.CreateChild("MushroomMask");
mushroomNode.Position = new Vector3(0, 0.0f, 20);
mushroomNode.Rotation = new Quaternion(0.0f, NextRandom(360.0f), 0.0f);
mushroomNode.SetScale(0.5f + NextRandom(2.0f));
var mushroomObject = mushroomNode.CreateComponent<StaticModel>();
mushroomObject.Model = cache.GetModel("Models/Mushroom.mdl");
mushroomObject.SetMaterial(cache.GetMaterial("Materials/Mushroom.xml"));
mushroomObject.CastShadows = true;
}
// Create randomly sized boxes. If boxes are big enough, make them occluders. Occluders will be software rasterized before
// rendering to a low-resolution depth-only buffer to test the objects in the view frustum for visibility
const uint numBoxes = 0;
for (uint i = 0; i < numBoxes; ++i)
{
var boxNode = scene.CreateChild("Box");
float size = 1.0f + NextRandom(10.0f);
boxNode.Position = new Vector3(NextRandom(80.0f) - 40.0f, size * 0.5f, NextRandom(80.0f) - 40.0f);
boxNode.SetScale(size);
var boxObject = boxNode.CreateComponent<StaticModel>();
boxObject.Model = cache.GetModel("Models/Box.mdl");
boxObject.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));
boxObject.CastShadows = true;
if (size >= 3.0f)
boxObject.Occluder = true;
}
// Create the camera. Limit far clip distance to match the fog
CameraNode = scene.CreateChild("Camera");
camera = CameraNode.CreateComponent<Camera>();
camera.FarClip = 300.0f;
// Set an initial position for the camera scene node above the plane
CameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f);
outlineScene = new Scene();
outlineScene.CreateComponent<Octree>();
//outlineCameraNode=CameraNode.Clone();
outlineCameraNode = outlineScene.CreateChild("Camera");
var outlinecamera = outlineCameraNode.CreateComponent<Camera>();
outlinecamera.FarClip = 300.0f;
// Set an initial position for the camera scene node above the plane
outlineCameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f);
}
bool Raycast(float maxDistance, out Vector3 hitPos, out Drawable hitDrawable)
{
hitDrawable = null;
hitPos = Vector3.Zero;
var graphics = Graphics;
var ui = UI;
IntVector2 pos = ui.CursorPosition;
// Check the cursor is visible and there is no UI element in front of the cursor
if (!ui.Cursor.Visible || ui.GetElementAt(pos, true) != null)
return false;
Ray cameraRay = camera.GetScreenRay((float)pos.X / graphics.Width, (float)pos.Y / graphics.Height);
var result = scene.GetComponent<Octree>().RaycastSingle(cameraRay, RayQueryLevel.Triangle, maxDistance, DrawableFlags.Geometry, uint.MaxValue);
if (result != null)
{
hitPos = result.Value.Position;
hitDrawable = result.Value.Drawable;
return true;
}
return false;
}
void PaintDecal()
{
Vector3 hitPos;
Drawable hitDrawable;
if (Raycast(250.0f, out hitPos, out hitDrawable))
{
var targetNode = hitDrawable.Node;
var decal = targetNode.GetComponent<DecalSet>();
if (decal == null)
{
var cache = ResourceCache;
decal = targetNode.CreateComponent<DecalSet>();
decal.Material = cache.GetMaterial("Materials/UrhoDecalAlpha.xml");
}
// Add a square decal to the decal set using the geometry of the drawable that was hit, orient it to face the camera,
// use full texture UV's (0,0) to (1,1). Note that if we create several decals to a large object (such as the ground
// plane) over a large area using just one DecalSet component, the decals will all be culled as one unit. If that is
// undesirable, it may be necessary to create more than one DecalSet based on the distance
decal.AddDecal(hitDrawable, hitPos, CameraNode.Rotation, 0.5f, 1.0f, 1.0f, Vector2.Zero,
Vector2.One, 0f, 0.1f, uint.MaxValue);
}
}
/// <summary>
/// Set custom Joystick layout for mobile platforms
/// </summary>
protected override string JoystickLayoutPatch =>
"<patch>" +
" <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" +
" <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Paint</replace>" +
" <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" +
" <element type=\"Text\">" +
" <attribute name=\"Name\" value=\"MouseButtonBinding\" />" +
" <attribute name=\"Text\" value=\"LEFT\" />" +
" </element>" +
" </add>" +
" <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" +
" <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Debug</replace>" +
" <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" +
" <element type=\"Text\">" +
" <attribute name=\"Name\" value=\"KeyBinding\" />" +
" <attribute name=\"Text\" value=\"SPACE\" />" +
" </element>" +
" </add>" +
"</patch>";
}
}