- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新:关于如何使用 Superformula 绘制的答案在最后
我需要使用 SuperEllipse 绘制一个这样的圆角矩形.
在能够轻松绘制任何地方的情况下绘制一个:
但是在 HLSL 中你不能,我被困在了绘制或不绘制像素的条件上:
这是 HLSL 代码:
sampler2D input : register(s0);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0</minValue>
/// <maxValue>10.0</maxValue>
/// <defaultValue>4.0</defaultValue>
float N : register(C1);
static const float pi = 3.1415926535f;
float2 superEllipse(float n, float a, float b, float theta)
{
float ct = cos(theta);
float st = sin(theta);
float x = a * sign(ct) * pow(abs(ct), 2.0f / n);
float y = b * sign(st) * pow(abs(st), 2.0f / n);
return float2(x, y);
}
float4 main(float2 uv : TEXCOORD) : COLOR
{
float2 uv1 = uv * float2(2.0f, 2.0f) - float2(1.0f, 1.0f);
float angle = degrees(atan2(uv1.y, uv1.x)) + 180.0f;
float tMax = pi * 2.0f;
float theta = 1.0f / 360.0f * angle * tMax;
float2 se = superEllipse(N, 1, 1, theta);
float angle1 = degrees(atan2(se.y, se.x)) + 180.0f;
float2 zero = float2(0.0f, 0.0f);
float dist1 = distance(se, zero);
float dist2 = distance(uv1, zero);
float4 color = float4(0, 0, 0, 1);
if(dist2 <= dist1)
color += float4(0, 1, 0, 1);
return color;
}
错误似乎与“dist1”和“dist2”变量有关。
(使用 Shazzam 创建)
这是工作的 C# 代码:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Run()
{
const double halfPi = Math.PI*2.0d;
double nValue = trackBar1.Value/10.0d;
double aValue = trackBar2.Value/100.0d;
double bValue = trackBar3.Value/100.0d;
label1.Text = nValue.ToString("F2");
label2.Text = aValue.ToString("F2");
label3.Text = bValue.ToString("F2");
double n = nValue;
double a = aValue;
double b = bValue;
// Build list of points
const int points = 100;
const double step = 360.0d/points;
var list = new List<PointF>();
for (int i = 0; i <= points; i++)
{
double angle = step*i;
double t = 1.0d/360.0d*angle;
double theta = t*halfPi;
double x;
double y;
SuperEllipse.Evaluate(n, a, b, theta, out x, out y);
list.Add(new PointF((float) x, (float) y));
}
/* Drawing */
// Scale and center
for (int index = 0; index < list.Count; index++)
{
PointF pointF = list[index];
pointF.X++;
pointF.Y++;
pointF.X *= (pictureBox1.Width - 10)/2f;
pointF.Y *= (pictureBox1.Height - 10)/2f;
pointF.X += 5;
pointF.Y += 5;
list[index] = pointF;
}
// Draw and show
var bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
//graphics.TranslateTransform(1, 1);
graphics.DrawLines(Pens.Red, list.ToArray());
// graphics.FillClosedCurve(Brushes.Red, fs, FillMode.Alternate);
}
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
}
pictureBox1.Image = bitmap;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
Run();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
Run();
}
private void trackBar3_Scroll(object sender, EventArgs e)
{
Run();
}
}
public static class SuperEllipse
{
public static void Evaluate(double n, double a, double b, double theta, out double x, out double y)
{
double cost = Math.Cos(theta);
double sint = Math.Sin(theta);
x = a*Math.Sign(cost)*Math.Pow(Math.Abs(cost), 2.0d/n);
y = b*Math.Sign(sint)*Math.Pow(Math.Abs(sint), 2.0d/n);
}
}
}
更新
HLSL 中的 SuperFormula(还是不对)
sampler2D input : register(s0);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0</minValue>
/// <maxValue>8.0</maxValue>
/// <defaultValue>1.0</defaultValue>
float A : register(C0);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0</minValue>
/// <maxValue>8.0</maxValue>
/// <defaultValue>1.0</defaultValue>
float B : register(C1);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0</minValue>
/// <maxValue>8.0</maxValue>
/// <defaultValue>1.0</defaultValue>
float M : register(C2);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0, 0.0, 0.0</minValue>
/// <maxValue>8.0, 8.0, 8.0</maxValue>
/// <defaultValue>1.0, 1.0, 1.0</defaultValue>
float3 N : register(C3);
float4 main(float2 uv : TEXCOORD) : COLOR
{
float2 uv1 = uv * float2(2.0f, 2.0f) - float2(1.0f, 1.0f);
float angle = degrees(atan2(uv1.y, uv1.x)) + 180.0f;
float mt = M * angle / 4.0f;
float magnitude = pow((pow((cos(mt) / A), N.y) + pow((sin(mt) / B), N.z)), -(1.0f / N.x));
float2 zero = float2(0.0f, 0.0f);
float dist1 = distance(uv1, zero);
float4 color = float4(0, 0, 0, 1);
if(dist1 <= magnitude)
color += float4(dist1, 1, 0, 1);
return color;
}
最后,一个圆角矩形使用Superformula感谢凯文:
sampler2D input : register(s0);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0</minValue>
/// <maxValue>8.0</maxValue>
/// <defaultValue>1.0</defaultValue>
float A : register(C0);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0</minValue>
/// <maxValue>8.0</maxValue>
/// <defaultValue>1.0</defaultValue>
float B : register(C1);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0</minValue>
/// <maxValue>8.0</maxValue>
/// <defaultValue>8.0</defaultValue>
float M : register(C2);
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0.0, 0.0, 0.0</minValue>
/// <maxValue>8.0, 8.0, 8.0</maxValue>
/// <defaultValue>1.0, 1.0, 1.0</defaultValue>
float3 N : register(C3);
float4 main(float2 uv : TEXCOORD) : COLOR
{
float2 uv1 = uv * float2(2.0f, 2.0f) - float2(1.0f, 1.0f);
float angle = atan2(uv1.y, uv1.x);
float mt = M * angle / 4.0f;
float magnitude = pow((pow((abs(cos(mt)) / A), N.y) + pow((abs(sin(mt)) / B), N.z)), -(1.0f / N.x));
float2 zero = float2(0.0f, 0.0f);
float dist1 = distance(uv1, zero);
float4 color = float4(0, 0, 0, 1);
float alpha = 1.0f / magnitude * dist1;
if(dist1 <= magnitude)
color += float4(1, 0.5, 0, 1);
return color;
}
最佳答案
问题的发生是因为 superEllipse(theta)
返回一个角度不一定是 theta
的点。例如,superEllipse(3.6)
返回一个角度为 3.2 弧度的点。因此,在您的 main()
函数中,比较 uv1
和 se
的大小实际上没有意义,因为它们有不同的角度。
参见 this image .扫过的直线表示传递给 superEllipse
的角度,曲线的末端表示新点实际放置的位置。新点很少位于扫描线上。
如果您对超椭圆使用极坐标方程而不是参数方程,则可以使用它来执行距离测试。方便,Wolfram Mathworld有这样一个等式:
您只需要编写代码并将其放入您的主函数中即可。
float4 main(float2 uv : TEXCOORD) : COLOR
{
float2 uv1 = uv * float2(2.0f, 2.0f) - float2(1.0f, 1.0f);
float angle = degrees(atan2(uv1.y, uv1.x)) + 180.0f;
//to do: implement equation shown above. Use `angle` for theta.
//`m` should be 4 if you want a four-pronged shape.
float magnitude = ???
float2 zero = float2(0.0f, 0.0f);
float dist1 = distance(uv1, zero);
float4 color = float4(0,0,0,1);
if (dist1 <= magnitude) //uv is inside the superellipse
color += float4(0,1,0,1);
return color;
}
关于c# - 在 HLSL 中绘制超椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12229671/
在C#中,我可以通过base关键字访问基类,在java中,我可以通过super关键字访问它。在delphi中如何做到这一点?假设我有以下代码: type TForm3 = class(TF
在这件事上我已经把头撞到墙上好几次了。所以我希望在正确的方向上得到一点帮助。 我有一张 table ,上面有订单,一张 table 上有火车,一张 table 上有航类,一张 table 上有巴士。每
在 Python 中,假设我有以下代码: class SuperClass(object): def __init__(self, x): self.x = x
我希望这个 subview 扩展到它的父 View 之外,但是父 View 的边框正在切入 subview 。有没有办法防止这种情况? class TheView : UIView { let
我有一个标准的高斯函数,看起来像这样: def gauss_fnc(x, amp, cen, sigma): return amp * np.exp(-(x - cen) ** 2 / (2
例如,我有下一个类,带有有界类型参数: public class ItemContainer { void addItems(List items); } 在另一个带有参数的类中使用: pub
如何将此设置转换为命令? 结果如下: // Manual Compression (see the image above) Compressed Size: 12,647,451 bytes //
请建议在应用继承时如何使用@Wither/@With。 我有一个抽象类Parent和具体的Child。 Child 应该是不可变的。将 @Wither 放在两者上会给我两个错误: 构造函数 Child
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在尝试向服务器(虚拟托管)发出 HTTP2 请求,该服务器根据主机 header 值 (SNI) 提供 SSL 证书。 # conn = hyper.HTTP20Connection('h
我有一个应用程序,必须将大约 1300 万行、大约 10 个平均长度的字符串插入到嵌入式 HSQLDB 中。我一直在调整一些东西(批量大小、单线程/多线程、缓存/非缓存表、MVCC 事务、log_si
我想定义一个函数f(x, t::Type)根据 isa(x, t) 是否执行不同的行为.假设我想调用b1(x)如果是,b2(x)除此以外。 我知道我可以像这样在运行时进行动态检查: function
我正在使用 Hyper-V WMI Provider在 Hyper-V 中导入虚拟机,特别是使用 ImportVirtualSystemEx Msvm_VirtualSystemManagementS
这几个星期以来一直困扰着我,我没有结束对它的研究,因为我目前重载并且它让我落后于第一年的 CS (opengl) 大学类(class),这首先让我研究了这个:如何只用一个 for 循环绘制立方体的所有
我正在我的计算机(操作系统:Windows 8)上开发一个 WP8 应用程序。我需要安装一个 VM 才能拥有 linux。同时我需要使用我的 Windows Phone 模拟器。 我下载了 VMWar
我是一名优秀的程序员,十分优秀!