-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathExample01.razor
More file actions
326 lines (294 loc) · 9.91 KB
/
Example01.razor
File metadata and controls
326 lines (294 loc) · 9.91 KB
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
@page "/example01"
@using HomagGroup.Blazor3D.Extras.Core;
@implements IDisposable
<h3>Example01</h3>
<div>
This example shows how to:
<ul>
<li>Control the Blazor3D Viewer component's dimensions with CSS</li>
<li>Add custom ViewerSettings</li>
<li>Add user-defined scene</li>
<li>Add user-defined lights and meshes with different geometries</li>
<li>Add meshes to the group</li>
<li>Turn on/off objects selecting mode </li>
<li>Subscribe ObjectSelected event</li>
<li>Select object by its UUID</li>
<li>Control view helper visibility</li>
<li>Draw edges on the meshes</li>
</ul>
</div>
<div class="row justify-content-center">
<div class="col-6 v3d">
<Viewer @ref="View3D1" ViewerSettings=@settings Scene=scene />
</div>
<div class="col-5">
<input type="text" @bind="selectObjectGuid"></input> <button @onclick="OnSelectObjectByUUIDClick">Select object</button>
</div>
</div>
<div>@msg</div>
@code {
private Viewer View3D1 = null!;
private Guid objGuid;
private string msg = string.Empty;
private ViewerSettings settings = new ViewerSettings()
{
ContainerId = "example1",
CanSelect = true,// default is false
SelectedColor = "yellow",
ShowViewHelper = false,//default is true
WebGLRendererSettings = new WebGLRendererSettings
{
Antialias = false // if you need poor quality for some reasons
}
};
private Scene scene = new Scene();
private Guid? selectObjectGuid = null;
public void Dispose()
{
View3D1.ObjectSelected -= OnObjectSelected;
}
protected override Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
// subscribe events only once
View3D1.ObjectSelected += OnObjectSelected;
}
return base.OnAfterRenderAsync(firstRender);
}
protected override Task OnInitializedAsync()
{
scene.Add(new AmbientLight());
scene.Add(new PointLight()
{
Position = new Vector3(1, 3, 0)
});
AddMeshes();
return base.OnInitializedAsync();
}
private void AddMeshes()
{
scene.Add(new Mesh());
scene.Add(new Mesh
{
Geometry = new BoxGeometry(width: 1.2f, height: 0.5f),
Position = new Vector3(-2, 0, 0),
Material = new MeshStandardMaterial()
{
Color = "magenta"
},
// draw edges. Note, this breaks selections, because edges are highligted instead of mesh itself.
// also edges uuid is returned, not the mesh id
EdgesMaterial = new LineBasicMaterial
{
Color = "black"
}
});
scene.Add(new Mesh
{
Geometry = new CircleGeometry(radius: 0.75f, segments: 12),
Position = new Vector3(2, 0, 0),
Scale = new Vector3(1, 0.75f, 1),
Material = new MeshStandardMaterial()
{
Color = "pink"
}
});
scene.Add(new Mesh
{
Geometry = new CapsuleGeometry(radius: 0.5f, length: 2),
Position = new Vector3(-4, 0, 0),
Material = new MeshStandardMaterial()
{
Color = "darkgreen"
},
// draw edges. Note, this breaks selections, because edges are highligted instead of mesh itself.
// also edges uuid is returned, not the mesh id
EdgesMaterial = new LineBasicMaterial
{
Color = "black"
}
});
scene.Add(new Mesh
{
Geometry = new ConeGeometry(radius: 0.5f, height: 2, radialSegments: 16),
Position = new Vector3(4, 0, 0),
Material = new MeshStandardMaterial()
{
Color = "green",
FlatShading = true,
Metalness = 0.5f,
Roughness = 0.5f
}
});
scene.Add(new Mesh
{
Geometry = new CylinderGeometry(radiusTop: 0.5f, height: 1.2f, radialSegments: 16),
Position = new Vector3(0, 0, -2),
Material = new MeshStandardMaterial()
{
Color = "red",
Wireframe = true
}
});
scene.Add(new Mesh
{
Geometry = new DodecahedronGeometry(radius: 0.8f),
Position = new Vector3(-2, 0, -2),
Material = new MeshStandardMaterial()
{
Color = "darkviolet",
Metalness = 0.5f,
Roughness = 0.5f
}
});
scene.Add(new Mesh
{
Geometry = new IcosahedronGeometry(radius: 0.8f),
Position = new Vector3(-4, 0, -2),
Material = new MeshStandardMaterial()
{
Color = "violet"
},
// draw edges. Note, this breaks selections, because edges are highligted instead of mesh itself.
// also edges uuid is returned, not the mesh id
EdgesMaterial = new LineBasicMaterial
{
Color = "black"
}
});
scene.Add(new Mesh
{
Geometry = new OctahedronGeometry(radius: 0.75f),
Position = new Vector3(2, 0, -2),
Material = new MeshStandardMaterial()
{
Color = "aqua"
}
});
scene.Add(new Mesh
{
Geometry = new PlaneGeometry(width: 0.5f, height: 2),
Position = new Vector3(4, 0, -2),
Material = new MeshStandardMaterial()
{
Color = "purple"
}
});
scene.Add(new Mesh
{
Geometry = new RingGeometry(innerRadius: 0.6f, outerRadius: 0.7f),
Position = new Vector3(0, 0, -4),
Material = new MeshStandardMaterial()
{
Color = "DodgerBlue"
}
});
scene.Add(new Mesh
{
Geometry = new SphereGeometry(radius: 0.6f),
Position = new Vector3(-2, 0, -4),
Material = new MeshStandardMaterial()
{
Color = "darkgreen"
},
});
scene.Add(new Mesh
{
Geometry = new TetrahedronGeometry(radius: 0.75f),
Position = new Vector3(2, 0, -4),
Material = new MeshStandardMaterial()
{
Color = "lightblue"
}
});
scene.Add(new Mesh
{
Geometry = new TorusGeometry(radius: 0.6f, tube: 0.4f, radialSegments: 12, tubularSegments: 12),
Position = new Vector3(4, 0, -4),
Material = new MeshStandardMaterial()
{
Color = "lightgreen"
}
});
scene.Add(new Mesh
{
Geometry = new TorusKnotGeometry(radius: 0.6f, tube: 0.1f),
Position = new Vector3(-4, 0, -4),
Material = new MeshStandardMaterial()
{
Color = "RosyBrown"
}
});
// Mimic ThreeJS approach to add shape verticies.
// For now only MoveTo and LineTo available.
var group = new Group()
{
Position = new Vector3 { Z = 2 },
};
var shape = new Shape();
shape.MoveTo(0, 0.25);
shape.LineTo(0.5, 0.5);
shape.LineTo(0.75, 0);
shape.LineTo(0, -0.75);
shape.LineTo(-0.75, 0);
shape.LineTo(-0.5, 0.5);
group.Add(new Mesh
{
Geometry = new ShapeGeometry(shape),
Position = new Vector3() { X = -1, Y = -1 },
Material = new MeshStandardMaterial()
{
Color = "DarkRed"
}
});
// Creating shape based on points collection.
var points = new Vector2[]
{
new Vector2(0, 0.25),
new Vector2(0.5, 0.5),
new Vector2(0.75, 0),
new Vector2(0, -0.75),
new Vector2(-0.75, 0),
new Vector2(-0.5, 0.5),
};
shape = new Shape();
shape.Points.AddRange(points);
var options = new ExtrudeGeometryOptions
{
Depth = 0.25,
BevelEnabled = false
};
group.Add(new Mesh
{
Geometry = new ExtrudeGeometry(shape, options),
Position = new Vector3() { X = 1, Y = -1 },
Material = new MeshStandardMaterial()
{
Color = "Red"
}
});
scene.Add(group);
}
private void OnObjectSelected(Object3DArgs e)
{
foreach (var item in scene.Children)
{
if (item.Uuid == e.UUID)
{
this.msg = $"Selected object with id = {e.UUID} and type {item.Type}";
StateHasChanged();
return;
}
}
this.msg = string.Empty;
StateHasChanged();
}
private async Task OnSelectObjectByUUIDClick()
{
if (selectObjectGuid.HasValue)
{
await View3D1.SelectByUuidAsync(selectObjectGuid.Value);
}
}
}