当前位置: 首页>>代码示例>>C#>>正文


C# Camera.ScreenToWorldPoint方法代码示例

本文整理汇总了C#中UnityEngine.Camera.ScreenToWorldPoint方法的典型用法代码示例。如果您正苦于以下问题:C# Camera.ScreenToWorldPoint方法的具体用法?C# Camera.ScreenToWorldPoint怎么用?C# Camera.ScreenToWorldPoint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UnityEngine.Camera的用法示例。


在下文中一共展示了Camera.ScreenToWorldPoint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DrawGizmos

 public void DrawGizmos(Camera cam)
 {
     var size = 0.1f * Vector3.one;
     Gizmos.color = Color.green;
     for (var i = 0; i < _count; i++) {
         var posScreen = (Vector3)_pointData[i];
         posScreen.z = cam.nearClipPlane;
         Gizmos.DrawCube(cam.ScreenToWorldPoint(posScreen), size);
     }
 }
开发者ID:nobnak,项目名称:DistanceFieldBaker,代码行数:10,代码来源:PointService.cs

示例2: GetWorldPosition

			// This will return the world position of this snapshot based on the distance from the camera
			public Vector3 GetWorldPosition(float distance, Camera camera = null)
			{
				if (camera == null) camera = Camera.main;
				
				if (camera != null)
				{
					var point = new Vector3(ScreenPosition.x, ScreenPosition.y, distance);
					
					return camera.ScreenToWorldPoint(point);
				}
				
				return default(Vector3);
			}
开发者ID:wchaney98,项目名称:Hero-Forever,代码行数:14,代码来源:LeanFinger.cs

示例3: CanvasToWorld

        public static Vector3 CanvasToWorld(this Canvas canvas,
                                            Vector3 canvasPosition,
                                            Camera camera = null)
        {
            if (camera == null)
            {
                camera = Camera.main;
            }

            Vector3 screen_position = Vector3.zero;
            screen_position.x = (canvasPosition.x / canvas.pixelRect.width) * Screen.width;
            screen_position.y = (canvasPosition.y / canvas.pixelRect.height) * Screen.height;
            //The z position is in world units from the camera, should be > 0
            screen_position.z = canvasPosition.z;

            Vector3 worldPoint = camera.ScreenToWorldPoint(screen_position);

            return worldPoint;
        }
开发者ID:SanBen,项目名称:UnityExtensions,代码行数:19,代码来源:CanvasExtensions.cs

示例4: Strike

            private void Strike(bool intense, int generations, float duration, float intensity, float chaosFactor, float glowIntensity, float glowWidth, float forkedness, int count, Camera camera, Camera visibleInCamera)
            {
                if (count < 1)
                {
                    return;
                }

                // find a point around the camera that is not too close
                System.Random r = new System.Random();
                const float minDistance = 500.0f;
                float minValue = (intense ? -1000.0f : -5000.0f);
                float maxValue = (intense ? 1000 : 5000.0f);
                float closestValue = (intense ? 500.0f : 2500.0f);
                float x = (UnityEngine.Random.Range(0, 2) == 0 ? UnityEngine.Random.Range(minValue, -closestValue) : UnityEngine.Random.Range(closestValue, maxValue));
                float y = 620.0f;
                float z = (UnityEngine.Random.Range(0, 2) == 0 ? UnityEngine.Random.Range(minValue, -closestValue) : UnityEngine.Random.Range(closestValue, maxValue));
                float delay = 0.0f;
                Vector3 start = script.Camera.transform.position;
                start.x += x;
                start.y = y;
                start.z += z;

                if (visibleInCamera != null)
                {
                    // try and make sure the strike is visible in the camera
                    Quaternion q = visibleInCamera.transform.rotation;
                    visibleInCamera.transform.rotation = Quaternion.Euler(0.0f, q.eulerAngles.y, 0.0f);
                    float screenX = UnityEngine.Random.Range(visibleInCamera.pixelWidth * 0.1f, visibleInCamera.pixelWidth * 0.9f);
                    float ScreenZ = UnityEngine.Random.Range(visibleInCamera.nearClipPlane + closestValue + closestValue, maxValue);
                    Vector3 point = visibleInCamera.ScreenToWorldPoint(new Vector3(screenX, 0.0f, ScreenZ));
                    start = point;
                    start.y = y;
                    visibleInCamera.transform.rotation = q;
                }

                while (count-- > 0)
                {
                    // for each strike, calculate the end position and perform the strike
                    Vector3 end = start;

                    x = UnityEngine.Random.Range(-100, 100.0f);

                    // 1 in 4 chance not to strike the ground
                    y = (UnityEngine.Random.Range(0, 4) == 0 ? UnityEngine.Random.Range(-1, 600.0f) : -1.0f);

                    z += UnityEngine.Random.Range(-100.0f, 100.0f);

                    end.x += x;
                    end.y = y;
                    end.z += z;

                    // make sure the bolt points away from the camera
                    end.x += (closestValue * camera.transform.forward.x);
                    end.z += (closestValue * camera.transform.forward.z);

                    while ((start - end).magnitude < minDistance)
                    {
                        end.x += (closestValue * camera.transform.forward.x);
                        end.z += (closestValue * camera.transform.forward.z);
                    }

                    if (script.LightningBoltScript != null)
                    {
                        if (UnityEngine.Random.value < script.CloudLightningChance)
                        {
                            // cloud only lightning
                            generations = 0;
                        }
                        LightningBoltParameters parameters = new LightningBoltParameters
                        {
                            Start = start,
                            End = end,
                            Generations = generations,
                            LifeTime = duration,
                            Delay = delay,
                            ChaosFactor = chaosFactor,
                            TrunkWidth = 8.0f,
                            EndWidthMultiplier = 0.25f,
                            GlowIntensity = glowIntensity,
                            GlowWidthMultiplier = glowWidth,
                            Forkedness = forkedness,
                            Random = r,
                            LightParameters = new LightningLightParameters
                            {
                                LightIntensity = intensity,
                                LightRange = 5000.0f,
                                LightShadowPercent = 1.0f,
                            }
                        };
                        script.LightningBoltScript.CreateLightningBolt(parameters);
                        delay += ((duration / count) * UnityEngine.Random.Range(0.2f, 0.5f));
                    }
                }
            }
开发者ID:andrewstarnes,项目名称:wwtd2,代码行数:94,代码来源:ThunderAndLightningScript.cs

示例5: GetPixelWidth

		private float GetPixelWidth(Transform cachedTransform, Camera cachedCamera)
		{
			var position = cachedTransform.position;
			var screenPos = cachedCamera.WorldToScreenPoint(position - cachedTransform.forward * .01f);
			var offset = Vector3.zero;

			if (screenPos.x > 0)
				offset = screenPos - Vector3.right;
			else
				offset = screenPos + Vector3.right;

			if (screenPos.y > 0)
				offset = screenPos - Vector3.up;
			else
				offset = screenPos + Vector3.up;

			offset = cachedCamera.ScreenToWorldPoint(offset);
			
			return 1f / (cachedTransform.InverseTransformPoint(position) - cachedTransform.InverseTransformPoint(offset)).magnitude;
		}
开发者ID:xiatianjin,项目名称:Tank,代码行数:20,代码来源:_CameraShake.cs

示例6: GetTouchWorldPosition

 public static Vector3 GetTouchWorldPosition( Camera camera )
 {
     return camera.ScreenToWorldPoint( GetTouchPosition() );
 }
开发者ID:makitsukasa,项目名称:UhoUho,代码行数:4,代码来源:BoxyLib.cs

示例7: HandleCameraMouseDrag

        private void HandleCameraMouseDrag(CameraState cameraState, Camera cam)
        {
            Event current = Event.current;
            switch (this.m_CurrentViewTool)
            {
                case ViewTool.Orbit:
                    this.OrbitCameraBehavior(cameraState, cam);
                    break;

                case ViewTool.Pan:
                {
                    cameraState.FixNegativeSize();
                    Vector3 position = cam.WorldToScreenPoint(cameraState.pivot.value) + new Vector3(-Event.current.delta.x, Event.current.delta.y, 0f);
                    Vector3 vector7 = cam.ScreenToWorldPoint(position) - cameraState.pivot.value;
                    if (current.shift)
                    {
                        vector7 = (Vector3) (vector7 * 4f);
                    }
                    AnimVector3 pivot = cameraState.pivot;
                    pivot.value += vector7;
                    break;
                }
                case ViewTool.Zoom:
                {
                    float num = HandleUtility.niceMouseDeltaZoom * (!current.shift ? ((float) 3) : ((float) 9));
                    this.m_TotalMotion += num;
                    if (this.m_TotalMotion >= 0f)
                    {
                        cameraState.viewSize.value += (num * this.m_ZoomSpeed) * 0.003f;
                        break;
                    }
                    cameraState.viewSize.value = this.m_StartZoom * (1f + (this.m_TotalMotion * 0.001f));
                    break;
                }
                case ViewTool.FPS:
                {
                    Vector3 vector = cameraState.pivot.value - ((Vector3) ((cameraState.rotation.value * Vector3.forward) * cameraState.GetCameraDistance()));
                    Quaternion quaternion = cameraState.rotation.value;
                    quaternion = Quaternion.AngleAxis((current.delta.y * 0.003f) * 57.29578f, (Vector3) (quaternion * Vector3.right)) * quaternion;
                    quaternion = Quaternion.AngleAxis((current.delta.x * 0.003f) * 57.29578f, Vector3.up) * quaternion;
                    cameraState.rotation.value = quaternion;
                    cameraState.pivot.value = vector + ((Vector3) ((quaternion * Vector3.forward) * cameraState.GetCameraDistance()));
                    break;
                }
            }
            current.Use();
        }
开发者ID:CarlosHBC,项目名称:UnityDecompiled,代码行数:47,代码来源:CameraControllerStandard.cs

示例8: GetJointPosDepthOverlay

        /// <summary>
        /// Gets the 3d overlay position of the given joint over the depth-image.
        /// </summary>
        /// <returns>The joint position for depth overlay.</returns>
        /// <param name="userId">User ID</param>
        /// <param name="joint">Joint index</param>
        /// <param name="camera">Camera used to visualize the 3d overlay position</param>
        /// <param name="imageRect">Depth image rectangle on the screen</param>
        public Vector3 GetJointPosDepthOverlay(Int64 userId, int joint, Camera camera, Rect imageRect)
        {
            if (dictUserIdToIndex.ContainsKey(userId) && camera != null)
            {
                int index = dictUserIdToIndex[userId];

                if (index >= 0 && index < sensorData.bodyCount &&
                   bodyFrame.bodyData[index].bIsTracked != 0)
                {
                    if (joint >= 0 && joint < sensorData.jointCount)
                    {
                        KinectInterop.JointData jointData = bodyFrame.bodyData[index].joint[joint];
                        Vector3 posJointRaw = jointData.kinectPos;

                        if (posJointRaw != Vector3.zero)
                        {
                            // 3d position to depth
                            Vector2 posDepth = MapSpacePointToDepthCoords(posJointRaw);

                            if (posDepth != Vector2.zero && sensorData != null)
                            {
                                if (!float.IsInfinity(posDepth.x) && !float.IsInfinity(posDepth.y))
                                {
                                    float xScaled = (float)posDepth.x * imageRect.width / sensorData.depthImageWidth;
                                    float yScaled = (float)posDepth.y * imageRect.height / sensorData.depthImageHeight;

                                    float xScreen = imageRect.x + xScaled;
                                    float yScreen = camera.pixelHeight - (imageRect.y + yScaled);

                                    Plane cameraPlane = new Plane(camera.transform.forward, camera.transform.position);
                                    float zDistance = cameraPlane.GetDistanceToPoint(posJointRaw);

                                    Vector3 vPosJoint = camera.ScreenToWorldPoint(new Vector3(xScreen, yScreen, zDistance));

                                    return vPosJoint;
                                }
                            }
                        }
                    }
                }
            }

            return Vector3.zero;
        }
开发者ID:BrainProject,项目名称:UnityTemp,代码行数:52,代码来源:KinectManager.cs

示例9: ScaleObjectRelative

        // This allows you to scale an object by a change in pinch scale, relative to a point on the screen
        public static void ScaleObjectRelative(Transform transform, float scale, Vector2 referencePoint, Camera camera = null)
        {
            if (transform != null && scale != 1.0f)
            {
                if (camera == null) camera = Camera.main;

                if (camera != null)
                {
                    // Find screen position of transform
                    var localPosition = camera.WorldToScreenPoint(transform.position);

                    // Scale screen position away from referencePoint
                    localPosition.x = referencePoint.x + (localPosition.x - referencePoint.x) * scale;
                    localPosition.y = referencePoint.y + (localPosition.y - referencePoint.y) * scale;

                    // Update position
                    transform.position = camera.ScreenToWorldPoint(localPosition);

                    // Scale up
                    transform.localScale *= scale;
                }
            }
        }
开发者ID:Kamille1989,项目名称:ARFootball,代码行数:24,代码来源:LeanTouch.cs

示例10: getScreenTopLeft

 private Vector3 getScreenTopLeft(Camera cam)
 {
     // 画面の左上を取得
     Vector3 topLeft = cam.ScreenToWorldPoint (Vector3.zero);
     // 上下反転させる
     topLeft.Scale(new Vector3(1f, -1f, 1f));
     topLeft.z = 0f;
     return topLeft;
 }
开发者ID:SatoshiKawabata,项目名称:education,代码行数:9,代码来源:CameraManager.cs

示例11: AddTouchPoint

        void AddTouchPoint(Vector3 touchPosition, Camera cam, Plane plane)
        {
            if (m_TouchPointsCount == m_TouchPoints.Length)
                return;

            // Get orthographic world point
            var ortho = cam.orthographic;
            cam.orthographic = true;
            var worldPoint = cam.ScreenToWorldPoint(new Vector3(touchPosition.x, touchPosition.y, cam.nearClipPlane));
            cam.orthographic = ortho;

            // Create ray
            var ray = new Ray(worldPoint, -plane.normal);
            m_TouchPoints[m_TouchPointsCount] = ray.GetPoint(plane.GetDistanceToPoint(ray.origin));

            ++m_TouchPointsCount;
        }
开发者ID:Togene,项目名称:BeCalm,代码行数:17,代码来源:FluidTouch.cs

示例12: UpdateChanges

 public void UpdateChanges(Touch info, Camera camera, float scaleFactor)
 {
     var newState = !(info.phase == TouchPhase.Ended || info.phase == TouchPhase.Canceled);
     IsStateChanged = newState != State;
     State = newState;
     IsDeltaChanged = info.phase == TouchPhase.Moved;
     IsCumulativeDeltaChanged |= IsDeltaChanged;
     Delta = info.deltaPosition * scaleFactor;
     ScreenPosition = info.position;
     GuiWorldPosition = camera.ScreenToWorldPoint (ScreenPosition);
     if (info.phase == TouchPhase.Began) {
         IsDeltaChanged = false;
         IsCumulativeDeltaChanged = false;
     }
 }
开发者ID:Leopotam,项目名称:LeopotamGroupLibraryUnity,代码行数:15,代码来源:GuiSystem.cs

示例13: ProcessMouse

            public bool ProcessMouse(Camera camera, float scaleFactor)
            {
                //                if (IsStateChanged || IsDeltaChanged) {
                //                    return true;
                //                }
                if (!Input.mousePresent) {
                    return false;
                }
                var newState = Input.GetMouseButton (0);
                IsStateChanged = newState != State;
                State = newState;
                if (State || IsStateChanged) {
                    var oldPos = ScreenPosition;
                    ScreenPosition = Input.mousePosition;
                    GuiWorldPosition = camera.ScreenToWorldPoint (ScreenPosition);

                    if (State && IsStateChanged) {
                        Delta = Vector2.zero;
                        IsDeltaChanged = false;
                        IsCumulativeDeltaChanged = false;
                    } else {
                        Delta = (ScreenPosition - oldPos) * scaleFactor;
                        IsDeltaChanged = Delta.sqrMagnitude > 0.1f;
                        IsCumulativeDeltaChanged |= IsDeltaChanged;
                    }
                }

                return IsStateChanged || IsDeltaChanged;
            }
开发者ID:Leopotam,项目名称:LeopotamGroupLibraryUnity,代码行数:29,代码来源:GuiSystem.cs

示例14: GetHitFromPointer

		public static Transform GetHitFromPointer(Camera camera, int layerMask = int.MaxValue)
		{
			RaycastHit2D hit = Physics2D.Raycast(new Vector2(camera.ScreenToWorldPoint(Input.mousePosition).x, camera.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0, layerMask);
			return hit.transform;
		}
开发者ID:paveltimofeev,项目名称:GameJamFramework,代码行数:5,代码来源:Utilities2D.cs

示例15: GetJointPosColorOverlay

        /// <summary>
        /// Gets the 3d overlay position of the given joint over the color-image.
        /// </summary>
        /// <returns>The joint position for color overlay.</returns>
        /// <param name="userId">User ID</param>
        /// <param name="joint">Joint index</param>
        /// <param name="camera">Camera used to visualize the 3d overlay position</param>
        /// <param name="imageRect">Color image rectangle on the screen</param>
        public Vector3 GetJointPosColorOverlay(Int64 userId, int joint, Camera camera, Rect imageRect)
        {
            if (dictUserIdToIndex.ContainsKey(userId) && camera != null)
            {
                int index = dictUserIdToIndex[userId];

                if (index >= 0 && index < sensorData.bodyCount &&
                   bodyFrame.bodyData[index].bIsTracked != 0)
                {
                    if (joint >= 0 && joint < sensorData.jointCount)
                    {
                        KinectInterop.JointData jointData = bodyFrame.bodyData[index].joint[joint];
                        Vector3 posJointRaw = jointData.kinectPos;

                        if (posJointRaw != Vector3.zero)
                        {
                            // 3d position to depth
                            Vector2 posDepth = MapSpacePointToDepthCoords(posJointRaw);
                            ushort depthValue = GetDepthForPixel((int)posDepth.x, (int)posDepth.y);

                            if (posDepth != Vector2.zero && depthValue > 0 && sensorData != null)
                            {
                                // depth pos to color pos
                                Vector2 posColor = MapDepthPointToColorCoords(posDepth, depthValue);

                                if (!float.IsInfinity(posColor.x) && !float.IsInfinity(posColor.y))
                                {
                                    //								float xNorm = (float)posColor.x / sensorData.colorImageWidth;
                                    //								float yNorm = 1.0f - (float)posColor.y / sensorData.colorImageHeight;

                                    float xScaled = (float)posColor.x * imageRect.width / sensorData.colorImageWidth;
                                    float yScaled = (float)posColor.y * imageRect.height / sensorData.colorImageHeight;

                                    float xScreen = imageRect.x + xScaled;
                                    float yScreen = camera.pixelHeight - (imageRect.y + yScaled);

                                    Plane cameraPlane = new Plane(camera.transform.forward, camera.transform.position);
                                    float zDistance = cameraPlane.GetDistanceToPoint(posJointRaw);
                                    //float zDistance = (jointData.kinectPos - camera.transform.position).magnitude;

                                    //Vector3 vPosJoint = camera.ViewportToWorldPoint(new Vector3(xNorm, yNorm, zDistance));
                                    Vector3 vPosJoint = camera.ScreenToWorldPoint(new Vector3(xScreen, yScreen, zDistance));

                                    return vPosJoint;
                                }
                            }
                        }
                    }
                }
            }

            return Vector3.zero;
        }
开发者ID:BrainProject,项目名称:UnityTemp,代码行数:61,代码来源:KinectManager.cs


注:本文中的UnityEngine.Camera.ScreenToWorldPoint方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。