通过对three官网案例标注学习three1

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<title>three.js css2d - label</title>
		<link type="text/css" rel="stylesheet" href="main.css">
		<style>
			.label {
				color: #FFF;
				font-family: sans-serif;
				padding: 2px;
				background: rgba( 0, 0, 0, .6 );
			}
		</style>
	</head>
	<body>
		<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css2d - label</div>

		<!-- Import maps polyfill -->
		<!-- Remove this when import maps will be widely supported -->
		<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>

		<script type="importmap">
			{
				"imports": {
					"three": "../js/three.module.js",
					"three/addons/": "./js/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';
            // 相机控件轨道控制器
			import { OrbitControls } from './js/OrbitControls.js';
            // 一个2D的render,可以在页面中渲染出一个div标签名称,旋转的时候文字依然能够保持直立和水平
			import { CSS2DRenderer, CSS2DObject } from './js/CSS2DRenderer.js';
            // 创建控制三维场景的UI界面
			import { GUI } from './js/lil-gui.module.min.js';

			let gui;
			let camera, scene, renderer, labelRenderer;

            // 只有物体的layer在相机的layers中three才渲染
            const layers = {
                /*
                    默认创建物体layer是0
                    如指定物体的layer为1不会渲染
                    需要渲染使用相机的​​​enable​​方法允许渲染

                    常用方法
                    ​​toggle​​方法 切换是否允许渲染状态
                    ​​enable​​方法 允许渲染某图层
                    ​​disableAll​​方法 关闭全部图层 - 黑屏
                    ​​disable​​方法 关闭指定图层 
                */
				'Toggle Name': function () {
					camera.layers.toggle( 0 );
				},
				'Toggle Mass': function () {
					camera.layers.toggle( 1 );
				},
				'Enable All': function () {
					camera.layers.enableAll();
				},
				'Disable All': function () {
					camera.layers.disableAll();
				}

			};
            // 控制三维场景的UI界面
			function initGui() {
				gui = new GUI();

				gui.title( 'Camera Layers' );
                // .add(控制对象,对象具体属性,其他参数)
				gui.add( layers, 'Toggle Name' );
				gui.add( layers, 'Toggle Mass' );
				gui.add( layers, 'Enable All' );
				gui.add( layers, 'Disable All' );

				gui.open();
			}

            // 通过 Threejs 编写一些和时间相关程序时候,不用在对 Date 进行封装,直接调用 Clock 对象的方法和属性即可
			const clock = new THREE.Clock();
            // 纹理加载器
			const textureLoader = new THREE.TextureLoader();

			let moon;

			init();
			animate();

			function init() {

				const EARTH_RADIUS = 1;
				const MOON_RADIUS = 0.27;
                // 相机
				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
				camera.position.set( 10, 5, 20 );
				camera.layers.enableAll();
                // 场景
				scene = new THREE.Scene();
                // 灯光
				const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
				dirLight.position.set( 0, 0, 1 );
				dirLight.layers.enableAll();
				scene.add( dirLight );
                // 坐标轴辅助器
				const axesHelper = new THREE.AxesHelper( 5 );
				axesHelper.layers.enableAll();
				scene.add( axesHelper );
                // 物体--地球
				const earthGeometry = new THREE.SphereGeometry( EARTH_RADIUS, 16, 16 );
                /* 
                    通过设置高光贴图来实现部分区域反光。
                    specular属性可以用来决定反光的颜色。

                    使用法向贴图创建表面不同凹凸程度的三维物体
                    
                */
				const earthMaterial = new THREE.MeshPhongMaterial( {
					specular: 0x333333,//添加高光颜色
					// specular: new THREE.Color(0x00ffff),//添加高光颜色
					shininess: 5,//添加高光的平滑度,默认为30,值越高越强烈
					map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),//添加平面贴图
					specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),//添加高光贴图
					normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),//添加法向量纹理
					normalScale: new THREE.Vector2( 0.85, 0.85 )//设置法向贴图的凹凸程度
				} );
                // sRGB颜色空间: sRGB是当今一般电子设备及互联网图像上的标准颜色空间。较适应人眼的感光。
                /* 
                    threejs 需要在线性颜色空间(linear colorspace)里渲染模型的材质,
                    而从一般软件中导出的模型中包含颜色信息的贴图一般都是sRGB颜色空间(sRGB colorspace)
                */
				earthMaterial.map.colorSpace = THREE.SRGBColorSpace;
				const earth = new THREE.Mesh( earthGeometry, earthMaterial );
				scene.add( earth );

                // 物体--月球
				const moonGeometry = new THREE.SphereGeometry( MOON_RADIUS, 16, 16 );
				const moonMaterial = new THREE.MeshPhongMaterial( {
					shininess: 5,
					map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
				} );
				moonMaterial.map.colorSpace = THREE.SRGBColorSpace;
				moon = new THREE.Mesh( moonGeometry, moonMaterial );
				scene.add( moon );
                
				earth.layers.enableAll();
				moon.layers.enableAll();
                // 标签--地球
				const earthDiv = document.createElement( 'div' );
				earthDiv.className = 'label';
				earthDiv.textContent = 'Earth';
				earthDiv.style.backgroundColor = 'transparent';

				const earthLabel = new CSS2DObject( earthDiv );
				earthLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
				earthLabel.center.set( 0, 1 );
				earth.add( earthLabel );
				earthLabel.layers.set( 0 );

                //标签--地球重量
				const earthMassDiv = document.createElement( 'div' );
				earthMassDiv.className = 'label';
				earthMassDiv.textContent = '5.97237e24 kg';
				earthMassDiv.style.backgroundColor = 'transparent';

				const earthMassLabel = new CSS2DObject( earthMassDiv );
				earthMassLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
				earthMassLabel.center.set( 0, 0 );
				earth.add( earthMassLabel );
				earthMassLabel.layers.set( 1 );

                // 标签--月球
				const moonDiv = document.createElement( 'div' );
				moonDiv.className = 'label';
				moonDiv.textContent = 'Moon';
				moonDiv.style.backgroundColor = 'transparent';

				const moonLabel = new CSS2DObject( moonDiv );
				moonLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
				moonLabel.center.set( 0, 1 );
				moon.add( moonLabel );
				moonLabel.layers.set( 0 );

                // 标签--月球重量
				const moonMassDiv = document.createElement( 'div' );
				moonMassDiv.className = 'label';
				moonMassDiv.textContent = '7.342e22 kg';
				moonMassDiv.style.backgroundColor = 'transparent';

				const moonMassLabel = new CSS2DObject( moonMassDiv );
				moonMassLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
				moonMassLabel.center.set( 0, 0 );
				moon.add( moonMassLabel );
				moonMassLabel.layers.set( 1 );

				//渲染器
				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );//设置设备像素比
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.useLegacyLights = false;//暂不清楚,好像是渲染管线
				document.body.appendChild( renderer.domElement );

				//标签渲染器
				labelRenderer = new CSS2DRenderer();
				labelRenderer.setSize( window.innerWidth, window.innerHeight );
				labelRenderer.domElement.style.position = 'absolute';
				labelRenderer.domElement.style.top = '0px';
				document.body.appendChild( labelRenderer.domElement );

                // 相机控件轨道控制器
				const controls = new OrbitControls( camera, labelRenderer.domElement );
				controls.minDistance = 5;
				controls.maxDistance = 100;

				//窗口大小改变
				window.addEventListener( 'resize', onWindowResize );

				initGui();

			}

			function onWindowResize() {
                // .aspect摄像机视锥体的长宽比,通常是使用画布的宽/画布的高。默认值是1(正方形画布)
				camera.aspect = window.innerWidth / window.innerHeight;
                // .updateProjectionMatrix更新摄像机投影矩阵。在任何参数被改变以后必须被调用。
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );
				labelRenderer.setSize( window.innerWidth, window.innerHeight );

			}


			function animate() {
                // 请求动画帧
				requestAnimationFrame( animate );
                // 方法.getElapsedTime(),获取自时钟启动后的秒数,
				const elapsed = clock.getElapsedTime();

				moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );

				renderer.render( scene, camera );
				labelRenderer.render( scene, camera );
			}


		</script>
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值