- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在学习 WebGL 和 Javascript。一项作业要求我使用 WebGL 创建多个形状,并且它们都具有不同的颜色,但是,我无法弄清楚如何设置它以便每个形状都有自己的颜色。
// HelloTriangle.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'void main() {\n' +
' gl_Position = a_Position;\n' +
'}\n';
// Fragment shader program
var FSHADER_SOURCE =
'precision mediump float;\n' +
'uniform vec4 u_FragColor;\n' +
'void main() {\n' +
' gl_FragColor = u_FragColor;\n' +
'}\n';
function main() {
// Retrieve <canvas> element
var canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
var gl = getWebGLContext(canvas);
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
// Initialize shaders
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}
// Write the positions of vertices to a vertex shader
var n = initVertexBuffers(gl);
if (n < 0) {
console.log('Failed to set the positions of the vertices');
return;
}
// Specify the color for clearing <canvas>
gl.clearColor(0, 0, 0, 1);
// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);
// Draw the rectangle
gl.drawArrays(gl.TRIANGLES, 0, 3); // Triangle One
gl.drawArrays(gl.TRIANGLES, 3, 3); // Triangle Two
gl.drawArrays(gl.TRIANGLE_FAN, 6, 6); // Triangle Fan
}
function randColor() // Assign Random color values
{
setR = Math.random();
setG = Math.random();
setB = Math.random();
}
function initVertexBuffers(gl) {
var vertices = new Float32Array([
0.1, 0.1, -0.1, -0.1, 0.1, -0.1, // First Triangle
0.15, 0.25, 0.1, 0.2, -0.1, 0.2, // Second Triangle
0.75, 0.65, 0.35, 0.25, 0.65, 0.55,
0.65, 0.25, 0.35, 0.45, 0.25, 0.15 // Triangle Fan
]);
var n = 6; // The number of vertices
// Create a buffer object
var vertexBuffer = gl.createBuffer();
if (!vertexBuffer) {
console.log('Failed to create the buffer object');
return -1;
}
// Bind the buffer object to target
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// Write date into the buffer object
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < 0) {
console.log('Failed to get the storage location of a_Position');
return -1;
}
// Get the storage location of u_FragColor
var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor');
if (!u_FragColor) {
console.log('Failed to get the storage location of u_FragColor');
return;
}
//Pass color of point to u_FragColor
randColor();
gl.uniform4f(u_FragColor, setR, setG, setB, 1.0);
// Assign the buffer object to a_Position variable
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
// Enable the assignment to a_Position variable
gl.enableVertexAttribArray(a_Position);
return n;
}
<canvas id="webgl" width="400" height="400">
Please use a browser that supports "canvas"
</canvas>
<script src="../lib/webgl-utils.js"></script>
<script src="../lib/webgl-debug.js"></script>
<script src="../lib/cuon-utils.js"></script>
我很确定我应该修改的代码如下:
//Pass color of point to u_FragColor
randColor();
gl.uniform4f(u_FragColor, setR, setG, setB, 1.0);
但是,我不知道如何制作它,以便它为我尝试绘制的每个形状存储一个值。我认为通过让它在每次绘制之前随机改变颜色就可以解决这个问题,但事实并非如此。任何对此的见解将不胜感激。
谢谢。
最佳答案
你尝试了什么?该代码存在很多问题,并且尝试运行它时会出错。
首先 initShaders
返回 gl.program
上的着色器,这毫无意义。 WebGL 应用程序通常有多个着色器程序。相反,您希望 initShaders
返回一个程序,以便您可以执行类似的操作
var program1 = initShaders(gl, vertex1ShaderSource, fragment1ShaderSource);
var program2 = initShaders(gl, vertex2ShaderSource, fragment2ShaderSource);
var program3 = initShaders(gl, vertex3ShaderSource, fragment3ShaderSource);
..etc...
接下来 initVertexBuffers
引用一个名为 program
的变量,但不存在这样的变量。
initVertexBuffers
正在设置制服,但您希望在开始绘制之前设置制服,而不是在初始化顶点时设置制服。
initVertexBuffers
还会查找属性和统一位置并检查错误。一方面,检查这些类型的错误本身并不坏,但它会使您的代码更难以其他方式进行调试。在大多数 WebGL 程序中,如果没有出现错误,但屏幕上没有出现某些内容,首先要做的就是让片段着色器返回纯色
precision mediump float;
uniform vec4 u_FragColor;
void main() {
//gl_FragColor = u_FragColor;
gl_FragColor = vec4(1, 0, 0, 1); // return red
}
当您这样做时,WebGL 将优化未使用的 u_FragColor
,并且检查您是否获取 u_FragColor
位置的代码将失败,从而无法调试着色器。
我建议reading some other tutorials on WebGL .
绘制同一形状的多个副本,过程通常是
初始化时
set up program
look up attribute locations and uniform locations
setup vertices for shape
绘制时
setup attributes for shape
gl.useProgram(program for shape)
for each instance of shape
set uniforms
set a matrix uniform to position and orient the shape
set a color uniform for the color
drawArrays/drawElements
要绘制多个不同的形状,过程通常是
初始化时
set up programs
look up attribute locations and uniform locations
for each shape
setup vertices for shape
绘制时
for each shape
gl.useProgram(program for shape) (if different from last shape)
setup attributes for shape (if different from last shape)
set uniforms
set a matrix uniform to position and orient the shape
set a color uniform for the color
drawArrays/drawElements
对于用于定位和定向形状的矩阵 see this article
关于javascript - WebGL 绘制多个不同颜色的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39758504/
我是 TensorFlow 菜鸟。我已经从 deeppose 的开源实现中训练了一个 TensorFlow 模型,现在必须针对一组新图像运行该模型。 该模型是在大小为 100 * 100 的图像上训练
我正在尝试以这种方式设置节点的大小: controller[shape=circle,width=.5,label="Controller",style=filled,fillcolor="#8EC1
是否有 VBA 代码可以在选择的每个单元格周围添加文本框。文本框应该是单元格的大小(类似于边框)? 最佳答案 您可以使用 .AddTextbox方法。循环遍历您选择的单元格,并使用单元格的尺寸属性来设
我有一个变量 a尺寸 (1, 5) 我想“平铺”的次数与我的小批量的大小一样多。例如,如果小批量大小为 32,那么我想构造一个张量 c维度为 (32, 5),其中每一行的值与原始 (1, 5) 变量
我在使用 javaFX 时遇到问题。我想每 1000 毫秒在应用程序窗口中显示一次时间。 public class Main extends Application { StackPane root
所以我目前正在创建这个 API。这个登录类应该只创建一个场景,其中包含制作 GUI 所需的所有框。我遇到的问题是,单击时我的形状不会执行任何操作。我有事件监听器,但它不起作用。 import
我正在用 python turtle 画一些东西,我使用了形状函数,但是形状 overdraw 了它们之前的其他形状(我可以看到形状在移动),并且我只得到了最后一个形状: `up() goto(-20
我正在读取多个 .csv 文件作为具有相同形状的 panda DataFrame。对于某些索引,某些值为零,因此我想选择具有相同形状的每个索引的值,并为相同的索引放置零值并删除零以成为相同的形状: a
我有一个简单的二维网格,格式为 myGrid[x,y] 我正在尝试找到一种方法来找到围绕选定网格的周长,这样我就有了一个可供选择的形状。 这是我的意思的一个例子: 这里的想法是找到所有相关的“角”,也
我有一个网络层,用于调用多个端点。我想减少重复代码的数量,并认为也许我可以将响应模型作为端点的一部分传递。 这个想法是不需要多个仅因响应而不同的函数,我可以调用我的网络层并根据路径进行设置。 我看到的
我正在创建一个自定义 ImageView,它将我的图像裁剪成六边形并添加边框。我想知道我的方法是否正确,或者我是否以错误的方式这样做。有很多自定义库已经在执行此操作,但开箱即用的库中没有一个具有我正在
我正在编写一些代码,这些代码需要识别一些基于节点云的相当基本的几何图形。我会对检测感兴趣: 板(简单有界平面) 圆柱体(两个节点循环) 半圆柱(圆弧+直线+圆弧+直线) 圆顶(n*loop+top n
我有这个形状: http://screencast.com/t/9UUhAXT5Wu 但边界在截止点处没有跟随它 - 我该如何解决? 这是我当前 View 的代码: self.view.backgro
我现在脑震荡,所以我想问一个非常简单的问题。 目前,我正在尝试打印出这样的开头 当输入为 7 时,输出为 * ** * ** * ** * 这里是我的代码,它打印 14 次而不是 7 次,或者当我输入
我想生成如下设计。计划选项卡顶部的"new"。我使用的属性适用于 chrome 和 mozilla,但在 Edge 中出现故障。 以下是我在 chrome 中应用的样式: a.subnav__item
我想要一个带有两种颜色边框轮廓的 shape 元素。我可以使用 solid 元素做一个单一的颜色轮廓,但这只允许我画一条线。我尝试在我的形状中使用两个 stroke 元素,但这也不起作用。 有没有办法
我需要为屏幕上的形状着色任何我想要的颜色。我目前正在尝试使用 UIImage 来执行此操作,我想根据自己的需要重新着色。据我所知,执行此操作的唯一方法是获取 UIImage 的各个像素,这需要更多我想
因此,经过多年的 OOP,我从我的一门大学类(class)中得到了一个非常简单的家庭作业,以实现一个简单的面向对象的结构。 要求的设计: 实现面向对象的解决方案以创建以下形状: 椭圆、圆形、正方形、矩
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我想知道是否可以使用类似于以下的 div 制作复杂的形状: 它基本上是一个四 Angular 向内收缩的圆 Angular 正方形。目标是使用背景图像来填充它。我可以使用具有以下 SVG 路径的剪辑蒙
我是一名优秀的程序员,十分优秀!