summaryrefslogtreecommitdiff
path: root/Runtime/Camera/BaseRenderer.h
blob: 26350fbed723d30fdb83808120b86255860706cb (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
#ifndef BASE_RENDERER_H
#define BASE_RENDERER_H

#include "Runtime/Math/Vector4.h"
#include "Runtime/Math/Matrix4x4.h"
#include "Runtime/Geometry/AABB.h"
#include "Runtime/Modules/ExportModules.h"
#include "Runtime/Camera/RenderLoops/GlobalLayeringData.h"

namespace Unity { class Material; }
class ChannelAssigns;
using namespace Unity;
class AABB;
class Matrix4x4f;
class Vector3f;
class Quaternionf;
class MaterialPropertyBlock;
class Shader;
class Renderer;
template<class T> class PPtr;

enum RendererType {
	kRendererUnknown,
	
	kRendererMesh,
	kRendererSkinnedMesh,
	kRendererCloth,
	kRendererSprite,
	
	kRendererParticle,
	kRendererTrail,
	kRendererLine,
	kRendererParticleSystem,	

	kRendererIntermediate,
	
	kRendererTypeCount
};

struct BatchInstanceData
{
	Matrix4x4f xform;			// 64
	Renderer* renderer;			// 4
	int subsetIndex;			// 4
	int xformType;				// 4
	int dummy;					// 4 byte padding
};

struct TransformInfo
{
	Matrix4x4f     worldMatrix;     // 64
	AABB           worldAABB;       // 24
	AABB           localAABB;       // 24	used by LightManager and Shadows
	float          invScale;        // 4
	TransformType  transformType;   // 4
};

// Abstract base class for renderers.
class EXPORT_COREMODULE BaseRenderer {
public:
	BaseRenderer(RendererType type);
	virtual ~BaseRenderer();
	
	// The main Render Function. Implement this in order to draw the graphics
	// When this is called, the correct material and transform have been set up.
	virtual void Render (int materialIndex, const ChannelAssigns& channels) = 0;

	void GetWorldAABB( AABB& result );
	const TransformInfo& GetTransformInfo ();
	void GetLocalAABB( AABB& result );

	virtual void UpdateTransformInfo() = 0;

	virtual void RendererBecameVisible() { m_IsVisibleInScene = true; }
	virtual void RendererBecameInvisible() { m_IsVisibleInScene = false; }
	virtual int GetLayer() const = 0;
	
	virtual float GetSortingFudge () const { return 0.0f; }

	virtual int GetMaterialCount() const = 0;
	virtual PPtr<Material> GetMaterial(int i) const = 0;
	virtual int GetSubsetIndex(int i) const { return i; }
	virtual int GetStaticBatchIndex() const { return 0; }
	virtual UInt32 GetMeshIDSmall() const { return 0; }

	RendererType GetRendererType() const { return static_cast<RendererType>(m_RendererType); }

	UInt32 GetLayerMask() const { return 1<<GetLayer(); }
	
	bool GetCastShadows() const { return m_CastShadows; }
	bool GetReceiveShadows() const { return m_ReceiveShadows; }

	void ApplyCustomProperties(Unity::Material& mat, Shader* shader, int subshaderIndex) const;
	
	const Vector4f& GetLightmapST() const { return m_LightmapST; }
	// If the renderer's mesh is batched, UVs were already transformed by lightmapST, so return identity transform.
	// The static batch index equal to 0 means there is no batched mesh, i.e. the renderer is not batched.
	const Vector4f GetLightmapSTForRendering () const { return GetStaticBatchIndex() == 0 ? m_LightmapST : Vector4f(1,1,0,0); }

	UInt8 GetLightmapIndex() const { return m_LightmapIndex; }
	int GetLightmapIndexInt() const;

	void SetLightmapIndexIntNoDirty(int index);
	
	bool IsLightmappedForRendering() const;
	bool IsLightmappedForShadows() const;

	#if UNITY_EDITOR
	float GetScaleInLightmap() const 		{ return m_ScaleInLightmap; }
	void SetScaleInLightmap(float scale) 	{ m_ScaleInLightmap = scale; }
	#endif
	
	void ComputeCustomPropertiesHash();
	UInt32 GetCustomPropertiesHash() const { return m_CustomPropertiesHash; }
	const MaterialPropertyBlock* GetCustomProperties() const { return m_CustomProperties; }


	GlobalLayeringData GetGlobalLayeringData () const		{ return m_GlobalLayeringData; }
	void SetGlobalLayeringData (GlobalLayeringData data)	{ m_GlobalLayeringData = data; }
protected:
	Vector4f			m_LightmapST;			///< Lightmap tiling and offset
	UInt8				m_RendererType;			// enum RendererType
	UInt8				m_LightmapIndex;
	bool				m_CastShadows;
	bool				m_ReceiveShadows;
	bool				m_IsVisibleInScene;
#if UNITY_EDITOR
	float				m_ScaleInLightmap;		///< A multiplier to object's area used in atlasing
#endif
	bool                m_TransformDirty;
	bool                m_BoundsDirty;
	TransformInfo       m_TransformInfo;
	MaterialPropertyBlock*	m_CustomProperties;
	UInt32				m_CustomPropertiesHash;
	GlobalLayeringData	m_GlobalLayeringData;
};

inline const TransformInfo& BaseRenderer::GetTransformInfo()
{
	if (m_TransformDirty || m_BoundsDirty)
	{
		UpdateTransformInfo();
		m_TransformDirty = false;
		m_BoundsDirty = false;
	}
	return m_TransformInfo;
}
#endif