|
@@ -1,188 +0,0 @@
|
|
|
-<!DOCTYPE html>
|
|
|
-<html lang="en">
|
|
|
- <head>
|
|
|
- <title>three.js webgl - buffer geometry constructed from geometry</title>
|
|
|
- <meta charset="utf-8">
|
|
|
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
|
|
- <link type="text/css" rel="stylesheet" href="main.css">
|
|
|
- </head>
|
|
|
- <body>
|
|
|
- <div id="info">
|
|
|
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry constructed from geometry<br/>
|
|
|
- by <a target="_blank" href="http://callum.com">Callum Prentice</a>
|
|
|
- </div>
|
|
|
-
|
|
|
- <script type="module">
|
|
|
-
|
|
|
- import * as THREE from '../build/three.module.js';
|
|
|
-
|
|
|
- import Stats from './jsm/libs/stats.module.js';
|
|
|
-
|
|
|
- import { TrackballControls } from './jsm/controls/TrackballControls.js';
|
|
|
-
|
|
|
- let camera, scene, renderer, controls, stats;
|
|
|
-
|
|
|
- init();
|
|
|
- animate();
|
|
|
-
|
|
|
- function init() {
|
|
|
-
|
|
|
- renderer = new THREE.WebGLRenderer( { antialias: true } );
|
|
|
- renderer.setPixelRatio( window.devicePixelRatio );
|
|
|
- renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
- document.body.appendChild( renderer.domElement );
|
|
|
-
|
|
|
- scene = new THREE.Scene();
|
|
|
-
|
|
|
- camera = new THREE.PerspectiveCamera( 45.0, window.innerWidth / window.innerHeight, 100, 1500.0 );
|
|
|
- camera.position.z = 480.0;
|
|
|
- scene.add( camera );
|
|
|
-
|
|
|
- controls = new TrackballControls( camera, renderer.domElement );
|
|
|
- controls.minDistance = 100.0;
|
|
|
- controls.maxDistance = 800.0;
|
|
|
- controls.dynamicDampingFactor = 0.1;
|
|
|
-
|
|
|
- scene.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
|
|
|
-
|
|
|
- const light = new THREE.PointLight( 0xffffff, 0.7 );
|
|
|
- camera.add( light );
|
|
|
-
|
|
|
- createScene();
|
|
|
-
|
|
|
- stats = new Stats();
|
|
|
- document.body.appendChild( stats.dom );
|
|
|
-
|
|
|
- window.addEventListener( 'resize', onWindowResize, false );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- function createGeometry() {
|
|
|
-
|
|
|
- const heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
|
|
|
- const x = 0, y = 0;
|
|
|
-
|
|
|
- heartShape.moveTo( x + 25, y + 25 );
|
|
|
- heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
|
|
|
- heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35, x - 30, y + 35 );
|
|
|
- heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
|
|
|
- heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
|
|
|
- heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
|
|
|
- heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
|
|
|
-
|
|
|
- const extrudeSettings = {
|
|
|
- depth: 16,
|
|
|
- bevelEnabled: true,
|
|
|
- bevelSegments: 1,
|
|
|
- steps: 2,
|
|
|
- bevelSize: 1,
|
|
|
- bevelThickness: 1
|
|
|
- };
|
|
|
-
|
|
|
- const geometry = new THREE.ExtrudeGeometry( heartShape, extrudeSettings );
|
|
|
- geometry.rotateX( Math.PI );
|
|
|
- geometry.scale( 0.4, 0.4, 0.4 );
|
|
|
-
|
|
|
- return geometry;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- function createScene() {
|
|
|
-
|
|
|
- const bufferGeometry = new THREE.BufferGeometry();
|
|
|
-
|
|
|
- const radius = 125;
|
|
|
- const count = 80;
|
|
|
-
|
|
|
- const positions = [];
|
|
|
- const normals = [];
|
|
|
- const colors = [];
|
|
|
-
|
|
|
- const vector = new THREE.Vector3();
|
|
|
-
|
|
|
- const color = new THREE.Color( 0xffffff );
|
|
|
- const heartGeometry = createGeometry();
|
|
|
- const geometry = new THREE.Geometry();
|
|
|
-
|
|
|
- for ( let i = 1, l = count; i <= l; i ++ ) {
|
|
|
-
|
|
|
- const phi = Math.acos( - 1 + ( 2 * i ) / l );
|
|
|
- const theta = Math.sqrt( l * Math.PI ) * phi;
|
|
|
-
|
|
|
- vector.setFromSphericalCoords( radius, phi, theta );
|
|
|
-
|
|
|
- geometry.copy( heartGeometry );
|
|
|
- geometry.lookAt( vector );
|
|
|
- geometry.translate( vector.x, vector.y, vector.z );
|
|
|
-
|
|
|
- color.setHSL( ( i / l ), 1.0, 0.7 );
|
|
|
-
|
|
|
- geometry.faces.forEach( function ( face ) {
|
|
|
-
|
|
|
- positions.push( geometry.vertices[ face.a ].x );
|
|
|
- positions.push( geometry.vertices[ face.a ].y );
|
|
|
- positions.push( geometry.vertices[ face.a ].z );
|
|
|
- positions.push( geometry.vertices[ face.b ].x );
|
|
|
- positions.push( geometry.vertices[ face.b ].y );
|
|
|
- positions.push( geometry.vertices[ face.b ].z );
|
|
|
- positions.push( geometry.vertices[ face.c ].x );
|
|
|
- positions.push( geometry.vertices[ face.c ].y );
|
|
|
- positions.push( geometry.vertices[ face.c ].z );
|
|
|
-
|
|
|
- normals.push( face.normal.x );
|
|
|
- normals.push( face.normal.y );
|
|
|
- normals.push( face.normal.z );
|
|
|
- normals.push( face.normal.x );
|
|
|
- normals.push( face.normal.y );
|
|
|
- normals.push( face.normal.z );
|
|
|
- normals.push( face.normal.x );
|
|
|
- normals.push( face.normal.y );
|
|
|
- normals.push( face.normal.z );
|
|
|
-
|
|
|
- colors.push( color.r );
|
|
|
- colors.push( color.g );
|
|
|
- colors.push( color.b );
|
|
|
- colors.push( color.r );
|
|
|
- colors.push( color.g );
|
|
|
- colors.push( color.b );
|
|
|
- colors.push( color.r );
|
|
|
- colors.push( color.g );
|
|
|
- colors.push( color.b );
|
|
|
-
|
|
|
- } );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- bufferGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
|
|
|
- bufferGeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
|
|
|
- bufferGeometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
|
|
|
-
|
|
|
- const material = new THREE.MeshPhongMaterial( { shininess: 80, vertexColors: true } );
|
|
|
-
|
|
|
- const mesh = new THREE.Mesh( bufferGeometry, material );
|
|
|
- scene.add( mesh );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- function onWindowResize() {
|
|
|
-
|
|
|
- camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
- camera.updateProjectionMatrix();
|
|
|
- renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- function animate() {
|
|
|
-
|
|
|
- requestAnimationFrame( animate );
|
|
|
-
|
|
|
- controls.update();
|
|
|
- stats.update();
|
|
|
-
|
|
|
- renderer.render( scene, camera );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- </script>
|
|
|
- </body>
|
|
|
-</html>
|