- Home /
Change or Manipulate Camera Perspective View
Hello,
i have a question about the perspective view of the camera. By standard, the camera view on a cube looks like this:
So you cant take a look on the sides of the cube. They got be hidden by the front side. I wanted to know if there is any possibility to change the camera view of unity to something like this or some other tricks that could result in the same way.
I want it to look like the back of the cube is stretched in every side so you are able to look what is going on on the other sides while you are still focused on the front side of the cube. Thanks for your help.
Answer by Skylar.S · Sep 28, 2013 at 08:14 PM
you could make a shaper like that, but i dont know of any camera mode that does that..
Answer by correia55 · Nov 03, 2015 at 05:23 PM
Unity Version: 5.2.2f1 Personal
First of all, I'm not an expert in shaders.
I've found a way of doing this by using a shader that modified and combined excerpts from other shader scripts I found online, the sources of which are on the first lines of the shader. The result shown here is from a (2, 2, 2) sized cube from unity.
To apply this to your cube you need to create a file with this shader and used this in your material. The shader used is this:
// Part of the Shader was created by Alastair Aitchison (url: https://alastaira.wordpress.com)
// Some parts come from https://en.wikibooks.org/wiki/Cg_Programming/Unity/Lighting_Textured_Surfaces
// Some parts come from https://en.wikibooks.org/wiki/Cg_Programming/Unity/Transparency
Shader "Custom/PerspectiveView" {
Properties{
[Header(Material)]
_MainTex ("Texture For Diffuse Material Color", 2D) = "white" {}
_Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
_SpecColor ("Specular Material Color", Color) = (1,1,1,1)
_Shininess ("Shininess", Float) = 10
}
SubShader{
Tags {"LightMode" = "ForwardBase"}
Tags {"Queue" = "Transparent"}
Pass{
Blend SrcAlpha OneMinusSrcAlpha // use alpha blending
CGPROGRAM
// Pragmas
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform fixed4 _LightColor0;
// User defined variables
uniform sampler2D _MainTex;
uniform fixed4 _Color;
uniform fixed4 _SpecColor;
uniform half _Shininess;
struct VertexInput{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct VertexOutput{
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
fixed3 diffuseColor : TEXCOORD1;
fixed3 specularColor : TEXCOORD2;
};
VertexOutput vert(VertexInput v){
VertexOutput o;
// Perspective Section
//-------------------------------------------------------------------
//
// Transform the vertex coordinates from model space into world space
float4 vv = mul( _Object2World, v.vertex);
// Now adjust the coordinates to be relative to the camera position
vv.xyz += _WorldSpaceCameraPos.xyz;
if(vv.y > 0)
vv.y = vv.z - _WorldSpaceCameraPos.z;
else
vv.y = _WorldSpaceCameraPos.z - vv.z - 2;
if(vv.x > 0)
vv.x = vv.z - _WorldSpaceCameraPos.z;
else
vv.x = _WorldSpaceCameraPos.z - vv.z - 2;
/*if(vv.z > _WorldSpaceCameraPos.z)
vv = float4(vv.x - _WorldSpaceCameraPos.x, vv.y - _WorldSpaceCameraPos.y, 0.0f, 0.0f);
else
vv = float4(0.0f, 0.0f, 0.0f, 0.0f);*/
// Now apply the offset back to the vertices in model space
v.vertex += mul(_World2Object, vv);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
// Light Section
//-------------------------------------------------------------------
//
// multiplication with unity_Scale.w is unnecessary
// because we normalize transformed vectors
float3 normalDirection = normalize(mul(float4(v.normal, 0.0), _World2Object).xyz);
float3 viewDirection = normalize(_WorldSpaceCameraPos - mul(_Object2World, v.vertex).xyz);
float3 lightDirection;
float attenuation;
if (0.0 == _WorldSpaceLightPos0.w) // directional light?
{
attenuation = 1.0; // no attenuation
lightDirection = normalize(_WorldSpaceLightPos0.xyz);
}
else // point or spot light
{
float3 vertexToLightSource = _WorldSpaceLightPos0.xyz - mul(_Object2World, v.vertex).xyz;
float distance = length(vertexToLightSource);
attenuation = 1.0 / distance; // linear attenuation
lightDirection = normalize(vertexToLightSource);
}
float3 ambientLighting = UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;
float3 diffuseReflection = attenuation * _LightColor0.rgb * _Color.rgb * max(0.0, dot(normalDirection, lightDirection));
float3 specularReflection;
if (dot(normalDirection, lightDirection) < 0.0)
// light source on the wrong side?
{
specularReflection = float3(0.0, 0.0, 0.0);
// no specular reflection
}
else // light source on the right side
{
specularReflection = attenuation * _LightColor0.rgb * _SpecColor.rgb * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), _Shininess);
}
o.diffuseColor = ambientLighting + diffuseReflection;
o.specularColor = specularReflection;
o.tex = v.texcoord;
return o;
}
fixed4 frag(VertexOutput i) : COLOR {
return fixed4(i.specularColor + i.diffuseColor * tex2D(_MainTex, i.tex.xy), _Color.a);
}
ENDCG
}
}
}
As I said before, this is a combination of multiple shaders I found online and the part that creates the perspective effect in this cube is presented below. As for adjusting it to a cube of a different size or to another model, it's probably doable by changing the shader in that part. There's another restriction to this shader which is the fact that the result depends on the camera position, this was done while the camera was at (-0.5, -0.5, -10) with a rotation of (0, 0, 0), by changing these values you'll get different results.
I take the opportunity to ask for some suggestions on how to improve and generalize this shader for all models, with no restrictions.
// Perspective Section
//-------------------------------------------------------------------
//
// Transform the vertex coordinates from model space into world space
float4 vv = mul( _Object2World, v.vertex);
// Now adjust the coordinates to be relative to the camera position
vv.xyz += _WorldSpaceCameraPos.xyz;
if(vv.y > 0)
vv.y = vv.z - _WorldSpaceCameraPos.z;
else
vv.y = _WorldSpaceCameraPos.z - vv.z - 2;
if(vv.x > 0)
vv.x = vv.z - _WorldSpaceCameraPos.z;
else
vv.x = _WorldSpaceCameraPos.z - vv.z - 2;
/*if(vv.z > _WorldSpaceCameraPos.z)
vv = float4(vv.x - _WorldSpaceCameraPos.x, vv.y - _WorldSpaceCameraPos.y, 0.0f, 0.0f);
else
vv = float4(0.0f, 0.0f, 0.0f, 0.0f);*/
// Now apply the offset back to the vertices in model space
v.vertex += mul(_World2Object, vv);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
Your answer
Follow this Question
Related Questions
Drawing on the same screen with different cameras? 1 Answer
How do projection matrices work in Unity? 1 Answer
What is the typical setup for switching between third and first person, in terms of model culling? 0 Answers
Viewer position lost in Space. How to get back to scene? 1 Answer
How do I calculate a view matrix using Matrix4x4.LookAt? 2 Answers