Three.js Example 注解 —— canvas_lights_pointlights.html

本文搬自我的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 - point light</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: #ff0080;
				text-decoration: none;
			}
			a:hover {
				color: #0080ff;
			}
		</style>
	</head>
	<body>
		<div id="container"></div>
		<div id="info">
			<a href="http://threejs.org" target="_blank">three.js</a> - point lights demo.<br />
			Walt Disney head by <a href="http://davidoreilly.com/post/18087489343/disneyhead" target="_blank">David OReilly</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,
			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.set( 0, - 6, 100 );
				//camera.position.y = -6;
				//camera.position.z = 100;

				scene = new THREE.Scene();
				
				/*
				环境光,这是一种基础光源,它的颜色会添加到整个场景和所有对象的当前颜色上 |
					AmbientLight( color, intensity)
					color		光源的颜色
					intensity	光照强度,默认为1
				*/
				scene.add( new THREE.AmbientLight( 0x00020 ) );

				/*
				点光源,空间中的一点,朝所有方向发射光线 
				PointLight( color, intensity, distance, decay );
					color:光的颜色
					intensity:光照强度,默认为1
					distance:光最长能照射的距离,默认为0
					decay:光的衰减系数,默认为1
				*/
				light1 = new THREE.PointLight( 0xff0040, 1, 50 );
				scene.add( light1 );

				light2 = new THREE.PointLight( 0x0040ff, 1, 50 );
				scene.add( light2 );

				light3 = new THREE.PointLight( 0x80ff80, 1, 50 );
				scene.add( light3 );

				var PI2 = Math.PI * 2;
				var program = function ( context ) {
					context.beginPath();
					//填充一个半径为0.5的圆
					context.arc( 0, 0, 0.5, 0, PI2, true );
					context.fill();
				};

				/*
				雪碧图材质,也叫精灵图

				CSS Sprites其实就是把网页中一些背景图片整合到一张图片文件中,再利用CSS的“background-image”,“background- repeat”,“background-position”的组合进行背景定位,background-position可以用数字精确的定位出背景图片的位置。

				在Three.js中意思有点稍微不一样
				A sprite is a plane that always faces towards the camera, generally with a partially transparent texture applied.
				Sprites do not cast shadows, setting castShadow = true will have no effect. 
				一个精灵也就是一个平面,它的面总是朝向相机,通常来说会采用一个部分透明的纹理
				精灵不能投影出阴影,设置 castShadow = true 没有任何效果

				SpriteCanvasMaterial: 
				Create a material that can draw custom sprites using a 2d canvas.
				创建一个使用2d画布绘制的自定义精灵图材质
				
				材质color会在program之前被设置到context.fillStyle上。因此如果内部还有对fillStyle的设置,将会直接覆盖掉外部的颜色配置
				*/
				var sprite = new THREE.Sprite( new THREE.SpriteCanvasMaterial( { color: 0xff0040, program: program } ) );
				light1.add( sprite );

				var sprite = new THREE.Sprite( new THREE.SpriteCanvasMaterial( { color: 0x0040ff, program: program } ) );
				light2.add( sprite );

				var sprite = new THREE.Sprite( new THREE.SpriteCanvasMaterial( { color: 0x80ff80, program: program } ) );
				light3.add( sprite );

				loader = new THREE.JSONLoader();
				loader.load( 'obj/WaltHeadLo.js', function ( geometry ) {
					/*
					MeshLambertMaterial,这种材质会考虑光照的影响,可以用来创建颜色暗淡的、不光亮的物体
					color: 即diffuse,漫射颜色,默认为0xffffff,白色
					ambient: 环境色,默认为0xffffff, 白色, 乘以环境光得到对象的颜色
					emissive: 自发光(荧光)颜色,默认为0x000000,黑色,实体颜色,不受其他灯光的影响.
					overdraw: 过渡描绘。如果用THREE.CanvasRenderer对象,有缝隙时需设置该值。例如当前如果使用0.5以下的值,三角形的分界线就很明显。但是使用WebGLRenderer则不会有分割线
					morphTargets: 表示是否启用变形
					*/
					mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0xffffff, overdraw: 0.5 } ) );
					scene.add( mesh );
				} );

				renderer = new THREE.CanvasRenderer();
				//设置屏幕像素比,与Android上的DIP相仿,作用是在所有设备上的显示效果都相近
				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;

				//绕着Y轴按右手螺旋定则进行旋转
				if ( mesh ) mesh.rotation.y -= 0.01;

				light1.position.x = Math.sin( time * 0.7 ) * 30;
				light1.position.y = Math.cos( time * 0.5 ) * 40;
				light1.position.z = Math.cos( time * 0.3 ) * 30;

				light2.position.x = Math.cos( time * 0.3 ) * 30;
				light2.position.y = Math.sin( time * 0.5 ) * 40;
				light2.position.z = Math.sin( time * 0.7 ) * 30;

				light3.position.x = Math.sin( time * 0.7 ) * 30;
				light3.position.y = Math.cos( time * 0.3 ) * 40;
				light3.position.z = Math.sin( time * 0.5 ) * 30;

				renderer.render( scene, camera );
			}
		</script>
	</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值