- Home /
How do i use a slider to adjust Brightness or Contrast?
I need a small line of code that I can adjust the Brightness/Contrast of my game. Make sure it uses a slider from -100 to +100, -100 would mean less brightness or Contrast and +100 would mean more brightness and contrast. Anyway I can approach this?
If you are using lighting in your game, you can adjust the ambient light:
http://docs.unity3d.com/ScriptReference/RenderSettings-ambientLight.html
couldn't I just use the camera to give an Illusion of game brightness to adjust?
http://forum.unity3d.com/threads/77779-Why-there-is-no-simple-brightness-or-gamma-slider-in-unity
Hack it out or use Pro image effects, apparently.
I actually tried the ambient light thing. Works for brightness, not so much contrast. Also it doesn't really add much to work with only grey and white makes things brighter the rest are completely different colors.
Answer by Jessespike · Sep 23, 2015 at 08:39 PM
I tried a quick crack at it. It's not perfect, but it might be a good starting point for someone who wants to pursue a similar effect... Import the ImageEffects from the StandardAsset. Put this script on the camera and hook up the shader reference.
To work with a slider, place the SetBrightness or SetContrast function in the slider's OnValueChanged list.
using System;
using UnityEngine;
using UnityStandardAssets.ImageEffects;
[ExecuteInEditMode]
[AddComponentMenu("Image Effects/Custom/Brightness Effect")]
public class BrightnessEffect : ImageEffectBase
{
[Range(0f, 2f)] public float _Brightness = 1f;
[Range(0f, 2f)] public float _Contrast = 1f;
// Called by camera to apply image effect
void OnRenderImage (RenderTexture source, RenderTexture destination)
{
material.SetFloat ("_Brightness", _Brightness);
material.SetFloat ("_Contrast", _Contrast);
Graphics.Blit (source, destination, material);
}
public void SetBrightness(float value)
{
_Brightness = value;
}
public void SetContrast(float value)
{
_Contrast = value;
}
}
//
Shader "Custom/Brightness Effect" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Brightness ("Brightness", float) = 1
_Contrast ("Contrast", float) = 1
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float _Brightness;
uniform float _Contrast;
fixed4 frag (v2f_img i) : SV_Target
{
fixed4 output = tex2D(_MainTex, i.uv);
output = output * _Brightness;
output = (output - 0.5) * _Contrast + 0.5;
return output;
}
ENDCG
}
}
Fallback off
}
Nice and simple. Works well.
For the uninitiated:
You can add the shader as a variable to the BrightnessEffect script and then call:
material = CheckShaderAndCreate$$anonymous$$aterial(shader, material);
The standard asset image effects do it as follows:
public override bool CheckResources () {
CheckSupport(false);
material = CheckShaderAndCreate$$anonymous$$aterial(shader, material);
if (!isSupported)
ReportAutoDisable ();
return isSupported;
}
// Called by camera to apply image effect
void OnRenderImage (RenderTexture source, RenderTexture destination) {
if (CheckResources() == false) {
Graphics.Blit(source, destination);
return;
}
Answer by Tony_T · Sep 23, 2015 at 08:43 PM
You have to do a bit of a research. I will link you to some pages that will help but you have to write the scripts yourself. It's the best way to understand how things work and learn at the same time. Take a look at the Slider, Slider UI, RenderSettings. Good luck !
As I already said, unfortunately, playing with the ambient intensity has no effect on object that are baked.
Answer by Twinklier · Sep 29, 2018 at 12:56 PM
Hey, why not use Colour Grading is Post Processing? Bump up those sliders and you'll have pretty much the same effect.
Well, "same effect" I mean same brightness effect you see in other games.