- Home /
Access ColorCorrectionCurves values from javascript?
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.
Here's the slider for the red channel:
charCam.GetComponent(ColorCorrectionCurves).redChannel = GUILayout.HorizontalSlider (charCam.GetComponent(ColorCorrectionCurves).redChannel, 0, 255);
Can someone tell me how to change that line so it will access the values correctly? (Javascript if possible)
Well, you've basically answered your own question here. It's not possible (or even sane) to attempt to boil down or assign a single number value to an AnimationCurve. An animtaioncurve contains way too much data for that to ever work. What, exactly, do you hope for it to do? It's possible that there's a better way than the method you're trying here.
I just read up on animationcurves, and it should be pretty simple to modify their values from a script. What part of the curve are you most interested in? Do you want to scale the entire curve by some amount?
Basically I'm trying to do a fairly simple brightness setting. I need to take the right side (2nd) value of the curve for each color channel and raise it or lower it to the slider amount. I've got the curves set to linear so there aren't any bezier curves or anything, just the two values for each curve.
That may not even be the best way to do a brightness setting but I've played with all of the Pro image effects and it seems to be the one that does it the best when I manually adjust the curves.
If you want a brightness setting, why not write your own shader? Experimenting with shaders can be rewarding- not many people here really know how they work, and they're really useful.
Because I just wanted to add a simple brightness setter to my game while this latest version is in beta testing. One of the beta testers asked for it and I figured I would try to put it in. I've already been two months on this latest update and not really wanting to write my own shader to do this as it's not that important. I just figured someone could tell me how to access the animation curves via a slider.
I understand the appeal of writing shaders, I've got a ton of them (skin shaders, etc.) all customized in the game already. It's rewarding I agree, but I am better at customizing them than writing from scratch.
Answer by syclamoth · Jan 28, 2012 at 09:03 AM
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
Whelp, time for the holistic solution. I use C#, so apologies if you need to translate it a bit.
$$anonymous$$ake sure you don't set 'brightness' higher than 1 or lower than 0. I've had experiences with random crashes and instability occurring when the values plugged into a 'range' input in a shader aren't clamped properly.
I just realized that neither of the solutions (this shader or the color correction one) affects the GUI. I wonder if that's even easily possible in Unity and I wish there was a simple brightness control already built in.
Answer by Stralor · Apr 01, 2015 at 12:28 PM
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.
Hat tip to Bunny83 for putting me on the scent.
Cheers.