• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Ony · Jan 28, 2012 at 07:10 AM · javascriptcolorimage effects

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)

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Jan 28, 2012 at 07:48 AM 0
Share

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.

avatar image syclamoth · Jan 28, 2012 at 07:49 AM 0
Share

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?

avatar image Ony · Jan 28, 2012 at 08:53 AM 0
Share

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.

avatar image syclamoth · Jan 28, 2012 at 08:57 AM 0
Share

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.

avatar image Ony · Jan 28, 2012 at 09:00 AM 0
Share

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.

avatar image syclamoth · Jan 28, 2012 at 09:04 AM 0
Share

Well, something like this shouldn't be too hard to write. All it really is is multiplying the colour values by a constant value between 0 and 1- the shader would only be a few lines.

avatar image syclamoth · Jan 28, 2012 at 09:17 AM 0
Share

Ok, I just wrote it. Do you want me to add it to my answer?

avatar image Ony · Jan 28, 2012 at 09:19 AM 0
Share

Sure, if you like, that would be great, thank you.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Jan 28, 2012 at 09:19 AM 0
Share

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

avatar image Ony · Jan 28, 2012 at 09:21 AM 1
Share

No problem at all.

avatar image syclamoth · Jan 28, 2012 at 09:22 AM 0
Share

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

avatar image Ony · Jan 28, 2012 at 09:25 AM 0
Share

PERFECT. Thank you!

avatar image Ony · Jan 28, 2012 at 09:39 AM 0
Share

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.

Show more comments
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A Javascript trying to access an image effect C# script 1 Answer

How to blend between LUT textures in Unity's color correction script? 0 Answers

Glow single Object 1 Answer

Unity 4.6 UI change button color when pressed. -Javascript 1 Answer

Changing the tint colour of materials 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges