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

206 lines
5.0 KiB

2 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - standard</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 physically based material<br/>
<a href="http://www.polycount.com/forum/showthread.php?t=130641" target="_blank" rel="noopener">Cerberus(FFVII Gun) model</a> by Andrew Maximov.
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
const statsEnabled = true;
let container, stats;
let camera, scene, renderer, controls;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.toneMapping = THREE.ReinhardToneMapping;
renderer.toneMappingExposure = 3;
//
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 1000 );
camera.position.z = 2;
controls = new TrackballControls( camera, renderer.domElement );
//
scene.add( new THREE.HemisphereLight( 0x443333, 0x222233, 4 ) );
//
const material = new THREE.MeshStandardMaterial();
new OBJLoader()
.setPath( 'models/obj/cerberus/' )
.load( 'Cerberus.obj', function ( group ) {
const loader = new THREE.TextureLoader()
.setPath( 'models/obj/cerberus/' );
material.roughness = 1; // attenuates roughnessMap
material.metalness = 1; // attenuates metalnessMap
const diffuseMap = loader.load( 'Cerberus_A.jpg' );
diffuseMap.encoding = THREE.sRGBEncoding;
material.map = diffuseMap;
// roughness is in G channel, metalness is in B channel
material.metalnessMap = material.roughnessMap = loader.load( 'Cerberus_RM.jpg' );
material.normalMap = loader.load( 'Cerberus_N.jpg' );
material.map.wrapS = THREE.RepeatWrapping;
material.roughnessMap.wrapS = THREE.RepeatWrapping;
material.metalnessMap.wrapS = THREE.RepeatWrapping;
material.normalMap.wrapS = THREE.RepeatWrapping;
group.traverse( function ( child ) {
if ( child.isMesh ) {
child.material = material;
}
} );
group.position.x = - 0.45;
group.rotation.y = - Math.PI / 2;
scene.add( group );
} );
const environments = {
'Venice Sunset': { filename: 'venice_sunset_1k.hdr' },
'Overpass': { filename: 'pedestrian_overpass_1k.hdr' }
};
function loadEnvironment( name ) {
if ( environments[ name ].texture !== undefined ) {
scene.background = environments[ name ].texture;
scene.environment = environments[ name ].texture;
return;
}
const filename = environments[ name ].filename;
new RGBELoader()
.setPath( 'textures/equirectangular/' )
.load( filename, function ( hdrEquirect ) {
hdrEquirect.mapping = THREE.EquirectangularReflectionMapping;
scene.background = hdrEquirect;
scene.environment = hdrEquirect;
environments[ name ].texture = hdrEquirect;
} );
}
const params = {
environment: Object.keys( environments )[ 0 ]
};
loadEnvironment( params.environment );
const gui = new GUI();
gui.add( params, 'environment', Object.keys( environments ) ).onChange( function ( value ) {
loadEnvironment( value );
} );
gui.open();
//
if ( statsEnabled ) {
stats = new Stats();
container.appendChild( stats.dom );
}
window.addEventListener( 'resize', onWindowResize );
}
//
function onWindowResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
//
function animate() {
requestAnimationFrame( animate );
controls.update();
renderer.render( scene, camera );
if ( statsEnabled ) stats.update();
}
</script>
</body>
</html>