Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ <h2>TSL</h2>
<li><a href="TSL.html#code">code</a></li>
<li><a href="TSL.html#colorBleeding">colorBleeding</a></li>
<li><a href="TSL.html#colorSpaceToWorking">colorSpaceToWorking</a></li>
<li><a href="TSL.html#colorToDirection">colorToDirection</a></li>
<li><a href="TSL.html#unpackRGBToNormal">unpackRGBToNormal</a></li>
<li><a href="TSL.html#compute">compute</a></li>
<li><a href="TSL.html#computeBuiltin">computeBuiltin</a></li>
<li><a href="TSL.html#computeKernel">computeKernel</a></li>
Expand Down Expand Up @@ -1126,7 +1126,7 @@ <h2>TSL</h2>
<li><a href="TSL.html#difference">difference</a></li>
<li><a href="TSL.html#diffuseColor">diffuseColor</a></li>
<li><a href="TSL.html#diffuseContribution">diffuseContribution</a></li>
<li><a href="TSL.html#directionToColor">directionToColor</a></li>
<li><a href="TSL.html#packNormalToRGB">packNormalToRGB</a></li>
<li><a href="TSL.html#directionToFaceDirection">directionToFaceDirection</a></li>
<li><a href="TSL.html#dispersion">dispersion</a></li>
<li><a href="TSL.html#distance">distance</a></li>
Expand Down
10 changes: 5 additions & 5 deletions docs/llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1403,8 +1403,8 @@ const matcap = texture( matcapMap, matcapUV );

| Variable | Description | Type |
| -- | -- | -- |
| `directionToColor( value )` | Converts direction vector to color. | `color` |
| `colorToDirection( value )` | Converts color to direction vector. | `vec3` |
| `packNormalToRGB( value )` | Converts normal vector to color. | `color` |
| `unpackRGBToNormal( value )` | Converts color to normal vector. | `vec3` |

## Render Pipeline

Expand Down Expand Up @@ -1442,18 +1442,18 @@ MRT allows capturing multiple outputs from a single render pass. Instead of rend
Use `setMRT()` with the `mrt()` function to define which outputs to capture:

```js
import { pass, mrt, output, normalView, velocity, directionToColor } from 'three/tsl';
import { pass, mrt, output, normalView, velocity, packNormalToRGB } from 'three/tsl';

const scenePass = pass( scene, camera );

scenePass.setMRT( mrt( {
output: output, // Final color output
normal: directionToColor( normalView ), // View-space normals encoded as colors
normal: packNormalToRGB( normalView ), // View-space normals encoded as colors
velocity: velocity // Motion vectors for temporal effects
} ) );
```

Each MRT entry accepts any TSL node, allowing you to customize outputs using formulas, encoders, or material accessors. For example, `directionToColor( normalView )` encodes view-space normals into RGB values. You can use any TSL function to transform, combine, or encode data before writing to the render target.
Each MRT entry accepts any TSL node, allowing you to customize outputs using formulas, encoders, or material accessors. For example, `packNormalToRGB( normalView )` encodes view-space normals into RGB values. You can use any TSL function to transform, combine, or encode data before writing to the render target.

Within a TSL function `Fn( ( { material, object } ) => { ... } )`, you have complete access to the current material and object being rendered, enabling full customization of outputs.

Expand Down
4 changes: 2 additions & 2 deletions docs/pages/TSL.html
Original file line number Diff line number Diff line change
Expand Up @@ -3487,7 +3487,7 @@ <h3 class="name name-method" id="colorSpaceToWorking" translate="no">.<a href="#
</tbody>
</table>
</div>
<h3 class="name name-method" id="colorToDirection" translate="no">.<a href="#colorToDirection">colorToDirection</a><span class="signature">( node : <span class="param-type"><a href="Node.html">Node</a>.&lt;vec3></span> )</span><span class="type-signature"> : <a href="Node.html">Node</a>.&lt;vec3></span> </h3>
<h3 class="name name-method" id="unpackRGBToNormal" translate="no">.<a href="#unpackRGBToNormal">unpackRGBToNormal</a><span class="signature">( node : <span class="param-type"><a href="Node.html">Node</a>.&lt;vec3></span> )</span><span class="type-signature"> : <a href="Node.html">Node</a>.&lt;vec3></span> </h3>
<div class="method">
<div class="description">
<p>Unpacks a color value into a direction vector.</p>
Expand Down Expand Up @@ -4251,7 +4251,7 @@ <h3 class="name name-method" id="difference" translate="no">.<a href="#differenc
</tbody>
</table>
</div>
<h3 class="name name-method" id="directionToColor" translate="no">.<a href="#directionToColor">directionToColor</a><span class="signature">( node : <span class="param-type"><a href="Node.html">Node</a>.&lt;vec3></span> )</span><span class="type-signature"> : <a href="Node.html">Node</a>.&lt;vec3></span> </h3>
<h3 class="name name-method" id="packNormalToRGB" translate="no">.<a href="#packNormalToRGB">packNormalToRGB</a><span class="signature">( node : <span class="param-type"><a href="Node.html">Node</a>.&lt;vec3></span> )</span><span class="type-signature"> : <a href="Node.html">Node</a>.&lt;vec3></span> </h3>
<div class="method">
<div class="description">
<p>Packs a direction vector into a color value.</p>
Expand Down
4 changes: 2 additions & 2 deletions examples/webgpu_mesh_batch.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { radixSort } from 'three/addons/utils/SortUtils.js';

import { normalView, directionToColor, diffuseColor, vec4 } from 'three/tsl';
import { normalView, packNormalToRGB, diffuseColor, vec4 } from 'three/tsl';

let camera, scene, renderer;
let controls;
Expand Down Expand Up @@ -131,7 +131,7 @@

material = new THREE.MeshBasicNodeMaterial();
material.outputNode = vec4(
diffuseColor.mul( directionToColor( normalView ).y.add( 0.5 ) ).rgb,
diffuseColor.mul( packNormalToRGB( normalView ).y.add( 0.5 ) ).rgb,
diffuseColor.a,
);

Expand Down
4 changes: 2 additions & 2 deletions examples/webgpu_mrt.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<script type="module">

import * as THREE from 'three/webgpu';
import { output, normalView, pass, step, diffuseColor, emissive, directionToColor, screenUV, mix, mrt, Fn } from 'three/tsl';
import { output, normalView, pass, step, diffuseColor, emissive, packNormalToRGB, screenUV, mix, mrt, Fn } from 'three/tsl';

import { Inspector } from 'three/addons/inspector/Inspector.js';

Expand Down Expand Up @@ -99,7 +99,7 @@
const scenePass = pass( scene, camera, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter } );
scenePass.setMRT( mrt( {
output: output,
normal: directionToColor( normalView ),
normal: packNormalToRGB( normalView ),
diffuse: diffuseColor,
emissive: emissive
} ) );
Expand Down
8 changes: 4 additions & 4 deletions examples/webgpu_multisampled_renderbuffers.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

const params = {
animated: true,
samples: 4
multisampling: true
};

const mat4 = new THREE.Matrix4();
Expand Down Expand Up @@ -81,7 +81,7 @@
function initGUI() {

const gui = renderer.inspector.createParameters( 'Settings' );
gui.add( params, 'samples', 0, 4, 1 );
gui.add( params, 'multisampling' );
gui.add( params, 'animated' );

}
Expand Down Expand Up @@ -126,7 +126,7 @@
document.body.appendChild( renderer.domElement );

renderTarget = new THREE.RenderTarget( window.innerWidth * dpr, window.innerHeight * dpr, {
samples: params.samples,
samples: params.multisampling ? 4 : 1,
depthBuffer: true,
} );

Expand Down Expand Up @@ -172,7 +172,7 @@

}

renderTarget.samples = params.samples;
renderTarget.samples = params.multisampling ? 4 : 1;

renderer.setRenderTarget( renderTarget );
renderer.render( scene, camera );
Expand Down
6 changes: 3 additions & 3 deletions examples/webgpu_postprocessing_ao.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<script type="module">

import * as THREE from 'three/webgpu';
import { sample, pass, mrt, screenUV, normalView, velocity, vec3, vec4, directionToColor, colorToDirection, colorSpaceToWorking, builtinAOContext } from 'three/tsl';
import { sample, pass, mrt, screenUV, normalView, velocity, vec3, vec4, packNormalToRGB, unpackRGBToNormal, colorSpaceToWorking, builtinAOContext } from 'three/tsl';
import { ao } from 'three/addons/tsl/display/GTAONode.js';
import { traa } from 'three/addons/tsl/display/TRAANode.js';

Expand Down Expand Up @@ -115,13 +115,13 @@
prePass.transparent = false;

prePass.setMRT( mrt( {
output: directionToColor( normalView ),
output: packNormalToRGB( normalView ),
velocity: velocity
} ) );

const prePassNormal = sample( ( uv ) => {

return colorToDirection( prePass.getTextureNode().sample( uv ) );
return unpackRGBToNormal( prePass.getTextureNode().sample( uv ) );

} );

Expand Down
6 changes: 3 additions & 3 deletions examples/webgpu_postprocessing_ssgi.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<script type="module">

import * as THREE from 'three/webgpu';
import { pass, mrt, output, normalView, diffuseColor, velocity, add, vec3, vec4, directionToColor, colorToDirection, sample } from 'three/tsl';
import { pass, mrt, output, normalView, diffuseColor, velocity, add, vec3, vec4, packNormalToRGB, unpackRGBToNormal, sample } from 'three/tsl';
import { ssgi } from 'three/addons/tsl/display/SSGINode.js';
import { traa } from 'three/addons/tsl/display/TRAANode.js';

Expand Down Expand Up @@ -81,7 +81,7 @@
scenePass.setMRT( mrt( {
output: output,
diffuseColor: diffuseColor,
normal: directionToColor( normalView ),
normal: packNormalToRGB( normalView ),
velocity: velocity
} ) );

Expand All @@ -106,7 +106,7 @@

const sceneNormal = sample( ( uv ) => {

return colorToDirection( scenePassNormal.sample( uv ) );
return unpackRGBToNormal( scenePassNormal.sample( uv ) );

} );

Expand Down
10 changes: 5 additions & 5 deletions examples/webgpu_postprocessing_ssgi_ballpool.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<script type="module">

import * as THREE from 'three/webgpu';
import { pass, mrt, output, normalView, diffuseColor, velocity, add, vec4, directionToColor, colorToDirection, sample } from 'three/tsl';
import { pass, mrt, output, normalView, diffuseColor, velocity, add, vec4, packNormalToRGB, unpackRGBToNormal, sample } from 'three/tsl';
import { ssgi } from 'three/addons/tsl/display/SSGINode.js';
import { traa } from 'three/addons/tsl/display/TRAANode.js';
import { World } from '@perplexdotgg/bounce';
Expand All @@ -68,8 +68,8 @@
let wallMeshes = [];
const boxSize = { w: 8, h: BOX_HEIGHT, d: BOX_DEPTH };

let mouseRayOrigin = new THREE.Vector3();
let mouseRayDir = new THREE.Vector3();
const mouseRayOrigin = new THREE.Vector3();
const mouseRayDir = new THREE.Vector3();
let mouseMoving = false;
let mouseStopTimer = 0;
let pointerDown = false;
Expand Down Expand Up @@ -102,7 +102,7 @@
scenePass.setMRT( mrt( {
output: output,
diffuseColor: diffuseColor,
normal: directionToColor( normalView ),
normal: packNormalToRGB( normalView ),
velocity: velocity
} ) );

Expand All @@ -122,7 +122,7 @@

const sceneNormal = sample( ( uv ) => {

return colorToDirection( scenePassNormal.sample( uv ) );
return unpackRGBToNormal( scenePassNormal.sample( uv ) );

} );

Expand Down
6 changes: 3 additions & 3 deletions examples/webgpu_postprocessing_ssr.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<script type="module">

import * as THREE from 'three/webgpu';
import { pass, mrt, output, normalView, metalness, roughness, screenUV, color, sample, directionToColor, colorToDirection, vec2, colorSpaceToWorking } from 'three/tsl';
import { pass, mrt, output, normalView, metalness, roughness, screenUV, color, sample, packNormalToRGB, unpackRGBToNormal, vec2, colorSpaceToWorking } from 'three/tsl';
import { ssr } from 'three/addons/tsl/display/SSRNode.js';
import { smaa } from 'three/addons/tsl/display/SMAANode.js';

Expand Down Expand Up @@ -146,7 +146,7 @@
const scenePass = pass( scene, camera );
scenePass.setMRT( mrt( {
output: output,
normal: directionToColor( normalView ),
normal: packNormalToRGB( normalView ),
metalrough: vec2( metalness, roughness ) // pack metalness and roughness into a single attachment
} ) );

Expand All @@ -173,7 +173,7 @@

const sceneNormal = sample( ( uv ) => {

return colorToDirection( scenePassNormal.sample( uv ) );
return unpackRGBToNormal( scenePassNormal.sample( uv ) );

} );

Expand Down
6 changes: 3 additions & 3 deletions examples/webgpu_test_memory.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<script type="module">

import * as THREE from 'three/webgpu';
import { pass, mrt, directionToColor, normalView, screenUV, context, sample, colorToDirection } from 'three/tsl';
import { pass, mrt, packNormalToRGB, normalView, screenUV, context, sample, unpackRGBToNormal } from 'three/tsl';
import { outline } from 'three/addons/tsl/display/OutlineNode.js';
import { ao } from 'three/addons/tsl/display/GTAONode.js';

Expand Down Expand Up @@ -193,12 +193,12 @@

prePass = pass( scene, camera );
prePass.setMRT( mrt( {
output: directionToColor( normalView )
output: packNormalToRGB( normalView )
} ) );

const prePassNormal = sample( ( uv ) => {

return colorToDirection( prePass.getTextureNode().sample( uv ) );
return unpackRGBToNormal( prePass.getTextureNode().sample( uv ) );

} );
const prePassDepth = prePass.getTextureNode( 'depth' );
Expand Down
2 changes: 2 additions & 0 deletions src/Three.TSL.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export const overloadingFn = TSL.overloadingFn;
export const packHalf2x16 = TSL.packHalf2x16;
export const packSnorm2x16 = TSL.packSnorm2x16;
export const packUnorm2x16 = TSL.packUnorm2x16;
export const packNormalToRGB = TSL.packNormalToRGB;
export const parabola = TSL.parabola;
export const parallaxDirection = TSL.parallaxDirection;
export const parallaxUV = TSL.parallaxUV;
Expand Down Expand Up @@ -588,6 +589,7 @@ export const uniformTexture = TSL.uniformTexture;
export const unpackHalf2x16 = TSL.unpackHalf2x16;
export const unpackSnorm2x16 = TSL.unpackSnorm2x16;
export const unpackUnorm2x16 = TSL.unpackUnorm2x16;
export const unpackRGBToNormal = TSL.unpackRGBToNormal;
export const unpremultiplyAlpha = TSL.unpremultiplyAlpha;
export const userData = TSL.userData;
export const uv = TSL.uv;
Expand Down
4 changes: 2 additions & 2 deletions src/materials/nodes/MeshNormalNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NodeMaterial from './NodeMaterial.js';
import { diffuseColor } from '../../nodes/core/PropertyNode.js';
import { directionToColor } from '../../nodes/utils/Packing.js';
import { packNormalToRGB } from '../../nodes/utils/Packing.js';
import { materialOpacity } from '../../nodes/accessors/MaterialNode.js';
import { normalView } from '../../nodes/accessors/Normal.js';
import { colorSpaceToWorking } from '../../nodes/display/ColorSpaceNode.js';
Expand Down Expand Up @@ -58,7 +58,7 @@ class MeshNormalNodeMaterial extends NodeMaterial {

// By convention, a normal packed to RGB is in sRGB color space. Convert it to working color space.

diffuseColor.assign( colorSpaceToWorking( vec4( directionToColor( normalView ), opacityNode ), SRGBColorSpace ) );
diffuseColor.assign( colorSpaceToWorking( vec4( packNormalToRGB( normalView ), opacityNode ), SRGBColorSpace ) );

}

Expand Down
39 changes: 35 additions & 4 deletions src/nodes/utils/Packing.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { nodeObject, vec3, float } from '../tsl/TSLBase.js';
import { dot, sqrt, saturate } from '../math/MathNode.js';
import { warnOnce } from '../../utils.js';

/**
* Packs a direction vector into a color value.
* Packs a normal vector into a color value.
*
* @tsl
* @function
* @param {Node<vec3>} node - The direction to pack.
* @return {Node<vec3>} The color.
*/
export const directionToColor = ( node ) => nodeObject( node ).mul( 0.5 ).add( 0.5 );
export const packNormalToRGB = ( node ) => nodeObject( node ).mul( 0.5 ).add( 0.5 );

/**
* Unpacks a color value into a direction vector.
* Unpacks a color value into a normal vector.
*
* @tsl
* @function
* @param {Node<vec3>} node - The color to unpack.
* @return {Node<vec3>} The direction.
*/
export const colorToDirection = ( node ) => nodeObject( node ).mul( 2.0 ).sub( 1 );
export const unpackRGBToNormal = ( node ) => nodeObject( node ).mul( 2.0 ).sub( 1 );

/**
* Unpacks a tangent space normal, reconstructing the Z component by projecting the X,Y coordinates onto the hemisphere.
Expand All @@ -31,3 +32,33 @@ export const colorToDirection = ( node ) => nodeObject( node ).mul( 2.0 ).sub( 1
* @return {Node<vec3>} The resulting normal.
*/
export const unpackNormal = ( xy ) => vec3( xy, sqrt( saturate( float( 1.0 ).sub( dot( xy, xy ) ) ) ) );

/**
* @tsl
* @function
* @deprecated since r185. Use {@link packNormalToRGB} instead.
* @param {Node<vec3>} node - The direction to pack.
* @returns {Node<vec3>}
*/
export const directionToColor = ( node ) => {

warnOnce( 'TSL: "directionToColor()" has been renamed to "packNormalToRGB()".' ); // @deprecated r185

return packNormalToRGB( node );

};

/**
* @tsl
* @function
* @deprecated since r185. Use {@link unpackRGBToNormal} instead.
* @param {Node<vec3>} node - The color to unpack.
* @returns {Node<vec3>}
*/
export const colorToDirection = ( node ) => {

warnOnce( 'TSL: "colorToDirection()" has been renamed to "unpackRGBToNormal()".' ); // @deprecated r185

return unpackRGBToNormal( node );

};
2 changes: 1 addition & 1 deletion src/renderers/webgpu/utils/WebGPUUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class WebGPUUtils {

}

samples = samples || 1;
samples = this.getSampleCount( samples || 1 );

const isMSAA = samples > 1 && texture.renderTarget !== null && ( texture.isDepthTexture !== true && texture.isFramebufferTexture !== true );
const primarySamples = isMSAA ? 1 : samples;
Expand Down
Loading