c# - 如何为渐变绘制斜角
问题描述
我不完全确定如何命名,但是,我正在尝试在 Unity 中编写一个函数来创建渐变边框图像,并且我可以获取渐变的第一个边缘来绘制角度但是,我不知道了解如何使其正常工作。
当前结果:
预期结果:
我目前的代码:
public static Texture2D CreateBorder ( this Gradient gradient, int width, int height, int borderSize ) {
Texture2D texture = new Texture2D ( width, height );
// Create Base
for(int y=0;y<height;y++) {
for(int x=0;x<width;x++) {
texture.SetPixel ( x, y, gradient.Evaluate ( 1f ) );
}
}
// Create Bottom
for ( int y = 0; y < borderSize; y++ ) {
for ( int x = borderSize; x < ( width - borderSize ); x++ ) {
float t = (float) y / borderSize;
texture.SetPixel ( x, y, gradient.Evaluate ( t ) );
}
}
// Create Left
for ( int y = borderSize; y < ( height - borderSize ); y++ ) {
for ( int x = 0; x < borderSize; x++ ) {
float t = (float) x / borderSize;
texture.SetPixel ( x, y, gradient.Evaluate ( t ) );
}
}
// Create Top
for ( int y = height-borderSize; y < height; y++ ) {
for ( int x = borderSize; x < (width-borderSize); x++ ) {
float t = (float) (height-y) / borderSize;
texture.SetPixel ( x, y, gradient.Evaluate ( t ) );
}
}
// Create Right
for ( int y = borderSize; y < (height-borderSize); y++ ) {
for ( int x = (width-borderSize); x < width; x++ ) {
float t = (float) ( width - x ) / borderSize;
texture.SetPixel ( x, y, gradient.Evaluate ( t ) );
}
}
texture.Apply ();
return texture;
}
我似乎无法弄清楚如何在不相互覆盖的情况下使角落相遇。
解决方案
这个答案可能不是最有效的,但它的工作原理......
public static Texture2D CreateBorder ( this Gradient gradient, int width, int height, int borderSize ) {
Texture2D texture = new Texture2D ( width, height );
// Create Gradient
for ( int i = 0; i < borderSize; i++ ) {
for ( int y = i; y < height - i; y++ ) {
for ( int x = i; x < width - i; x++ ) {
float t = (float) i / borderSize;
texture.SetPixel ( x, y, gradient.Evaluate ( t ) );
}
}
}
// Create Background
for ( int y = borderSize; y < height - borderSize; y++ ) {
for ( int x = borderSize; x < width - borderSize; x++ ) {
texture.SetPixel ( x, y, Color.clear );
}
}
texture.Apply ();
return texture;
}
推荐阅读
- scala - java.lang.ClassCastException:org.apache.avro.generic.GenericData$Record 无法转换为 java.lang.String
- crystal-lang - 如何为 C 库实现事件 IO(特别是 NCurses)
- flutter - FLUTTER-WEB file.writeAsBytes 不工作
- python - api post请求上的多对多关系生成值错误
- java - 如何使用 Gson 将 json 时区对象转换为 java TimeZone?
- c# - 我像往常一样这样做,但为什么绑定不起作用?
- azure-ad-b2c - Azure AD B2C - 如何向访问令牌传播新用户声明
- python - 我如何从 csv fille 获取 dict 作为 dict 返回
- debugging - 如何对存储在天蓝色工件中的符号进行同步?
- python - 如何让参与者在python中按下特定的数字键?