-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
TSL: Introduce Override Context #32328
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
Open
RenaudRohlinger
wants to merge
1
commit into
mrdoob:dev
Choose a base branch
from
RenaudRohlinger:utsubo/feat/override-context
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−0
Open
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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,136 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>Three.js webgpu - tsl override context</title> | ||
| <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="example.css"> | ||
| </head> | ||
| <body> | ||
|
|
||
| <div id="info"> | ||
| <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a> - WebGPU - TSL Override Context<br /> | ||
| Left: Default Normals | Right: Overridden Normals (Rotated via Context) | ||
| </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 { float, vec3, vec4, mat4, normalLocal, positionLocal, Fn, overrideContext, instanceIndex, transformNormal } from 'three/tsl'; | ||
|
|
||
| import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; | ||
|
|
||
| let camera, scene, renderer; | ||
|
|
||
| init(); | ||
|
|
||
| function init() { | ||
|
|
||
| camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 ); | ||
| camera.position.z = 10; | ||
|
|
||
| scene = new THREE.Scene(); | ||
|
|
||
| // TSL Function that uses normalLocal | ||
| const visualizeNormal = Fn( () => { | ||
|
|
||
| return normalLocal.normalize().mul( 0.5 ).add( 0.5 ); | ||
|
|
||
| } ); | ||
|
|
||
| const geometry = new THREE.SphereGeometry( 0.5, 32, 16 ); | ||
| const count = 5; | ||
|
|
||
| // 1. Left Mesh: Instanced, Vertical Stack, Default Normals | ||
| const material1 = new THREE.MeshBasicNodeMaterial(); | ||
|
|
||
| // Move instances in Y based on instanceIndex | ||
| const offset1 = vec3( 0, instanceIndex.toFloat().mul( 1.5 ).sub( 3 ), 0 ); | ||
| material1.positionNode = positionLocal.add( offset1 ); | ||
|
|
||
| material1.colorNode = visualizeNormal(); | ||
|
|
||
| const mesh1 = new THREE.Mesh( geometry, material1 ); | ||
| mesh1.position.x = - 2; | ||
| mesh1.count = count; // Enable instancing | ||
| scene.add( mesh1 ); | ||
|
|
||
| // 2. Right Mesh: Instanced, Vertical Stack, Overridden Normals | ||
| const material2 = new THREE.MeshBasicNodeMaterial(); | ||
|
|
||
| // Move instances in Y based on instanceIndex | ||
| const y = instanceIndex.toFloat().mul( 1.5 ).sub( 3 ); | ||
| const offset2 = vec3( 0, y, 0 ); | ||
| material2.positionNode = positionLocal.add( offset2 ); | ||
|
|
||
| // Construct instance matrix with rotation to demonstrate normal transformation | ||
| const angle = instanceIndex.toFloat().mul( 0.5 ); | ||
| const s = angle.sin(); | ||
| const c = angle.cos(); | ||
|
|
||
| // Rotation around Y axis + Translation | ||
| const instanceMatrixNode = mat4( | ||
| vec4( c, 0, s, 0 ), | ||
| vec4( 0, 1, 0, 0 ), | ||
| vec4( s.negate(), 0, c, 0 ), | ||
| vec4( 0, y, 0, 1 ) | ||
| ); | ||
|
|
||
| // Transform normalLocal using the instance matrix | ||
| // Note: Since it's a pure translation, the normal direction won't change, | ||
| // but this demonstrates the mechanism. | ||
| const instanceNormal = transformNormal( normalLocal, instanceMatrixNode ); | ||
|
|
||
| // Override normalLocal in the visualizeNormal function context | ||
| material2.colorNode = overrideContext( visualizeNormal(), normalLocal, instanceNormal ); | ||
|
|
||
| const mesh2 = new THREE.Mesh( geometry, material2 ); | ||
| mesh2.position.x = 2; | ||
| mesh2.count = count; // Enable instancing | ||
| scene.add( mesh2 ); | ||
|
|
||
| // | ||
|
|
||
| renderer = new THREE.WebGPURenderer( { antialias: true } ); | ||
| renderer.setPixelRatio( window.devicePixelRatio ); | ||
| renderer.setSize( window.innerWidth, window.innerHeight ); | ||
| renderer.setAnimationLoop( animate ); | ||
| document.body.appendChild( renderer.domElement ); | ||
|
|
||
| const controls = new OrbitControls( camera, renderer.domElement ); | ||
| controls.enableDamping = true; | ||
|
|
||
| window.addEventListener( 'resize', onWindowResize ); | ||
|
|
||
| } | ||
|
|
||
| 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> | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note