public static Matrix4x4 GetProjectionMatrix(this Camera camera, float texelOffsetX, float texelOffsetY) { if (camera == null) { return(Matrix4x4.identity); } Vector4 extents = GetProjectionExtents(camera, texelOffsetX, texelOffsetY); float cf = camera.farClipPlane; float cn = camera.nearClipPlane; float xm = extents.z - extents.x; float xp = extents.z + extents.x; float ym = extents.w - extents.y; float yp = extents.w + extents.y; if (camera.orthographic) { return(Matrix4x4Extension.GetOrthographicProjection(xm, xp, ym, yp, cn, cf)); } else { return(Matrix4x4Extension.GetPerspectiveProjection(xm * cn, xp * cn, ym * cn, yp * cn, cn, cf)); } }
private void Rotate(float angle) { Matrix4x4 rotationMatrix = Matrix4x4Extension.RotationXZ(angle); //Matrix4x4 localRotationMatrix = points * transform.worldToLocalMatrix.inverse; rotatedPoints = points * rotationMatrix; }
private void DrawGizmo(EditorPanel sender) { if (ready) { ImGuizmo.SetOrthographic(_viewportPanel.ProjectioMode == ViewportPanel.ViewMode.Orthographic); _viewportPanel.EnableSelect = !ImGuizmo.IsOver(); ImGuizmo.SetRect(_viewportPanel.WindowContentAreaMin.X, _viewportPanel.WindowContentAreaMin.Y, _viewportPanel.WindowContentAreaMax.X - _viewportPanel.WindowContentAreaMin.X, _viewportPanel.WindowContentAreaMax.Y - _viewportPanel.WindowContentAreaMin.Y); //ref *(float*)(void*)null ImGuizmo.SetDrawlist(); if (ImGuizmo.Manipulate(ref cameraView.M11, ref cameraProjection.M11, currentGizmoOperation, (currentGizmoOperation != OPERATION.SCALE) ? currentGizmoMode : MODE.LOCAL, ref transformMat.M11)) { // safety feature if (!Matrix4x4Extension.HasNaNElement(transformMat)) { Context.ActiveEntity.Transform.SetWorldTransform(transformMat); } } } }
public static Matrix4x4 GetPerspectiveProjection(this Camera camera, float texelOffsetX, float texelOffsetY) { if (camera == null) { return(Matrix4x4.identity); } float oneExtentY = Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView); float oneExtentX = oneExtentY * camera.aspect; float texelSizeX = oneExtentX / (0.5f * camera.pixelWidth); float texelSizeY = oneExtentY / (0.5f * camera.pixelHeight); float oneJitterX = texelSizeX * texelOffsetX; float oneJitterY = texelSizeY * texelOffsetY; float cf = camera.farClipPlane; float cn = camera.nearClipPlane; float xm = (oneJitterX - oneExtentX) * cn; float xp = (oneJitterX + oneExtentX) * cn; float ym = (oneJitterY - oneExtentY) * cn; float yp = (oneJitterY + oneExtentY) * cn; return(Matrix4x4Extension.GetPerspectiveProjection(xm, xp, ym, yp, cn, cf)); }
private void Start() { lr = GetComponent <LineRenderer>(); //pointsVector3 = new Vector3[pointsTr.Length]; //for (int i = 0; i < pointsTr.Length; i++) //{ // pointsVector3[i] = pointsTr[i].localPosition; //} // points = new Matrix4x4(pointsVector3); points = Matrix4x4Extension.TriangleToMatrixLocal(pointsTr[0], pointsTr[1], pointsTr[2]); Debug.Log(points.ToString()); //for (int i = 0; i < pointsTr.Length; i++) //{ // pointsVector3[i] = pointsTr[i].localPosition; //} ////3x3 //points = new Matrix(pointsVector3); ////1x3 //Vector3 normal = GameMath.GetNormal(pointsTr[0].localPosition, pointsTr[1].localPosition, pointsTr[2].localPosition); //normalMatrix = new Matrix(normal); //Matrix transposedNormal = Matrix.Transpose(normalMatrix); //double[,] normalIdentityArray = { // { normalMatrix [0,0] , 0 , 0}, // { 0 , normalMatrix [0,1] , 0}, // { 0 , 0 , normalMatrix [0,2]}, //}; //Matrix normalIdentity = new Matrix(normalIdentityArray); //Matrix result = Matrix.StupidMultiply(points, no); // Debug.Log("input \n" + points.ToString()); // Debug.Log("normal \n" + normalIdentity.ToString()); // Debug.Log("result \n" + result.ToString()); }
//public static float GetProjectedMouseX(Rendering.Cameras.Camera camera) //{ // float currentX = (mousePosition.X / (float)Application.Instance.Window.Width) * 2.0f - 1.0f; // return Vector3.Transform(new Vector3(currentX, 0, 0), Matrix4x4Extension.Invert(camera.ViewProjectionMatrix)).X; //} //public static float GetProjectedMouseY(Rendering.Cameras.Camera camera) //{ // float currentY = (mousePosition.Y / (float)Application.Instance.Window.Height) * 2.0f - 1.0f; // return Vector3.Transform(new Vector3(0, -currentY, 0), Matrix4x4Extension.Invert(camera.ViewProjectionMatrix)).Y; //} public static Vector2 GetProjectedMouse(Rendering.Cameras.Camera camera, Vector2 mouse, Vector2 window, Vector3 position, Quaternion rotation) { float currentX = (mouse.X / window.X) * 2.0f - 1.0f; float currentY = (mouse.Y / window.Y) * 2.0f - 1.0f; Vector3 projected = Vector3.Transform(new Vector3(currentX, -currentY, 0), Matrix4x4Extension.Invert(Matrix4x4.CreateTranslation(-position) * Matrix4x4.CreateFromQuaternion(Quaternion.Inverse(rotation)) * camera.ProjectionMatrix)); return(new Vector2(projected.X, projected.Y)); }