本文搬自我的Github,https://github.com/555chy/three.js-example-comment,有兴趣的可以一起来完善,这个为Three.js的Example进行注解,方便初学者阅读
three.js 官网 Example 地址:https://threejs.org/examples/
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js canvas - spherical reflection</title>
<meta charset="utf-8">
<!--
如果没有设置viewport的width的话,网页很可能会超出手机屏幕宽度,具体多宽,要看浏览器定义的默认宽度是多少
user-scalable=no,规定了用户不能缩放网页,但有些浏览器对该项支持不是很好,故需要设置minimum-scale和maximum-scale相同来限制用户缩放
-->
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
color: #ffffff;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: center;
}
a {
color: #ffffff;
}
a:hover {
color: #0080ff;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="http://threejs.org" target="_blank">three.js</a> - spherical reflection demo.<br />
Walt Disney head by <a href="http://davidoreilly.com/post/18087489343/disneyhead" target="_blank">David OReilly</a>. Reflection texture by <a href="http://kewlers.scene.org/" target="_blank">Kewlers</a>.
</div>
<script src="../build/three.js"></script>
<!--
想要使用CanvasRenderer,必须添加如下两个js文件
Projector.js顾名思义上将3d图像投影到Canvas("2d")上,如果没有该文件会报如下错误
THREE.Projector has been moved to /examples/js/renderers/Projector.js. three.js:42883:3
TypeError: THREE.RenderableVertex is not a constructor
-->
<script src="js/renderers/Projector.js"></script>
<script src="js/renderers/CanvasRenderer.js"></script>
<script>
var camera, scene, renderer,
particle1, particle2, particle2,
light1, light2, light3,
loader, mesh;
init();
animate();
function init() {
var container = document.getElementById( 'container' );
/*
透视相机
PerspectiveCamera(fov, aspect, near, far)
fov(视场):从相机位置能够看到的部分场景。推荐默认值45
aspect(长宽比):渲染结果输出区域的横向长度和纵向长度的比值。推荐默认值window.innerWidth/window.innerHeight
near(近面):定义从距离相机多近的地方开始渲染场景。推荐默认值0.1
far(远面):定义相机可以从它所处的位置看多远。默认值1000
*/
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
//定义相机的位置,有如下两种方式。如果不设置的话,相机位置为默认的Vector3{x:0,y:0,z:0}
//camera.position.y = -6;
//camera.position.z = 100;
camera.position.set( 0, - 6, 100 );
scene = new THREE.Scene();
loader = new THREE.JSONLoader();
loader.load( 'obj/WaltHeadLo.js', function ( geometry ) {
//计算geometry.faces[n].vertexNormals[3],即点法向量,用以后续的反射(注意,它和面法向量faces[n].normal不一样)
geometry.computeVertexNormals();
var envMap = new THREE.TextureLoader().load( 'textures/metal.jpg' );
/*
exports.UVMapping = UVMapping; 平展-映射
exports.CubeReflectionMapping = CubeReflectionMapping; 立方体反射-映射
exports.CubeRefractionMapping = CubeRefractionMapping; 立方体折射-映射
exports.EquirectangularReflectionMapping = EquirectangularReflectionMapping; 矩形球面反射-映射
exports.EquirectangularRefractionMapping = EquirectangularRefractionMapping; 矩形球面折射-映射
exports.SphericalReflectionMapping = SphericalReflectionMapping; 球面反射-映射
exports.CubeUVReflectionMapping = CubeUVReflectionMapping; 立方体纹理反射-映射
exports.CubeUVRefractionMapping = CubeUVRefractionMapping; 立方体纹理折射-映射
*/
envMap.mapping = THREE.SphericalReflectionMapping;
/*
MeshBasicMaterial:与光照无关,仅根据材质的颜色或贴图来渲染物体
color:材质的颜色
map:材质的贴图
wireframe: 显示三角形线框还是显示面
side:可选的值有THREE.FrontSide(仅渲染正面)、THREE.BackSide(仅渲染背面)、THREE.DoubleSide(双面渲染)
overdraw: 过渡描绘。如果用THREE.CanvasRenderer对象,有缝隙时需设置该值。例如当前如果使用0.5以下的值,三角形的分界线就很明显。但是使用WebGLRenderer则不会有分割线
visible: 对象是否可见
*/
mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { envMap: envMap, overdraw: 0.5 } ) );
scene.add( mesh );
} );
//renderer = new THREE.WebGLRenderer();
renderer = new THREE.CanvasRenderer();
//window.devicePixelRatio是设备上物理像素和设备独立像素(device-independent pixels (dips))的比例。
renderer.setPixelRatio( window.devicePixelRatio );
//设置渲染场景的大小
renderer.setSize( window.innerWidth, window.innerHeight );
//将渲染器的DOM元素(即Canvas)添加到HTML中
container.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
//重新设置相机的宽高比。如果宽高比不对,那么正方形可能就不是正方形了
camera.aspect = window.innerWidth / window.innerHeight;
//更新透视相机的投影矩阵
camera.updateProjectionMatrix();
//设置待渲染场景的大小
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
//Date.now()得到的是当前时间戳,单位毫秒
var time = Date.now() * 0.0005;
if ( mesh ) mesh.rotation.y -= 0.01;
renderer.render( scene, camera );
}
</script>
</body>
</html>