1.webgl的平行光漫反射示例
在现实生活中,我们看到物体是什么颜色,都是要通过光的反射进入我们眼睛我们才能看到物体是什么颜色。
所以在webgl中我们要感受到光照明暗差异的效果,最终我们是通过改变每个顶点的颜色来实现的。
2.反射光的颜色
物体朝哪个方向反射光,反射光的颜色是什么?取决与:入射光的颜色, 入射光的方向, 物体的本身的颜色,物体反射特性,共四个参数来决定。
而反射类型又分为: 漫反射,环境反射。
漫反射光的颜色 = 物体表面基底色 * 入射光的颜色 * cosθ
好了现在我们知道漫反射光的颜色公式,所以我们要知道 cosθ是多少,因为物体表面基底色 ,入射光的颜色都是已知的。
2.1根据光线和表面的方向计算入射角
这里我们需要知道向量点积
向量点乘积有两种计算方式
第一种:根据两个向量的摸(长度),两个向量的夹角
第二种:是根据两个向量的坐标来计算
所以根据上面两个公式计算最终cosθ
cosθ = 入射光向量 * 法线向量
前提是入射光向量,法线向量的摸一定要是1就是一定要做归一化处理。要不然分母就不再是1了,公式就不成立了。
2.2法向量
简单理解就是垂直于物体表面的向量
3.详细示例代码
const VSHADER_SOURCE =
'attribute vec4 a_Position;\n' + // 顶点坐标
'attribute vec4 a_Color;\n' + // 颜色值 物体表面基底色
'varying vec4 c_Color;\n' + // 这个是用来传递给片源着色器的变量
'uniform mat4 u_ViewProjMatrix;\n' + // 视图透视投影矩阵
'uniform vec3 u_LightColor;\n' + // 光线的颜色
'uniform vec3 u_LightDirection;\n' + // 光线的向量
'attribute vec4 a_Normal;\n' + // 顶点法向量
' void main(){\n' +
' gl_Position = u_ViewProjMatrix * a_Position;\n' +
' vec3 normal = normalize(a_Normal.xyz);\n' +
' float nDotl = max(dot(u_LightDirection, normal), 0.0);\n' +
' vec3 diffuse = u_LightColor * a_Color.rgb* nDotl;\n' +
' c_Color = vec4(diffuse, a_Color);\n' +
'}\n';
const FSHADER_SOURCE =
'#ifdef GL_ES\n' +
'precision mediump float;\n' +
'#endif\n' +
'varying vec4 c_Color;\n' +
'void main(){\n' +
' gl_FragColor = c_Color;\n' +
'}\n';
从上面的着色器代码,我们可以看出它对顶点的操作很简单,只有
透视视图投影矩阵
'uniform mat4 u_ViewProjMatrix;\n' + // 视图透视投影矩阵
接受js传递的顶点数据
'attribute vec4 a_Position;\n' + // 顶点坐标
赋值操作
gl_Position = u_ViewProjMatrix * a_Position;
剩下的操作都是来计算漫反射光的颜色
1.先对顶点坐标的法向量进行归一化处理一定要进行归一化要不公式不成立
' vec3 normal = normalize(a_Normal.xyz);\n' +
计算入射角 与 法向量的夹角
'float nDotl = max(dot(u_LightDirection, normal), 0.0);\n' +
dot()就是两个向量点积 (u_LightDirection * normal)
赋值给c_Color变量
v_Color = vec4(diffuse, a_Color)
然后在片源着色器中,就是v_Color变量,并且赋值给gl_FragColor(内置变量)
gl_FragColor = v_Color
详细代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- <canvas id="canvas" width="400" height="400"></canvas> -->
<canvas id="canvas" width="400" height="400"></canvas>
<script src="../三维/lib/cuon-matrix.js"></script>
<script>
const gl = canvas.getContext('webgl');
// 顶点着色器
const VSHADER_SOURCE =
'attribute vec4 a_Position;\n' + // 顶点坐标
'attribute vec4 a_Color;\n' + // 颜色值
'varying vec4 c_Color;\n' +
'uniform mat4 u_ViewProjMatrix;\n' + // 视图透视投影矩阵
'uniform vec3 u_LightColor;\n' + // 光线的颜色
'uniform vec3 u_LightDirection;\n' + // 光线的向量
'attribute vec4 a_Normal;\n' + // 顶点法向量
' void main(){\n' +
' gl_Position = u_ViewProjMatrix * a_Position;\n' +
' vec3 normal = normalize(a_Normal.xyz);\n' +
' float nDotl = max(dot(u_LightDirection, normal), 0.0);\n' +
' vec3 diffuse = u_LightColor * a_Color.rgb* nDotl;\n' +
' c_Color = vec4(diffuse, a_Color);\n' +
'}\n';
const FSHADER_SOURCE =
'#ifdef GL_ES\n' +
'precision mediump float;\n' +
'#endif\n' +
'varying vec4 c_Color;\n' +
'void main(){\n' +
' gl_FragColor = c_Color;\n' +
'}\n';
const createShader = (type, source) => {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
const compileState = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!compileState) {
const info = gl.getShaderInfoLog(shader);
console.log(`编译报错信息为:${info}`)
gl.deleteShader(shader);
return null
}
return shader
}
// createShader(gl.FRAGMENT_SHADER,FSHADER_SOURCE)
const createProgram = (vshader, fshader) => {
const vertexShader = createShader(gl.VERTEX_SHADER, vshader);
const fragementShader = createShader(gl.FRAGMENT_SHADER, fshader);
if (!fragementShader || !vertexShader) {
console.log('初始着色器失败')
return false
}
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragementShader);
gl.linkProgram(program);
const linkState = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!linkState) {
const err = gl.getProgramInfoLog(program);
console.log(`链接报错信息为:${err}`);
gl.deleteShader(vertexShader);
gl.deleteShader(fragementShader);
gl.deleteProgram(program);
return null
}
return program;
}
const program = createProgram(VSHADER_SOURCE, FSHADER_SOURCE);
// gl.useProgram(program);
gl.program = program;
// console.log(program, 'programprogram')
gl.useProgram(program);
const createBuffers = () => {
// const vertices = new Float32Array([
// 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, // v0-v1-v2-v3 前面
// 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, // v0-v3-v4-v5 右边
// 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, // v0-v5-v6-v1 上
// -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, // v1-v6-v7-v2 左边
// 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, // v7-v4-v3-v2 下面
// 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0 // v4-v7-v6-v5 后面
// ])
var vertices = new Float32Array([ // Coordinates
1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, // v0-v1-v2-v3 front
1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, // v0-v3-v4-v5 right
1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, // v0-v5-v6-v1 up
-1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, // v1-v6-v7-v2 left
-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, // v7-v4-v3-v2 down
1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0 // v4-v7-v6-v5 back
]); // 顶点坐标
// 顶点颜色
// const colors = new Float32Array([
// 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
// 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
// 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
// 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
// 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
// 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
// ])
var colors = new Float32Array([ // Colors
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v1-v2-v3 front
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4-v5 right
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v5-v6-v1 up
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v1-v6-v7-v2 left
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v7-v4-v3-v2 down
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0 // v4-v7-v6-v5 back
]); // 顶点颜色
// const normals = new Float32Array([
// 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
// 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
// 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
// -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0,
// 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0,
// 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0
// ])
var normals = new Float32Array([ // Normal
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, // v0-v1-v2-v3 front
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, // v0-v3-v4-v5 right
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, // v0-v5-v6-v1 up
-1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, // v1-v6-v7-v2 left
0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, // v7-v4-v3-v2 down
0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0 // v4-v7-v6-v5 back
]); // 顶点法向量
initArrayBuffer('a_Position', vertices, 3, gl.FLOAT);
initArrayBuffer('a_Color', colors, 3, gl.FLOAT);
initArrayBuffer('a_Normal', normals, 3, gl.FLOAT);
// const indices = new Uint8Array([
// 0, 1, 2, 0, 2, 3, // 前面
// 4, 5, 6, 4, 6, 7, // 右面
// 8, 9, 10, 8, 10, 11, // 上面
// 12, 13, 14, 12, 14, 15, // 左面
// 16, 17, 18, 16, 18, 19, // 下面
// 20, 21, 22, 20, 22, 23, // 后面
// ])
var indices = new Uint8Array([
0, 1, 2, 0, 2, 3, // front
4, 5, 6, 4, 6, 7, // right
8, 9, 10, 8, 10, 11, // up
12, 13, 14, 12, 14, 15, // left
16, 17, 18, 16, 18, 19, // down
20, 21, 22, 20, 22, 23 // back
]); // 顶点索引
const bufferIndex = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferIndex)
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
return indices.length;
}
const initArrayBuffer = (attribute, data, num, type) => {
// console.log(attribute, 'attribute')
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
// const a_attribute = gl.getAttribLocation(gl.program, attribute);
var a_attribute = gl.getAttribLocation(gl.program, attribute);
if (a_attribute < 0) {
console.log('Failed to get the storage location of ' + attribute);
return false;
}
gl.vertexAttribPointer(a_attribute, num, type, false, 0, 0);
gl.enableVertexAttribArray(a_attribute);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
const n = createBuffers();
var u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_ViewProjMatrix');
console.log(u_MvpMatrix, 'u_MvpMatrix')
var u_LightColor = gl.getUniformLocation(gl.program, 'u_LightColor');
var u_LightDirection = gl.getUniformLocation(gl.program, 'u_LightDirection');
gl.uniform3f(u_LightColor, 5.0, 5.0, 5.0);
// console.log(Matrix4, 'Matrix4')
const mvpMatrix = new Matrix4();
// mvpMatrix.setPerspective(30, canvas.with / canvas.height, 0.0, 100);
mvpMatrix.setPerspective(30, canvas.width / canvas.height, 1, 100);
mvpMatrix.lookAt(3, 3, 7, 0, 0, 0, 0, 1, 0);
// gl.uniform3f(u_MvpMatrix, false, mvpMatrix.elements);
gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);
var lightDirection = new Vector3([0.5, 3.0, 4.0]);
lightDirection.normalize();
gl.uniform3fv(u_LightDirection, lightDirection.elements);
gl.clearColor(0, 0, 0, 1);
gl.enable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
console.log(n, 'n')
gl.drawElements(gl.TRIANGLES, n, gl.UNSIGNED_BYTE, 0);
</script>
</body>
</html>
效果图
是不是有种明暗的效果。