|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 6 | + <title>three.js css2d - label</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + background-color: #000; |
| 10 | + margin: 0; |
| 11 | + overflow: hidden; |
| 12 | + } |
| 13 | + #info { |
| 14 | + position: absolute; |
| 15 | + top: 0px; |
| 16 | + width: 100%; |
| 17 | + color: #FFF; |
| 18 | + padding: 5px; |
| 19 | + font-family: Monospace; |
| 20 | + font-size: 13px; |
| 21 | + text-align: center; |
| 22 | + z-index: 1; |
| 23 | + } |
| 24 | + |
| 25 | + .label{ |
| 26 | + color: #FFF; |
| 27 | + font-family: sans-serif; |
| 28 | + padding: 2px; |
| 29 | + background: rgba( 0, 0, 0, .6 ); |
| 30 | + } |
| 31 | + |
| 32 | + a { |
| 33 | + color: #000000; |
| 34 | + } |
| 35 | + |
| 36 | + </style> |
| 37 | + </head> |
| 38 | + <body> |
| 39 | + <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - three.js css2d - label</div> |
| 40 | + |
| 41 | + <script src="../build/three.js"></script> |
| 42 | + |
| 43 | + <script src="js/controls/OrbitControls.js"></script> |
| 44 | + |
| 45 | + <script src="js/renderers/CSS2DRenderer.js"></script> |
| 46 | + |
| 47 | + <script> |
| 48 | + |
| 49 | + var camera, scene, renderer, labelRenderer; |
| 50 | + var controls; |
| 51 | + var clock = new THREE.Clock(); |
| 52 | + var textureLoader = new THREE.TextureLoader(); |
| 53 | + |
| 54 | + var earth, moon; |
| 55 | + |
| 56 | + init(); |
| 57 | + animate(); |
| 58 | + |
| 59 | + function init() { |
| 60 | + |
| 61 | + var EARTH_RADIUS = 1; |
| 62 | + var MOON_RADIUS = 0.27; |
| 63 | + |
| 64 | + camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 ); |
| 65 | + camera.position.set( 10, 5, 20 ); |
| 66 | + |
| 67 | + controls = new THREE.OrbitControls( camera ); |
| 68 | + |
| 69 | + scene = new THREE.Scene(); |
| 70 | + |
| 71 | + scene2 = new THREE.Scene(); |
| 72 | + |
| 73 | + dirLight = new THREE.DirectionalLight( 0xffffff ); |
| 74 | + dirLight.position.set( 0, 0, 1 ); |
| 75 | + scene.add( dirLight ); |
| 76 | + |
| 77 | + var axesHelper = new THREE.AxesHelper( 5 ); |
| 78 | + scene.add( axesHelper ); |
| 79 | + |
| 80 | + // |
| 81 | + |
| 82 | + var earthGeometry = new THREE.SphereBufferGeometry( EARTH_RADIUS, 16, 16 ); |
| 83 | + var earthMaterial = new THREE.MeshPhongMaterial( { |
| 84 | + specular: 0x333333, |
| 85 | + shininess: 5, |
| 86 | + map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ), |
| 87 | + specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ), |
| 88 | + normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ), |
| 89 | + normalScale: new THREE.Vector2( 0.85, 0.85 ) |
| 90 | + } ); |
| 91 | + earth = new THREE.Mesh( earthGeometry, earthMaterial ); |
| 92 | + scene.add( earth ); |
| 93 | + |
| 94 | + var moonGeometry = new THREE.SphereBufferGeometry( MOON_RADIUS, 16, 16 ); |
| 95 | + var moonMaterial = new THREE.MeshPhongMaterial( { |
| 96 | + shininess: 5, |
| 97 | + map: textureLoader.load( 'textures/planets/moon_1024.jpg' ) |
| 98 | + } ); |
| 99 | + moon = new THREE.Mesh( moonGeometry, moonMaterial ); |
| 100 | + scene.add( moon ); |
| 101 | + |
| 102 | + // |
| 103 | + |
| 104 | + var earthDiv = document.createElement( 'div' ); |
| 105 | + earthDiv.className = 'label'; |
| 106 | + earthDiv.textContent = 'Earth'; |
| 107 | + earthDiv.style.marginTop = '-1em'; |
| 108 | + var earthLabel = new THREE.CSS2DObject( earthDiv ); |
| 109 | + earthLabel.position.set( 0, EARTH_RADIUS, 0 ); |
| 110 | + earth.add( earthLabel ); |
| 111 | + |
| 112 | + var moonDiv = document.createElement( 'div' ); |
| 113 | + moonDiv.className = 'label'; |
| 114 | + moonDiv.textContent = 'Moon'; |
| 115 | + moonDiv.style.marginTop = '-1em'; |
| 116 | + var moonLabel = new THREE.CSS2DObject( moonDiv ); |
| 117 | + moonLabel.position.set( 0, MOON_RADIUS, 0 ); |
| 118 | + moon.add( moonLabel ); |
| 119 | + |
| 120 | + // |
| 121 | + |
| 122 | + renderer = new THREE.WebGLRenderer(); |
| 123 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 124 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 125 | + document.body.appendChild( renderer.domElement ); |
| 126 | + |
| 127 | + labelRenderer = new THREE.CSS2DRenderer(); |
| 128 | + labelRenderer.setSize( window.innerWidth, window.innerHeight ); |
| 129 | + labelRenderer.domElement.style.position = 'absolute'; |
| 130 | + labelRenderer.domElement.style.top = 0; |
| 131 | + document.body.appendChild( labelRenderer.domElement ); |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + function animate() { |
| 136 | + |
| 137 | + requestAnimationFrame( animate ); |
| 138 | + |
| 139 | + var elapsed = clock.getElapsedTime(); |
| 140 | + |
| 141 | + moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 ); |
| 142 | + |
| 143 | + renderer.render( scene, camera ); |
| 144 | + labelRenderer.render( scene, camera ); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + </script> |
| 149 | + </body> |
| 150 | +</html> |
0 commit comments