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

202 lines
3.9 KiB
HTML

<!-- 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>