You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
6.6 KiB

2 years ago
<!DOCTYPE html>
<html>
2 years ago
2 years ago
<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;
2 years ago
background: rgba(0, 0, 0, .6);
2 years ago
}
2 years ago
2 years ago
</style>
</head>
2 years ago
2 years ago
<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.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
let gui;
let camera, scene, renderer, labelRenderer;
const layers = {
'Toggle Name': function () {
2 years ago
camera.layers.toggle(0);
2 years ago
},
'Toggle Mass': function () {
2 years ago
camera.layers.toggle(1);
2 years ago
},
'Enable All': function () {
camera.layers.enableAll();
},
'Disable All': function () {
camera.layers.disableAll();
}
};
const clock = new THREE.Clock();
const textureLoader = new THREE.TextureLoader();
let moon;
2 years ago
let moonLabel;
let i = 0;
2 years ago
init();
animate();
function init() {
const EARTH_RADIUS = 1;
const MOON_RADIUS = 0.27;
2 years ago
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(10, 5, 20);
2 years ago
camera.layers.enableAll();
2 years ago
camera.layers.toggle(1);
2 years ago
scene = new THREE.Scene();
2 years ago
const dirLight = new THREE.DirectionalLight(0xffffff);
dirLight.position.set(0, 0, 1);
2 years ago
dirLight.layers.enableAll();
2 years ago
scene.add(dirLight);
2 years ago
2 years ago
const axesHelper = new THREE.AxesHelper(5);
2 years ago
axesHelper.layers.enableAll();
2 years ago
scene.add(axesHelper);
2 years ago
//
2 years ago
const earthGeometry = new THREE.SphereGeometry(EARTH_RADIUS, 16, 16);
const earthMaterial = new THREE.MeshPhongMaterial({
2 years ago
specular: 0x333333,
shininess: 5,
2 years ago
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)
});
const earth = new THREE.Mesh(earthGeometry, earthMaterial);
scene.add(earth);
const moonGeometry = new THREE.SphereGeometry(MOON_RADIUS, 16, 16);
const moonMaterial = new THREE.MeshPhongMaterial({
2 years ago
shininess: 5,
2 years ago
map: textureLoader.load('textures/planets/moon_1024.jpg')
});
moon = new THREE.Mesh(moonGeometry, moonMaterial);
scene.add(moon);
2 years ago
//
earth.layers.enableAll();
moon.layers.enableAll();
2 years ago
const earthDiv = document.createElement('div');
2 years ago
earthDiv.className = 'label';
earthDiv.textContent = 'Earth';
earthDiv.style.marginTop = '-1em';
2 years ago
const earthLabel = new CSS2DObject(earthDiv);
earthLabel.position.set(0, EARTH_RADIUS, 0);
earth.add(earthLabel);
earthLabel.layers.set(0);
2 years ago
2 years ago
const earthMassDiv = document.createElement('div');
2 years ago
earthMassDiv.className = 'label';
earthMassDiv.textContent = '5.97237e24 kg';
earthMassDiv.style.marginTop = '-1em';
2 years ago
const earthMassLabel = new CSS2DObject(earthMassDiv);
earthMassLabel.position.set(0, - 2 * EARTH_RADIUS, 0);
earth.add(earthMassLabel);
earthMassLabel.layers.set(1);
2 years ago
2 years ago
const moonDiv = document.createElement('div');
2 years ago
moonDiv.className = 'label';
moonDiv.textContent = 'Moon';
moonDiv.style.marginTop = '-1em';
2 years ago
moonLabel = new CSS2DObject(moonDiv);
moonLabel.position.set(0, MOON_RADIUS, 0);
moon.add(moonLabel);
moonLabel.layers.set(0);
2 years ago
2 years ago
const moonMassDiv = document.createElement('div');
2 years ago
moonMassDiv.className = 'label';
moonMassDiv.textContent = '7.342e22 kg';
moonMassDiv.style.marginTop = '-1em';
2 years ago
const moonMassLabel = new CSS2DObject(moonMassDiv);
moonMassLabel.position.set(0, - 2 * MOON_RADIUS, 0);
moon.add(moonMassLabel);
moonMassLabel.layers.set(1);
2 years ago
//
renderer = new THREE.WebGLRenderer();
2 years ago
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
2 years ago
labelRenderer = new CSS2DRenderer();
2 years ago
labelRenderer.setSize(window.innerWidth, window.innerHeight);
2 years ago
labelRenderer.domElement.style.position = 'absolute';
labelRenderer.domElement.style.top = '0px';
2 years ago
document.body.appendChild(labelRenderer.domElement);
2 years ago
2 years ago
const controls = new OrbitControls(camera, labelRenderer.domElement);
2 years ago
controls.minDistance = 5;
controls.maxDistance = 100;
2 years ago
let newlable= AddCSSLabel();
scene.add(newlable);
2 years ago
2 years ago
window.addEventListener('resize', onWindowResize);
2 years ago
initGui();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
2 years ago
renderer.setSize(window.innerWidth, window.innerHeight);
2 years ago
2 years ago
labelRenderer.setSize(window.innerWidth, window.innerHeight);
2 years ago
}
function animate() {
2 years ago
requestAnimationFrame(animate);
2 years ago
const elapsed = clock.getElapsedTime();
2 years ago
moon.position.set(Math.sin(elapsed) * 5, 0, Math.cos(elapsed) * 5);
2 years ago
2 years ago
renderer.render(scene, camera);
labelRenderer.render(scene, camera);
2 years ago
}
//
function initGui() {
gui = new GUI();
2 years ago
gui.add(layers, 'Toggle Name');
gui.add(layers, 'Toggle Mass');
gui.add(layers, 'Enable All');
gui.add(layers, 'Disable All');
}
function update() {
i = i + 1;
moonLabel.element.textContent = i;
setTimeout(update(), 1000);
2 years ago
}
2 years ago
setInterval(function () {
i = i + 1;
moonLabel.element.textContent = i;
}, 1000);
function AddCSSLabel() {
const labelDiv = document.createElement('div');
labelDiv.className = 'label';
labelDiv.name = "csslabeldiv"
labelDiv.textContent = '中国11';
labelDiv.style.pointerEvents = 'none';//避免HTML标签遮挡三维场景的鼠标事件
const moonLabel = new CSS2DObject(labelDiv);
moonLabel.position.set(1, 2, 0);
moonLabel.name = "datalabel"
moonLabel.userData = { name: "datalable", id: "", url: "" }
return moonLabel;
}
2 years ago
</script>
</body>
2 years ago
2 years ago
</html>