Hello,
when i try to rotate/animate a loaded mesh, then the GUI disappears.
Anybody has an idea?
Here is the code:
import * as THREE from 'three';
import Stats from './src/libs/stats.module.js';
import { GUI } from './src/libs/lil-gui.module.min.js';
import { OrbitControls } from './src/controls/OrbitControls.js';
import { OBJLoader } from './src/loaders/OBJLoader.js';
let stats;
let camera, scene, renderer, controls;
const settings = {
metalness: 1.0,
roughness: 0.4,
ambientIntensity: 0.2,
aoMapIntensity: 1.0,
envMapIntensity: 1.0
};
let rotormesh, mesh, material;
let pointLight, ambientLight;
const height = 500; // of camera frustum
let r = 0.0;
init();
animate();
initGui();
// Init gui
function initGui() {
const gui = new GUI();
//let gui = gui.addFolder( "Material" );
gui.add( settings, 'metalness' ).min( 0 ).max( 1 ).onChange( function ( value ) {
material.metalness = value;
} );
gui.add( settings, 'roughness' ).min( 0 ).max( 1 ).onChange( function ( value ) {
material.roughness = value;
} );
gui.add( settings, 'aoMapIntensity' ).min( 0 ).max( 1 ).onChange( function ( value ) {
material.aoMapIntensity = value;
} );
gui.add( settings, 'ambientIntensity' ).min( 0 ).max( 1 ).onChange( function ( value ) {
ambientLight.intensity = value;
} );
gui.add( settings, 'envMapIntensity' ).min( 0 ).max( 3 ).onChange( function ( value ) {
material.envMapIntensity = value;
} );
}
function init() {
const container = document.createElement( 'div' );
document.body.appendChild( container );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
renderer.outputEncoding = THREE.sRGBEncoding;
//
scene = new THREE.Scene();
scene.background = new THREE.Color('white');
const aspect = window.innerWidth / window.innerHeight;
camera = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1, 10000 );
camera.position.z = 1500;
scene.add( camera );
controls = new OrbitControls( camera, renderer.domElement );
controls.enableZoom = false;
controls.enableDamping = true;
// lights
ambientLight = new THREE.AmbientLight( 0x44ffff, settings.ambientIntensity );
scene.add( ambientLight );
pointLight = new THREE.PointLight( 0xffffff, 0.5 );
pointLight.position.z = 2500;
scene.add( pointLight );
const pointLight2 = new THREE.PointLight( 0xffffff, 1 );
camera.add( pointLight2 );
const pointLight3 = new THREE.PointLight( 0xffffff, 0.5 );
pointLight3.position.x = - 1000;
pointLight3.position.z = 1000;
scene.add( pointLight3 );
// env map
const path = 'textures/cube/';
const format = '.jpg';
const urls = [
path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format
];
const reflectionCube = new THREE.CubeTextureLoader().load( urls );
reflectionCube.encoding = THREE.sRGBEncoding;
// textures
const textureLoader = new THREE.TextureLoader();
const aoMap = textureLoader.load( 'models/ao.jpg' );
// material
material = new THREE.MeshStandardMaterial( {
color: 0xCCCCCC,
roughness: settings.roughness,
metalness: settings.metalness,
aoMap: aoMap,
aoMapIntensity: 1,
envMap: reflectionCube,
envMapIntensity: settings.envMapIntensity,
side: THREE.DoubleSide
} );
//
const loader = new OBJLoader();
loader.load( 'models/body3.obj', function ( body ) {
const geometry = body.children[ 0 ].geometry;
//geometry.attributes.uv2 = geometry.attributes.uv;
//geometry.center();
mesh = new THREE.Mesh( geometry, material );
mesh.scale.multiplyScalar( 30 );
scene.add( mesh );
} );
loader.load( 'models/rotor.obj', function ( rotor ) {
const geometry = rotor.children[ 0 ].geometry;
//geometry.attributes.uv2 = geometry.attributes.uv;
//geometry.center();
rotormesh = new THREE.Mesh( geometry, material );
rotormesh.scale.multiplyScalar( 30 );
scene.add( rotormesh );
} );
//
stats = new Stats();
container.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
const aspect = window.innerWidth / window.innerHeight;
camera.left = - height * aspect;
camera.right = height * aspect;
camera.top = height;
camera.bottom = - height;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
controls.update();
stats.begin();
rotormesh.rotation.z += 0.09;
stats.end();
render();
}
function render() {
pointLight.position.x = 2500 * Math.cos( r );
pointLight.position.z = 2500 * Math.sin( r );
r = 0.01;
renderer.render( scene, camera );
//rotormesh.rotation.z += 0.09;
//gui.update();
}