x


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)

more ▼

asked Jan 28 '12 at 07:10 AM

Ony gravatar image

Ony
888 29 36 48

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.

Jan 28 '12 at 07:48 AM syclamoth

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?

Jan 28 '12 at 07:49 AM syclamoth

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.

Jan 28 '12 at 08:53 AM Ony

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.

Jan 28 '12 at 08:57 AM syclamoth

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.

Jan 28 '12 at 09:00 AM Ony
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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
more ▼

answered Jan 28 '12 at 09:03 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Whelp, time for the holistic solution. I use C#, so apologies if you need to translate it a bit.

Jan 28 '12 at 09:19 AM syclamoth

No problem at all.

Jan 28 '12 at 09:21 AM Ony

Make 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.

Jan 28 '12 at 09:22 AM syclamoth

PERFECT. Thank you!

Jan 28 '12 at 09:25 AM Ony

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.

Jan 28 '12 at 09:39 AM Ony
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3455
x506
x57
x53
x3

asked: Jan 28 '12 at 07:10 AM

Seen: 997 times

Last Updated: Jan 28 '12 at 10:59 AM