Camera类(一)

  1. 视口的宽高比值
        print(Camera.main.aspect);
        Camera.main.aspect = 1f;
        Camera.main.ResetAspect();
  1. 视口的宽高比值
        print("camera旋转前位置:" + transform.position);
        //返回从摄像机的局部坐标到世界坐标系的变化矩阵(只读)Camera的Forward方向为自身坐标系的-Z轴方向
        Matrix4x4 m = Camera.main.cameraToWorldMatrix;
        //v3值为沿着Camera局部坐标系的-Z轴方向前移5个单位的位置在世界坐标系中的位置
        Vector3 v3 = m.MultiplyPoint(Vector3.forward * 5f);
        //v4值为沿着Camera世界坐标系的-Z轴方向前移5个单位的位置在世界坐标系中的位置
        Vector3 v4 = m.MultiplyPoint(transform.forward * 5f);
        print("旋转前,v3坐标值:" + v3);
        print("旋转前,v4坐标值:" + v4);
        transform.Rotate(Vector3.up * 90f);
        print("camera旋转后" + transform.position);
        m = Camera.main.cameraToWorldMatrix;
        //v3值为沿着Camera局部坐标系的-Z轴方向前移5个单位的位置在世界坐标系中的位置
         v3 = m.MultiplyPoint(Vector3.forward * 5f);
        //v4值为沿着Camera世界坐标系的-Z轴方向前移5个单位的位置在世界坐标系中的位置
         v4 = m.MultiplyPoint(transform.forward * 5f);
        print("旋转后,v3坐标值:" + v3);
        print("旋转后,v4坐标值:" + v4);
  1. 摄像机按层渲染
	Camera.main.cullingMask = (1<<2)+(1<<3);
  1. 按层响应事件
	void Start () {

        //print(Mathf.Pow(2, 3));
        //Camera.main.eventMask = -1 ;
        //与运算相关
        //int k = 2 & 8;
    }
    private void OnMouseDown()
    {
        print("down");
    }
    private void OnMouseUp()
    {
        print("up");
    }
  1. 层消隐的距离
    public Transform cb1;
	// Use this for initialization
	void Start () {
        float[] distances = new float[32];
        distances[8] = Vector3.Distance(transform.position, cb1.position);
        Camera.main.layerCullDistances = distances;
    }

    // Update is called once per frame
    void Update () {
        transform.Translate(transform.right* Time.deltaTime);
	}
  1. 是否基于球面剔除
	Camera.main.layerCullSpherical = true;
  1. pixelRect属性:摄像机渲染区间
        Camera.main.pixelRect = new Rect(10f, 10f, 100f, 100f);
        print(Screen.width);
        print(Screen.height);
        print(Camera.main.pixelWidth);
        print(Camera.main.pixelHeight);
  1. 自定义投影矩阵
 public Transform sp, cb;
    public Matrix4x4 originalProjection;
    //晃动振幅
    float q = 0.1f;
    //晃动频率
    float p = 1.5f;
    int which_change = -1;
	// Use this for initialization
	void Start () {
        originalProjection = Camera.main.projectionMatrix;
    }


    void Update () {
        sp.RotateAround(cb.position, cb.up, 45.0f * Time.deltaTime);
        Matrix4x4 pr = originalProjection;
        switch (which_change)
        {
            case -1:
                break;
            case 0:
                //绕摄像机X轴晃动
                pr.m11 += Mathf.Sin(Time.time * p) * q;
                break;
            case 3:
                //摄像机左右移动
                pr.m02 += Mathf.Sin(Time.time * p) * q;
                break;
            case 4:
                //摄像机视口缩放
                pr.m00 += Mathf.Sin(Time.time * p) * q;
                break;
        }
        Camera.main.projectionMatrix = pr;
	}

    private void OnGUI()
    {
        if (true)
        {
            //重置下
            Camera.main.ResetProjectionMatrix();
        }
    }
  1. Rect属性摄像机视图位置和大小
	Camera.main.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
  1. 渲染路径属性
        //渲染路径
        Camera.main.renderingPath = RenderingPath.UsePlayerSettings;
  1. 目标渲染纹理
    public Camera cc;
	void Start () {
        cc.targetTexture = xx;
    }
  1. 渲染矩阵
        Camera.main.worldToCameraMatrix = cc.worldToCameraMatrix;
        //这样也可以
        Camera.main.CopyFrom(cc);
        //要先重置
        Camera.main.ResetWorldToCameraMatrix();
  1. 生成CubeMap静态贴图
        //生成Cubemap静态贴图,反面数默认值为63;
        Cubemap cc;
        if (Camera.main.RenderToCubemap(cc))
        {

        }
  1. 使用其他Shader渲染
        //渲染一帧
        Camera.main.RenderWithShader(Shader.Find("Specular"), "RenderType");
        //每帧
        Camera.main.SetReplacementShader(Shader.Find("Specular"), "RenderType");
        //重置摄像机的shader渲染模式
        Camera.main.ResetReplacementShader();
  1. 近视口到屏幕的射线
    Ray ray;
    RaycastHit hit;
    Vector3 v3 = new Vector3(Screen.width / 2f, Screen.height / 2f, 0.0f);
    Vector3 hitpoint = Vector3.zero;

    private void Update()
    {
        v3.x = v3.x >= Screen.width ? 0.0f : v3.x + 1.0f;
        ray = Camera.main.ScreenPointToRay(v3);
        if(Physics.Raycast(ray,out hit, 100.0f))
        {
            Debug.DrawLine(ray.origin, hit.point, Color.green);
            Debug.Log("射线探测到的物体名称" + hit.transform.name);
            print(hit.point);
        }
    }
  1. 坐标系的转换
        //transform.position = new Vector3(0.0f, 0.0f, 1.0f);
        //transform.rotation = Quaternion.identity;
        //print("1:" + Camera.main.ScreenToViewportPoint(new Vector3(Screen.width / 2f, Screen.height / 2f, 100f)));

        //print("2:" + Camera.main.ViewportToScreenPoint(new Vector3(0.5f, 0.5f, 100.0f)));
  1. 坐标系的转换2
        //e为fieldOfView值 asp宽高比
        //Vector3 v3 = Camera.main.ScreenToWorldPoint(ps);//ps已知参考点

        //v3.x = Camera.main.transform.position.x + ps.z * asp * tan(e / 2);
        //v3.y = Camera.main.transform.position.y + ps.z * tan(e / 2);
        //v3.z = Camera.main.transform.position.z + ps.z;

        transform.position = Vector3.zero;
        Camera.main.fieldOfView = 60f;
        Camera.main.aspect = 16f / 10f;
        //z轴前方10米处对应的屏幕左下角的世界坐标值
        //print("1:" + Camera.main.ScreenToWorldPoint(new Vector3(0f, 0f, 10f)));

        //z轴前方10米对应屏幕中间的世界坐标值
        print("2:" + Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2f, Screen.height / 2f, 10f)));
  1. 重设摄像机到TargetTexture的渲染
        //Camera.main.SetTargetBuffers();
  1. 近视口到屏幕的射线2
        Camera.main.ViewportPointToRay(Vector3.one*0.5f);
  1. 坐标点的坐标系转换
        //x,y是比例值,z非比例值
        //Vector3 v3 = Camera.main.ViewportToWorldPoint(ps);
        // v3各个分量值一长串呢
        transform.position = new Vector3(1f, 0f, 1f);
        Camera.main.fieldOfView = 60f;
        Camera.main.aspect = 16f / 10f;
        //左上角
        print("3" + Camera.main.ViewportToWorldPoint(new Vector3(0f, 0f, 100f)));
        //屏幕中间
        print("1" + Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 100f)));
        //右上角
        print("2" + Camera.main.ViewportToWorldPoint(new Vector3(1f, 1f, 100f)));

猜你喜欢

转载自blog.csdn.net/qq_37811712/article/details/86251280
今日推荐