3d-realm/main.js

245 lines
6.7 KiB
JavaScript
Raw Normal View History

2023-08-02 12:45:25 +10:00
// import * as THREE from 'three';
2023-08-02 00:10:44 +10:00
2023-08-02 12:45:25 +10:00
// const scene = new THREE.Scene();
// const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
2023-08-02 00:10:44 +10:00
2023-08-02 12:45:25 +10:00
// const renderer = new THREE.WebGLRenderer();
// renderer.setSize( window.innerWidth, window.innerHeight );
// document.body.appendChild( renderer.domElement );
2023-08-02 00:10:44 +10:00
2023-08-02 12:45:25 +10:00
// const geometry = new THREE.BoxGeometry( 1, 2, 1 );
// const material = new THREE.MeshBasicMaterial( { color: 0x277BC0 } );
// const cube = new THREE.Mesh( geometry, material );
// scene.add( cube );
2023-08-02 00:10:44 +10:00
2023-08-02 12:45:25 +10:00
// camera.position.z = 5;
2023-08-02 00:10:44 +10:00
2023-08-02 12:45:25 +10:00
// const points = [];
// points.push( new THREE.Vector3( - 10, 0, 0 ) );
// points.push( new THREE.Vector3( 0, 10, 0 ) );
// points.push( new THREE.Vector3( 10, 0, 0 ) );
2023-08-02 00:37:09 +10:00
2023-08-02 00:37:30 +10:00
2023-08-02 12:45:25 +10:00
// const material2 = new THREE.LineBasicMaterial( { color: 0x0000ff } );
// const geometry2 = new THREE.BufferGeometry().setFromPoints( points );
// const line = new THREE.Line( geometry2, material2 );
2023-08-02 00:37:09 +10:00
2023-08-02 12:45:25 +10:00
// scene.add( line );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
// function animate() {
// requestAnimationFrame( animate );
// renderer.render( scene, camera );
// }
// animate();
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
import * as THREE from 'three';
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
import Stats from 'three/addons/libs/stats.module.js';
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { OutlineEffect } from 'three/addons/effects/OutlineEffect.js';
import { FontLoader } from 'three/addons/loaders/FontLoader.js';
import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
2023-08-02 00:40:50 +10:00
2023-08-02 13:11:47 +10:00
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { GlitchPass } from 'three/addons/postprocessing/GlitchPass.js';
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
2023-08-02 13:30:27 +10:00
import { OutlinePass } from 'three/addons/postprocessing/OutlinePass.js';
2023-08-02 13:11:47 +10:00
2023-08-02 12:45:25 +10:00
let container, stats;
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
let camera, scene, renderer, effect;
let particleLight;
2023-08-02 00:40:50 +10:00
2023-08-02 13:30:27 +10:00
let composer, glitchPass, outlinePass;
2023-08-02 13:12:24 +10:00
2023-08-02 12:45:25 +10:00
const loader = new FontLoader();
2023-08-02 12:47:09 +10:00
loader.load( 'https://raw.githubusercontent.com/mrdoob/three.js/master/examples/fonts/gentilis_regular.typeface.json', function ( font ) {
//'fonts/gentilis_regular.typeface.json', function ( font ) {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
init( font );
animate();
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
} );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
function init( font ) {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
container = document.createElement( 'div' );
document.body.appendChild( container );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2500 );
camera.position.set( 0.0, 400, 400 * 3.5 );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
//
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x444488 );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
//
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
// Materials
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const cubeWidth = 400;
const numberOfSphersPerSide = 5;
const sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
const stepSize = 1.0 / numberOfSphersPerSide;
const format = ( renderer.capabilities.isWebGL2 ) ? THREE.RedFormat : THREE.LuminanceFormat;
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const geometry = new THREE.SphereGeometry( sphereRadius, 32, 16 );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
for ( let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const colors = new Uint8Array( alphaIndex + 2 );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
for ( let c = 0; c <= colors.length; c ++ ) {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
colors[ c ] = ( c / colors.length ) * 256;
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
}
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const gradientMap = new THREE.DataTexture( colors, colors.length, 1, format );
gradientMap.needsUpdate = true;
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
// basic monochromatic energy preservation
const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const material = new THREE.MeshToonMaterial( {
color: diffuseColor,
gradientMap: gradientMap
} );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const mesh = new THREE.Mesh( geometry, material );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
mesh.position.x = alpha * 400 - 200;
mesh.position.y = beta * 400 - 200;
mesh.position.z = gamma * 400 - 200;
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
scene.add( mesh );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
}
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
}
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
}
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
function addLabel( name, location ) {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const textGeo = new TextGeometry( name, {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
font: font,
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
size: 20,
height: 1,
curveSegments: 1
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
} );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const textMaterial = new THREE.MeshBasicMaterial();
const textMesh = new THREE.Mesh( textGeo, textMaterial );
textMesh.position.copy( location );
scene.add( textMesh );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
}
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
addLabel( '-gradientMap', new THREE.Vector3( - 350, 0, 0 ) );
addLabel( '+gradientMap', new THREE.Vector3( 350, 0, 0 ) );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
addLabel( '-diffuse', new THREE.Vector3( 0, 0, - 300 ) );
addLabel( '+diffuse', new THREE.Vector3( 0, 0, 300 ) );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
particleLight = new THREE.Mesh(
new THREE.SphereGeometry( 4, 8, 8 ),
new THREE.MeshBasicMaterial( { color: 0xffffff } )
);
scene.add( particleLight );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
// Lights
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
scene.add( new THREE.AmbientLight( 0xc1c1c1, 3 ) );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const pointLight = new THREE.PointLight( 0xffffff, 2, 800, 0 );
particleLight.add( pointLight );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
//
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
effect = new OutlineEffect( renderer );
2023-08-02 00:40:50 +10:00
2023-08-02 13:11:47 +10:00
// postprocessing
composer = new EffectComposer( renderer );
composer.addPass( new RenderPass( scene, camera ) );
2023-08-02 13:16:05 +10:00
2023-08-02 13:20:05 +10:00
outlinePass = new OutlinePass( scene, camera );
composer.addPass( outlinePass );
2023-08-02 13:16:05 +10:00
2023-08-02 13:17:53 +10:00
//composer.addPass( new OutlineEffect( ) );
2023-08-02 13:11:47 +10:00
glitchPass = new GlitchPass();
composer.addPass( glitchPass );
const outputPass = new OutputPass();
composer.addPass( outputPass );
2023-08-02 12:45:25 +10:00
//
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
stats = new Stats();
container.appendChild( stats.dom );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 200;
controls.maxDistance = 2000;
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
window.addEventListener( 'resize', onWindowResize );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
}
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
function onWindowResize() {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
renderer.setSize( window.innerWidth, window.innerHeight );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
}
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
//
2023-08-02 00:37:44 +10:00
2023-08-02 12:45:25 +10:00
function animate() {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
requestAnimationFrame( animate );
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
stats.begin();
2023-08-02 13:11:47 +10:00
//render();
composer.render();
2023-08-02 12:45:25 +10:00
stats.end();
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
}
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
function render() {
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
const timer = Date.now() * 0.00025;
2023-08-02 00:40:50 +10:00
2023-08-02 12:45:25 +10:00
particleLight.position.x = Math.sin( timer * 7 ) * 300;
particleLight.position.y = Math.cos( timer * 5 ) * 400;
particleLight.position.z = Math.cos( timer * 3 ) * 300;
2023-08-02 00:40:50 +10:00
2023-08-02 13:11:47 +10:00
//effect.render( scene, camera );
2023-08-02 00:40:50 +10:00
2023-08-02 13:11:47 +10:00
composer.render();
2023-08-02 12:45:25 +10:00
}