123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <!-- Licensed under a BSD license. See license.html for license -->
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
- <title>Three.js - Primitives</title>
- <style>
- html, body {
- height: 100%;
- margin: 0;
- }
- #c {
- width: 100%;
- height: 100%;
- display: block;
- }
- </style>
- </head>
- <body>
- <canvas id="c"></canvas>
- </body>
- <script type="importmap">
- {
- "imports": {
- "three": "../../build/three.module.js",
- "three/addons/": "../../examples/jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { FontLoader } from 'three/addons/loaders/FontLoader.js';
- import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
- function main() {
- const canvas = document.querySelector( '#c' );
- const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
- const fov = 40;
- const aspect = 2; // the canvas default
- const near = 0.1;
- const far = 1000;
- const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
- camera.position.z = 40;
- const scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xAAAAAA );
- {
- const color = 0xFFFFFF;
- const intensity = 3;
- const light = new THREE.DirectionalLight( color, intensity );
- light.position.set( - 1, 2, 4 );
- scene.add( light );
- }
- {
- const color = 0xFFFFFF;
- const intensity = 3;
- const light = new THREE.DirectionalLight( color, intensity );
- light.position.set( 1, - 2, - 4 );
- scene.add( light );
- }
- const objects = [];
- const spread = 15;
- function addObject( x, y, obj ) {
- obj.position.x = x * spread;
- obj.position.y = y * spread;
- scene.add( obj );
- objects.push( obj );
- }
- function createMaterial() {
- const material = new THREE.MeshPhongMaterial( {
- side: THREE.DoubleSide,
- } );
- const hue = Math.random();
- const saturation = 1;
- const luminance = .5;
- material.color.setHSL( hue, saturation, luminance );
- return material;
- }
- function addSolidGeometry( x, y, geometry ) {
- const mesh = new THREE.Mesh( geometry, createMaterial() );
- addObject( x, y, mesh );
- }
- {
- const loader = new FontLoader();
- // promisify font loading
- function loadFont( url ) {
- return new Promise( ( resolve, reject ) => {
- loader.load( url, resolve, undefined, reject );
- } );
- }
- async function doit() {
- const font = await loadFont( '/examples/fonts/helvetiker_regular.typeface.json' ); /* threejs.org: url */
- const geometry = new TextGeometry( 'three.js', {
- font: font,
- size: 3.0,
- depth: .2,
- curveSegments: 12,
- bevelEnabled: true,
- bevelThickness: 0.15,
- bevelSize: .3,
- bevelSegments: 5,
- } );
- addSolidGeometry( - .5, 0, geometry );
- const mesh = new THREE.Mesh( geometry, createMaterial() );
- geometry.computeBoundingBox();
- geometry.boundingBox.getCenter( mesh.position ).multiplyScalar( - 1 );
- const parent = new THREE.Object3D();
- parent.add( mesh );
- addObject( .5, 0, parent );
- }
- doit();
- }
- function resizeRendererToDisplaySize( renderer ) {
- const canvas = renderer.domElement;
- const width = canvas.clientWidth;
- const height = canvas.clientHeight;
- const needResize = canvas.width !== width || canvas.height !== height;
- if ( needResize ) {
- renderer.setSize( width, height, false );
- }
- return needResize;
- }
- function render( time ) {
- time *= 0.001;
- if ( resizeRendererToDisplaySize( renderer ) ) {
- const canvas = renderer.domElement;
- camera.aspect = canvas.clientWidth / canvas.clientHeight;
- camera.updateProjectionMatrix();
- }
- objects.forEach( ( obj, ndx ) => {
- const speed = .5 + ndx * .05;
- const rot = time * speed;
- obj.rotation.x = rot;
- obj.rotation.y = rot;
- } );
- renderer.render( scene, camera );
- requestAnimationFrame( render );
- }
- requestAnimationFrame( render );
- }
- main();
- </script>
- </html>
|