UnityEngine.Vector3.Project(UnityEngine.Vector3, UnityEngine.Vector3)

Here are the examples of the csharp api UnityEngine.Vector3.Project(UnityEngine.Vector3, UnityEngine.Vector3) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

12 Examples 7

19 View Source File : Bullet.cs
License : GNU General Public License v3.0
Project Creator : aelariane

public void Launch(Vector3 v, Vector3 v2, string launcher_ref, bool isLeft, HERO hero, bool leviMode = false)
    {
        if (this.phase == 2)
        {
            return;
        }
        this.master = hero;
        this.velocity = v;
        float f = Mathf.Acos(Vector3.Dot(v.normalized, v2.normalized)) * 57.29578f;
        if (Mathf.Abs(f) > 90f)
        {
            this.velocity2 = Vectors.zero;
        }
        else
        {
            this.velocity2 = Vector3.Project(v2, v);
        }
        switch (launcher_ref)
        {
            case "hookRefL1":
                this.myRef = hero.hookRefL1;
                break;

            case "hookRefL2":
                this.myRef = hero.hookRefL2;
                break;

            case "hookRefR1":
                this.myRef = hero.hookRefR1;
                break;

            case "hookRefR2":
                this.myRef = hero.hookRefR2;
                break;
        }
        myRefT = myRef.transform;
        this.nodes = new List<Vector3>();
        this.nodes.Add(myRefT.position);
        this.phase = 0;
        this.leviMode = leviMode;
        this.left = isLeft;
        if (IN_GAME_MAIN_CAMERA.GameType != GameType.Single && BasePV.IsMine)
        {
            BasePV.RPC("myMasterIs", PhotonTargets.Others, new object[] { hero.BasePV.viewID, launcher_ref });
            BasePV.RPC("setVelocityAndLeft", PhotonTargets.Others, new object[] { v, this.velocity2, this.left });
        }
        baseT.position = myRefT.position;
        baseT.rotation = Quaternion.LookRotation(v.normalized);
    }

19 View Source File : Vector3.cs
License : MIT License
Project Creator : bbepis

[Obsolete( "Use Vector3.ProjectOnPlane instead." )]
      public static Vector3 Exclude( Vector3 excludeThis, Vector3 fromThat )
      {
         return fromThat - Project( fromThat, excludeThis );
      }

19 View Source File : Vector3.cs
License : MIT License
Project Creator : bbepis

public static Vector3 ProjectOnPlane( Vector3 vector, Vector3 planeNormal )
      {
         return vector - Project( vector, planeNormal );
      }

19 View Source File : ControllerButtonHints.cs
License : MIT License
Project Creator : dag10

private void ComputeTextEndTransforms()
		{
			//This is done as a separate step after all the ButtonHintInfos have been initialized
			//to make the text hints fan out appropriately based on the button's position on the controller.

			centerPosition /= buttonHintInfos.Count;
			float maxDistanceFromCenter = 0.0f;

			foreach ( var hintInfo in buttonHintInfos )
			{
				hintInfo.Value.distanceFromCenter = Vector3.Distance( hintInfo.Value.textStartAnchor.position, centerPosition );

				if ( hintInfo.Value.distanceFromCenter > maxDistanceFromCenter )
				{
					maxDistanceFromCenter = hintInfo.Value.distanceFromCenter;
				}
			}

			foreach ( var hintInfo in buttonHintInfos )
			{
				Vector3 centerToButton = hintInfo.Value.textStartAnchor.position - centerPosition;
				centerToButton.Normalize();

				centerToButton = Vector3.Project( centerToButton, renderModel.transform.forward );

				//Spread out the text end positions based on the distance from the center
				float t = hintInfo.Value.distanceFromCenter / maxDistanceFromCenter;
				float scale = hintInfo.Value.distanceFromCenter * Mathf.Pow( 2, 10 * ( t - 1.0f ) ) * 20.0f;

				//Flip the direction of the end pos based on which hand this is
				float endPosOffset = 0.1f;

				Vector3 hintEndPos = hintInfo.Value.textStartAnchor.position + ( hintInfo.Value.textEndOffsetDir * endPosOffset ) + ( centerToButton * scale * 0.1f );
				hintInfo.Value.textEndAnchor.position = hintEndPos;

				hintInfo.Value.canvasOffset.position = hintEndPos;
				hintInfo.Value.canvasOffset.localRotation = Quaternion.idenreplacedy;
			}
		}

19 View Source File : GvrPointerPhysicsRaycaster.cs
License : MIT License
Project Creator : harshitjuneja

public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList) {
    if (eventCamera == null) {
      return;
    }

    if (!IsPointerAvailable()) {
      return;
    }

    Ray ray = GetRay();
    float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
    float radius = PointerRadius;
    RaycastHit[] hits;

    if (radius > 0.0f) {
      hits = Physics.SphereCastAll(ray, radius, dist, finalEventMask);
    } else {
      hits = Physics.RaycastAll(ray, dist, finalEventMask);
    }

    if (hits.Length == 0) {
      return;
    }

    System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));

    for (int b = 0, bmax = hits.Length; b < bmax; ++b) {
      Vector3 projection = Vector3.Project(hits[b].point - ray.origin, ray.direction);
      Vector3 hitPosition = projection + ray.origin;

      RaycastResult result = new RaycastResult {
        gameObject = hits[b].collider.gameObject,
        module = this,
        distance = hits[b].distance,
        worldPosition = hitPosition,
        worldNormal = hits[b].normal,
        screenPosition = eventCamera.WorldToScreenPoint(hitPosition),
        index = resultAppendList.Count,
        sortingLayer = 0,
        sortingOrder = 0
      };

      resultAppendList.Add(result);
    }
  }

19 View Source File : MalbersTools.cs
License : MIT License
Project Creator : Interactml

public static float AngleAroundAxis(Vector3 dirA, Vector3 dirB, Vector3 axis)
        {
            // Project A and B onto the plane orthogonal target axis
            dirA = dirA - Vector3.Project(dirA, axis);
            dirB = dirB - Vector3.Project(dirB, axis);

            // Find (positive) angle between A and B
            float angle = Vector3.Angle(dirA, dirB);

            // Return angle multiplied with 1 or -1
            return angle * (Vector3.Dot(axis, Vector3.Cross(dirA, dirB)) < 0 ? -1 : 1);
        }

19 View Source File : PolyMeshBuilder.cs
License : MIT License
Project Creator : IxxyXR

public static Mesh BuildMeshFromConwayPoly(
		ConwayPoly conway,
		bool generateSubmeshes = false,
		Color[] colors = null,
		PolyHydraEnums.ColorMethods colorMethod = PolyHydraEnums.ColorMethods.ByRole,
		PolyHydraEnums.UVMethods uvMethod = PolyHydraEnums.UVMethods.FirstEdge,
		bool largeMeshFormat = true
    )
    {

		Vector2 calcUV(Vector3 point, Vector3 xAxis, Vector3 yAxis)
		{
			float u, v;
			u = Vector3.Project(point, xAxis).magnitude;
			u *= Vector3.Dot(point, xAxis) > 0 ? 1 : -1;
			v = Vector3.Project(point, yAxis).magnitude;
			v *= Vector3.Dot(point, yAxis) > 0  ? 1 : -1;
			return new Vector2(u, v);
		}

		if (colors == null) colors = DefaultFaceColors;
		var target = new Mesh();
		if (largeMeshFormat)
		{
			target.indexFormat = IndexFormat.UInt32;
		}
		var meshTriangles = new List<int>();
		var meshVertices = new List<Vector3>();
		var meshNormals = new List<Vector3>();
		var meshColors = new List<Color32>();
		var meshUVs = new List<Vector2>();
		var edgeUVs = new List<Vector2>();
		var barycentricUVs = new List<Vector3>();
		var miscUVs1 = new List<Vector4>();
		var miscUVs2 = new List<Vector4>();

		List<ConwayPoly.Roles> uniqueRoles = null;
		List<int> uniqueSides = null;
		List<string> uniqueTags = null;

		var submeshTriangles = new List<List<int>>();

		// TODO
		// var hasNaked = conway.HasNaked();

		// Strip down to Face-Vertex structure
		var points = conway.ListVerticesByPoints();
		var faceIndices = conway.ListFacesByVertexIndices();

		// Add faces
		int index = 0;

		if (generateSubmeshes)
		{
			switch (colorMethod)
			{
				case PolyHydraEnums.ColorMethods.ByRole:
					uniqueRoles = new HashSet<ConwayPoly.Roles>(conway.FaceRoles).ToList();
					for (int i = 0; i < uniqueRoles.Count; i++) submeshTriangles.Add(new List<int>());
					break;
				case PolyHydraEnums.ColorMethods.BySides:
					for (int i = 0; i < colors.Length; i++) submeshTriangles.Add(new List<int>());
					break;
				case PolyHydraEnums.ColorMethods.ByFaceDirection:
					for (int i = 0; i < colors.Length; i++) submeshTriangles.Add(new List<int>());
					break;
				case PolyHydraEnums.ColorMethods.ByTags:
					var flattenedTags = conway.FaceTags.SelectMany(d => d.Select(i => i.Item1));
					uniqueTags = new HashSet<string>(flattenedTags).ToList();
					for (int i = 0; i < uniqueTags.Count + 1; i++) submeshTriangles.Add(new List<int>());
					break;
			}
		}

		for (var i = 0; i < faceIndices.Length; i++)
		{

			var faceIndex = faceIndices[i];
			var face = conway.Faces[i];
			var faceNormal = face.Normal;
			var faceCentroid = face.Centroid;

			ConwayPoly.Roles faceRole = conway.FaceRoles[i];

			// Axes for UV mapping

			Vector3 xAxis = Vector3.right;
			Vector3 yAxis = Vector3.up;
			switch (uvMethod)
			{
				case PolyHydraEnums.UVMethods.FirstEdge:
					xAxis = face.Halfedge.Vector;
					yAxis = Vector3.Cross(xAxis, faceNormal);
					break;
				case PolyHydraEnums.UVMethods.BestEdge:
					xAxis = face.GetBestEdge().Vector;
					yAxis = Vector3.Cross(xAxis, faceNormal);
					break;
				case PolyHydraEnums.UVMethods.FirstVertex:
					yAxis = face.Centroid - face.GetVertices()[0].Position;
					xAxis = Vector3.Cross(yAxis, faceNormal);
					break;
				case PolyHydraEnums.UVMethods.BestVertex:
					yAxis = face.Centroid - face.GetBestEdge().Vertex.Position;
					xAxis = Vector3.Cross(yAxis, faceNormal);
					break;
				case PolyHydraEnums.UVMethods.ObjectAligned:
					// Align towards the highest vertex or edge midpoint (measured in the y direction)
					Vertex chosenVert = face.GetVertices().OrderBy(vert => vert.Position.y).First();
					Halfedge chosenEdge = face.GetHalfedges().OrderBy(edge => edge.Midpoint.y).First();
					Vector3 chosenPoint;
					if (chosenVert.Position.y > chosenEdge.Midpoint.y + 0.01f)  // favour edges slightly
						chosenPoint = chosenVert.Position;
					else
						chosenPoint = chosenEdge.Midpoint;
					
					yAxis = face.Centroid - chosenPoint;
					xAxis = Vector3.Cross(yAxis, faceNormal);
					break;
			}
			
			Color32 color = CalcFaceColor(conway, colors, colorMethod, i);

			float faceScale = 0;
			foreach (var v in face.GetVertices())
			{
				faceScale += Vector3.Distance(v.Position, faceCentroid);
			}
			faceScale /= face.Sides;

			var miscUV1 = new Vector4(faceScale, face.Sides, faceCentroid.magnitude, ((float)i)/faceIndices.Length);
			var miscUV2 = new Vector4(faceCentroid.x, faceCentroid.y, faceCentroid.z, i);

			var faceTris = new List<int>();

			if (face.Sides > 3)
			{
				for (var edgeIndex = 0; edgeIndex < faceIndex.Count; edgeIndex++)
				{

					meshVertices.Add(faceCentroid);
					meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis));
					faceTris.Add(index++);
					edgeUVs.Add(new Vector2(0, 0));
					barycentricUVs.Add(new Vector3(0, 0, 1));

					meshVertices.Add(points[faceIndex[edgeIndex]]);
					meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis));
					faceTris.Add(index++);
					edgeUVs.Add(new Vector2(1, 1));
					barycentricUVs.Add(new Vector3(0, 1, 0));

					meshVertices.Add(points[faceIndex[(edgeIndex + 1) % face.Sides]]);
					meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis));
					faceTris.Add(index++);
					edgeUVs.Add(new Vector2(1, 1));
					barycentricUVs.Add(new Vector3(1, 0, 0));

					meshNormals.AddRange(Enumerable.Repeat(faceNormal, 3));
					meshColors.AddRange(Enumerable.Repeat(color, 3));
					miscUVs1.AddRange(Enumerable.Repeat(miscUV1, 3));
					miscUVs2.AddRange(Enumerable.Repeat(miscUV2, 3));
				}
			}
			else
			{

				meshVertices.Add(points[faceIndex[0]]);
				meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis));
				faceTris.Add(index++);
				barycentricUVs.Add(new Vector3(0, 0, 1));

				meshVertices.Add(points[faceIndex[1]]);
				meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis));
				faceTris.Add(index++);
				barycentricUVs.Add(new Vector3(0, 1, 0));

				meshVertices.Add(points[faceIndex[2]]);
				meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis));
				faceTris.Add(index++);
				barycentricUVs.Add(new Vector3(1, 0, 0));

				edgeUVs.AddRange(Enumerable.Repeat(new Vector2(1, 1), 3));
				meshNormals.AddRange(Enumerable.Repeat(faceNormal, 3));
				meshColors.AddRange(Enumerable.Repeat(color, 3));
				miscUVs1.AddRange(Enumerable.Repeat(miscUV1, 3));
				miscUVs2.AddRange(Enumerable.Repeat(miscUV2, 3));
			}

			if (generateSubmeshes)
			{
				switch (colorMethod)
				{
					case PolyHydraEnums.ColorMethods.ByRole:
						int uniqueRoleIndex = uniqueRoles.IndexOf(faceRole);
						submeshTriangles[uniqueRoleIndex].AddRange(faceTris);
						break;
					case PolyHydraEnums.ColorMethods.BySides:
						submeshTriangles[face.Sides].AddRange(faceTris);
						break;
					case PolyHydraEnums.ColorMethods.ByFaceDirection:
						submeshTriangles[CalcDirectionIndex(face, colors.Length - 1)].AddRange(faceTris);
						break;
					case PolyHydraEnums.ColorMethods.ByTags:
						if (conway.FaceTags[i].Count > 0)
						{
							string htmlColor = conway.FaceTags[i].First(t => t.Item1.StartsWith("#")).Item1;
							int uniqueTagIndex = uniqueTags.IndexOf(htmlColor);
							submeshTriangles[uniqueTagIndex + 1].AddRange(faceTris);
						}
						else
						{
							submeshTriangles[0].AddRange(faceTris);

						}
						break;
				}

			}
			else
			{
				meshTriangles.AddRange(faceTris);
			}
		}

		target.vertices = meshVertices.Select(x => Jitter(x)).ToArray();
		target.normals = meshNormals.ToArray();
		if (generateSubmeshes)
		{
			target.subMeshCount = submeshTriangles.Count;
			for (var i = 0; i < submeshTriangles.Count; i++)
			{
				target.SetTriangles(submeshTriangles[i], i);
			}
		}
		else
		{
			target.triangles = meshTriangles.ToArray();
		}

		target.colors32 = meshColors.ToArray();
		target.SetUVs(0, meshUVs);
		target.SetUVs(1, edgeUVs);
		target.SetUVs(2, barycentricUVs);
		target.SetUVs(3, miscUVs1);
		target.SetUVs(4, miscUVs2);

		target.RecalculateTangents();
		return target;
	}

19 View Source File : PolyMeshBuilder.cs
License : MIT License
Project Creator : IxxyXR

public static Mesh BuildMeshFromWythoffPoly(WythoffPoly source, Color[] colors)
    {
	    if (colors == null) colors = DefaultFaceColors;
	    var meshVertices = new List<Vector3>();
	    var meshTriangles = new List<int>();
	    var MeshVertexToVertex = new List<int>(); // Mapping of mesh vertices to poly vertices (one to many as we duplicate verts)
	    var meshColors = new List<Color>();
	    var meshUVs = new List<Vector2>();

	    var mesh = new Mesh();
	    int meshVertexIndex = 0;

	    foreach (Wythoff.Face face in source.faces) {
		    face.CalcTriangles();
	    }

	    for (int faceType = 0; faceType < source.FaceTypeCount; faceType++) {
		    foreach (Wythoff.Face face in source.faces) {
			    if (face.configuration == source.FaceSidesByType[faceType])
			    {
				    var v0 = source.Vertices[face.points[0]].getVector3();
				    var v1 = source.Vertices[face.points[1]].getVector3();
				    var v2 = source.Vertices[face.points[2]].getVector3();
				    var normal = Vector3.Cross(v1 - v0, v2 - v0);
				    var c = face.center.getVector3();
				    var yAxis = c - v0;
				    var xAxis = Vector3.Cross(yAxis, normal);

				    var faceColor = colors[(int) ((face.configuration + 2) % colors.Length)];
				    // Vertices
				    for (int i = 0; i < face.triangles.Length; i++) {
					    Vector3 vcoords = source.Vertices[face.triangles[i]].getVector3();
					    meshVertices.Add(vcoords);
					    meshColors.Add(faceColor);

					    var u = Vector3.Project(vcoords, xAxis).magnitude;
					    var v = Vector3.Project(vcoords, yAxis).magnitude;
					    meshUVs.Add(new Vector2(u, v));

					    meshTriangles.Add(meshVertexIndex);
					    MeshVertexToVertex.Add(face.triangles[i]);
					    meshVertexIndex++;
				    }
			    }
		    }
	    }

	    mesh.vertices = meshVertices.ToArray();
	    mesh.triangles = meshTriangles.ToArray();
	    mesh.colors = meshColors.ToArray();
	    mesh.uv = meshUVs.ToArray();
	    mesh.RecalculateNormals();
	    mesh.RecalculateTangents();
	    mesh.RecalculateBounds();
	    return mesh;

    }

19 View Source File : PolyMeshBuilder.cs
License : MIT License
Project Creator : IxxyXR

public static Mesh BuildMeshFromWythoffPoly(WythoffPoly source, Color[] colors)
    {
	    if (colors == null) colors = DefaultFaceColors;
	    var meshVertices = new List<Vector3>();
	    var meshTriangles = new List<int>();
	    var MeshVertexToVertex = new List<int>(); // Mapping of mesh vertices to poly vertices (one to many as we duplicate verts)
	    var meshColors = new List<Color>();
	    var meshUVs = new List<Vector2>();

	    var mesh = new Mesh();
	    int meshVertexIndex = 0;

	    foreach (Wythoff.Face face in source.faces) {
		    face.CalcTriangles();
	    }

	    for (int faceType = 0; faceType < source.FaceTypeCount; faceType++) {
		    foreach (Wythoff.Face face in source.faces) {
			    if (face.configuration == source.FaceSidesByType[faceType])
			    {
				    var v0 = source.Vertices[face.points[0]].getVector3();
				    var v1 = source.Vertices[face.points[1]].getVector3();
				    var v2 = source.Vertices[face.points[2]].getVector3();
				    var normal = Vector3.Cross(v1 - v0, v2 - v0);
				    var c = face.center.getVector3();
				    var yAxis = c - v0;
				    var xAxis = Vector3.Cross(yAxis, normal);

				    var faceColor = colors[(int) ((face.configuration + 2) % colors.Length)];
				    // Vertices
				    for (int i = 0; i < face.triangles.Length; i++) {
					    Vector3 vcoords = source.Vertices[face.triangles[i]].getVector3();
					    meshVertices.Add(vcoords);
					    meshColors.Add(faceColor);

					    var u = Vector3.Project(vcoords, xAxis).magnitude;
					    var v = Vector3.Project(vcoords, yAxis).magnitude;
					    meshUVs.Add(new Vector2(u, v));

					    meshTriangles.Add(meshVertexIndex);
					    MeshVertexToVertex.Add(face.triangles[i]);
					    meshVertexIndex++;
				    }
			    }
		    }
	    }

	    mesh.vertices = meshVertices.ToArray();
	    mesh.triangles = meshTriangles.ToArray();
	    mesh.colors = meshColors.ToArray();
	    mesh.uv = meshUVs.ToArray();
	    mesh.RecalculateNormals();
	    mesh.RecalculateTangents();
	    mesh.RecalculateBounds();
	    return mesh;

    }

19 View Source File : Explosive.cs
License : MIT License
Project Creator : joanby

private IEnumerator OnCollisionEnter(Collision col)
        {
            if (enabled)
            {
                if (col.contacts.Length > 0)
                {
                    // compare relative velocity to collision normal - so we don't explode from a fast but gentle glancing collision
                    float velocityAlongCollisionNormal =
                        Vector3.Project(col.relativeVelocity, col.contacts[0].normal).magnitude;

                    if (velocityAlongCollisionNormal > detonationImpactVelocity || m_Exploded)
                    {
                        if (!m_Exploded)
                        {
                            Instantiate(explosionPrefab, col.contacts[0].point,
                                        Quaternion.LookRotation(col.contacts[0].normal));
                            m_Exploded = true;

                            SendMessage("Immobilize");

                            if (reset)
                            {
                                m_ObjectResetter.DelayedReset(resetTimeDelay);
                            }
                        }
                    }
                }
            }

            yield return null;
        }

19 View Source File : Vector3.cs
License : MIT License
Project Creator : NathanWarden

public static Vector3 ProjectOnPlane (Vector3 vector, Vector3 planeNormal)
		{
			return vector - Vector3.Project (vector, planeNormal);
		}

19 View Source File : Vector3.cs
License : MIT License
Project Creator : Viagi

public static Vector3 Exclude(Vector3 excludeThis, Vector3 fromThat)
        {
            return fromThat - Project(fromThat, excludeThis);
        }