// Update is called once per frame void OnDrawGizmos() { if (ShowDebugInfo) { foreach (var tri in Tris) { var InnerList = new string[] { GearRenderer.TRI_SideInnerTop, GearRenderer.TRI_SideInnerBottom }; if (GearRenderer.TagContains(tri.Tag, InnerList)) { //Calc Normal of tri... Vector3 d = tri.Center(); Vector3 n = tri.GetNormal(); float norma = Mathf.Atan2(n.y, n.x); float xdira = norma + Mathf.PI / 2.0f; Vector3 xdir = new Vector3(n.magnitude * Mathf.Cos(xdira), n.magnitude * Mathf.Sin(xdira), n.z); Gizmos.color = Color.red; Gizmos.DrawRay(d, xdir * 1.0f); Gizmos.color = Color.yellow; Gizmos.DrawRay(d, n * 1.0f); } var OuterList = new string[] { GearRenderer.TRI_SideOuterTop, GearRenderer.TRI_SideOuterBottom }; if (GearRenderer.TagContains(tri.Tag, OuterList)) { //Calc Normal of tri... Vector3 d = tri.Center(); Vector3 n = tri.GetNormal(); //Vector3 xdir = ??? //x = (p-o) dot x //y=(p-o) dot (n cross x) float norma = Mathf.Atan2(n.y, n.x); float xdira = norma + Mathf.PI / 2.0f; Vector3 xdir = new Vector3(n.magnitude * Mathf.Cos(xdira), n.magnitude * Mathf.Sin(xdira), n.z); Gizmos.color = Color.red; Gizmos.DrawRay(d, xdir * 1.0f); Gizmos.color = Color.yellow; Gizmos.DrawRay(d, n * 1.0f); } } } }
/* //For debugging verts & tris. private GameObject CreateTextMesh(GameObject parent, string Text, Vector3 pos){ if(Text == null){ Text = "null"; } GameObject gt = new GameObject(Text); TextMesh tm = (TextMesh)gt.AddComponent( typeof(TextMesh) ); gt.AddComponent( typeof(MeshRenderer) ); //gt.renderer.material = ; tm.text = Text; tm.fontSize = 0; tm.characterSize = 0.05f; tm.font = DebugTextFont; MeshRenderer mr = gt.GetComponent<MeshRenderer>(); mr.material = DebugTextMaterial; gt.transform.parent = parent.transform; pos = new Vector3(pos.x, pos.y, Random.Range(0,1.0f) + pos.z); gt.transform.position = pos; tm.anchor = TextAnchor.MiddleCenter; //tm.font = guiFont; return gt; } */ public void ProcessGear(GameObject Gear, Vector3 origin){ System.Collections.Generic.List<Vector3> verts = new System.Collections.Generic.List<Vector3>(); System.Collections.Generic.List<int> triInt = new System.Collections.Generic.List<int>(); System.Collections.Generic.List<Vector2> uvs = new System.Collections.Generic.List<Vector2>(); GearRenderer g = new GearRenderer(); g.Height = Height; g.HubSides = HubSides; g.HubHeight = Height; g.HubRadius = HubRadius; g.InnerRadius = InnerToothRadius; g.OuterRadius = OutsideToothRadius; g.RingRadius = RingRadius; g.OuterWidthPct = ToothWidthPct / 100.0f; g.ToothRotation = ToothRotation; g.TopScale = TopScalePct / 100.0f; g.TopRotation = TopRotation; g.SpokeCount = SpokeCount; g.QtyTeeth = QtyTeeth; g.SpokeWidthInner = SpokeWidthInner; g.SpokeWidthOuter = SpokeWidthOuter; g.SpokeType = SpokeType; g.SpokeIndent = SpokeIndent; g.HubHeight = hubHeight; var shape = g.GetRingGear(); var tris = shape.tris; var tuvs = shape.UV; Mesh m = new Mesh(); int i = 0; foreach(var tri in tris){ //CreateTextMesh(o, tri.Tag + " " + tri.SortOrder.ToString(), tri.Center()); //CreateTextMesh(o, tri.Tag + "-a:" + tri.a.Tag, tri.a.Value); //CreateTextMesh(o, tri.Tag + "-b:" + tri.b.Tag, tri.b.Value); //CreateTextMesh(o, tri.Tag + "-c:" + tri.c.Tag, tri.c.Value); /* if (!vertcache.ContainsKey(tri.a.Value)){ vertcache.Add(tri.a.Value, i); verts.Add(tri.a.Value); i += 1; } triInt.Add(vertcache[tri.a.Value]); if (!vertcache.ContainsKey(tri.b.Value)){ vertcache.Add(tri.b.Value, i); verts.Add(tri.b.Value); i += 1; } triInt.Add(vertcache[tri.b.Value]); if (!vertcache.ContainsKey(tri.c.Value)){ vertcache.Add(tri.c.Value,i); verts.Add(tri.c.Value); i += 1; } triInt.Add(vertcache[tri.c.Value]); */ //i += 1; //Raw tris... verts.Add(tri.a.Value); triInt.Add(i); i += 1; verts.Add(tri.b.Value); triInt.Add(i); i += 1; verts.Add(tri.c.Value); triInt.Add(i); i += 1; } foreach(var uv in tuvs){ uvs.Add(uv.Value); } MeshFilter f = Gear.GetComponent<MeshFilter>(); if(f == null){ f = Gear.AddComponent<MeshFilter>(); } MeshRenderer r = Gear.GetComponent<MeshRenderer>(); if(r == null){ r = Gear.AddComponent<MeshRenderer>(); } //Debug Stuff //GearDebugHelper h = o.AddComponent<GearDebugHelper>(); /* if(Gear.GetComponent<GearTuner>() == null){ var tuner = Gear.AddComponent<GearTuner>(); tuner.RenderedGear = g; } */ //h.Tris = tris; //h.ShowDebugInfo = true; f.mesh = m; m.vertices = verts.ToArray(); m.triangles = triInt.ToArray(); m.uv = uvs.ToArray(); m.RecalculateNormals(); m.Optimize(); r.material = this.GearMaterial; }
public void CreateGear(GameObject Gear){ GearRenderer g = new GearRenderer(); Vector3 origin; var sv = SceneView.currentDrawingSceneView; if(sv == null && SceneView.lastActiveSceneView != null){ sv = SceneView.lastActiveSceneView; } if(sv == null){ Debug.LogError("No Scene present!"); return; } Camera cam = SceneView.lastActiveSceneView.camera; Ray ray = cam.ScreenPointToRay(new Vector3(cam.pixelWidth / 2.0f, cam.pixelHeight / 2.0f,0)); origin = ray.GetPoint(5.0f); g.Height = Height; g.HubSides = HubSides; g.HubHeight = Height; g.HubRadius = HubRadius; g.InnerRadius = InnerToothRadius; g.OuterRadius = OutsideToothRadius; g.RingRadius = RingRadius; g.OuterWidthPct = ToothWidthPct / 100.0f; g.ToothRotation = ToothRotation; g.TopScale = TopScalePct / 100.0f; g.TopRotation = TopRotation; g.SpokeCount = SpokeCount; g.QtyTeeth = QtyTeeth; g.SpokeWidthInner = SpokeWidthInner; g.SpokeWidthOuter = SpokeWidthOuter; g.SpokeType = SpokeType; g.SpokeIndent = SpokeIndent; g.HubHeight = hubHeight; GearSupport gs = Gear.GetComponent<GearSupport>(); if(gs == null){ gs = Gear.AddComponent<GearSupport>(); } gs.GearMaterial = GearMaterial; gs.RenderedGear = g; gs.OutsideToothRadius = this.OutsideToothRadius; gs.InnerToothRadius = this.InnerToothRadius; gs.ToothRotation = this.ToothRotation; gs.QtyTeeth = this.QtyTeeth; gs.ToothWidthPct = this.ToothWidthPct; gs.RingRadius = this.RingRadius; gs.GearMaterial = this.GearMaterial; gs.Height = this.Height; gs.TopRotation = this.TopRotation; gs.TopScalePct = this.TopScalePct; gs.SpokeCount = this.SpokeCount; gs.SpokeIndent = this.SpokeIndent; gs.SpokeType = this.SpokeType; gs.hubHeight = this.hubHeight; gs.HubRadius = this.HubRadius; gs.HubSides = this.HubSides; gs.SpokeWidthInner = this.SpokeWidthInner; gs.SpokeWidthOuter = this.SpokeWidthOuter; gs.LockRatio = this.LockRatio; //gs.DebugTextMaterial = this.DebugTextMaterial; //gs.DebugTextFont = this.DebugTextFont; gs.ProcessGear(Gear, origin); Gear.transform.position = origin; Debug.Log("Gear Rendered " + origin.ToString()); }
/* * //For debugging verts & tris. * private GameObject CreateTextMesh(GameObject parent, string Text, Vector3 pos){ * * if(Text == null){ * Text = "null"; * } * * GameObject gt = new GameObject(Text); * TextMesh tm = (TextMesh)gt.AddComponent( typeof(TextMesh) ); * gt.AddComponent( typeof(MeshRenderer) ); * //gt.renderer.material = ; * tm.text = Text; * tm.fontSize = 0; * tm.characterSize = 0.05f; * tm.font = DebugTextFont; * MeshRenderer mr = gt.GetComponent<MeshRenderer>(); * mr.material = DebugTextMaterial; * gt.transform.parent = parent.transform; * pos = new Vector3(pos.x, pos.y, Random.Range(0,1.0f) + pos.z); * gt.transform.position = pos; * tm.anchor = TextAnchor.MiddleCenter; * //tm.font = guiFont; * * * return gt; * } */ public void ProcessGear(GameObject Gear, Vector3 origin) { System.Collections.Generic.List <Vector3> verts = new System.Collections.Generic.List <Vector3>(); System.Collections.Generic.List <int> triInt = new System.Collections.Generic.List <int>(); System.Collections.Generic.List <Vector2> uvs = new System.Collections.Generic.List <Vector2>(); GearRenderer g = new GearRenderer(); g.Height = Height; g.HubSides = HubSides; g.HubHeight = Height; g.HubRadius = HubRadius; g.InnerRadius = InnerToothRadius; g.OuterRadius = OutsideToothRadius; g.RingRadius = RingRadius; g.OuterWidthPct = ToothWidthPct / 100.0f; g.ToothRotation = ToothRotation; g.TopScale = TopScalePct / 100.0f; g.TopRotation = TopRotation; g.SpokeCount = SpokeCount; g.QtyTeeth = QtyTeeth; g.SpokeWidthInner = SpokeWidthInner; g.SpokeWidthOuter = SpokeWidthOuter; g.SpokeType = SpokeType; g.SpokeIndent = SpokeIndent; g.HubHeight = hubHeight; var shape = g.GetRingGear(); var tris = shape.tris; var tuvs = shape.UV; Mesh m = new Mesh(); int i = 0; foreach (var tri in tris) { //CreateTextMesh(o, tri.Tag + " " + tri.SortOrder.ToString(), tri.Center()); //CreateTextMesh(o, tri.Tag + "-a:" + tri.a.Tag, tri.a.Value); //CreateTextMesh(o, tri.Tag + "-b:" + tri.b.Tag, tri.b.Value); //CreateTextMesh(o, tri.Tag + "-c:" + tri.c.Tag, tri.c.Value); /* * if (!vertcache.ContainsKey(tri.a.Value)){ * vertcache.Add(tri.a.Value, i); * verts.Add(tri.a.Value); * i += 1; * } * triInt.Add(vertcache[tri.a.Value]); * * if (!vertcache.ContainsKey(tri.b.Value)){ * vertcache.Add(tri.b.Value, i); * verts.Add(tri.b.Value); * i += 1; * } * triInt.Add(vertcache[tri.b.Value]); * * if (!vertcache.ContainsKey(tri.c.Value)){ * vertcache.Add(tri.c.Value,i); * verts.Add(tri.c.Value); * i += 1; * } * triInt.Add(vertcache[tri.c.Value]); */ //i += 1; //Raw tris... verts.Add(tri.a.Value); triInt.Add(i); i += 1; verts.Add(tri.b.Value); triInt.Add(i); i += 1; verts.Add(tri.c.Value); triInt.Add(i); i += 1; } foreach (var uv in tuvs) { uvs.Add(uv.Value); } MeshFilter f = Gear.GetComponent <MeshFilter>(); if (f == null) { f = Gear.AddComponent <MeshFilter>(); } MeshRenderer r = Gear.GetComponent <MeshRenderer>(); if (r == null) { r = Gear.AddComponent <MeshRenderer>(); } //Debug Stuff //GearDebugHelper h = o.AddComponent<GearDebugHelper>(); /* * if(Gear.GetComponent<GearTuner>() == null){ * var tuner = Gear.AddComponent<GearTuner>(); * * tuner.RenderedGear = g; * * * * } */ //h.Tris = tris; //h.ShowDebugInfo = true; f.mesh = m; m.vertices = verts.ToArray(); m.triangles = triInt.ToArray(); m.uv = uvs.ToArray(); m.RecalculateNormals(); m.Optimize(); r.material = this.GearMaterial; }
public void CreateGear(GameObject Gear) { GearRenderer g = new GearRenderer(); Vector3 origin; var sv = SceneView.currentDrawingSceneView; if (sv == null && SceneView.lastActiveSceneView != null) { sv = SceneView.lastActiveSceneView; } if (sv == null) { Debug.LogError("No Scene present!"); return; } Camera cam = SceneView.lastActiveSceneView.camera; Ray ray = cam.ScreenPointToRay(new Vector3(cam.pixelWidth / 2.0f, cam.pixelHeight / 2.0f, 0)); origin = ray.GetPoint(5.0f); g.Height = Height; g.HubSides = HubSides; g.HubHeight = Height; g.HubRadius = HubRadius; g.InnerRadius = InnerToothRadius; g.OuterRadius = OutsideToothRadius; g.RingRadius = RingRadius; g.OuterWidthPct = ToothWidthPct / 100.0f; g.ToothRotation = ToothRotation; g.TopScale = TopScalePct / 100.0f; g.TopRotation = TopRotation; g.SpokeCount = SpokeCount; g.QtyTeeth = QtyTeeth; g.SpokeWidthInner = SpokeWidthInner; g.SpokeWidthOuter = SpokeWidthOuter; g.SpokeType = SpokeType; g.SpokeIndent = SpokeIndent; g.HubHeight = hubHeight; GearSupport gs = Gear.GetComponent <GearSupport>(); if (gs == null) { gs = Gear.AddComponent <GearSupport>(); } gs.GearMaterial = GearMaterial; gs.RenderedGear = g; gs.OutsideToothRadius = this.OutsideToothRadius; gs.InnerToothRadius = this.InnerToothRadius; gs.ToothRotation = this.ToothRotation; gs.QtyTeeth = this.QtyTeeth; gs.ToothWidthPct = this.ToothWidthPct; gs.RingRadius = this.RingRadius; gs.GearMaterial = this.GearMaterial; gs.Height = this.Height; gs.TopRotation = this.TopRotation; gs.TopScalePct = this.TopScalePct; gs.SpokeCount = this.SpokeCount; gs.SpokeIndent = this.SpokeIndent; gs.SpokeType = this.SpokeType; gs.hubHeight = this.hubHeight; gs.HubRadius = this.HubRadius; gs.HubSides = this.HubSides; gs.SpokeWidthInner = this.SpokeWidthInner; gs.SpokeWidthOuter = this.SpokeWidthOuter; gs.LockRatio = this.LockRatio; //gs.DebugTextMaterial = this.DebugTextMaterial; //gs.DebugTextFont = this.DebugTextFont; gs.ProcessGear(Gear, origin); Gear.transform.position = origin; Debug.Log("Gear Rendered " + origin.ToString()); }