Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
lmj01 committed Jul 4, 2024
2 parents 4e15d89 + baf57f5 commit c53f65a
Show file tree
Hide file tree
Showing 10 changed files with 73,938 additions and 50 deletions.
73,784 changes: 73,784 additions & 0 deletions build/three.webgpu.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions build/three.webgpu.min.js

Large diffs are not rendered by default.

31 changes: 27 additions & 4 deletions examples/jsm/nodes/accessors/BatchNode.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Node, { addNodeClass } from '../core/Node.js';
import { normalLocal } from './NormalNode.js';
import { positionLocal } from './PositionNode.js';
import { nodeProxy, vec3, mat3, mat4, int, ivec2, float } from '../shadernode/ShaderNode.js';
import { nodeProxy, vec3, mat3, mat4, int, ivec2, float, tslFn } from '../shadernode/ShaderNode.js';
import { textureLoad } from './TextureNode.js';
import { textureSize } from './TextureSizeNode.js';
import { attribute } from '../core/AttributeNode.js';
import { tangentLocal } from './TangentNode.js';
import { instanceIndex, drawIndex } from '../core/IndexNode.js';

class BatchNode extends Node {

Expand All @@ -28,14 +28,37 @@ class BatchNode extends Node {

if ( this.batchingIdNode === null ) {

this.batchingIdNode = attribute( 'batchId' );
if ( builder.getDrawIndex() === null ) {

this.batchingIdNode = instanceIndex;

} else {

this.batchingIdNode = drawIndex;

}

}

const getIndirectIndex = tslFn( ( [ id ] ) => {

const size = textureSize( textureLoad( this.batchMesh._indirectTexture ), 0 );
const x = int( id ).remainder( int( size ) );
const y = int( id ).div( int( size ) );
return textureLoad( this.batchMesh._indirectTexture, ivec2( x, y ) ).x;

} ).setLayout( {
name: 'getIndirectIndex',
type: 'uint',
inputs: [
{ name: 'id', type: 'int' }
]
} );

const matriceTexture = this.batchMesh._matricesTexture;

const size = textureSize( textureLoad( matriceTexture ), 0 );
const j = float( int( this.batchingIdNode ) ).mul( 4 ).toVar();
const j = float( getIndirectIndex( int( this.batchingIdNode ) ) ).mul( 4 ).toVar();

const x = int( j.mod( size ) );
const y = int( j ).div( int( size ) );
Expand Down
27 changes: 3 additions & 24 deletions examples/jsm/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,38 +169,17 @@ class NodeBuilder {

const bindGroupsCache = this.getBingGroupsCache();

// cache individual uniforms group
//

const bindingsArray = [];

let sharedGroup = true;

for ( const binding of bindings ) {

if ( binding.groupNode.shared === true ) {

// nodes is the chainmap key
const nodes = binding.getNodes();

let sharedBinding = bindGroupsCache.get( nodes );

if ( sharedBinding === undefined ) {
bindingsArray.push( binding );

bindGroupsCache.set( nodes, binding );

sharedBinding = binding;

}

bindingsArray.push( sharedBinding );

} else {

bindingsArray.push( binding );

sharedGroup = false;

}
sharedGroup = sharedGroup && binding.groupNode.shared !== true;

}

Expand Down
38 changes: 36 additions & 2 deletions examples/jsm/nodes/materials/MeshBasicNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';

import { MeshBasicMaterial } from 'three';
import { diffuseColor } from '../core/PropertyNode.js';
import { materialSpecularStrength, materialReflectivity } from '../accessors/MaterialNode.js';
import { MeshBasicMaterial } from '../../materials/MeshBasicMaterial.js';
import { MultiplyOperation, MixOperation, AddOperation } from '../../constants.js';
import { mix } from '../math/MathNode.js';

const defaultValues = new MeshBasicMaterial();

Expand All @@ -21,6 +24,37 @@ class MeshBasicNodeMaterial extends NodeMaterial {

}

setupVariants( builder ) {

const envNode = this.getEnvNode( builder );

if ( envNode !== null ) {

switch ( this.combine ) {

case MultiplyOperation:
diffuseColor.assign( mix( diffuseColor.rgb, diffuseColor.rgb.mul( envNode.rgb ), materialSpecularStrength.mul( materialReflectivity ) ) );
break;

case MixOperation:
diffuseColor.assign( mix( diffuseColor.rgb, envNode.rgb, materialSpecularStrength.mul( materialReflectivity ) ) );
break;

case AddOperation:
diffuseColor.addAssign( envNode.rgb.mul( materialSpecularStrength.mul( materialReflectivity ) ) );
break;

default:
console.warn( 'THREE.MeshBasicNodeMaterial: Unsupported .combine value:', this.combine );
break;

}

}


}

}

export default MeshBasicNodeMaterial;
Expand Down
32 changes: 26 additions & 6 deletions examples/webgl_materials_channels.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<body>

<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - <span id="description">Normal, Velocity, Depth, DepthRGBA, DepthRGBAUnpacked, Materials</span><br/>
by <a href="https://Clara.io">Ben Houston</a>. ninja head from <a href="https://gpuopen.com/archive/gamescgi/amd-gpu-meshmapper/" target="_blank" rel="noopener">AMD GPU MeshMapper</a>
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - <span id="description">Normal, Velocity, and Depth Materials</span><br/>
ninja head from <a href="https://gpuopen.com/archive/gamescgi/amd-gpu-meshmapper/" target="_blank" rel="noopener">AMD GPU MeshMapper</a>
</div>

<script type="importmap">
Expand Down Expand Up @@ -53,7 +53,7 @@
let cameraOrtho, cameraPerspective;
let controlsOrtho, controlsPerspective;

let mesh, materialStandard, materialDepthBasic, materialDepthRGBA, materialNormal, materialVelocity;
let mesh, materialStandard, materialDepthBasic, materialDepthRGBA, materialDepthRGB, materialDepthRG, materialNormal, materialVelocity;

const SCALE = 2.436143; // from original model
const BIAS = - 0.428408; // from original model
Expand Down Expand Up @@ -90,13 +90,11 @@
controlsPerspective = new OrbitControls( cameraPerspective, renderer.domElement );
controlsPerspective.minDistance = 1000;
controlsPerspective.maxDistance = 2400;
controlsPerspective.enablePan = false;
controlsPerspective.enableDamping = true;

controlsOrtho = new OrbitControls( cameraOrtho, renderer.domElement );
controlsOrtho.minZoom = 0.5;
controlsOrtho.maxZoom = 1.5;
controlsOrtho.enablePan = false;
controlsOrtho.enableDamping = true;

// lights
Expand Down Expand Up @@ -165,6 +163,26 @@
side: THREE.DoubleSide
} );

materialDepthRGB = new THREE.MeshDepthMaterial( {
depthPacking: THREE.RGBDepthPacking,

displacementMap: displacementMap,
displacementScale: SCALE,
displacementBias: BIAS,

side: THREE.DoubleSide
} );

materialDepthRG = new THREE.MeshDepthMaterial( {
depthPacking: THREE.RGDepthPacking,

displacementMap: displacementMap,
displacementScale: SCALE,
displacementBias: BIAS,

side: THREE.DoubleSide
} );

materialNormal = new THREE.MeshNormalMaterial( {
displacementMap: displacementMap,
displacementScale: SCALE,
Expand Down Expand Up @@ -213,7 +231,7 @@
//

const gui = new GUI();
gui.add( params, 'material', [ 'standard', 'normal', 'velocity', 'depthBasic', 'depthRGBA' ] );
gui.add( params, 'material', [ 'standard', 'normal', 'velocity', 'depthBasic', 'depthRGBA', 'depthRGB', 'depthRG' ] );
gui.add( params, 'camera', [ 'perspective', 'ortho' ] );
gui.add( params, 'side', [ 'front', 'back', 'double' ] );

Expand Down Expand Up @@ -263,6 +281,8 @@
case 'standard': material = materialStandard; break;
case 'depthBasic': material = materialDepthBasic; break;
case 'depthRGBA': material = materialDepthRGBA; break;
case 'depthRGB': material = materialDepthRGB; break;
case 'depthRG': material = materialDepthRG; break;
case 'normal': material = materialNormal; break;
case 'velocity': material = materialVelocity; break;

Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_compute_particles_snow.html
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
const vignet = viewportTopLeft.distance( .5 ).mul( 1.35 ).clamp().oneMinus();

const teapotTreePass = pass( teapotTree, camera ).getTextureNode();
const teapotTreePassBlurred = teapotTreePass.gaussianBlur( 3 );
const teapotTreePassBlurred = teapotTreePass.gaussianBlur( vec2( 1 ), 3 );
teapotTreePassBlurred.resolution = new THREE.Vector2( .2, .2 );

const scenePassColorBlurred = scenePassColor.gaussianBlur();
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ export const TriangleStripDrawMode = 1;
export const TriangleFanDrawMode = 2;
export const BasicDepthPacking = 3200;
export const RGBADepthPacking = 3201;
export const RGBDepthPacking = 3202;
export const RGDepthPacking = 3203;
export const TangentSpaceNormalMap = 0;
export const ObjectSpaceNormalMap = 1;

Expand Down
58 changes: 45 additions & 13 deletions src/renderers/shaders/ShaderChunk/packing.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,68 @@ vec3 unpackRGBToNormal( const in vec3 rgb ) {
const float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1)
const float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1)
const float ShiftRight8 = 1. / 256.;
const float Inv255 = 1. / 255.;
const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
const vec4 PackFactors = vec4( 1.0, 256.0, 256.0 * 256.0, 256.0 * 256.0 * 256.0 );
const float ShiftRight8 = 1. / 256.;
const vec2 UnpackFactors2 = vec2( UnpackDownscale, 1.0 / PackFactors.g );
const vec3 UnpackFactors3 = vec3( UnpackDownscale / PackFactors.rg, 1.0 / PackFactors.b );
const vec4 UnpackFactors4 = vec4( UnpackDownscale / PackFactors.rgb, 1.0 / PackFactors.a );
vec4 packDepthToRGBA( const in float v ) {
vec4 r = vec4( fract( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
if( v <= 0.0 )
return vec4( 0., 0., 0., 0. );
if( v >= 1.0 )
return vec4( 1., 1., 1., 1. );
float vuf;
float af = modf( v * PackFactors.a, vuf );
float bf = modf( vuf * ShiftRight8, vuf );
float gf = modf( vuf * ShiftRight8, vuf );
return vec4( vuf * Inv255, gf * PackUpscale, bf * PackUpscale, af );
}
vec3 packDepthToRGB( const in float v ) {
if( v <= 0.0 )
return vec3( 0., 0., 0. );
if( v >= 1.0 )
return vec3( 1., 1., 1. );
float vuf;
float bf = modf( v * PackFactors.b, vuf );
float gf = modf( vuf * ShiftRight8, vuf );
// the 0.9999 tweak is unimportant, very tiny empirical improvement
// return vec3( vuf * Inv255, gf * PackUpscale, bf * 0.9999 );
return vec3( vuf * Inv255, gf * PackUpscale, bf );
}
vec2 packDepthToRG( const in float v ) {
if( v <= 0.0 )
return vec2( 0., 0. );
if( v >= 1.0 )
return vec2( 1., 1. );
float vuf;
float gf = modf( v * 256., vuf );
return vec2( vuf * Inv255, gf );
}
float unpackRGBAToDepth( const in vec4 v ) {
return dot( v, UnpackFactors );
return dot( v, UnpackFactors4 );
}
vec2 packDepthToRG( in highp float v ) {
return packDepthToRGBA( v ).yx;
float unpackRGBToDepth( const in vec3 v ) {
return dot( v, UnpackFactors3 );
}
float unpackRGToDepth( const in highp vec2 v ) {
return unpackRGBAToDepth( vec4( v.xy, 0.0, 0.0 ) );
float unpackRGToDepth( const in vec2 v ) {
return v.r * UnpackFactors2.r + v.g * UnpackFactors2.g;
}
vec4 pack2HalfToRGBA( vec2 v ) {
vec4 pack2HalfToRGBA( const in vec2 v ) {
vec4 r = vec4( v.x, fract( v.x * 255.0 ), v.y, fract( v.y * 255.0 ) );
return vec4( r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w );
}
vec2 unpackRGBATo2Half( vec4 v ) {
vec2 unpackRGBATo2Half( const in vec4 v ) {
return vec2( v.x + ( v.y / 255.0 ), v.z + ( v.w / 255.0 ) );
}
Expand Down
8 changes: 8 additions & 0 deletions src/renderers/shaders/ShaderLib/depth.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ void main() {
gl_FragColor = packDepthToRGBA( fragCoordZ );
#elif DEPTH_PACKING == 3202
gl_FragColor = vec4( packDepthToRGB( fragCoordZ ), 1.0 );
#elif DEPTH_PACKING == 3203
gl_FragColor = vec4( packDepthToRG( fragCoordZ ), 0.0, 1.0 );
#endif
}
Expand Down

0 comments on commit c53f65a

Please sign in to comment.