-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TSL: Introduce node.toTexture()
and rtt()
#28773
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2c091ce
TextureNode: Fix analyze reference
sunag 87c971e
Add RTTNode
sunag f2cef2c
rgbShift: add `.toTexture()` and updated example
sunag 30c29c3
Update GaussianBlurNode.js
sunag 19a372f
add procedural texture
sunag 00ea1f7
Update AnamorphicNode.js
sunag b4a07d3
update imports
sunag 2726fc8
update build for now
sunag bf64e77
Merge branch 'dev' into dev-rtt-node
sunag 21742d6
update build
sunag 0a0bc5b
update names
sunag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excited to see
getTextureNode()
is not required here anymore! 🙌