123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>threejs webgpu - transmission</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="container"></div>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> webgpu - transmission</div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/tsl": "../build/three.webgpu.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
- const params = {
- color: 0xffffff,
- transmission: 1,
- opacity: 1,
- metalness: 0,
- roughness: 0,
- ior: 1.5,
- thickness: 0.01,
- specularIntensity: 1,
- specularColor: 0xffffff,
- envMapIntensity: 1,
- lightIntensity: 1,
- exposure: 1
- };
- let camera, scene, renderer;
- let mesh;
- const hdrEquirect = new RGBELoader()
- .setPath( 'textures/equirectangular/' )
- .load( 'royal_esplanade_1k.hdr', function () {
- hdrEquirect.mapping = THREE.EquirectangularReflectionMapping;
- init();
- render();
- } );
- function init() {
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( render );
- document.body.appendChild( renderer.domElement );
- renderer.toneMapping = THREE.ACESFilmicToneMapping;
- renderer.toneMappingExposure = params.exposure;
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
- camera.position.set( 0, 0, 120 );
- //
- scene.background = hdrEquirect;
- //
- const geometry = new THREE.SphereGeometry( 20, 64, 32 );
- const texture = new THREE.CanvasTexture( generateTexture() );
- texture.magFilter = THREE.NearestFilter;
- texture.wrapT = THREE.RepeatWrapping;
- texture.wrapS = THREE.RepeatWrapping;
- texture.repeat.set( 1, 3.5 );
- const material = new THREE.MeshPhysicalMaterial( {
- color: params.color,
- metalness: params.metalness,
- roughness: params.roughness,
- ior: params.ior,
- alphaMap: texture,
- envMap: hdrEquirect,
- envMapIntensity: params.envMapIntensity,
- transmission: params.transmission, // use material.transmission for glass materials
- specularIntensity: params.specularIntensity,
- specularColor: params.specularColor,
- opacity: params.opacity,
- side: THREE.DoubleSide,
- transparent: true
- } );
- mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- //
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 10;
- controls.maxDistance = 150;
- window.addEventListener( 'resize', onWindowResize );
- //
- const gui = new GUI();
- gui.addColor( params, 'color' )
- .onChange( function () {
- material.color.set( params.color );
- } );
- gui.add( params, 'transmission', 0, 1, 0.01 )
- .onChange( function () {
- material.transmission = params.transmission;
- } );
- gui.add( params, 'opacity', 0, 1, 0.01 )
- .onChange( function () {
- material.opacity = params.opacity;
- } );
- gui.add( params, 'metalness', 0, 1, 0.01 )
- .onChange( function () {
- material.metalness = params.metalness;
- } );
- gui.add( params, 'roughness', 0, 1, 0.01 )
- .onChange( function () {
- material.roughness = params.roughness;
- } );
- gui.add( params, 'ior', 1, 2, 0.01 )
- .onChange( function () {
- material.ior = params.ior;
- } );
- gui.add( params, 'thickness', 0, 5, 0.01 )
- .onChange( function () {
- material.thickness = params.thickness;
- } );
- gui.add( params, 'specularIntensity', 0, 1, 0.01 )
- .onChange( function () {
- material.specularIntensity = params.specularIntensity;
- } );
- gui.addColor( params, 'specularColor' )
- .onChange( function () {
- material.specularColor.set( params.specularColor );
- } );
- gui.add( params, 'envMapIntensity', 0, 1, 0.01 )
- .name( 'envMap intensity' )
- .onChange( function () {
- material.envMapIntensity = params.envMapIntensity;
- } );
- gui.add( params, 'exposure', 0, 1, 0.01 )
- .onChange( function () {
- renderer.toneMappingExposure = params.exposure;
- } );
- gui.open();
- }
- function onWindowResize() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- //
- function generateTexture() {
- const canvas = document.createElement( 'canvas' );
- canvas.width = 2;
- canvas.height = 2;
- const context = canvas.getContext( '2d' );
- context.fillStyle = 'white';
- context.fillRect( 0, 1, 2, 1 );
- return canvas;
- }
- function render() {
- renderer.renderAsync( scene, camera );
- }
- </script>
- </body>
- </html>
|