博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebGL笔记(六):简单灯光
阅读量:4495 次
发布时间:2019-06-08

本文共 3024 字,大约阅读时间需要 10 分钟。

按MDN的顺序,这篇应该是讲贴图的。但贴图有两个问题不好处理:

1、贴图来源跨域。这种约束的感觉和AJAX差不多,而且MDN也有解释,我想根据经验应该很容易理解;

2、图片格式。这个交待得比较含糊,仅仅有一个提示:

Note: Textures' widths and heights must be a power of two number of pixels (that is, 1, 2, 4, 8, 16, etc).

其实图片格式的限制远不止这么蛋疼。

相比之下灯光问题更容易解决,所以打算先说这个。

首先,使用iWebGL和ArrayHelper/GroupHelper等这几个库,快速创建一个白色的立方体:

 
 
 
WebGL Step 03
canvas{ background-color:#666; }
 
 
varying highp vec2 vTextureCoord;
varying highp vec3 vLighting;
varying lowp vec4 vColor;
uniform sampler2D uSampler;
void main(void) {
highp vec4 texelColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
gl_FragColor = vColor;
}
 
 
attribute highp vec3 aVertexNormal;
attribute highp vec3 aVertexPosition;
attribute highp vec2 aTextureCoord;
attribute highp vec4 aVertexColor;
 
uniform highp mat4 uNormalMatrix;
uniform highp mat4 uMVMatrix;
uniform highp mat4 uPMatrix;
 
varying highp vec2 vTextureCoord;
varying highp vec3 vLighting;
varying lowp vec4 vColor;
 
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
highp vec3 ambientLight = vec3(0.6, 0.6, 0.6);
highp vec3 directionalLightColor = vec3(0.5, 0.5, 0.75);
highp vec3 directionalVector = vec3(0.85, 0.8, 0.75);
highp vec4 transformedNormal = uNormalMatrix * vec4(aVertexNormal, 1.0);
highp float directional = max(dot(transformedNormal.xyz, directionalVector), 0.0);
vLighting = ambientLight + (directionalLightColor * directional);
vColor = aVertexColor;
}
 
 
 
 
 
 
 
看来您的浏览器不支持
<canvas>标记
 
//iWebGL对象初始化
var igl = new iWebGL('glcanvas', 0);
//顶点管理对象
var vg = CubeVertices();
//颜色管理对象
var cg = CubeColors();
//设置六个面的颜色
cg.Front('f');
cg.Back('f');
cg.Left('f');
cg.Right('f');
cg.Top('f');
cg.Bottom('f');
 
//顶点位置数据
igl.paramVertices('aVertexPosition').define(vg.data);
//顶点着色数据
igl.paramColors('aVertexColor').define(cg.data);
//面三角形顶点索引顺序
igl.paramVerticesIndex().define(vg.indices);
//顶点光照数据
 
//设置场景
igl.matrix.trans([0.0, 0.0, -6.0]);
igl.matrix.make(40, 640 / 480, 0.1, 100.0);
 
//动画函数
var animate = function(){
igl.matrix.rotate(1, [1, 0, 1]);
igl.drawCube();
}
 
//转吧
setInterval(animate, 40);
 
 
 
 

运行效果如下:

 

下面我们修改一下FragmentShader脚本中的main函数:

void main(void) {
highp vec4 texelColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
gl_FragColor = vec4(vColor.a * vLighting, vColor.a);
}

猜测一下第三行:计算vColor的Alpha通道值与灯光值vLighting的乘积,并重新赋值给vColor的Alpha属性。这句算法是关键。

下面定义顶点光源数据并传递给Shader:

//顶点光源数据
igl.paramVertices('aVertexNormal').define([
// Front
0.0,  0.0,  1.0,
0.0,  0.0,  1.0,
0.0,  0.0,  1.0,
0.0,  0.0,  1.0,
 
// Back
0.0,  0.0, -1.0,
0.0,  0.0, -1.0,
0.0,  0.0, -1.0,
0.0,  0.0, -1.0,
 
// Top
0.0,  1.0,  0.0,
0.0,  1.0,  0.0,
0.0,  1.0,  0.0,
0.0,  1.0,  0.0,
 
// Bottom
0.0, -1.0,  0.0,
0.0, -1.0,  0.0,
0.0, -1.0,  0.0,
0.0, -1.0,  0.0,
 
// Right
1.0,  0.0,  0.0,
1.0,  0.0,  0.0,
1.0,  0.0,  0.0,
1.0,  0.0,  0.0,
 
// Left
-1.0,  0.0,  0.0,
-1.0,  0.0,  0.0,
-1.0,  0.0,  0.0,
-1.0,  0.0,  0.0
]);

 

见下面的效果

 

完整示例:

 

下篇讲贴图

转载于:https://www.cnblogs.com/muse/archive/2011/12/04/2276250.html

你可能感兴趣的文章
配置带用户权限的docker registry v2
查看>>
springboot入门
查看>>
MATLAB的符号运算基础
查看>>
继续截取长文本显示省略号(多行)
查看>>
python字符串连接的N种方式
查看>>
android脚步---简单图片浏览器改变图像透明度
查看>>
mysql中insert into select from的使用
查看>>
【Luogu】P2536病毒检测(Trie上DP)
查看>>
上传文件
查看>>
typeof
查看>>
@Mapper 和 @MapperScan 区别
查看>>
Unity笔记(3)自学第二天
查看>>
[NOIP2013] 华容道
查看>>
(转)java并发编程--Executor框架
查看>>
算法竞赛入门经典 2.2 循环结构程序设计
查看>>
sql server 2000/2005 判断存储过程、触发器、视图是否存在并删除
查看>>
mysql 隔离级别 脏读 测试
查看>>
Datagridview获取列名为“”的值
查看>>
Python 爬虫的工具列表 附Github代码下载链接
查看>>
IE6/7中li浮动外边距无法撑开ul的解决方法
查看>>