forked from Ryogo-X/d3dlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadedVisualObject.cs
296 lines (264 loc) · 12.4 KB
/
LoadedVisualObject.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
using D3DLab.ECS;
using D3DLab.ECS.Components;
using D3DLab.FileFormats.GeoFormats;
using D3DLab.SDX.Engine.Components;
using D3DLab.Toolkit;
using D3DLab.Toolkit.Components;
using D3DLab.Toolkit.D3Objects;
using D3DLab.Toolkit.Math3D;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
namespace D3DLab.Viewer.D3D {
struct LoadedObjectDetails {
public int VertexCount { get; set; }
public int TriangleCount { get; set; }
}
enum WorldAxisTypes {
X,Y,Z,All
}
class LoadedVisualObject : MultiVisualObject {
static readonly Vector4 color;
static readonly Random random = new Random();
VisualPolylineObject? bounds;
VisualPolylineObject? worldX;
VisualPolylineObject? worldY;
VisualPolylineObject? worldZ;
public LoadedObjectDetails Details { get; private set; }
public SharpDX.Direct3D11.CullMode CullMode { get; private set; }
static LoadedVisualObject() {
var c = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#B3B598");
color = c.ToVector4();
}
public static LoadedVisualObject Create(IContextState context, IEnumerable<IFileGeometry3D> meshes,
FileInfo texture, string name) {
var visual = new LoadedVisualObject(name);
visual.CullMode = SharpDX.Direct3D11.CullMode.Front;
_Create(context, meshes, texture,name, visual);
return visual;
}
static void _Create(IContextState context, IEnumerable<IFileGeometry3D> meshes,
FileInfo texture, string name, LoadedVisualObject visual) {
List<ElementTag> t = new List<ElementTag>();
var details = new LoadedObjectDetails();
var baseTag = ElementTag.New();
var index = 0;
AxisAlignedBox fullBox = AxisAlignedBox.Zero;
foreach (var geo in meshes) {
var cc = geo.Name.Split(',').Select(x => float.Parse(x.Trim(new[] { ' ', '<', '>' }), System.Globalization.NumberStyles.Float)).ToArray();
var color = new Vector4(cc[0]/256f,cc[1] / 256f, cc[2] / 256f, 1);
var tag = Create(context, baseTag.WithPrefix(geo.Name ?? index.ToString()),
new GeometryStructures<IFileGeometry3D>(geo), texture, out var box, color);
t.Add(tag);
fullBox = fullBox.Merge(box.Bounds);
details.VertexCount += geo.Positions.Count;
details.TriangleCount += (geo.Indices.Count / 3);
index++;
}
visual.tags.AddRange(t);
visual.Details = details;
var size = fullBox.Size();
visual.worldX = VisualPolylineObject.Create(context, baseTag.WithPrefix("WorldX"),
new[] { Vector3.Zero + Vector3.UnitX * size.X * -2f, Vector3.Zero + Vector3.UnitX * size.X * 2f }, V4Colors.Red, false);
visual.worldX.IsVisible = false;
visual.worldY = VisualPolylineObject.Create(context, baseTag.WithPrefix("WorldY"),
new[] { Vector3.Zero + Vector3.UnitY * size.Y * -2f, Vector3.Zero + Vector3.UnitY * size.Y * 2f }, V4Colors.Green, false);
visual.worldY.IsVisible = false;
visual.worldZ = VisualPolylineObject.Create(context, baseTag.WithPrefix("WorldZ"),
new[] { Vector3.Zero + Vector3.UnitZ * size.Z * -2f, Vector3.Zero + Vector3.UnitZ * size.Z * 2f }, V4Colors.Blue, false);
visual.worldZ.IsVisible = false;
}
static ElementTag Create(IContextState context, ElementTag tag, GeometryStructures gdata, FileInfo texture,
out GeometryBoundsComponent boundsComponent, Vector4 color) {
var manager = context.GetEntityManager();
if (!gdata.Normals.Any()) {
gdata.ReCalculateNormals();
}
gdata.BuildTreeAsync();
var cullmode = SharpDX.Direct3D11.CullMode.Front;
var geo = context.GetGeometryPool().AddGeometry(gdata);
var en = manager.CreateEntity(tag);
MaterialColorComponent material;
RenderableComponent renderable;
if (gdata.TexCoor.Any() && texture != null) {
material = MaterialColorComponent.CreateTransparent().ApplyAlpha(1);
renderable = RenderableComponent.AsTriangleTexturedList(cullmode);
en.AddComponent(new D3DTexturedMaterialSamplerComponent(texture));
} else {
material = MaterialColorComponent.Create(color);// V4Colors.NextColor(random));
renderable = RenderableComponent.AsTriangleColoredList(cullmode);
}
boundsComponent = GeometryBoundsComponent.Create(gdata.Positions);
en.AddComponent(TransformComponent.Identity())
.AddComponent(HittableComponent.Create(0))
.AddComponent(boundsComponent)
.AddComponent(material)
.AddComponent(geo)
.AddComponent(renderable);
return tag;
}
public void ReCreate(IContextState context, IEnumerable<IFileGeometry3D> meshes, FileInfo material, string name) {
this.Cleanup(context);
_Create(context, meshes, material, name, this);
}
LoadedVisualObject(string filename) : base(filename) {
}
//public override void Hide(IEntityManager manager) {
// foreach (var tag in Tags) {
// var en = manager.GetEntity(tag);
// var rend = en.GetComponent<RenderableComponent>();
// en.UpdateComponent(rend.Disable());
// }
//}
//public override void Show(IEntityManager manager) {
// foreach (var tag in Tags) {
// var en = manager.GetEntity(tag);
// var rend = en.GetComponent<RenderableComponent>();
// en.UpdateComponent(rend.Enable());
// }
//}
public override void Cleanup(IContextState context) {
var manager = context.GetEntityManager();
foreach (var tag in Tags) {
manager.RemoveEntity(tag);
}
bounds?.Cleanup(context);
worldX?.Cleanup(context);
worldY?.Cleanup(context);
worldZ?.Cleanup(context);
base.Cleanup(context);
}
public GeometryStructures<IFileGeometry3D> GetMesh(IContextState context, in ElementTag tag) {
var id = context.GetComponentManager().GetComponent<GeometryPoolComponent>(tag);
return (GeometryStructures<IFileGeometry3D>)context.GetGeometryPool().GetGeometry<GeometryStructures>(id);
}
public T GetComponent<T>(IEntityManager manager, in ElementTag tag) where T : IGraphicComponent {
return manager.GetEntity(tag).GetComponent<T>();
}
public void Transform(IEntityManager manager, in Matrix4x4 move) {
foreach (var t in Tags) {
manager.GetEntity(t).UpdateComponent(MovingComponent.Create(move));
}
worldX?.GetEntity(manager).UpdateComponent(MovingComponent.Create(move));
worldY?.GetEntity(manager).UpdateComponent(MovingComponent.Create(move));
worldZ?.GetEntity(manager).UpdateComponent(MovingComponent.Create(move));
bounds?.GetEntity(manager).UpdateComponent(MovingComponent.Create(move));
}
public void ShowBoundingBox(IContextState context, out AxisAlignedBox fullBox) {
if (bounds != null) throw new Exception("Bounds has already showed.");
fullBox = GetAllBounds(context);
bounds = VisualPolylineObject.CreateBox(context, ElementTag.New("Bounds_"), fullBox, V4Colors.White);
}
public AxisAlignedBox GetAllBounds(IContextState context) {
var fullBox = new AxisAlignedBox();
var manager = context.GetEntityManager();
foreach (var t in Tags) {
var en = manager.GetEntity(t);
var renderable = en.GetComponent<RenderableComponent>();
if (renderable.IsRenderable) {
var box = en.GetComponent<GeometryBoundsComponent>();
var tr = en.GetComponent<TransformComponent>();
fullBox = fullBox.Merge(box.Bounds.Transform(tr.MatrixWorld));
}
}
return fullBox;
}
public void HideBoundingBox(IContextState context) {
bounds?.Cleanup(context);
bounds = null;
}
public void ShowWorldAxis(IContextState context, WorldAxisTypes axis) {
var manager = context.GetEntityManager();
switch (axis) {
case WorldAxisTypes.X when !worldX.IsVisible:
worldX.Show(context);
break;
case WorldAxisTypes.Y when !worldY.IsVisible:
worldY.Show(context);
break;
case WorldAxisTypes.Z when !worldZ.IsVisible:
worldZ.Show(context);
break;
}
}
public void HideWorldAxis(IContextState context, WorldAxisTypes axis) {
var manager = context.GetEntityManager();
switch (axis) {
case WorldAxisTypes.All when worldX.IsVisible:
worldX.Hide(context);
worldY.Hide(context);
worldZ.Hide(context);
break;
case WorldAxisTypes.X when worldX.IsVisible:
worldX.Hide(context);
break;
case WorldAxisTypes.Y when worldY.IsVisible:
worldY.Hide(context);
break;
case WorldAxisTypes.Z when worldZ.IsVisible:
worldZ.Hide(context);
break;
}
}
public void TurnFlatshadingOff(IContextState context) {
var manager = context.GetComponentManager();
foreach (var t in Tags) {
manager.AddComponent(t, FlatShadingGeometryComponent.Create());
}
}
public void TurnFlatshadingOn(IContextState context) {
var manager = context.GetComponentManager();
foreach (var t in Tags) {
manager.RemoveComponent<FlatShadingGeometryComponent>(t);
}
}
public void TurnSolidWireframeOff(IContextState context) {
var manager = context.GetComponentManager();
foreach (var t in Tags) {
manager.RemoveComponent<WireframeGeometryComponent>(t);
}
}
public void TurnSolidWireframeOn(IContextState context) {
var manager = context.GetComponentManager();
foreach (var t in Tags) {
manager.AddComponent(t, WireframeGeometryComponent.Create());
}
}
public void TurnTransparentWireframeOff(IContextState context) {
var manager = context.GetEntityManager();
foreach (var t in Tags) {
var en = manager.GetEntity(t);
en.UpdateComponent(en
.GetComponent<RenderableComponent>()
.SwitchFillModeTo(SharpDX.Direct3D11.FillMode.Solid));
}
}
public void TurnTransparentWireframeOn(IContextState context) {
var manager = context.GetEntityManager();
foreach (var t in Tags) {
var en = manager.GetEntity(t);
en.UpdateComponent(en
.GetComponent<RenderableComponent>()
.SwitchFillModeTo(SharpDX.Direct3D11.FillMode.Wireframe));
}
}
public void ChangeCullMode(IContextState context, SharpDX.Direct3D11.CullMode mode) {
var manager = context.GetEntityManager();
foreach (var t in Tags) {
var en = manager.GetEntity(t);
var renderable = en.GetComponent<RenderableComponent>();
if (renderable.IsRenderable) {
en.UpdateComponent(en
.GetComponent<RenderableComponent>()
.SwitchCullModeTo(mode));
}
}
this.CullMode = mode;
}
}
}