|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgl - pmrem cubemap</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <link type="text/css" rel="stylesheet" href="example.css"> |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + |
| 11 | + <script type="importmap"> |
| 12 | + { |
| 13 | + "imports": { |
| 14 | + "three": "../build/three.module.js", |
| 15 | + "three/addons/": "./jsm/" |
| 16 | + } |
| 17 | + } |
| 18 | + </script> |
| 19 | + |
| 20 | + <script type="module"> |
| 21 | + |
| 22 | + import * as THREE from 'three'; |
| 23 | + |
| 24 | + import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js'; |
| 25 | + |
| 26 | + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 27 | + |
| 28 | + let camera, scene, renderer; |
| 29 | + |
| 30 | + init(); |
| 31 | + |
| 32 | + async function init() { |
| 33 | + |
| 34 | + const container = document.createElement( 'div' ); |
| 35 | + document.body.appendChild( container ); |
| 36 | + |
| 37 | + camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 ); |
| 38 | + camera.position.set( 0, 0, 8 ); |
| 39 | + |
| 40 | + scene = new THREE.Scene(); |
| 41 | + |
| 42 | + renderer = new THREE.WebGLRenderer( { antialias: true } ); |
| 43 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 44 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 45 | + renderer.setAnimationLoop( render ); |
| 46 | + renderer.toneMapping = THREE.ACESFilmicToneMapping; |
| 47 | + |
| 48 | + container.appendChild( renderer.domElement ); |
| 49 | + |
| 50 | + const controls = new OrbitControls( camera, renderer.domElement ); |
| 51 | + controls.minDistance = 2; |
| 52 | + controls.maxDistance = 10; |
| 53 | + controls.update(); |
| 54 | + |
| 55 | + new HDRCubeTextureLoader() |
| 56 | + .setPath( './textures/cube/pisaHDR/' ) |
| 57 | + .load( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ], function ( map ) { |
| 58 | + |
| 59 | + const pmremGenerator = new THREE.PMREMGenerator( renderer ); |
| 60 | + const envMap = pmremGenerator.fromCubemap( map ).texture; |
| 61 | + |
| 62 | + scene.background = envMap; |
| 63 | + scene.backgroundBlurriness = 0.5; |
| 64 | + |
| 65 | + pmremGenerator.dispose(); |
| 66 | + |
| 67 | + const geometry = new THREE.SphereGeometry( 0.4, 64, 64 ); |
| 68 | + |
| 69 | + for ( let i = 0; i < 6; i ++ ) { |
| 70 | + |
| 71 | + for ( let j = 0; j < 5; j ++ ) { |
| 72 | + |
| 73 | + const material = new THREE.MeshPhysicalMaterial( { |
| 74 | + roughness: i / 5, |
| 75 | + metalness: j / 4, |
| 76 | + envMap: envMap |
| 77 | + } ); |
| 78 | + |
| 79 | + const mesh = new THREE.Mesh( geometry, material ); |
| 80 | + mesh.position.x = i - 2.5; |
| 81 | + mesh.position.y = j - 2; |
| 82 | + scene.add( mesh ); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + } ); |
| 89 | + |
| 90 | + window.addEventListener( 'resize', onWindowResize ); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + function onWindowResize() { |
| 95 | + |
| 96 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 97 | + camera.updateProjectionMatrix(); |
| 98 | + |
| 99 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + // |
| 104 | + |
| 105 | + function render() { |
| 106 | + |
| 107 | + renderer.render( scene, camera ); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + </script> |
| 112 | + |
| 113 | + </body> |
| 114 | +</html> |
0 commit comments