gpt4 book ai didi

javascript - 纯 WebGL 虚线

转载 作者:行者123 更新时间:2023-12-04 01:16:55 26 4
gpt4 key购买 nike

我正在尝试使用纯 webgl 创建虚线。我知道这已经有一个问题,也许我很笨,但我不知道如何让它发挥作用。我理解这个概念,但我不知道如何在着色器中获取沿路径的距离。以前的答案有以下行:

varying float LengthSoFar; // <-- passed in from the vertex shader

那么我如何获得 LengthSoFar?我如何在顶点着色器中计算它?

我是不是完全错过了什么?有人可以给我一个工作示例吗?或者至少有一些好的线索?几天来我一直在用头撞墙。

最佳答案

我假设它是这样工作的。你有一个头寸缓冲区。你制作了一个相应的 lengthSoFar 的缓冲区,所以,

function distance(array, ndx1, ndx2) 
{
ndx1 *= 3;
ndx2 *= 3;

var dx = array[ndx1 + 0] - array[ndx2 + 0];
var dy = array[ndx1 + 1] - array[ndx2 + 1];
var dz = array[ndx1 + 2] - array[ndx2 + 2];

return Math.sqrt(dx * dx + dy * dy + dz * dz);
}

var positions =
[
0.123, 0.010, 0.233,
0.423, 0.312, 0.344,
0.933, 1.332, 0.101,
];

var lengthSoFar = [0]; // the length so far starts at 0
for (var ii = 1; ii < positions.length / 3; ++ii)
{
lengthSoFar.push(lengthSoFar[ii - 1] + distance(positions, ii - 1, ii));
}

现在您可以为 positionslengthSoFar 创建缓冲区,并将 lengthSoFar 作为属性传递给顶点着色器,然后从那里将其作为对您的片段着色器进行更改。

不幸的是,它不适用于索引几何(最常见的类型?)。换句话说,它不适用于 gl.drawElements,只能用于 gl.drawArrays。此外,虚线在 3D 中是虚线而不是 2D,因此进入屏幕(远离观察者)的线看起来与穿过屏幕的线不同。当然,如果你画的是二维的,那就没问题了。

如果这些限制对您有好处,这是否回答了您的问题?

const gl = document.querySelector("#c").getContext("webgl");

// Note: createProgramFromScripts will call bindAttribLocation
// based on the index of the attibute names we pass to it.
var program = twgl.createProgramFromScripts(
gl,
["vshader", "fshader"],
["a_position", "a_lengthSoFar"]);
gl.useProgram(program);

function distance(array, ndx1, ndx2)
{
ndx1 *= 3;
ndx2 *= 3;

var dx = array[ndx1 + 0] - array[ndx2 + 0];
var dy = array[ndx1 + 1] - array[ndx2 + 1];
var dz = array[ndx1 + 2] - array[ndx2 + 2];

return Math.sqrt(dx * dx + dy * dy + dz * dz);
}

// Used this line in the console to generate the positions
// var sub = 10; for (var ii = 0; ii <= sub; ++ii) { r = (ii & 1) ? 0.5 : 0.9; a = ii * Math.PI * 2 / sub; console.log((Math.cos(a) * r).toFixed(3) + ", " + (Math.sin(a) * r).toFixed(3) + ", "); }

var positions = [
0.900, 0.000, 0,
0.405, 0.294, 0,
0.278, 0.856, 0,
-0.155, 0.476, 0,
-0.728, 0.529, 0,
-0.500, 0.000, 0,
-0.728, -0.529, 0,
-0.155, -0.476, 0,
0.278, -0.856, 0,
0.405, -0.294, 0,
0.900, -0.000, 0,
];

var lengthSoFar = [0]; // the length so far starts at 0
for (var ii = 1; ii < positions.length / 3; ++ii)
{
lengthSoFar.push(lengthSoFar[ii - 1] + distance(positions, ii - 1, ii));
}

var vertBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);

var vertBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(lengthSoFar), gl.STATIC_DRAW);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 1, gl.FLOAT, false, 0, 0);

// Since uniforms default to '0' and since
// the default active texture is '0' we don't
// have to setup the texture uniform.
var pixels = [
0, 0, 255, 255,
0, 0, 255, 255,
0, 0, 255, 255,
0, 0, 255, 255,
0, 0, 0, 0,
0, 0, 0, 0,
255, 0, 0, 255,
0, 0, 0, 0,
0, 0, 0, 0,
255, 0, 0, 255,
0, 0, 0, 0,
0, 0, 0, 0,
];

var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.RGBA, pixels.length / 4, 1, 0,
gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(pixels));
gl.texParameteri(
gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(
gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

gl.drawArrays(gl.LINE_STRIP, 0, positions.length / 3);
canvas { border: 1px solid black; }
<script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
<script id="vshader" type="whatever">
attribute vec4 a_position;
attribute float a_lengthSoFar;
varying float v_lengthSoFar;
void main() {
gl_Position = a_position;
v_lengthSoFar = a_lengthSoFar;
}
</script>
<script id="fshader" type="whatever">
precision mediump float;
varying float v_lengthSoFar;
uniform sampler2D u_pattern;
#define NumDashes 6.0
void main() {
gl_FragColor = texture2D(
u_pattern,
vec2(fract(v_lengthSoFar * NumDashes)), 0.5);
}
</script>
<canvas id="c" width="300" height="300"></canvas>

备注:Here's an article that might help explain how varyings work

另请注意,您无法更改线条的粗细。为此,您需要 draw lines from triangles

关于javascript - 纯 WebGL 虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19341590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com