视口的宽高比值
print ( Camera. main. aspect) ;
Camera. main. aspect = 1f ;
Camera. main. ResetAspect ( ) ;
视口的宽高比值
print ( "camera旋转前位置:" + transform. position) ;
Matrix4x4 m = Camera. main. cameraToWorldMatrix;
Vector3 v3 = m. MultiplyPoint ( Vector3. forward * 5f ) ;
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 = m. MultiplyPoint ( Vector3. forward * 5f ) ;
v4 = m. MultiplyPoint ( transform. forward * 5f ) ;
print ( "旋转后,v3坐标值:" + v3) ;
print ( "旋转后,v4坐标值:" + v4) ;
摄像机按层渲染
Camera. main. cullingMask = ( 1 < < 2 ) + ( 1 < < 3 ) ;
按层响应事件
void Start ( ) {
}
private void OnMouseDown ( )
{
print ( "down" ) ;
}
private void OnMouseUp ( )
{
print ( "up" ) ;
}
层消隐的距离
public Transform cb1;
void Start ( ) {
float [ ] distances = new float [ 32 ] ;
distances[ 8 ] = Vector3. Distance ( transform. position, cb1. position) ;
Camera. main. layerCullDistances = distances;
}
void Update ( ) {
transform. Translate ( transform. right* Time. deltaTime) ;
}
是否基于球面剔除
Camera. main. layerCullSpherical = true ;
pixelRect属性:摄像机渲染区间
Camera. main. pixelRect = new Rect ( 10f , 10f , 100f , 100f ) ;
print ( Screen. width) ;
print ( Screen. height) ;
print ( Camera. main. pixelWidth) ;
print ( Camera. main. pixelHeight) ;
自定义投影矩阵
public Transform sp, cb;
public Matrix4x4 originalProjection;
float q = 0.1f ;
float p = 1.5f ;
int which_change = - 1 ;
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 :
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 ( ) ;
}
}
Rect属性摄像机视图位置和大小
Camera. main. rect = new Rect ( 0.0f , 0.0f , 1.0f , 1.0f ) ;
渲染路径属性
Camera. main. renderingPath = RenderingPath. UsePlayerSettings;
目标渲染纹理
public Camera cc;
void Start ( ) {
cc. targetTexture = xx;
}
渲染矩阵
Camera. main. worldToCameraMatrix = cc. worldToCameraMatrix;
Camera. main. CopyFrom ( cc) ;
Camera. main. ResetWorldToCameraMatrix ( ) ;
生成CubeMap静态贴图
Cubemap cc;
if ( Camera. main. RenderToCubemap ( cc) )
{
}
使用其他Shader渲染
Camera. main. RenderWithShader ( Shader. Find ( "Specular" ) , "RenderType" ) ;
Camera. main. SetReplacementShader ( Shader. Find ( "Specular" ) , "RenderType" ) ;
Camera. main. ResetReplacementShader ( ) ;
近视口到屏幕的射线
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) ;
}
}
坐标系的转换
坐标系的转换2
transform. position = Vector3. zero;
Camera. main. fieldOfView = 60f ;
Camera. main. aspect = 16f / 10f ;
print ( "2:" + Camera. main. ScreenToWorldPoint ( new Vector3 ( Screen. width / 2f , Screen. height / 2f , 10f ) ) ) ;
重设摄像机到TargetTexture的渲染
近视口到屏幕的射线2
Camera. main. ViewportPointToRay ( Vector3. one* 0.5f ) ;
坐标点的坐标系转换
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 ) ) ) ;