我创造了一个重现问题的简单例子(或者更确切地说是我的误解):
string text = @"eaisjdoaisjdoaisjdai_osjdaisodjasizzi_ojiozaziasjz_";
int[] score = new int[123];
foreach(char letter in text)
{
int val = score[letter]; //give me the value stored at the index
score[letter] = val++; //increment it and store it back into the array at the index
}
...
通过上面的调试,正确地为val
分配了数组指定索引处的值。但是当递增时,val
不会被分配回数组。那是为什么?
图片显示了当从数组中检索val
时评估score[letter]
的值的即时窗口,分配给val
后的gl.LINES
的值以及<!doctype html>
<head>
<title>Triangles</title>
<style>
html, body {
background: #000;
height: 100%;
margin: 0;
}
canvas {
width: 1280px;
height: 720px;
position: absolute;
margin: auto;
top: 0;
right: 0;
left: 0;
bottom: 0;
}
</style>
</head>
<body>
<script>
'use strict';
const triangleCount = 1e6;
const antialias = true;
const generateTriangles = (count, width, height) => {
const coords = new Float32Array(9 * count);
for (var i = 0; i < coords.length;) {
const x = Math.random() * 2 - 1;
const y = Math.random() * 2 - 1;
const z = Math.random() * 2 - 1;
const theta = Math.random() * Math.PI;
const ax = 10 * Math.cos(theta) / width;
const ay = 10 * Math.sin(theta) / height;
const bx = 10 * Math.cos(theta + 0.3) / width;
const by = 10 * Math.sin(theta + 0.3) / height;
coords[i++] = x + ax; coords[i++] = y + ay; coords[i++] = z;
coords[i++] = x + bx; coords[i++] = y + by; coords[i++] = z;
coords[i++] = x - ax; coords[i++] = y - ay; coords[i++] = z;
coords[i++] = x - ax; coords[i++] = y - ay; coords[i++] = z;
coords[i++] = x - bx; coords[i++] = y - by; coords[i++] = z;
coords[i++] = x + ax; coords[i++] = y + ay; coords[i++] = z;
}
return coords;
};
const vertexShaderSource = `
precision lowp float;
attribute vec3 aPosition;
uniform float uWobble;
void main() {
float p = 1.0 / (0.3 * aPosition.z - 1.4 + 0.1 * uWobble);
gl_Position = vec4(p * aPosition.x, p * aPosition.y, aPosition.z, 1.0);
}
`;
const fragmentShaderSource = `
precision lowp float;
void main() {
float z = gl_FragCoord.z;
gl_FragColor = vec4(1.2 * z, z * z, z, 1.0);
}
`;
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
const gl = canvas.getContext('webgl', {alpha: false, antialias});
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderSource);
gl.compileShader(vertexShader);
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentShaderSource);
gl.compileShader(fragmentShader);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
const aVertexPosition = gl.getAttribLocation(program, 'aPosition');
gl.enableVertexAttribArray(aVertexPosition);
const uWobble = gl.getUniformLocation(program, 'uWobble');
gl.uniform1f(uWobble, 1);
const vertices = generateTriangles(triangleCount, canvas.width, canvas.height);
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
gl.vertexAttribPointer(aVertexPosition, 3, gl.FLOAT, false, 0, 0);
const render = (timestamp) => {
requestAnimationFrame(render);
gl.uniform1f(uWobble, Math.sin(0.002 * timestamp));
gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3);
};
window.requestAnimationFrame(render);
</script>
</body>
的递增值
我显然做了一些愚蠢的事情但却无法弄清楚是什么。
答案 0 :(得分:3)
这是因为您正在使用后增量运算符,该运算符会在>>返回后增加值。
将其更改为预增量运算符++val
,它应该可以正常工作。
第一种形式(
++val
)是前缀增量操作。结果 operation是操作数增加后的值。第二种形式(
val++
)是后缀增量操作。结果 operation是操作数增加之前的值。