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.

209 lines
5.0 KiB

2 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - materialx nodes</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 - MaterialX - Noise
</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/",
"three/nodes": "./jsm/nodes/Nodes.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { MeshPhysicalNodeMaterial, add, mul, normalWorld, timerLocal, mx_noise_vec3, mx_worley_noise_vec3, mx_cell_noise_float, mx_fractal_noise_vec3 } from 'three/nodes';
import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
import Stats from 'three/addons/libs/stats.module.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js';
let container, stats;
let camera, scene, renderer;
let particleLight;
let group;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;
scene = new THREE.Scene();
group = new THREE.Group();
scene.add( group );
new HDRCubeTextureLoader()
.setPath( 'textures/cube/pisaHDR/' )
.load( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ],
function ( hdrTexture ) {
const geometry = new THREE.SphereGeometry( 80, 64, 32 );
const offsetNode = timerLocal();
const customUV = add( mul( normalWorld, 10 ), offsetNode );
// left top
let material = new MeshPhysicalNodeMaterial();
material.colorNode = mx_noise_vec3( customUV );
let mesh = new THREE.Mesh( geometry, material );
mesh.position.x = - 100;
mesh.position.y = 100;
group.add( mesh );
// right top
material = new MeshPhysicalNodeMaterial();
material.colorNode = mx_cell_noise_float( customUV );
mesh = new THREE.Mesh( geometry, material );
mesh.position.x = 100;
mesh.position.y = 100;
group.add( mesh );
// left bottom
material = new MeshPhysicalNodeMaterial();
material.colorNode = mx_worley_noise_vec3( customUV );
mesh = new THREE.Mesh( geometry, material );
mesh.position.x = - 100;
mesh.position.y = - 100;
group.add( mesh );
// right bottom
material = new MeshPhysicalNodeMaterial();
material.colorNode = mx_fractal_noise_vec3( mul( customUV, .2 ) );
mesh = new THREE.Mesh( geometry, material );
mesh.position.x = 100;
mesh.position.y = - 100;
group.add( mesh );
//
scene.background = hdrTexture;
scene.environment = hdrTexture;
}
);
// LIGHTS
particleLight = new THREE.Mesh(
new THREE.SphereGeometry( 4, 8, 8 ),
new THREE.MeshBasicMaterial( { color: 0xffffff } )
);
scene.add( particleLight );
particleLight.add( new THREE.PointLight( 0xffffff, 1 ) );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.25;
//
renderer.outputEncoding = THREE.sRGBEncoding;
//
stats = new Stats();
container.appendChild( stats.dom );
// EVENTS
new OrbitControls( camera, renderer.domElement );
window.addEventListener( 'resize', onWindowResize );
}
//
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
}
//
function animate() {
requestAnimationFrame( animate );
nodeFrame.update();
render();
stats.update();
}
function render() {
const timer = Date.now() * 0.00025;
particleLight.position.x = Math.sin( timer * 7 ) * 300;
particleLight.position.y = Math.cos( timer * 5 ) * 400;
particleLight.position.z = Math.cos( timer * 3 ) * 300;
for ( let i = 0; i < group.children.length; i ++ ) {
const child = group.children[ i ];
child.rotation.y += 0.005;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>