コード例 #1
0
        private void GenerateHeightMarkerTrack(TrackSegment4 trackSegment)
        {
            Transform transform = this.transform.Find ("HeightMarkerPlane");
            GameObject heightMarkerGo;
            MeshFilter meshFilter;
            if (transform == null) {
                heightMarkerGo = new GameObject ("HeightMarkerPlane");
                meshFilter = heightMarkerGo.AddComponent<MeshFilter> ();
                MeshRenderer meshRenderer = heightMarkerGo.AddComponent<MeshRenderer> ();
                meshRenderer.sharedMaterial = Main.AssetBundleManager.MaterialPlane;
                heightMarkerGo.transform.SetParent (this.transform);
            } else {
                heightMarkerGo = transform.gameObject;
                meshFilter = transform.gameObject.GetComponent<MeshFilter> ();
                meshFilter.mesh.Clear ();

            }


            heightMarkerGo.transform.localPosition = Vector3.zero;
            heightMarkerGo.transform.localRotation = Quaternion.identity;

            List<Vector3> verticies = new List<Vector3> ();
            List<int> triangles = new List<int> ();
            List<Vector2> uvs = new List<Vector2> ();

            float sample = trackSegment.getLength(0) / (float)Mathf.RoundToInt(trackSegment.getLength(0)/ .2f);
            float pos = 0.0f;
            int index = 0;



            float tForDistance = trackSegment.getTForDistance (0,0);
            Vector3 position = trackSegment.getPoint (tForDistance,0);

            LandPatch terrain = GameController.Instance.park.getTerrain(base.transform.position);
            Vector3 vector = position;
            if (terrain != null)
            {
                vector = terrain.getPoint(base.transform.position);
            }
            float magnitude = (position - vector).magnitude;



            verticies.Add (this.transform.InverseTransformPoint(position));
            verticies.Add (this.transform.InverseTransformPoint(position + Vector3.down*magnitude* Mathf.Sign(position.y - vector.y)));

            uvs.Add (new Vector2 (0, magnitude));
            uvs.Add (new Vector2 (0, 0));

            Vector3 previous = position;
            float xoffset = 0;
            while (pos < trackSegment.getLength (0)) {
                tForDistance = trackSegment.getTForDistance (pos,0);


                index++;
                pos += sample;

                position = trackSegment.getPoint (tForDistance,0);

                terrain = GameController.Instance.park.getTerrain(position);
                vector = position;
                if (terrain != null)
                {
                    vector = terrain.getPoint(position);
                }
                magnitude = (position - vector).magnitude;



                verticies.Add (this.transform.InverseTransformPoint(position));
                verticies.Add (this.transform.InverseTransformPoint(position + Vector3.down*magnitude  * Mathf.Sign(position.y - vector.y)));

                xoffset+= Vector3.Distance (previous, position);
                uvs.Add (new Vector2 (xoffset,vector.y + magnitude ));
                uvs.Add (new Vector2 (xoffset,vector.y - 0));


                int last = verticies.Count - 1;
                triangles.Add (last - 3);
                triangles.Add (last - 2);
                triangles.Add (last - 1);

                triangles.Add (last - 1);
                triangles.Add (last - 2);
                triangles.Add (last);

                previous = position;


            }
            meshFilter.mesh.vertices = verticies.ToArray ();
            meshFilter.mesh.triangles = triangles.ToArray ();;
            meshFilter.mesh.uv = uvs.ToArray ();

        }
コード例 #2
0
		private void recalculate(MeshGenerator meshGenerator, TrackSegment4 segment)
		{

			foreach(Transform child in segment.gameObject.transform) {
				if (child.name != "BetweenTracksMouseCollider" && !child.name.Contains("StationPlatformTrackTile") && child.name != "MouseSelectionCollider") {
					var mesh_filter = child.gameObject.GetComponent<MeshFilter> ();
					if (mesh_filter != null) {
						UnityEngine.Object.Destroy (mesh_filter.mesh);
						UnityEngine.Object.Destroy (mesh_filter.sharedMesh);
					}
					UnityEngine.Object.Destroy (child.gameObject);

				}
			}


			if (segment.getLength() <= 0f)
			{
				Debug.LogWarning("Can't extrude this segment! Has a length of 0.");
			}
			meshGenerator.prepare(segment, segment.gameObject);
			float num = 0f;
			float num2 = 0f;
			meshGenerator.sampleAt(segment, 0f);
			int num3 = 0;
			int num4 = 0;
			Vector3 b = segment.getStartpoint();
			do
			{
				float num5 = 1f - num2;
				if (Vector3.Angle(segment.getDirection(), segment.getPoint(num2 + num5) - segment.getPoint(num2)) > 5f)
				{
					num5 /= 2f;
				}
				int num6 = 0;
				Vector3 point = segment.getPoint(num2 + num5);
				float num7 = Vector3.Angle(segment.getTangentPoint(num2), segment.getTangentPoint(num2 + num5));
				num7 = Mathf.Max(num7, Vector3.Angle(segment.getNormal(num2), segment.getNormal(num2 + num5)));
				while (num5 > 0.01f && (num7 > 10f || (num7 > 2f && (point - b).magnitude > 0.225f)))
				{
					num4++;
					num5 /= 2f;
					point = segment.getPoint(num2 + num5);
					num7 = Vector3.Angle(segment.getTangentPoint(num2), segment.getTangentPoint(num2 + num5));
					num7 = Mathf.Max(num7, Vector3.Angle(segment.getNormal(num2), segment.getNormal(num2 + num5)));
					num6++;
					if (num6 > 50)
					{
						break;
					}
				}
				num += (point - b).magnitude;
				num2 += num5;
				b = point;
				if (num2 > 1f)
				{
					break;
				}
				meshGenerator.sampleAt(segment, num2);
				num3++;
			}
			while (num2 < 1f && num3 < 300);
			if (!Mathf.Approximately(num2, 1f))
			{
				meshGenerator.sampleAt(segment, 1f);
			}

			meshGenerator.afterExtrusion(segment, segment.gameObject);
			MeshFilter component = segment.gameObject.GetComponent<MeshFilter>();
			Mesh mesh = meshGenerator.getMesh(segment.gameObject);
			UnityEngine.Object.Destroy (component.sharedMesh);
			UnityEngine.Object.Destroy (component.mesh);

			component.sharedMesh = mesh;
			meshGenerator.afterMeshGeneration(segment, segment.gameObject);

			Extruder buildVolumeMeshExtruder = meshGenerator.getBuildVolumeMeshExtruder();
			buildVolumeMeshExtruder.transform(segment.gameObject.transform.worldToLocalMatrix);
			BoundingMesh boundingMesh = segment.gameObject.GetComponent<BoundingMesh>();
			boundingMesh.layers = BoundingVolume.Layers.Buildvolume;
			boundingMesh.setMesh(buildVolumeMeshExtruder.vertices.ToArray(), buildVolumeMeshExtruder.indizes.ToArray());

			GameObject track_mouse_collider = segment.transform.Find ("BetweenTracksMouseCollider").gameObject;// new GameObject("BetweenTracksMouseCollider");
			track_mouse_collider.transform.parent = segment.gameObject.transform;
			track_mouse_collider.transform.localPosition = Vector3.zero;
			track_mouse_collider.transform.localRotation = Quaternion.identity;
			track_mouse_collider.layer = LayerMasks.ID_MOUSECOLLIDERS;
			MeshCollider meshCollider = track_mouse_collider.GetComponent<MeshCollider>();
			Mesh collisionMesh = meshGenerator.getCollisionMesh(segment.gameObject);

			UnityEngine.Object.Destroy (meshCollider.sharedMesh);
			meshCollider.sharedMesh = collisionMesh;

			MouseCollider mouseCollider = segment.gameObject.GetComponent<MouseCollider>();
			mouseCollider.colliderObject = track_mouse_collider;

		}