4 namespace UnityStandardAssets.ImageEffects
7 [AddComponentMenu (
"Image Effects/Color Adjustments/Color Correction (3D Lookup Texture)")]
11 private Material material;
14 public Texture3D converted3DLut = null;
15 public string basedOnTempTex =
"";
18 public override bool CheckResources () {
21 material = CheckShaderAndCreateMaterial (shader, material);
23 if (!isSupported || !SystemInfo.supports3DTextures)
30 DestroyImmediate (material);
37 DestroyImmediate (converted3DLut);
38 converted3DLut = null;
41 public void SetIdentityLut () {
43 var newC =
new Color[dim*dim*dim];
44 float oneOverDim = 1.0f / (1.0f * dim - 1.0f);
46 for(
int i = 0; i < dim; i++) {
47 for(
int j = 0; j < dim; j++) {
48 for(
int k = 0; k < dim; k++) {
49 newC[i + (j*dim) + (k*dim*dim)] =
new Color((i*1.0f)*oneOverDim, (j*1.0f)*oneOverDim, (k*1.0f)*oneOverDim, 1.0f);
55 DestroyImmediate (converted3DLut);
56 converted3DLut =
new Texture3D (dim, dim, dim, TextureFormat.ARGB32,
false);
57 converted3DLut.SetPixels (newC);
58 converted3DLut.Apply ();
62 public bool ValidDimensions ( Texture2D tex2d) {
63 if (!tex2d)
return false;
65 if (h != Mathf.FloorToInt(Mathf.Sqrt(tex2d.width))) {
71 public void Convert ( Texture2D temp2DTex,
string path) {
77 int dim = temp2DTex.width * temp2DTex.height;
78 dim = temp2DTex.height;
80 if (!ValidDimensions(temp2DTex)) {
81 Debug.LogWarning (
"The given 2D texture " + temp2DTex.name +
" cannot be used as a 3D LUT.");
86 var c = temp2DTex.GetPixels();
87 var newC =
new Color[c.Length];
89 for(
int i = 0; i < dim; i++) {
90 for(
int j = 0; j < dim; j++) {
91 for(
int k = 0; k < dim; k++) {
93 newC[i + (j*dim) + (k*dim*dim)] = c[k*dim+i+j_*dim*dim];
99 DestroyImmediate (converted3DLut);
100 converted3DLut =
new Texture3D (dim, dim, dim, TextureFormat.ARGB32,
false);
101 converted3DLut.SetPixels (newC);
102 converted3DLut.Apply ();
103 basedOnTempTex = path;
107 Debug.LogError (
"Couldn't color correct with 3D LUT texture. Image Effect will be disabled.");
111 void OnRenderImage (RenderTexture source, RenderTexture destination) {
112 if (CheckResources () ==
false || !SystemInfo.supports3DTextures) {
113 Graphics.Blit (source, destination);
117 if (converted3DLut == null) {
121 int lutSize = converted3DLut.width;
122 converted3DLut.wrapMode = TextureWrapMode.Clamp;
123 material.SetFloat(
"_Scale", (lutSize - 1) / (1.0f*lutSize));
124 material.SetFloat(
"_Offset", 1.0f / (2.0f * lutSize));
125 material.SetTexture(
"_ClutTex", converted3DLut);
127 Graphics.Blit (source, destination, material, QualitySettings.activeColorSpace == ColorSpace.Linear ? 1 : 0);