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
40 changes: 31 additions & 9 deletions src/nodes/accessors/Bitangent.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,71 @@
import { varying } from '../core/VaryingNode.js';
import { Fn } from '../tsl/TSLCore.js';
import { cameraViewMatrix } from './Camera.js';
import { normalGeometry, normalLocal, normalView, normalWorld, transformedNormalView } from './Normal.js';
import { tangentGeometry, tangentLocal, tangentView, tangentWorld, transformedTangentView } from './Tangent.js';

const getBitangent = ( crossNormalTangent ) => crossNormalTangent.mul( tangentGeometry.w ).xyz;
/**
* Returns the bitangent node and assigns it to a varying if the material is not flat shaded.
*
* @tsl
* @private
* @param {Node<vec3>} crossNormalTangent - The cross product of the normal and tangent vectors.
* @param {string} varyingName - The name of the varying to assign the bitangent to.
* @returns {Node<vec3>} The bitangent node.
*/
const getBitangent = /*@__PURE__*/ Fn( ( [ crossNormalTangent, varyingName ], builder ) => {

let bitangent = crossNormalTangent.mul( tangentGeometry.w ).xyz;

if ( builder.material.flatShading !== true ) {

bitangent = varying( crossNormalTangent, varyingName );

}

return bitangent;

} ).once();

/**
* TSL object that represents the bitangent attribute of the current rendered object.
*
* @tsl
* @type {Node<vec3>}
*/
export const bitangentGeometry = /*@__PURE__*/ varying( getBitangent( normalGeometry.cross( tangentGeometry ) ), 'v_bitangentGeometry' ).normalize().toVar( 'bitangentGeometry' );
export const bitangentGeometry = /*@__PURE__*/ getBitangent( normalGeometry.cross( tangentGeometry ), 'v_bitangentGeometry' ).normalize().toVar( 'bitangentGeometry' );

/**
* TSL object that represents the vertex bitangent in local space of the current rendered object.
*
* @tsl
* @type {Node<vec3>}
*/
export const bitangentLocal = /*@__PURE__*/ varying( getBitangent( normalLocal.cross( tangentLocal ) ), 'v_bitangentLocal' ).normalize().toVar( 'bitangentLocal' );
export const bitangentLocal = /*@__PURE__*/ getBitangent( normalLocal.cross( tangentLocal ), 'v_bitangentLocal' ).normalize().toVar( 'bitangentLocal' );

/**
* TSL object that represents the vertex bitangent in view space of the current rendered object.
*
* @tsl
* @type {Node<vec4>}
* @type {Node<vec3>}
*/
export const bitangentView = /*@__PURE__*/ varying( getBitangent( normalView.cross( tangentView ) ), 'v_bitangentView' ).normalize().toVar( 'bitangentView' );
export const bitangentView = getBitangent( normalView.cross( tangentView ), 'v_bitangentView' ).normalize().toVar( 'bitangentView' );

/**
* TSL object that represents the vertex bitangent in world space of the current rendered object.
*
* @tsl
* @type {Node<vec4>}
* @type {Node<vec3>}
*/
export const bitangentWorld = /*@__PURE__*/ varying( getBitangent( normalWorld.cross( tangentWorld ) ), 'v_bitangentWorld' ).normalize().toVar( 'bitangentWorld' );
export const bitangentWorld = /*@__PURE__*/ getBitangent( normalWorld.cross( tangentWorld ), 'v_bitangentWorld' ).normalize().toVar( 'bitangentWorld' );

/**
* TSL object that represents the transformed vertex bitangent in view space of the current rendered object.
*
* @tsl
* @type {Node<vec4>}
* @type {Node<vec3>}
*/
export const transformedBitangentView = /*@__PURE__*/ getBitangent( transformedNormalView.cross( transformedTangentView ) ).normalize().toVar( 'transformedBitangentView' );
export const transformedBitangentView = /*@__PURE__*/ getBitangent( transformedNormalView.cross( transformedTangentView ), 'v_transformedBitangentView' ).normalize().toVar( 'transformedBitangentView' );

/**
* TSL object that represents the transformed vertex bitangent in world space of the current rendered object.
Expand Down
14 changes: 13 additions & 1 deletion src/nodes/accessors/Normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,19 @@ export const normalView = /*@__PURE__*/ ( Fn( ( builder ) => {
* @tsl
* @type {Node<vec3>}
*/
export const normalWorld = /*@__PURE__*/ varying( normalView.transformDirection( cameraViewMatrix ), 'v_normalWorld' ).normalize().toVar( 'normalWorld' );
export const normalWorld = /*@__PURE__*/ ( Fn( ( builder ) => {

let normal = normalView.transformDirection( cameraViewMatrix );

if ( builder.material.flatShading !== true ) {

normal = varying( normal, 'v_normalWorld' );

}

return normal;

}, 'vec3' ).once() )().normalize().toVar( 'normalWorld' );

/**
* TSL object that represents the transformed vertex normal in view space of the current rendered object.
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class TextureNode extends UniformNode {

if ( ( uvNode === null || builder.context.forceUVContext === true ) && builder.context.getUV ) {

uvNode = builder.context.getUV( this );
uvNode = builder.context.getUV( this, builder );

}

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ class WGSLNodeBuilder extends NodeBuilder {
const textureDimension = this.generateTextureDimension( texture, textureProperty, levelSnippet );

const vecType = texture.isData3DTexture ? 'vec3' : 'vec2';
const coordSnippet = `${ vecType }<u32>(${ wrapFunction }(${ uvSnippet }) * ${ vecType }<f32>(${ textureDimension }))`;
const coordSnippet = `${ vecType }<u32>( ${ wrapFunction }( ${ uvSnippet } ) * ${ vecType }<f32>( ${ textureDimension } ) )`;

return this.generateTextureLoad( texture, textureProperty, coordSnippet, depthSnippet, levelSnippet );

Expand Down