123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <html lang="en">
- <head>
- <title>three.js webgpu - texture gradient</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>
- <br />This example demonstrate texture gradient
- <br /> Left canvas is using WebGPU Backend, right canvas is WebGL Backend.
- <br /> The bottom half of the texture benefits from the gradient to achieve better blur quality.
- </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 { If, vec4, float, timerLocal, cos, pow, vec2, uv, texture, Fn } from 'three/tsl';
- // WebGPU Backend
- init();
- // WebGL Backend
- init( true );
- async function init( forceWebGL = false ) {
- const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
- const camera = new THREE.OrthographicCamera( - aspect, aspect );
- camera.position.z = 2;
- const scene = new THREE.Scene();
- // texture
- const material = new THREE.MeshBasicNodeMaterial( { color: 0xffffff } );
- // load async brick_diffuse
- const map = await new THREE.TextureLoader().loadAsync( 'textures/uv_grid_opengl.jpg' );
- const elapsedTime = timerLocal();
- material.colorNode = Fn( () => {
- const color = vec4( 1. ).toVar();
- const vuv = uv().toVar();
- const blur = pow( float( 0.0625 ).sub( cos( vuv.x.mul( 20.0 ).add( elapsedTime ) ) ).mul( 0.0625 ), 2.0 );
- const grad = vec2( blur ).toVar();
- If( vuv.y.greaterThan( 0.5 ), () => {
- grad.assign( 0 );
-
- } );
- color.assign(
- texture( map, vuv.add( vec2( blur, blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 )
- .add( texture( map, vuv.add( vec2( blur, blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
- .add( texture( map, vuv.add( vec2( blur.negate(), blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
- .add( texture( map, vuv.add( vec2( blur.negate(), blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
- );
- If( vuv.y.greaterThan( 0.497 ).and( vuv.y.lessThan( 0.503 ) ), () => {
- color.assign( 1 );
- } );
-
- return color;
- } )();
-
- //
- const box = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
- scene.add( box );
- const renderer = new THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL, trackTimestamp: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth / 2, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- renderer.domElement.style.position = 'absolute';
- renderer.domElement.style.top = '0';
- renderer.domElement.style.left = '0';
- renderer.domElement.style.width = '50%';
- renderer.domElement.style.height = '100%';
- if ( forceWebGL ) {
- renderer.domElement.style.left = '50%';
- scene.background = new THREE.Color( 0x212121 );
-
- } else {
- scene.background = new THREE.Color( 0x313131 );
- }
- //
- const animate = async function () {
- await renderer.renderAsync( scene, camera );
-
- };
- renderer.setAnimationLoop( animate );
- window.addEventListener( 'resize', onWindowResize );
- function onWindowResize() {
- renderer.setSize( window.innerWidth / 2, window.innerHeight );
- const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
- const frustumHeight = camera.top - camera.bottom;
- camera.left = - frustumHeight * aspect / 2;
- camera.right = frustumHeight * aspect / 2;
- camera.updateProjectionMatrix();
- renderer.render( scene, camera );
- }
- }
- </script>
- </body>
- </html>
|