I’d like to use Color Correction Curves image effect in my Unity Pro 3.x project and be able to adjust the curves from sliders (Javascript).
The values in the script are from animation curves and I’m unable to figure out how to address them via a slider without getting an error. I realize it’s because I’m trying to set the AnimationCurve as an int, but I don’t know how to access it otherwise.
If you don't want to go with the 'write your own shader' option, what you're trying to do is quite possible.
Probably the most straightforward way to manage this should be this:
var newValue = GUILayout.HorizontalSlider (charCam.GetComponent(ColorCorrectionCurves).redChannel[1].value, 0, 255);
charCam.GetComponent(ColorCorrectionCurves).redChannel.MoveKey(1, new Keyframe(1, newValue));
(to a given definition of 'straightforward'...)
EDIT nah screw that, here have a 'brightness' screen effect.
CODE:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class ScreenBrightness : MonoBehaviour
{
public Shader shader;
public float brightness;
private Material m_Material;
// Called by the camera to apply the image effect
protected Material material {
get {
if (m_Material == null) {
m_Material = new Material (shader);
m_Material.hideFlags = HideFlags.HideAndDontSave;
}
return m_Material;
}
}
protected void OnDisable() {
if( m_Material ) {
DestroyImmediate( m_Material );
}
}
void OnRenderImage (RenderTexture source, RenderTexture destination) {
material.SetFloat ("_Intensity", brightness);
Graphics.Blit(source, destination, material);
}
}
SHADER: You need to attach this to it before it'll work.
Shader "Hidden/Brightness" {
Properties {
_MainTex ("Base (RGB)", 2D) = "" {}
_Intensity ("Brightness", Range (0, 1)) = 1
}
// Shader code pasted into all further CGPROGRAM blocks
CGINCLUDE
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
float4 pos : POSITION;
half2 uv : TEXCOORD0;
};
sampler2D _MainTex;
float _Intensity;
v2f vert( appdata_img v )
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
return o;
}
fixed4 frag(v2f i) : COLOR
{
fixed4 color = tex2D(_MainTex, i.uv);
return color * _Intensity;
}
ENDCG
Subshader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback off
} // shader
Time to necro this post. I’ve figured out how to access curve variables directly from a script, and update them at runtime.
The key ingredient is ColorCorrectionCurves.UpdateParameters().
Here’s an example of updating a two-key blue curve (aka, “channel”):
void SetBlueChannel(){
ColorCorrectionCurves myCCC = myCam.GetComponent<ColorCorrectionCurves>();
//Set left and right key values
myCCC.blueChannel.MoveKey(0, new Keyframe(0, myNewLeftSideValue));
myCCC.blueChannel.MoveKey(1, new Keyframe(1, myNewRightSideValue));
//Enforce linear tangents off each key. (Optional. Seems to default to curved.)
//These can also be used to explicitly curve the tangents by a specified weight.
myCCC.blueChannel.SmoothTangents(0, 0);
myCCC.blueChannel.SmoothTangents(1, 0);
//Update the component. Basically, redraw.
myCCC.UpdateParameters();
}
For more info on MoveKey(int index, Keyframe key) and SmoothTangents(int index, float weight), see AnimationCurve in the scripting docs.
I couldn’t find any docs explicitly about ColorCorrectionCurves.
Still, UpdateParameters() appears to do everything we wanted. Just call everything you want changed, then use it to enforce the change.