public Shape3D GetRingGear() { System.Collections.Generic.List<Triangle3> tris = new List<Triangle3> (); float OuterWidth = 0.0f; float ToothBaseWidth = 0.0f; Shape3D ret; _origin.Tag = VERT_origin; ToothBaseWidth = (float)(_innerRadius * 2 * Mathf.PI / (((float)(_qtyTeeth)) * (_outerWidthPct + 1))); OuterWidth = ToothBaseWidth * _outerWidthPct; //Calc the angle to sweep... float inctheta = 2 * Mathf.PI / ((float)_qtyTeeth * (_outerWidthPct + 1)); float theta = 0.0f; float ToothRotAngle = _toothRot * Mathf.PI / 360.0f; float phi = (inctheta *_outerRadius - OuterWidth) / (2 *_outerRadius); for (int i = 0; i < _qtyTeeth * 2; i++) { TVector3 a; TVector3 b; TVector3 c; TVector3 ta; TVector3 tb; TVector3 tc; float adjAngle = 0.0f; float angleBuffera = 0; float angleBufferc = 0; Triangle3 t = new Triangle3(); if (i % 2 == 0) { adjAngle = inctheta; angleBuffera = inctheta / 2.0f; //LargePoints angleBufferc = inctheta + _outerWidthPct * inctheta / 2.0f; } else { adjAngle = _outerWidthPct * inctheta; angleBuffera = _outerWidthPct * inctheta / 2.0f; // Small points angleBufferc = inctheta / 2.0f + inctheta * _outerWidthPct; } b = new TVector3 (_innerRadius * Mathf.Cos (theta) +_origin.Value.x, _innerRadius * Mathf.Sin (theta) +_origin.Value.y,_origin.Value.z, VERT_OuterRingBorderTop); c = new TVector3 (_innerRadius * Mathf.Cos (theta + adjAngle) +_origin.Value.x, _innerRadius * Mathf.Sin (theta + adjAngle) +_origin.Value.y,_origin.Value.z, VERT_OuterRingBorderTop); a =_origin.Copy(); if (_ringRadius != 0) { if (_outerWidthPct != 0) { a = new TVector3(_ringRadius * Mathf.Cos(theta + angleBuffera) + a.Value.x, _ringRadius * Mathf.Sin(theta + angleBuffera) + a.Value.y, a.Value.z, VERT_InnerRingBorderTop); } else { a = new TVector3(_ringRadius * Mathf.Cos(theta + angleBuffera) + a.Value.x, _ringRadius * Mathf.Sin(theta + angleBuffera) + a.Value.y, a.Value.z, VERT_InnerRingBorderTop); } } t.a = a; t.b = b; t.c = c; t.SortOrder = _triOrder; _triOrder += 2; if (i % 2 == 0) { t.Tag = TRI_GearPlaneTopA1; }else{ t.Tag = TRI_GearPlaneTopA2; } tris.Add(t); t = new Triangle3(); if (_ringRadius != 0) { if (_outerWidthPct != 0) { Triangle3 tFill = new Triangle3(); tFill.a = a.Copy(); tFill.b = c.Copy(); tFill.c = new TVector3(_ringRadius * Mathf.Cos(theta + angleBufferc) + _origin.Value.x, _ringRadius * Mathf.Sin(theta + angleBufferc) + _origin.Value.y, _origin.Value.z, VERT_InnerRingBorderTop); tFill.Tag = TRI_GearPlaneTopB; tFill.SortOrder = _triOrder; _triOrder += 2; tris.Add(tFill); }else{ Triangle3 tFill = new Triangle3(); tFill.a = a.Copy(); tFill.b = c.Copy(); tFill.c = new TVector3(_ringRadius * Mathf.Cos(theta + angleBufferc) + _origin.Value.x, _ringRadius * Mathf.Sin(theta + angleBufferc) + _origin.Value.y, _origin.Value.z, VERT_InnerRingBorderTop); tFill.Tag = TRI_GearPlaneTopC; tFill.SortOrder = _triOrder; _triOrder += 2; tris.Add(tFill); } } if (_outerWidthPct == 0 && i % 2 != 0) { continue; //If no 'squared' teeth, then bail quickly. } if (i % 2 == 0) { Triangle3 t1 = new Triangle3(); Triangle3 t2 = new Triangle3(); ta = b.Copy(); //First corner of tooth. tb = new TVector3 (_outerRadius * Mathf.Cos (theta + phi + ToothRotAngle) +_origin.Value.x,_outerRadius * Mathf.Sin (theta + phi + ToothRotAngle) +_origin.Value.y,_origin.Value.z, VERT_ToothBorderTop); tc = c.Copy(); t1.a = ta; t1.b = tb; t1.c = tc; t1.Tag = TRI_GearPlaneTopD; t1.SortOrder = _triOrder; _triOrder += 2; tris.Add (t1); tb = new TVector3 (_outerRadius * Mathf.Cos (theta + inctheta - phi + ToothRotAngle) +_origin.Value.x,_outerRadius * Mathf.Sin (theta + inctheta - phi + ToothRotAngle) +_origin.Value.y,_origin.Value.z, VERT_ToothBorderTop); //Second Corner of Outer Tooth. ta = new TVector3 (_outerRadius * Mathf.Cos (theta + phi + ToothRotAngle) +_origin.Value.x,_outerRadius * Mathf.Sin (theta + phi + ToothRotAngle) +_origin.Value.y,_origin.Value.z, VERT_ToothBorderTop); //First corner of outer tooth. tc = c.Copy(); t2.a = ta; t2.b = tb; t2.c = tc; t2.Tag = TRI_GearPlaneTopE; t2.SortOrder = _triOrder; _triOrder += 2; tris.Add (t2); } theta += adjAngle; //If next to last tooth then set theta = 0 (for full circle...); if (i == (_qtyTeeth * 2 - 1)) { theta = 0; } } var opptris = BuildOppositePlane(tris.ToArray(), new string[] { TRI_GearPlaneTopA1 }, TRI_GearPlaneBottomA1, _topScale, _topRotation); tris.AddRange(opptris); opptris = BuildOppositePlane(tris.ToArray(), new string[] { TRI_GearPlaneTopA2 }, TRI_GearPlaneBottomA2, _topScale, _topRotation); tris.AddRange(opptris); opptris = BuildOppositePlane(tris.ToArray(), new string[] { TRI_GearPlaneTopE }, TRI_GearPlaneBottomE, _topScale, _topRotation); tris.AddRange(opptris); opptris = BuildOppositePlane(tris.ToArray(), new string[] { TRI_GearPlaneTopD }, TRI_GearPlaneBottomD, _topScale, _topRotation); tris.AddRange(opptris); opptris = BuildOppositePlane(tris.ToArray(), new string[] { TRI_GearPlaneTopB }, TRI_GearPlaneBottomB, _topScale, _topRotation); tris.AddRange(opptris); opptris = BuildOppositePlane(tris.ToArray(), new string[] { TRI_GearPlaneTopC }, TRI_GearPlaneBottomC, _topScale, _topRotation); tris.AddRange(opptris); tris.AddRange(GetSpokes()); //Stitch Teeth System.Collections.Generic.List<Triangle3> stitching = new List<Triangle3>(); System.Collections.Generic.SortedList<int, Triangle3> sortedTris = new SortedList<int, Triangle3>(); foreach (Triangle3 item in tris) { if(sortedTris.ContainsKey(item.SortOrder)){ Debug.Log("Item exists: " + item.SortOrder.ToString() + " " + item.Tag); } sortedTris.Add(item.SortOrder, item); } foreach (Triangle3 item in sortedTris.Values) { stitching.AddRange(StitchEdge(item, TRI_GearPlaneTopA2, TRI_GearPlaneBottomA2, "b", "c", "a", "b", TRI_SideOuterTop, TRI_SideOuterBottom)); stitching.AddRange(StitchEdge(item, TRI_GearPlaneTopD, TRI_GearPlaneBottomD, "a", "b", "b", "c", TRI_SideOuterTop, TRI_SideOuterBottom)); stitching.AddRange(StitchEdge(item, TRI_GearPlaneTopE, TRI_GearPlaneBottomE, "a", "b", "b", "c", TRI_SideOuterTop, TRI_SideOuterBottom)); stitching.AddRange(StitchEdge(item, TRI_GearPlaneTopE, TRI_GearPlaneBottomE, "b", "c", "a", "b", TRI_SideOuterTop, TRI_SideOuterBottom)); //Stitch Inner Ring. stitching.AddRange(StitchEdge(item, TRI_GearPlaneTopB, TRI_GearPlaneBottomB, "c", "a", "c", "a", TRI_SideInnerTop, TRI_SideInnerBottom)); stitching.AddRange(StitchEdge(item, TRI_GearPlaneTopC, TRI_GearPlaneBottomC, "c", "a", "c", "a", TRI_SideInnerTop, TRI_SideInnerBottom)); //Stitch Straight Spokes stitching.AddRange(StitchEdge(item, TRI_SpokeTop1, TRI_SpokeBottom1, "a", "b", "b", "c", TRI_SpokeSideTop, TRI_SpokeSideBottom)); stitching.AddRange(StitchEdge(item, TRI_SpokeTop2, TRI_SpokeBottom2, "b", "c", "a", "b", TRI_SpokeSideTop, TRI_SpokeSideBottom)); //Stitch Hub stitching.AddRange(StitchEdge(item, TRI_HubTop, TRI_HubBottom, "b", "c", "a", "b", TRI_HubSideTop, TRI_HubSideBottom)); } tris.AddRange(stitching.ToArray()); ret.UV = BuildUVs(tris); ret.tris = tris.ToArray(); return ret; }
Triangle3[] GetStraightSpokes (string TagTop1,string TagTop2, string TagBottom1, string TagBottom2) { TVector3 origin1 = new TVector3(0,0, ((-_spokeIndent/2)), GearRenderer.VERT_origin); TVector3 origin2 = new TVector3(0, 0, ((-_height + _spokeIndent / 2)), GearRenderer.VERT_origin); System.Collections.Generic.List<Triangle3> tris = new System.Collections.Generic.List<Triangle3>(); Triangle3[] p1tris = GetStraightSpokePlane(origin1.Copy(), (+_spokeIndent / 2), TagTop1, TagTop2); Triangle3[] p2tris = GetStraightSpokePlane(origin2.Copy(), (-_spokeIndent / 2), TagBottom1, TagBottom2); MirrorVertTags(p2tris); Triangle3 trinew; for (int i = 0; i < p1tris.Length; i++) //Works since both arrays are same len. { var tri1 = p1tris[i].Copy(); var tri2 = p2tris[i].Copy(); trinew = new Triangle3(); trinew.a = tri1.a; trinew.b = tri1.b; trinew.c = tri1.c; trinew.Tag = tri1.Tag; trinew.SortOrder = _triOrder; _triOrder += 2; tris.Add(trinew); trinew = new Triangle3(); trinew.a = tri2.c; trinew.b = tri2.b; trinew.c = tri2.a; trinew.Tag = tri2.Tag; trinew.SortOrder = _triOrder + 1; trinew.Complement = tri1; tris.Add(trinew); } if(_hubRadius != 0){ Triangle3[] p3tris = GetHubPlane(origin1.Copy(), (+_hubHeight / 2.0f), TRI_HubTop); Triangle3[] p4tris = GetHubPlane(origin2.Copy(), (-_hubHeight / 2.0f), TRI_HubBottom); MirrorVertTags(p4tris); for (int i = 0; i < p3tris.Length; i++) //Works since both arrays are same len. { var tri1 = p3tris[i].Copy(); var tri2 = p4tris[i].Copy(); trinew = new Triangle3(); trinew.a = tri1.a; trinew.b = tri1.b; trinew.c = tri1.c; trinew.Tag = tri1.Tag; trinew.SortOrder = _triOrder; _triOrder += 2; tris.Add(trinew); trinew = new Triangle3(); trinew.a = tri2.c; trinew.b = tri2.b; trinew.c = tri2.a; trinew.Tag = tri2.Tag; trinew.Complement = tri1; trinew.SortOrder = _triOrder + 1; tris.Add(trinew); } } return tris.ToArray(); }
TVector3 RotatePoint(float Rotation, TVector3 pt){ TVector3 ret; float Rot = Mathf.Deg2Rad * Rotation; if (Rotation == 0 || pt.Value == Vector3.zero){ ret = pt.Copy(); }else{ ret = new TVector3(pt.Value.x * Mathf.Cos(Rot) - pt.Value.y * Mathf.Sin(Rot), pt.Value.x * Mathf.Sin(Rot) + pt.Value.y * Mathf.Cos(Rot), pt.Value.z, pt.Tag); } return ret; }
public Triangle3[] GetSolidSpokePlane (TVector3 origin, string Tag, float scale) { System.Collections.Generic.List<Triangle3> tris = new List<Triangle3> (); //Calc the angle to sweep... float inctheta = 2 * Mathf.PI / ((float)_qtyTeeth * 2.0f); float theta = 0.0f; _origin.Tag = VERT_origin; float width = _ringRadius * 1.05f; for (int i = 0; i < (_qtyTeeth * 2); i++) { TVector3 a; TVector3 b; TVector3 c; float adjAngle = 0.0f; Triangle3 t = new Triangle3(); adjAngle = inctheta; a =origin.Copy(); b = new TVector3(width * Mathf.Cos(theta) + origin.Value.x, width * Mathf.Sin(theta) + origin.Value.y, a.Value.z, VERT_SpokePerimeterTop); b = new TVector3(b.Value.x * scale, b.Value.y * scale, b.Value.z, b.Tag); c = new TVector3(width * Mathf.Cos(theta + adjAngle) + origin.Value.x, width * Mathf.Sin(theta + adjAngle) + origin.Value.y, a.Value.z, VERT_SpokePerimeterTop); c = new TVector3(c.Value.x * scale, c.Value.y * scale, c.Value.z, c.Tag); t.a = a; t.b = b; t.c = c; t.Tag = Tag; t.SortOrder = _triOrder; _triOrder += 2; tris.Add (t); theta += adjAngle; //If next to last tooth then set theta = 0 (for full circle...); if (i == (_qtyTeeth * 2 - 1)) { theta = 0; } } return tris.ToArray (); }