Bläddra i källkod

Examples: Make layers example a bit more interesting. (#31036)

* Examples: Make layers example a bit more interesting.

* E2E: Update screenshot.
Michael Herzog 9 månader sedan
förälder
incheckning
99dd5af311

+ 1 - 1
docs/api/ar/core/Layers.html

@@ -25,7 +25,7 @@
 	 
 		<h2>أمثلة (Examples)</h2>
 	 
-		<p>[example:webgl_layers WebGL / layers]</p>
+		<p>[example:webgpu_layers WebGPU / layers]</p>
 	 
 		<h2>المنشئ (Constructor)</h2>
 	 

+ 1 - 1
docs/api/en/core/Layers.html

@@ -25,7 +25,7 @@
 
 		<h2>Examples</h2>
 
-		<p>[example:webgl_layers WebGL / layers]</p>
+		<p>[example:webgpu_layers WebGPU / layers]</p>
 
 		<h2>Constructor</h2>
 

+ 1 - 3
docs/api/it/core/Layers.html

@@ -22,9 +22,7 @@
 
 		<h2>Esempi</h2>
 
-		<p>
-			[example:webgl_layers WebGL / layers]
-		</p>
+		<p>[example:webgpu_layers WebGPU / layers]</p>
 
 		<h2>Costruttore</h2>
 

+ 1 - 3
docs/api/ko/core/Layers.html

@@ -21,9 +21,7 @@
 
 		<h2>예제</h2>
 
-		<p>
-			[example:webgl_layers WebGL / layers]
-		</p>
+		<p>[example:webgpu_layers WebGPU / layers]</p>
 
 		<h2>생성자</h2>
 

+ 1 - 1
examples/files.json

@@ -56,7 +56,6 @@
 		"webgl_interactive_points",
 		"webgl_interactive_raycasting_points",
 		"webgl_interactive_voxelpainter",
-		"webgl_layers",
 		"webgl_lensflares",
 		"webgl_lightprobe",
 		"webgl_lightprobe_cubecamera",
@@ -335,6 +334,7 @@
 		"webgpu_instance_sprites",
 		"webgpu_instance_uniform",
 		"webgpu_instancing_morph",
+		"webgpu_layers",
 		"webgpu_lensflares",
 		"webgpu_lightprobe",
 		"webgpu_lightprobe_cubecamera",

BIN
examples/screenshots/webgl_layers.jpg


BIN
examples/screenshots/webgpu_layers.jpg


BIN
examples/textures/sprites/blossom.png


+ 0 - 186
examples/webgl_layers.html

@@ -1,186 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-	<head>
-		<title>three.js webgl - layers</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">
-		<style>
-			body {
-				background-color: #f0f0f0;
-				color: #444;
-			}
-			a {
-				color: #08f;
-			}
-		</style>
-	</head>
-	<body>
-
-		<div id="info">
-			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl layers
-		</div>
-
-		<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';
-
-			let container, stats;
-			let camera, scene, renderer;
-
-			let theta = 0;
-			const radius = 5;
-
-			init();
-
-			function init() {
-
-				container = document.createElement( 'div' );
-				document.body.appendChild( container );
-
-				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
-				camera.layers.enable( 0 ); // enabled by default
-				camera.layers.enable( 1 );
-				camera.layers.enable( 2 );
-
-				scene = new THREE.Scene();
-				scene.background = new THREE.Color( 0xf0f0f0 );
-
-				const light = new THREE.PointLight( 0xffffff, 3, 0, 0 );
-				light.layers.enable( 0 );
-				light.layers.enable( 1 );
-				light.layers.enable( 2 );
-
-				scene.add( camera );
-				camera.add( light );
-
-				const colors = [ 0xff0000, 0x00ff00, 0x0000ff ];
-				const geometry = new THREE.BoxGeometry();
-
-				for ( let i = 0; i < 300; i ++ ) {
-
-					const layer = ( i % 3 );
-
-					const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: colors[ layer ] } ) );
-
-					object.position.x = Math.random() * 40 - 20;
-					object.position.y = Math.random() * 40 - 20;
-					object.position.z = Math.random() * 40 - 20;
-
-					object.rotation.x = Math.random() * 2 * Math.PI;
-					object.rotation.y = Math.random() * 2 * Math.PI;
-					object.rotation.z = Math.random() * 2 * Math.PI;
-
-					object.scale.x = Math.random() + 0.5;
-					object.scale.y = Math.random() + 0.5;
-					object.scale.z = Math.random() + 0.5;
-
-					object.layers.set( layer );
-
-					scene.add( object );
-
-				}
-
-				renderer = new THREE.WebGLRenderer( { antialias: true } );
-				renderer.setPixelRatio( window.devicePixelRatio );
-				renderer.setSize( window.innerWidth, window.innerHeight );
-				renderer.setAnimationLoop( animate );
-				container.appendChild( renderer.domElement );
-
-				stats = new Stats();
-				container.appendChild( stats.dom );
-
-				const layers = {
-
-					'toggle red': function () {
-
-						camera.layers.toggle( 0 );
-
-					},
-
-					'toggle green': function () {
-
-						camera.layers.toggle( 1 );
-
-					},
-
-					'toggle blue': function () {
-
-						camera.layers.toggle( 2 );
-
-					},
-
-					'enable all': function () {
-
-						camera.layers.enableAll();
-
-					},
-
-					'disable all': function () {
-
-						camera.layers.disableAll();
-
-					}
-
-				};
-
-				//
-				// Init gui
-				const gui = new GUI();
-				gui.add( layers, 'toggle red' );
-				gui.add( layers, 'toggle green' );
-				gui.add( layers, 'toggle blue' );
-				gui.add( layers, 'enable all' );
-				gui.add( layers, 'disable all' );
-
-				window.addEventListener( 'resize', onWindowResize );
-
-			}
-
-			function onWindowResize() {
-
-				camera.aspect = window.innerWidth / window.innerHeight;
-				camera.updateProjectionMatrix();
-
-				renderer.setSize( window.innerWidth, window.innerHeight );
-
-			}
-
-			//
-
-			function animate() {
-
-				render();
-				stats.update();
-
-			}
-
-			function render() {
-
-				theta += 0.1;
-
-				camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
-				camera.position.y = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
-				camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
-				camera.lookAt( scene.position );
-
-				renderer.render( scene, camera );
-
-			}
-
-		</script>
-
-	</body>
-</html>

+ 212 - 0
examples/webgpu_layers.html

@@ -0,0 +1,212 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgpu - layers</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> - webgpu layers
+		</div>
+
+		<script type="importmap">
+			{
+				"imports": {
+					"three": "../build/three.webgpu.js",
+					"three/webgpu": "../build/three.webgpu.js",
+					"three/tsl": "../build/three.tsl.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 { positionLocal, time, mod, instancedBufferAttribute, rotate, screenUV, color, vec2 } from 'three/tsl';
+
+			let camera, scene, renderer;
+
+
+			init();
+
+			function init() {
+
+				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
+				camera.layers.enable( 0 ); // enabled by default
+				camera.layers.enable( 1 );
+				camera.layers.enable( 2 );
+
+				camera.position.z = 10;
+
+				scene = new THREE.Scene();
+			
+				const horizontalEffect = screenUV.x.mix( color( 0xf996ae ), color( 0xf6f0a3 ) );
+				const lightEffect = screenUV.distance( vec2( 0.5, 1.0 ) ).oneMinus().mul( color( 0xd9b6fd ) );
+
+				scene.backgroundNode = horizontalEffect.add( lightEffect );
+
+
+				scene.add( camera );
+
+				const sprite = new THREE.TextureLoader().load( 'textures/sprites/blossom.png' );
+				sprite.colorSpace = THREE.SRGBColorSpace;
+
+				const count = 2500;
+
+				const geometry = new THREE.PlaneGeometry( 0.25, 0.25 );
+
+				const colors = [ 0xD70654, 0xFFD95F, 0xB8D576 ];
+
+				for ( let i = 0; i < 3; i ++ ) {
+
+					const particles = new THREE.Mesh( geometry, getMaterial( count, colors[ i ], sprite ) );
+					particles.layers.set( i );
+					particles.count = count;
+					scene.add( particles );
+
+				}
+
+				renderer = new THREE.WebGPURenderer( { antialias: true } );
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				renderer.setAnimationLoop( animate );
+				document.body.appendChild( renderer.domElement );
+
+				// GUI
+
+				const layers = {
+
+					'toggle red': function () {
+
+						camera.layers.toggle( 0 );
+
+					},
+
+					'toggle yellow': function () {
+
+						camera.layers.toggle( 1 );
+
+					},
+
+					'toggle green': function () {
+
+						camera.layers.toggle( 2 );
+
+					},
+
+					'enable all': function () {
+
+						camera.layers.enableAll();
+
+					},
+
+					'disable all': function () {
+
+						camera.layers.disableAll();
+
+					}
+
+				};
+
+				const gui = new GUI();
+				gui.add( layers, 'toggle red' );
+				gui.add( layers, 'toggle yellow' );
+				gui.add( layers, 'toggle green' );
+				gui.add( layers, 'enable all' );
+				gui.add( layers, 'disable all' );
+
+				//
+
+				window.addEventListener( 'resize', onWindowResize );
+
+			}
+
+			function getMaterial( count, color, sprite ) {
+
+				// instance data
+
+				const positions = [];
+				const rotations = [];
+				const directions = [];
+				const timeOffsets = [];
+
+				const v = new THREE.Vector3();
+
+				for ( let i = 0; i < count; i ++ ) {
+			
+					positions.push(
+						THREE.MathUtils.randFloat( - 25, - 20 ),
+						THREE.MathUtils.randFloat( - 10, 50 ),
+						THREE.MathUtils.randFloat( - 5, 5 )
+					);
+
+					v.set( THREE.MathUtils.randFloat( 0.7, 0.9 ), THREE.MathUtils.randFloat( - 0.3, - 0.15 ), 0 ).normalize();
+
+					rotations.push( Math.random(), Math.random(), Math.random() );
+
+					directions.push( v.x, v.y, v.z );
+
+					timeOffsets.push( i / count );
+
+				}
+
+				const positionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( positions ), 3 );
+				const rotationAttribute = new THREE.InstancedBufferAttribute( new Float32Array( rotations ), 3 );
+				const directionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( directions ), 3 );
+				const timeAttribute = new THREE.InstancedBufferAttribute( new Float32Array( timeOffsets ), 1 );
+
+				// material
+
+				const material = new THREE.MeshBasicNodeMaterial( {
+					color: color,
+					map: sprite,
+					alphaMap: sprite,
+					alphaTest: 0.1,
+					side: THREE.DoubleSide,
+					forceSinglePass: true
+				} );
+
+				// TSL
+
+				const instancePosition = instancedBufferAttribute( positionAttribute );
+				const instanceDirection = instancedBufferAttribute( directionAttribute );
+				const instanceRotation = instancedBufferAttribute( rotationAttribute );
+
+				const localTime = instancedBufferAttribute( timeAttribute ).add( time.mul( 0.02 ) );
+				const modTime = mod( localTime, 1.0 );
+
+				const rotatedPositon = rotate( positionLocal, instanceRotation.mul( modTime.mul( 20 ) ) );
+				material.positionNode = rotatedPositon.add( instancePosition ).add( instanceDirection.mul( modTime.mul( 50 ) ) );
+
+				return material;
+
+			}
+
+			function onWindowResize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			//
+
+			function animate() {
+
+				renderer.render( scene, camera );
+
+			}
+
+		</script>
+
+	</body>
+</html>

粤ICP备19079148号