summaryrefslogtreecommitdiff
path: root/Runtime/Shaders/UnityPropertySheet.cpp
blob: 7dc894d056a3b3046640c76b08e48e224ee7929d (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
#include "UnityPrefix.h"
#include "UnityPropertySheet.h"
#include "External/shaderlab/Library/shaderlab.h"
#include "External/shaderlab/Library/properties.h"
#include "External/shaderlab/Library/texenv.h"
#include "External/shaderlab/Library/SLParserData.h"
#include "Runtime/Math/ColorSpaceConversion.h"

UnityPropertySheet::UnityTexEnv::UnityTexEnv()
{
	m_Scale = Vector2f(1,1);
	m_Offset = Vector2f(0,0);
	m_Texture = 0;
}


#if UNITY_EDITOR
void UnityPropertySheet::CullUnusedProperties (const ShaderLab::ParserShader* source)
{
	if (!source)
		return;
	
	TexEnvMap::iterator	curTex = m_TexEnvs.begin();
	TexEnvMap::iterator	texEnd = m_TexEnvs.end();
	
	while (curTex != texEnd)
	{
		const char*	propName = curTex->first.GetName(); 
		
		bool propFound = false;
		for (unsigned i = 0; i < source->m_PropInfo.m_Props.size(); ++i)
		{
			const ShaderLab::ParserProperty& srcProp = source->m_PropInfo.m_Props[i];
			if(srcProp.m_Type == ShaderLab::ParserProperty::kTexture 
				&& ::strcmp( srcProp.m_Name.c_str(), propName ) == 0 
			  )
			{
				propFound = true;
				break;
			}
		}
		
		if (propFound)
			++curTex;
		else
			m_TexEnvs.erase(curTex++);
		
	}
}
#endif

void UnityPropertySheet::AssignDefinedPropertiesTo (ShaderLab::PropertySheet &target)
{
	for (ShaderLab::PropertySheet::Floats::iterator i = target.GetFloatsMap().begin(); i != target.GetFloatsMap().end(); i++)
	{
		FloatMap::iterator j = m_Floats.find(i->first);
		if (j != m_Floats.end())
		{
			i->second = j->second;
		}
	}
	for (ShaderLab::PropertySheet::Vectors::iterator i = target.GetVectorMap().begin(); i != target.GetVectorMap().end(); i++)
	{
		ColorMap::iterator j = m_Colors.find(i->first);
		if (j != m_Colors.end())
		{
			if (target.GetColorTag(i->first))
				target.SetVector (i->first, GammaToActiveColorSpace(j->second).GetPtr());				
			else
				target.SetVector (i->first, j->second.GetPtr());
		}
	}
	for (ShaderLab::PropertySheet::TexEnvs::iterator i = target.GetTexEnvsMap().begin(); i != target.GetTexEnvsMap().end(); i++)
	{
		TexEnvMap::iterator j = m_TexEnvs.find(i->first);
		if (j != m_TexEnvs.end())
		{
			target.SetTextureWithPlacement( i->first, j->second.m_Texture, j->second.m_Scale, j->second.m_Offset );
		}
	}
}


bool UnityPropertySheet::AddNewShaderlabProps (const ShaderLab::PropertySheet &source)
{
	bool addedAny = false;
	for (ShaderLab::PropertySheet::Floats::const_iterator i = source.GetFloatsMap().begin(); i != source.GetFloatsMap().end(); i++)
	{
		if (m_Floats.insert( std::make_pair(i->first, i->second) ).second)
			addedAny = true;
	}
	for (ShaderLab::PropertySheet::Vectors::const_iterator i = source.GetVectorMap().begin(); i != source.GetVectorMap().end(); i++)
	{
		// skip texture _ST & _TexelSize properties
		bool isTextureProp = false;
		for (ShaderLab::PropertySheet::TexEnvs::const_iterator j = source.GetTexEnvsMap().begin(); j != source.GetTexEnvsMap().end(); ++j)
		{
			if (j->second.scaleOffsetValue == &i->second || j->second.texelSizeValue == &i->second)
			{
				isTextureProp = true;
				break;
			}
		}
		if (isTextureProp)
			continue;

		if (m_Colors.insert( std::make_pair(i->first, ColorRGBAf(i->second.x, i->second.y, i->second.z, i->second.w)) ).second)
			addedAny = true;
	}
	for (ShaderLab::PropertySheet::TexEnvs::const_iterator i = source.GetTexEnvsMap().begin(); i != source.GetTexEnvsMap().end(); i++)
	{
		if (m_TexEnvs.find (i->first) == m_TexEnvs.end())
		{
			UnityTexEnv ut;
			const Matrix4x4f& mat = i->second.texEnv->GetMatrix();
			ut.m_Scale.Set( mat.Get(0,0), mat.Get(1,1) );
			Vector3f pos = mat.GetPosition();
			ut.m_Offset = Vector2f (pos.x, pos.y);
			m_TexEnvs [i->first] = ut;
			addedAny = true;
		}
	}

	return addedAny;
}

void UnityPropertySheet::AddNewSerializedProps (const UnityPropertySheet &source)
{
	for (UnityPropertySheet::FloatMap::const_iterator i = source.m_Floats.begin(); i != source.m_Floats.end(); i++)
	{
		if (m_Floats.find (i->first) == m_Floats.end())
			m_Floats [i->first] = i->second;
	}
	for (UnityPropertySheet::ColorMap::const_iterator i = source.m_Colors.begin(); i != source.m_Colors.end(); i++)
	{
		if (m_Colors.find (i->first) == m_Colors.end())
			m_Colors [i->first] = i->second;
	}
	for (UnityPropertySheet::TexEnvMap::const_iterator i = source.m_TexEnvs.begin(); i != source.m_TexEnvs.end(); i++)
	{
		if (m_TexEnvs.find (i->first) == m_TexEnvs.end())
			m_TexEnvs [i->first] = i->second;
	}
}