summaryrefslogtreecommitdiff
path: root/Runtime/Camera/Skybox.cpp
blob: 528859e308ed5cab589388f99db3f2a37ae87cb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "UnityPrefix.h"
#include "Skybox.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Camera.h"
#include "CameraUtil.h"
#include "Runtime/Shaders/Material.h"
#include "External/shaderlab/Library/intshader.h"
#include "Runtime/Shaders/Shader.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/Shaders/VBO.h"
#include "Runtime/Profiler/ExternalGraphicsProfiler.h"

PROFILER_INFORMATION(gRenderSkyboxProfile, "Camera.RenderSkybox", kProfilerRender);


using namespace ShaderLab;

enum
{
	kSkyboxVertexChannels = (1<<kShaderChannelVertex) | (1<<kShaderChannelTexCoord0)
};

struct SkyboxVertex {
	float x, y, z;
	float tu, tv;
};

const int kSkyboxVertexCount = 6*4;
static const SkyboxVertex kSkyboxVB[kSkyboxVertexCount] = {
	{ -1,  1,  1, 0,1 }, {  1,  1,  1, 1,1 }, {  1, -1,  1, 1,0 }, { -1, -1,  1, 0,0 },
	{  1,  1, -1, 0,1 }, { -1,  1, -1, 1,1 }, { -1, -1, -1, 1,0 }, {  1, -1, -1, 0,0 },
	{  1,  1,  1, 0,1 }, {  1,  1, -1, 1,1 }, {  1, -1, -1, 1,0 }, {  1, -1,  1, 0,0 },
	{ -1,  1, -1, 0,1 }, { -1,  1,  1, 1,1 }, { -1, -1,  1, 1,0 }, { -1, -1, -1, 0,0 },
	{ -1,  1, -1, 0,1 }, {  1,  1, -1, 1,1 }, {  1,  1,  1, 1,0 }, { -1,  1,  1, 0,0 },
	{ -1, -1,  1, 0,1 }, {  1, -1,  1, 1,1 }, {  1, -1, -1, 1,0 }, { -1, -1, -1, 0,0 },
};

const int kSkyplaneVertexCount = 4;
static const SkyboxVertex kSkyplaneVB[kSkyplaneVertexCount] = {
	{  1,  1, -1, 0,1 }, { -1,  1, -1, 1,1 }, { -1, -1, -1, 1,0 }, {  1, -1, -1, 0,0 },
};

Skybox::Skybox (MemLabelId label, ObjectCreationMode mode)
:	Super(label, mode)
{
}

Skybox::~Skybox ()
{
}

void Skybox::AddToManager ()
{

}

void Skybox::RemoveFromManager ()
{

}

void Skybox::RenderSkybox (Material* mat, const Camera& camera)
{
	if( !mat )
		return;

	PROFILER_AUTO_GFX(gRenderSkyboxProfile, &camera)

	Shader* shader = mat->GetShader();
	Assert (shader);

	GfxDevice& device = GetGfxDevice();

	DeviceMVPMatricesState preserveMVP;

	#if UNITY_WP8
	void RotateScreenIfNeeded(Matrix4x4f& mat);
	#endif

	if (camera.GetOrthographic())
	{
		const float epsilon = 1e-6f;
		const float nearPlane = camera.GetNear () * 0.01f;
		const float dist = camera.GetFar() * 10.0f;

		// Make Ortho matrix which passes W to Z
		Matrix4x4f projection = Matrix4x4f::identity;
		//camera.GetImplicitProjectionMatrix( nearPlane, projection );
		projection.Get(2, 2) = -1.0f + epsilon;
		projection.Get(2, 3) = (-2.0f + epsilon) * nearPlane;
		projection.Get(3, 2) = -1.0f;

		#if UNITY_WP8
		if (!RenderTexture::GetActive()) {
			RotateScreenIfNeeded(projection);
		}
		#endif

		Matrix4x4f matrix = Matrix4x4f::identity;
		//device.SetViewMatrix(matrix.GetPtr());
		matrix.SetScale(Vector3f(dist,dist,dist));
		matrix.SetPosition( camera.GetPosition() );
		device.SetWorldMatrix(matrix.GetPtr());
		device.SetProjectionMatrix(projection);
	}
	else
	{
		// Modify Projection matrix to make Infinite Projection Matrix (which passes W into Z)
		// perspective divide will lend ZBuffer value to always be 1.0 (all points on far plane)
		// NOTE: However we need to compensate on floating point precision errors
		// by bringing Z slightly closer to viewer by adding Epsilon
		// See "Projection Matrix Tricks" by Eric Lengyel GDC2007
		// http://www.terathon.com/gdc07_lengyel.ppt

		// In order to avoid clipping of skybox polys we increase skybox size and pull NearPlane as close as possible
		// Higher epsilon values that Z/W < 1.0, but drastically reduces zBuffer precision close to FarPlane
		// Epsilon 1e-6 gives good results as long as NearPlane >= 0.05
		const float epsilon = 1e-6f;
		const float nearPlane = camera.GetNear () * 0.01f;
		const float dist = camera.GetFar() * 10.0f;

		Matrix4x4f projection;
		camera.GetImplicitProjectionMatrix( nearPlane, projection );
		projection.Get(2, 2) = -1.0f + epsilon;
		projection.Get(2, 3) = (-2.0f + epsilon) * nearPlane;
		projection.Get(3, 2) = -1.0f;

		#if UNITY_WP8
		if (!RenderTexture::GetActive()) {
			RotateScreenIfNeeded(projection);
		}
		#endif

		Matrix4x4f matrix = Matrix4x4f::identity;
		matrix.SetScale( Vector3f(dist,dist,dist) );
		matrix.SetPosition( camera.GetPosition() );
		device.SetWorldMatrix( matrix.GetPtr() );
		device.SetProjectionMatrix(projection);
	}

	ShaderLab::IntShader* slshader = shader->GetShaderLabShader();
	const int passCount = slshader->GetActiveSubShader().GetValidPassCount();
	if( passCount == 6 )
	{
		// regular skybox with 6 separate textures per face
		DynamicVBO& vbo = device.GetDynamicVBO();
		for( int j = 0; j < 6; j++ )
		{
			SkyboxVertex* vbPtr = 0;
			if(vbo.GetChunk(kSkyboxVertexChannels, 4, 0, DynamicVBO::kDrawQuads, (void**)&vbPtr, NULL))
			{
				vbPtr[0] = kSkyboxVB[4*j+0];
				vbPtr[1] = kSkyboxVB[4*j+1];
				vbPtr[2] = kSkyboxVB[4*j+2];
				vbPtr[3] = kSkyboxVB[4*j+3];

				vbo.ReleaseChunk(4, 0);

				const ChannelAssigns* channels = mat->SetPassWithShader( j, shader, 0 );
				vbo.DrawChunk (*channels);
				GPU_TIMESTAMP();
			}
		}
	}
	else
	{
		// cube mapped skybox
		for (int pass = 0; pass < passCount; pass++)
		{
			mat->SetPassWithShader( pass, shader, 0 );
			device.ImmediateBegin( kPrimitiveQuads );
			const SkyboxVertex* verts = kSkyboxVB;
			for ( int i = 0; i < kSkyboxVertexCount; i++ ) {
				device.ImmediateTexCoordAll( verts->x, verts->y, verts->z );
				device.ImmediateVertex( verts->x, verts->y, verts->z );
				++verts;
			}
			device.ImmediateEnd();
			GPU_TIMESTAMP();
		}
	}
}

void Skybox::SetMaterial (Material* material) {
	m_CustomSkybox = material;
}

Material* Skybox::GetMaterial ()const {
	return m_CustomSkybox;
}

template<class TransferFunction>
void Skybox::Transfer (TransferFunction& transfer) {
	Super::Transfer (transfer);
	TRANSFER_SIMPLE (m_CustomSkybox);
}

IMPLEMENT_CLASS (Skybox)
IMPLEMENT_OBJECT_SERIALIZE (Skybox)