-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* TextureNode: Fix analyze reference * Add RTTNode * rgbShift: add `.toTexture()` and updated example * Update GaussianBlurNode.js * add procedural texture * Update AnamorphicNode.js * update imports * update build for now * update build * update names
- Loading branch information
Showing
11 changed files
with
388 additions
and
76 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<html lang="en"> | ||
<head> | ||
<title>three.js webgpu - procedural texture</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 - procedural texture | ||
</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 { checker, uv, uniform } from 'three/tsl'; | ||
|
||
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; | ||
|
||
let camera, scene, renderer; | ||
|
||
init(); | ||
render(); | ||
|
||
function init() { | ||
|
||
const aspect = window.innerWidth / window.innerHeight; | ||
camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 ); | ||
camera.position.z = 1; | ||
|
||
scene = new THREE.Scene(); | ||
|
||
// procedural to texture | ||
|
||
const uvScale = uniform( 4 ); | ||
const blurAmount = uniform( .5 ); | ||
|
||
const procedural = checker( uv().mul( uvScale ) ); | ||
const proceduralToTexture = procedural.toTexture( 512, 512 ); // ( width, height ) <- texture size | ||
|
||
const colorNode = proceduralToTexture.gaussianBlur( blurAmount, 10 ); | ||
|
||
// extra | ||
|
||
//proceduralToTexture.autoUpdate = false; // update just once | ||
//proceduralToTexture.textureNeedsUpdate = true; // manually update | ||
|
||
// scene | ||
|
||
const material = new THREE.MeshBasicNodeMaterial(); | ||
material.colorNode = colorNode; | ||
|
||
const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material ); | ||
scene.add( plane ); | ||
|
||
// renderer | ||
|
||
renderer = new THREE.WebGPURenderer( { antialias: true } ); | ||
renderer.setPixelRatio( window.devicePixelRatio ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
renderer.setAnimationLoop( render ); | ||
document.body.appendChild( renderer.domElement ); | ||
|
||
window.addEventListener( 'resize', onWindowResize ); | ||
|
||
// gui | ||
|
||
const gui = new GUI(); | ||
gui.add( uvScale, 'value', 1, 10 ).name( 'uv scale ( before rtt )' ); | ||
gui.add( blurAmount, 'value', 0, 2 ).name( 'blur amount ( after rtt )' ); | ||
gui.add( proceduralToTexture, 'autoUpdate' ).name( 'auto update' ); | ||
|
||
} | ||
|
||
function onWindowResize() { | ||
|
||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
|
||
const aspect = window.innerWidth / window.innerHeight; | ||
|
||
const frustumHeight = camera.top - camera.bottom; | ||
|
||
camera.left = - frustumHeight * aspect / 2; | ||
camera.right = frustumHeight * aspect / 2; | ||
|
||
camera.updateProjectionMatrix(); | ||
|
||
} | ||
|
||
function render() { | ||
|
||
renderer.renderAsync( scene, camera ); | ||
|
||
} | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { nodeObject, addNodeElement } from '../shadernode/ShaderNode.js'; | ||
import TextureNode from '../accessors/TextureNode.js'; | ||
import { NodeUpdateType } from '../core/constants.js'; | ||
import { addNodeClass } from '../core/Node.js'; | ||
import NodeMaterial from '../materials/NodeMaterial.js'; | ||
import { uv } from '../accessors/UVNode.js'; | ||
import QuadMesh from '../../renderers/common/QuadMesh.js'; | ||
|
||
import { RenderTarget } from '../../core/RenderTarget.js'; | ||
import { Vector2 } from '../../math/Vector2.js'; | ||
import { HalfFloatType } from '../../constants.js'; | ||
|
||
const _quadMesh = new QuadMesh( new NodeMaterial() ); | ||
const _size = new Vector2(); | ||
|
||
class RTTNode extends TextureNode { | ||
|
||
constructor( node, width = null, height = null, options = { type: HalfFloatType } ) { | ||
|
||
const renderTarget = new RenderTarget( width, height, options ); | ||
|
||
super( renderTarget.texture, uv() ); | ||
|
||
this.node = node; | ||
this.width = width; | ||
this.height = height; | ||
|
||
this.renderTarget = renderTarget; | ||
|
||
this.textureNeedsUpdate = true; | ||
this.autoUpdate = true; | ||
|
||
this.updateMap = new WeakMap(); | ||
|
||
this.updateBeforeType = NodeUpdateType.RENDER; | ||
|
||
} | ||
|
||
get autoSize() { | ||
|
||
return this.width === null; | ||
|
||
} | ||
|
||
setSize( width, height ) { | ||
|
||
this.width = width; | ||
this.height = height; | ||
|
||
this.renderTarget.setSize( width, height ); | ||
|
||
this.textureNeedsUpdate = true; | ||
|
||
} | ||
|
||
updateBefore( { renderer } ) { | ||
|
||
if ( this.textureNeedsUpdate === false && this.autoUpdate === false ) return; | ||
|
||
this.textureNeedsUpdate = false; | ||
|
||
// | ||
|
||
if ( this.autoSize === true ) { | ||
|
||
const size = renderer.getSize( _size ); | ||
|
||
this.setSize( size.width, size.height ); | ||
|
||
} | ||
|
||
// | ||
|
||
_quadMesh.material.fragmentNode = this.node; | ||
|
||
// | ||
|
||
const currentRenderTarget = renderer.getRenderTarget(); | ||
|
||
renderer.setRenderTarget( this.renderTarget ); | ||
|
||
_quadMesh.render( renderer ); | ||
|
||
renderer.setRenderTarget( currentRenderTarget ); | ||
|
||
} | ||
|
||
clone() { | ||
|
||
const newNode = new TextureNode( this.value, this.uvNode, this.levelNode ); | ||
newNode.sampler = this.sampler; | ||
newNode.referenceNode = this; | ||
|
||
return newNode; | ||
|
||
} | ||
|
||
} | ||
|
||
export default RTTNode; | ||
|
||
export const rtt = ( node, ...params ) => nodeObject( new RTTNode( nodeObject( node ), ...params ) ); | ||
|
||
addNodeElement( 'toTexture', ( node, ...params ) => node.isTextureNode ? node : rtt( node, ...params ) ); | ||
|
||
addNodeClass( 'RTTNode', RTTNode ); |