/
ProceduralMeshModifiers.cs
675 lines (605 loc) · 29.8 KB
/
ProceduralMeshModifiers.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
-----------------------------------------------------------------------------
This source file is part of mogre-procedural
For the latest info, see http://code.google.com/p/mogre-procedural/
my blog:http://hi.baidu.com/rainssoft
this is overwrite ogre-procedural c++ project using c#, look ogre-procedural c++ source http://code.google.com/p/ogre-procedural/
Copyright (c) 2013-2020 rains soft
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.
-----------------------------------------------------------------------------
*/
//#ifndef PROCEDURAL_MESH_MODIFIERS_INCLUDED
#define PROCEDURAL_MESH_MODIFIERS_INCLUDED
//wrapper with new std class...ok
namespace Mogre_Procedural
{
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using Math = Mogre.Math;
using TRect = Mogre.RealRect;
using Mogre_Procedural.std;
//*
// \brief Projects all TriangleBufferVertices on a sphere
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport SpherifyModifier
public class SpherifyModifier
{
private TriangleBuffer mInputTriangleBuffer;
private Vector3 mCenter = new Vector3();
private float mRadius = 0f;
public SpherifyModifier() {
mInputTriangleBuffer = null;
mCenter = Vector3.ZERO;
mRadius = 1f;
}
/// \exception Ogre::InvalidParametersException Input triangle buffer must not be null
public SpherifyModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
if (inputTriangleBuffer == null)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Input triangle buffer must not be null",
"Procedural::SpherifyModifier::setInputTriangleBuffer(Procedural::TriangleBuffer*)");
;
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
public SpherifyModifier setRadius(float radius) {
if (mRadius <= 0)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Radius must be positive",
"Procedural::SpherifyModifier::setInputTriangleBuffer(Procedural::TriangleBuffer*)");
;
mRadius = radius;
return this;
}
public SpherifyModifier setCenter(Vector3 center) {
//
//ORIGINAL LINE: mCenter = center;
mCenter = (center);
return this;
}
/// \exception Ogre::InvalidStateException Input triangle buffer must be set
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "Procedural::SpherifyModifier::modify()");
;
//for (List<TriangleBuffer.Vertex>.Enumerator it = mInputTriangleBuffer.getVertices().begin(); it != mInputTriangleBuffer.getVertices().end(); ++it)
foreach (var it in mInputTriangleBuffer.getVertices()) {
float l = (it.mPosition - mCenter).Length;
if (l > 1e-6) {
it.mNormal = (it.mPosition - mCenter) / l;
it.mPosition = mCenter + mRadius * it.mNormal;
}
}
}
}
//--------------------------------------------------------------
//*
//WIP
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport CalculateNormalsModifier
public class CalculateNormalsModifier
{
public CalculateNormalsModifier() {
mComputeMode = NormalComputeMode.NCM_VERTEX;
mInputTriangleBuffer = null;
mMustWeldUnweldFirst = true;
}
public enum NormalComputeMode : int
{
NCM_VERTEX,
NCM_TRIANGLE
}
public NormalComputeMode mComputeMode;
public TriangleBuffer mInputTriangleBuffer;
public bool mMustWeldUnweldFirst;
public CalculateNormalsModifier setComputeMode(NormalComputeMode computeMode) {
mComputeMode = computeMode;
return this;
}
public CalculateNormalsModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
// *
// * Tells if the mesh must be first weld (NCM_VERTEX mode) or unweld (NCM_TRIANGLE) before computing normals.
// * Has a performance impact if enabled.
// * Default : true.
//
public CalculateNormalsModifier setMustWeldUnweldFirst(bool mustWeldUnweldFirst) {
mMustWeldUnweldFirst = mustWeldUnweldFirst;
return this;
}
//--------------------------------------------------------------
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
;
if (mComputeMode == NormalComputeMode.NCM_TRIANGLE) {
if (mMustWeldUnweldFirst)
new UnweldVerticesModifier().setInputTriangleBuffer(mInputTriangleBuffer).modify();
std_vector<int> indices = mInputTriangleBuffer.getIndices();
std_vector<TriangleBuffer.Vertex> vertices = mInputTriangleBuffer.getVertices();
for (int i = 0; i < indices.size(); i += 3) {
Vector3 v1 = vertices[indices[i]].mPosition;
Vector3 v2 = vertices[indices[i + 1]].mPosition;
Vector3 v3 = vertices[indices[i + 2]].mPosition;
Vector3 n = (v2 - v1).CrossProduct(v3 - v1).NormalisedCopy;
//
//ORIGINAL LINE: vertices[indices[i]].mNormal = n;
vertices[indices[i]].mNormal = (n);
//
//ORIGINAL LINE: vertices[indices[i+1]].mNormal = n;
vertices[indices[i + 1]].mNormal = (n);
//
//ORIGINAL LINE: vertices[indices[i+2]].mNormal = n;
vertices[indices[i + 2]].mNormal = (n);
}
}
else {
if (mMustWeldUnweldFirst)
new WeldVerticesModifier().setInputTriangleBuffer(mInputTriangleBuffer).modify();
std_vector<int> indices = mInputTriangleBuffer.getIndices();
std_vector<TriangleBuffer.Vertex> vertices = mInputTriangleBuffer.getVertices();
std_vector<std_vector<Vector3>> tmpNormals = new std_vector<std_vector<Vector3>>();
tmpNormals.resize(vertices.size());
for (int i = 0; i < indices.size(); i += 3) {
Vector3 v1 = vertices[indices[i]].mPosition;
Vector3 v2 = vertices[indices[i + 1]].mPosition;
Vector3 v3 = vertices[indices[i + 2]].mPosition;
Vector3 n = (v2 - v1).CrossProduct(v3 - v1);
tmpNormals[indices[i]].push_back(n);
tmpNormals[indices[i + 1]].push_back(n);
tmpNormals[indices[i + 2]].push_back(n);
}
for (int i = 0; i < vertices.size(); i++) {
Vector3 n = (Vector3.ZERO);
for (int j = 0; j < tmpNormals[i].size(); j++)
n += tmpNormals[i][j];
vertices[i].mNormal = n.NormalisedCopy;
}
}
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
}
//--------------------------------------------------------------
//*
// * Welds together the vertices which are 'close enough' one to each other
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport WeldVerticesModifier
public class WeldVerticesModifier
{
public WeldVerticesModifier() {
mInputTriangleBuffer = null;
mTolerance = 1e-3f;
}
public TriangleBuffer mInputTriangleBuffer;
public float mTolerance = 0f;
/// The triangle buffer to modify
public WeldVerticesModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
/// The tolerance in position to consider that 2 vertices are the same (default = 1e-3)
public WeldVerticesModifier setTolerance(float tolerance) {
mTolerance = tolerance;
return this;
}
//--------------------------------------------------------------
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
;
//std.map<Vector3, int, Vector3Comparator> mapExistingVertices = new std.map<Vector3, int, Vector3Comparator>();
std_map<Vector3, int> mapExistingVertices = new std_map<Vector3, int>(new Vector3Comparator());
std_vector<TriangleBuffer.Vertex> vertices = mInputTriangleBuffer.getVertices();
std_vector<int> indices = mInputTriangleBuffer.getIndices();
int newSize = vertices.size();
// for (std::vector<TriangleBuffer::Vertex>::iterator it = vertices.begin(); it!= vertices.end(); ++it)
for (int i = 0; i < vertices.Count; i++) {
//size_t currentIndex = it - vertices.begin();
TriangleBuffer.Vertex it = vertices[i];
int currentIndex = i;
if (currentIndex >= newSize)
break;
//if (mapExistingVertices.find(it.mPosition) == mapExistingVertices.end())
// mapExistingVertices[it.mPosition] = currentIndex;
if (mapExistingVertices.find(it.mPosition) == -1) {
mapExistingVertices.insert(it.mPosition, currentIndex);
}
else {
int existingIndex = mapExistingVertices[it.mPosition];
--newSize;
if (currentIndex == newSize) {
//for (std::vector<int>::iterator it2 = indices.begin(); it2 != indices.end(); ++it2)
for (int j = 0; j < indices.Count; j++) {
int it2 = indices[j];
if (it2 == currentIndex) {
//*it2 = existingIndex;
indices[j] = existingIndex;
}
}
}
else {
int lastIndex = newSize;
//*it = vertices[lastIndex];
it = vertices[lastIndex];
//for (std::vector<int>::iterator it2 = indices.begin(); it2 != indices.end(); ++it2)
for (int j = 0; j < indices.Count; j++) {
int it2 = indices[j];
//if (*it2 == currentIndex)
if (it2 == currentIndex) {
//*it2 = existingIndex;
indices[j] = existingIndex;
}
//else if (*it2 == lastIndex)
else if (it2 == lastIndex) {
//*it2 = currentIndex;
indices[j] = currentIndex;
}
}
}
}
}
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
}
//--------------------------------------------------------------
//*
// * \brief Switches the triangle buffer from indexed triangles to (pseudo) triangle list
// * It can be used if you want discontinuities between all your triangles.
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport UnweldVerticesModifier
public class UnweldVerticesModifier
{
public UnweldVerticesModifier() {
mInputTriangleBuffer = null;
}
public TriangleBuffer mInputTriangleBuffer;
public UnweldVerticesModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
//--------------------------------------------------------------
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
;
std_vector<TriangleBuffer.Vertex> newVertices = new std_vector<TriangleBuffer.Vertex>();
std_vector<TriangleBuffer.Vertex> originVertices = mInputTriangleBuffer.getVertices();
std_vector<int> originIndices = mInputTriangleBuffer.getIndices();
for (int i = 0; i < originIndices.size(); i += 3) {
newVertices.push_back(originVertices[originIndices[i]]);
newVertices.push_back(originVertices[originIndices[i + 1]]);
newVertices.push_back(originVertices[originIndices[i + 2]]);
}
mInputTriangleBuffer.getVertices().clear();
mInputTriangleBuffer.getVertices().reserve(newVertices.size());
//for (List<TriangleBuffer.Vertex>.Enumerator it = newVertices.GetEnumerator(); it.MoveNext(); ++it)
// mInputTriangleBuffer.getVertices().push_back(it.Current);
foreach (var it in newVertices) {
mInputTriangleBuffer.getVertices().push_back(it);
}
mInputTriangleBuffer.getIndices().clear();
mInputTriangleBuffer.getIndices().reserve(newVertices.size());
for (int i = 0; i < newVertices.size(); i++)
mInputTriangleBuffer.getIndices().push_back(i);
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
}
//--------------------------------------------------------------
//*
// * \brief Recomputes the mesh's UVs based on its projection on a plane
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport PlaneUVModifier
public class PlaneUVModifier
{
private Vector3 mPlaneNormal = new Vector3();
private Vector3 mPlaneCenter = new Vector3();
private Vector2 mPlaneSize = new Vector2();
private TriangleBuffer mInputTriangleBuffer;
public PlaneUVModifier() {
mPlaneNormal = Vector3.UNIT_Y;
mPlaneCenter = Vector3.ZERO;
mPlaneSize = Vector2.UNIT_SCALE;
mInputTriangleBuffer = null;
}
public PlaneUVModifier setPlaneNormal(Vector3 planeNormal) {
//
//ORIGINAL LINE: mPlaneNormal = planeNormal;
mPlaneNormal = (planeNormal);
return this;
}
public PlaneUVModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
public PlaneUVModifier setPlaneCenter(Vector3 planeCenter) {
//
//ORIGINAL LINE: mPlaneCenter = planeCenter;
mPlaneCenter = (planeCenter);
return this;
}
public PlaneUVModifier setPlaneSize(Vector2 planeSize) {
//
//ORIGINAL LINE: mPlaneSize = planeSize;
mPlaneSize = (planeSize);
return this;
}
/// \exception Ogre::InvalidStateException Input triangle buffer must be set
//--------------------------------------------------------------
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
;
Vector3 xvec = mPlaneNormal.Perpendicular;
Vector3 yvec = mPlaneNormal.CrossProduct(xvec);
//for (List<TriangleBuffer.Vertex>.Enumerator it = mInputTriangleBuffer.getVertices().begin(); it != mInputTriangleBuffer.getVertices().end(); ++it)
foreach (var it in mInputTriangleBuffer.getVertices()) {
Vector3 v = it.mPosition - mPlaneCenter;
float it_mUV_x = v.DotProduct(xvec);
float it_mUV_y = v.DotProduct(yvec);
it.mUV = new Vector2(it_mUV_x, it_mUV_y);
}
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
}
//--------------------------------------------------------------
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport SphereUVModifier
public class SphereUVModifier
{
private TriangleBuffer mInputTriangleBuffer;
//--------------------------------------------------------------
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
;
//for (List<TriangleBuffer.Vertex>.Enumerator it = mInputTriangleBuffer.getVertices().begin(); it != mInputTriangleBuffer.getVertices().end(); ++it)
foreach (var it in mInputTriangleBuffer.getVertices()) {
Vector3 v = it.mPosition.NormalisedCopy;
Vector2 vxz = new Vector2(v.x, v.z);
it.mUV.x = Utils.angleTo(Vector2.UNIT_X, vxz).ValueRadians / Math.TWO_PI;
it.mUV.y = (Math.ATan(v.y / vxz.Length).ValueRadians + Math.HALF_PI) / Math.PI;
}
}
public SphereUVModifier() {
mInputTriangleBuffer = null;
}
public SphereUVModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
}
//--------------------------------------------------------------
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport HemisphereUVModifier
public class HemisphereUVModifier
{
private TriangleBuffer mInputTriangleBuffer;
//private TRect<float> mTextureRectangleTop = new TRect<float>();
//private TRect<float> mTextureRectangleBottom = new TRect<float>();
private Mogre.RealRect mTextureRectangleTop = new Mogre.RealRect();
private Mogre.RealRect mTextureRectangleBottom = new Mogre.RealRect();
//--------------------------------------------------------------
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
;
//for (List<TriangleBuffer.Vertex>.Enumerator it = mInputTriangleBuffer.getVertices().begin(); it != mInputTriangleBuffer.getVertices().end(); ++it)
foreach (var it in mInputTriangleBuffer.getVertices()) {
Vector3 input = it.mPosition.NormalisedCopy;
Vector3 v = new Vector3();
Radian r = new Radian();
if (input.y > 0)
Vector3.UNIT_Y.GetRotationTo(input).ToAngleAxis(out r, out v);
else
Vector3.NEGATIVE_UNIT_Y.GetRotationTo(input).ToAngleAxis(out r, out v);
Vector2 v2 = new Vector2(input.x, input.z);
v2.Normalise();
Vector2 uv = new Vector2(0.5f, 0.5f) + 0.5f * (r / Math.HALF_PI).ValueRadians * v2;
if (input.y > 0)
it.mUV = Utils.reframe(mTextureRectangleTop, uv);
else
it.mUV = Utils.reframe(mTextureRectangleBottom, uv);
}
}
public HemisphereUVModifier() {
mInputTriangleBuffer = null;
mTextureRectangleTop = new Mogre.RealRect(0, 0, 1, 1);
mTextureRectangleBottom = new Mogre.RealRect(0, 0, 1, 1);
}
public HemisphereUVModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
public HemisphereUVModifier setTextureRectangleTop(TRect textureRectangleTop) {
//
//ORIGINAL LINE: mTextureRectangleTop = textureRectangleTop;
mTextureRectangleTop = (textureRectangleTop);
return this;
}
public HemisphereUVModifier setTextureRectangleBottom(TRect textureRectangleBottom) {
//
//ORIGINAL LINE: mTextureRectangleBottom = textureRectangleBottom;
mTextureRectangleBottom = (textureRectangleBottom);
return this;
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
}
//--------------------------------------------------------------
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport CylinderUVModifier
public class CylinderUVModifier
{
private TriangleBuffer mInputTriangleBuffer;
private float mHeight = 0f;
private float mRadius = 0f;
//--------------------------------------------------------------
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
if (mHeight <= 0f)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Height must be strictly positive", "__FUNCTION__");
if (mRadius <= 0f)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Radius must be strictly positive", "__FUNCTION__");
;
float angleThreshold = Math.ATan(mHeight / mRadius).ValueRadians;
//for (List<TriangleBuffer.Vertex>.Enumerator it = mInputTriangleBuffer.getVertices().begin(); it != mInputTriangleBuffer.getVertices().end(); ++it)
foreach (var it in mInputTriangleBuffer.getVertices()) {
Vector2 nxz = new Vector2(it.mNormal.x, it.mNormal.z);
float alpha = (Math.ATan(it.mNormal.y / nxz.Length).ValueRadians + Math.HALF_PI);
if (Math.Abs(alpha) > angleThreshold) {
Vector2 vxz = new Vector2(it.mPosition.x, it.mPosition.z);
it.mUV = vxz / mRadius;
}
else {
Vector2 vxz = new Vector2(it.mPosition.x, it.mPosition.z);
it.mUV.x = Utils.angleTo(Vector2.UNIT_X, vxz).ValueRadians / Math.TWO_PI;
it.mUV.y = it.mPosition.y / mHeight - 0.5f;
}
}
}
public CylinderUVModifier() {
mInputTriangleBuffer = null;
mRadius = 1.0f;
mHeight = 1.0f;
}
public CylinderUVModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
public CylinderUVModifier setRadius(float radius) {
mRadius = radius;
return this;
}
public CylinderUVModifier setHeight(float height) {
mHeight = height;
return this;
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
}
//--------------------------------------------------------------
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport BoxUVModifier
public class BoxUVModifier
{
public enum MappingType : int
{
MT_FULL,
MT_CROSS,
MT_PACKED,
}
private TriangleBuffer mInputTriangleBuffer;
private MappingType mMappingType;
private Vector3 mBoxSize = new Vector3();
private Vector3 mBoxCenter = new Vector3();
//--------------------------------------------------------------
public void modify() {
if (mInputTriangleBuffer == null)
OGRE_EXCEPT("Exception::ERR_INVALID_STATE", "Input triangle buffer must be set", "__FUNCTION__");
;
Vector3[] directions = new Vector3[6] { Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z, Vector3.NEGATIVE_UNIT_X, Vector3.NEGATIVE_UNIT_Y, Vector3.NEGATIVE_UNIT_Z };
//for (List<TriangleBuffer.Vertex>.Enumerator it = mInputTriangleBuffer.getVertices().begin(); it != mInputTriangleBuffer.getVertices().end(); ++it)
foreach (var it in mInputTriangleBuffer.getVertices()) {
Vector3 v = it.mPosition - mBoxCenter;
if (v.IsZeroLength)
continue;
//v.normalise();
v.x /= mBoxSize.x;
v.y /= mBoxSize.y;
v.z /= mBoxSize.z;
//C++ TO C# CONVERTER WARNING: The following line was determined to be a copy constructor call - this should be verified and a copy constructor should be created if it does not yet exist:
//ORIGINAL LINE: Vector3 n = it->mNormal;
Vector3 n = (it.mNormal);
float maxAxis = 0;
int principalAxis = 0;
for (byte i = 0; i < 6; i++) {
if (directions[i].DotProduct(n) > maxAxis) {
maxAxis = directions[i].DotProduct(n);
principalAxis = i;
}
}
Vector3 vX = new Vector3();
Vector3 vY = new Vector3();
if (principalAxis % 3 == 1)
vY = Vector3.UNIT_X;
else
vY = Vector3.UNIT_Y;
vX = directions[principalAxis].CrossProduct(vY);
Vector2 uv = new Vector2(0.5f - vX.DotProduct(v), 0.5f - vY.DotProduct(v));
if (mMappingType == MappingType.MT_FULL)
it.mUV = uv;
else if (mMappingType == MappingType.MT_CROSS) {
}
else if (mMappingType == MappingType.MT_PACKED)
it.mUV = new Vector2((uv.x + principalAxis % 3) / 3, (uv.y + principalAxis / 3) / 2);
}
}
public BoxUVModifier() {
mInputTriangleBuffer = null;
mMappingType = MappingType.MT_FULL;
mBoxSize = Vector3.UNIT_SCALE;
mBoxCenter = Vector3.ZERO;
}
public BoxUVModifier setInputTriangleBuffer(TriangleBuffer inputTriangleBuffer) {
mInputTriangleBuffer = inputTriangleBuffer;
return this;
}
public BoxUVModifier setBoxSize(Vector3 boxSize) {
//
//ORIGINAL LINE: mBoxSize = boxSize;
mBoxSize = (boxSize);
return this;
}
public BoxUVModifier setBoxCenter(Vector3 boxCenter) {
//
//ORIGINAL LINE: mBoxCenter = boxCenter;
mBoxCenter = (boxCenter);
return this;
}
public BoxUVModifier setMappingType(MappingType mappingType) {
mMappingType = mappingType;
return this;
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
}
}