vector.xy()
时间: 2024-01-06 14:25:39 浏览: 75
根据提供的引用内容,vector是C++标准模板库(STL)中的一个容器,用于存储一组元素。下面是关于vector的基本使用方法:
1. 初始化vector:
```cpp
vector<int> v; // 初始化一个空的vector
vector<int> v(10, 1); // 初始化一个包含10个值为1的元素的vector
vector<int> v(v1); // 将另一个vector v1 的元素拷贝给v
vector<int> v(v1.begin(), v2.end()); // 将v1中从第一个元素到最后一个元素(不包括最后一个元素)的范围拷贝给v
```
2. 插入元素到vector中:
```cpp
v.push_back(5); // 在vector的末尾插入一个元素5
v.insert(v.begin() + 2, 10); // 在vector的第3个位置(索引为2)插入一个元素10
```
3. 遍历vector中的元素:
```cpp
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " "; // 输出vector中的每个元素
}
```
4. 删除vector中的元素:
```cpp
v.pop_back(); // 删除vector的最后一个元素
v.erase(v.begin() + 2); // 删除vector的第3个元素(索引为2)
```
相关问题
const float HEAT_MAX = 10.; const float PointRadius = .42; // test data const int PointCount = 18; const vec3 Points[] = vec3[]( vec3(0., 0., 10.), vec3(.2, .6, 5.), vec3(.25, .7, 8.), vec3(.33, .9, 5.), vec3(.35, .8, 6.), vec3(.1, .1, 6.), vec3(-.45, .8, 4.), vec3(-.2, -.6, 5.), vec3(-.25, -.7, 8.), vec3(-.33, -.9, 8.), vec3(.35, -.45, 10.), vec3(-.1, -.8, 10.), vec3(.33, -.3, 5.), vec3(-.35, .75, 6.), vec3(.6, .4, 10.), vec3(-.4, -.8, 4.), vec3(.7, -.3, 6.), vec3(.3, -.8, 8.) ); vec3 gradient(float w, vec2 uv) { w = pow(clamp(w, 0., 1.) * 3.14159 * .5, .9); vec3 c = vec3(sin(w), sin(w * 2.), cos(w)) * 1.1; return mix(texture(iChannel0, uv).rgb, c, w); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2. * fragCoord - iResolution.xy) / min(iResolution.x, iResolution.y); float d = 0.; for (int i = 0; i < PointCount; i++) { vec3 v = Points[i]; float intensity = v.z / HEAT_MAX; float pd = (1. - length(uv - v.xy) / PointRadius) * intensity; d += pow(max(0., pd), 2.); } fragColor = vec4(gradient(d, uv), 1.); } 将上述代码转为unity可读的shader
可以将上述代码转为以下的 Unity Shader:
```
Shader "Custom/HeatMap" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_HeatMax ("Heat Max", Range(0, 100)) = 10
_PointRadius ("Point Radius", Range(0, 1)) = 0.42
_PointCount ("Point Count", Range(0, 50)) = 18
_Points ("Points", Vector) = (
(0, 0, 10),
(0.2, 0.6, 5),
(0.25, 0.7, 8),
(0.33, 0.9, 5),
(0.35, 0.8, 6),
(0.1, 0.1, 6),
(-0.45, 0.8, 4),
(-0.2, -0.6, 5),
(-0.25, -0.7, 8),
(-0.33, -0.9, 8),
(0.35, -0.45, 10),
(-0.1, -0.8, 10),
(0.33, -0.3, 5),
(-0.35, 0.75, 6),
(0.6, 0.4, 10),
(-0.4, -0.8, 4),
(0.7, -0.3, 6),
(0.3, -0.8, 8)
)
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float _HeatMax;
uniform float _PointRadius;
uniform int _PointCount;
uniform float3 _Points[_PointCount];
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
vec3 gradient(float w, vec2 uv) {
w = pow(clamp(w, 0., 1.) * 3.14159 * .5, .9);
vec3 c = vec3(sin(w), sin(w * 2.), cos(w)) * 1.1;
return mix(tex2D(_MainTex, uv).rgb, c, w);
}
void frag (v2f i, out float4 o : SV_Target) {
vec2 uv = (2. * i.uv - _ScreenParams.xy) / min(_ScreenParams.x, _ScreenParams.y);
float d = 0.;
for (int i = 0; i < _PointCount; i++) {
float3 v = _Points[i];
float intensity = v.z / _HeatMax;
float pd = (1. - length(uv - v.xy) / _PointRadius) * intensity;
d += pow(max(0., pd), 2.);
}
o = float4(gradient(d, uv), 1.);
}
ENDCG
}
}
FallBack "Diffuse"
}
```
在这个 Unity Shader 中,我们使用了 _MainTex 来加载纹理,_HeatMax、_PointRadius、_PointCount 和 _Points 分别表示热力图的最大值、点半径、点数量和点的位置。然后在 Pass 中进行了渲染,使用了 vertex 和 uv 的数据类型,同时还使用了 UnityCG.cginc 中的函数。最后,使用了 gradient 函数和 frag 函数来实现对应的渲染效果。
Shader "Custom/Circle" { Properties { _Center("Center", Vector) = (0,0,0,0) _Radius("Radius", Range(0,1)) = 0.5 _Color("Color", Color) = (1,1,1,1) _MainTex("Texture", 2D) = "" } SubShader { Tags { "RenderType"="Opaque" } LOD 200 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; float2 uv : TEXCOORD0; }; float4 _Center; float _Radius; float4 _Color; sampler2D _MainTex; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } fixed4 frag(v2f i) : SV_TARGET0{ float2 uv = i.uv; float dist = distance(uv, _Center.xy); if(dist <= _Radius) { fixed4 texColor = tex2D(_MainTex, uv); return texColor * _Color; } else { discard; } } ENDCG } } FallBack "Diffuse" }
这是一个自定义的Shader代码示例,用于绘制一个圆形的渲染效果。该Shader在Unity中命名为"Custom/Circle"。
在这个Shader中,定义了几个属性用于控制圆形的位置、半径、颜色和纹理:
1. _Center:表示圆形的中心点位置,默认为(0,0,0,0)。
2. _Radius:表示圆形的半径,范围在0到1之间,默认为0.5。
3. _Color:表示圆形的颜色,默认为白色(1,1,1,1)。
4. _MainTex:表示圆形的纹理,为一个2D纹理,默认为空。
在SubShader中,使用了"RenderType"="Opaque"标签来指定渲染类型为不透明。LOD设置为200,表示使用中等级别的细节层次。
在Pass中,使用CGPROGRAM和ENDCG之间的代码块来定义顶点和片段着色器函数。
顶点着色器函数vert接收输入的顶点数据,并将顶点位置和纹理坐标传递给片段着色器函数frag。
片段着色器函数frag根据传入的纹理坐标计算与中心点的距离,并判断是否在半径范围内。如果在范围内,则从纹理中采样颜色,并与_Color属性相乘返回;否则,丢弃该像素。
最后,使用FallBack "Diffuse"指定了一个回退的渲染效果,即如果设备不支持该Shader,将使用Diffuse着色器作为回退方案。
这个Shader可以用于在物体表面绘制一个圆形区域,并根据纹理和颜色属性进行渲染。可以根据需要自定义属性值来实现不同的效果。
阅读全文
相关推荐
















