广东快3 Unity编辑器工具:一键配色,与更直不益看的颜色表现

原标题:Unity编辑器工具:一键配色,与更直不益看的颜色表现

做了个unity editor插件。配色时,保持颜色之间的亮度和饱和度相反,是一栽保险高效的配色原则。吾把这栽配色手段经历自定义inspector实现,一键使一切颜色的亮度和饱和度相反。另表倘若能直不益看地在inspector面板望到颜色的亮度,有利于美术人员确定明黑有关(素描有关)。

分两步来实现:①单个颜色的表现,②一组颜色的表现与配色。

对于一个Color变量,吾们期待在变量上方添一个attribute,就能在inspector里表现亮度和饱和度。

而在用一组颜色配色时,用一个“调色板”(palette)的类来装下这组颜色,并在调色板类里实现亮度饱和度的同一,配色的操作在inspector里表现为一个按键广东快3,一键实走。

1.单个颜色的表现

先定义一个attribute (ArtistColorAttribute.cs):

// [ArtistColor] attribute is generated and can be attached to variables

publicclassArtistColorAttribute: PropertyAttribute

{

}

现在要让凡是添上这个attribute的变量都能以吾们想要的手段表现广东快3,也就是同时表现出亮度和饱和度。吾们用PropertyDrawer的继承类来实现广东快3,property drawer能够控制一个对象在inspector里表现的手段。之后,用[CustomPropertyDrawer(typeof(ArtistColorAttribute))]来把吾们的attribute和这个property drawer有关首来。

经历Color.RGBToHSV这个method能够把颜色转换到HSV空间,自然得到了其亮度和饱和度。(仔细望吾写的注解)

#if UNITY_EDITOR

// this line is to make every variable attached by [ArtistColor] to be drawn in the inspector in the way described in the following class

[CustomPropertyDrawer(typeof(ArtistColorAttribute))]

publicclassArtistColorDrawer: PropertyDrawer

{

publicoverridevoidOnGUI(Rect position, SerializedProperty property, GUIContent label)

{

// check if the variable attached by [ArtistColor] is Color or not

if(property.propertyType == SerializedPropertyType.Color)

{

// overwrite the default color value to white

if(property.colorValue == newColor( 0f, 0f, 0f, 0f))

property.colorValue = Color.white;

Rect defaultRect = position;

defaultRect.width = position.width * 0.64f;

// draw the color its self, but without the alpha bar

property.colorValue = EditorGUI.ColorField(defaultRect, label, property.colorValue, true, false, false);

Rect customRect = position;

customRect.x = defaultRect.width 16;

customRect.width = position.width * 0.18f- 2.0f;

floatlightness, saturation, hue;

// get the color's saturation and lightness value for later display

Color.RGBToHSV(property.colorValue, outhue, outsaturation, outlightness);

// draw the 2 values as gray values, which is more intuitive

Color lightnessColor = newColor(lightness, lightness, lightness);

Color saturationColor = newColor(saturation, saturation, saturation);

EditorGUI.ColorField(customRect, GUIContent.none, lightnessColor, false, false, false);

customRect.x = customRect.width 4;

customRect.width = position.width * 0.18f- 2.0f;

EditorGUI.ColorField(customRect, GUIContent.none, saturationColor, false, false, false);

}

else

EditorGUI.HelpBox(position, "Error: [ArtistColor] must be put on a Color!", MessageType.Error);

}

}

#endif

为了行使它,吾们在正本的field上添上attribute (ArtistPaletteExample.cs):

[ArtistColor]

publicColor myColor;

于是Color变量的表现变成了云云:

两个灰框是颜色的亮度和饱和度,亮度的直不益看表现有利于美术人员确定明黑有关(素描有关)。能够不必调色板,单独行使这个attribute,转折颜色的展现手段。

2.一组颜色的表现与配色

在用一组颜色配色时,用一个“艺术家调色板”(ArtistPalette)的类来装下这组颜色,并在调色板类里实现亮度饱和度的同一,配色的操作在inspector里表现为一个按键一键实走。

先竖立一个ArtistPalette类来行为这组颜色的容器。对于其中的每个颜色,照样用吾们刚才做益的表现手段,因此给Color[]数组添上吾们的attribute。为了给美术人员更高的可控性,亮度和饱和度要能人造指定,因此用两个变量来存储这个竖立,并规定其周围(Range)。值得仔细的是,为了防止“除以0”的发生,这个周围并不克取到0。末了增补一个method,能够同一Color[]数组里一切颜色的亮度与饱和度,这个method将会在按键按下的时候被调用。(仔细望吾写的注解)

(ArtistPalette.cs)

// to use property drawer for a class, the class needs to be serializable

[System.Serializable]

publicclassArtistPalette

{ [ArtistColor]

publicColor []colors;

#if UNITY_EDITOR

// the lightness to be assigned to all colors in the array

// avoid dividing by zero

[Range(0.001f, 1.0f)]

publicfloatcommonLightness;

// the saturation to be assigned to all colors in the array

// avoid dividing by zero

[Range(0.001f, 1.0f)]

publicfloatcommonSaturation;

// make the lightness and saturation of the colors all the same

// this method is provoked by a button in the property drawer

publicvoidUnifyLightnessSaturation

{

for( inti = 0; i < colors.Length; i )

{

floath, s, v;

Color.RGBToHSV(colors [i], outh, outs, outv);

s = commonSaturation;

v = commonLightness;

colors [i]= Color.HSVToRGB(h, s, v);

}

}

#endif

}

现在来实现这个按键,以及ArtistPalette类在inspector里的表现手段。

如图,调色板相对于单个颜色要众增补三个东西:①在一切颜色的亮度一列下增补"lightness"的文字,在饱和度一列下方增补“saturation”文字;②共有的亮度和饱和度最益能人造控制,因此增补两个slider;③一个按键,用来实走同一亮度饱和度的method(仔细望吾写的注解)

(ArtistPaletteDrawer.cs)

// don't draw any content if the array is not expanded

// it's also unnecessary to draw these if there's no element in the array

if(colors.isExpanded && colors.arraySize > 0)

{

// draw the 2 words "lightness" and "saturation" under those colors gray blocks

// to indicate what those gray blocks stand for

Rect myRect = position;

myRect.y = base.GetPropertyHeight(property, label) * (colors.arraySize 2) 10;

myRect.x = myRect.width* 0.64f 1f;

intpreviousIndentLevel = EditorGUI.indentLevel;

EditorGUI.indentLevel = previousIndentLevel 1;

EditorGUI.LabelField(myRect, "lightness");

myRect.x = myRect.width* 0.18f 1f;

EditorGUI.LabelField(myRect, "saturation");

// draw 2 sliders for user to tweak the holistic lightness and saturation

// but they don't instantly kick in, the colors are unified only when you click the button (implemented later)

myRect = newRect(position.x, position.y, position.width, GUI.skin.textField.lineHeight 3);

myRect.y = base.GetPropertyHeight(property, label) * (colors.arraySize 3) 20;

SerializedProperty commonLightness = property.FindPropertyRelative( "commonLightness");

EditorGUI.PropertyField(myRect, commonLightness);

myRect.y = base.GetPropertyHeight(property, label);

SerializedProperty commonSaturation = property.FindPropertyRelative( "commonSaturation");

EditorGUI.PropertyField(myRect, commonSaturation);

// get a reference of the palette object, so later we can provoke its public method

ArtistPalette thePalette = fieldInfo.GetValue(property.serializedObject.targetObject) asArtistPalette;

myRect.x = position.width* 0.36f;

myRect.y = base.GetPropertyHeight(property, label);

myRect.width = 240f;

myRect.height = 10f;

// generate a button

// the colors' lightnesses and saturations are unified only when we press the button

if(GUI.Button(myRect, "Unify Lightnesses & Saturations"))

{

thePalette.UnifyLightnessSaturation;

}

EditorGUI.indentLevel = previousIndentLevel;

}

3.奏效怎么样

配色之前:

配色之后:

4.挺进的倾向

在异日还能够铺开对亮度的局限,能够行使有对比的迥异的亮度,但同时保持配色的协和,配色协和最主要的照样饱和度相反。

但是按照孟赛尔(Munsell)色立体的理论,迥异的色相,迥异的亮度,其(最高)感知饱和度是迥异的(图中每走的长度),而吾们刚才行使的HSV空间中的S值只是这个最高饱和度的一个百分比,也就是说S值是一个相对值,不是绝对的感知饱和度。因此,倘若在亮度迥异的情况下,倘若两个颜色行使相通的S值,能够实际感知到的饱和度相差最远。

因此异日能够把亮度交给美术人员,然后饱和度用孟赛尔色立体来决定,得到(相反的)真实的感知饱和度。

5.源码与Demo

把一切文件导入你的工程的肆意位置即可,不必放在稀奇的文件夹(Editor)。然后把ArtistPaletteExample.cs挂在场景中的一个物体上。

https://github.com/MarcusXie3D/ArtistPalettegithub.com

来源知乎专栏:Graphics Fanatic

原标题:中国基建传来好消息!中欧150亿铁路合作项目获批,已经等待近7年

原标题:“大头婴儿”炼成记,错把饮料等奶粉,手把手教父母擦亮眼睛

林伟 中国网时事评论员

原标题:闷了太久,出来走走!文旅市场回暖,潜力有多大?

原标题:线报 | 「粒界科技」、「宏景智驾」与「闪马智能」加入「英伟达初创加速计划」

posted on 2020-05-24  作者:admin  阅读量:

栏目导航

Powered by 10分彩app @2018 RSS地图 html地图

追求更好 技术支持