Skip to content

Commit c337d03

Browse files
authored
MaterialX: add normal, tangent, texcoord, geomcolor, position space support (#27456)
rename to VertexColorNode move vertex color fallback to VertexColorNode remove unused Vector4 import add index support to UV and color nodes linting another rename
1 parent 4a73862 commit c337d03

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

examples/jsm/loaders/MaterialXLoader.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
import {
99
MeshPhysicalNodeMaterial,
1010
float, bool, int, vec2, vec3, vec4, color, texture,
11-
positionLocal,
11+
positionLocal, positionWorld, uv, vertexColor,
12+
normalLocal, normalWorld, tangentLocal, tangentWorld,
1213
add, sub, mul, div, mod, abs, sign, floor, ceil, round, pow, sin, cos, tan,
1314
asin, acos, atan2, sqrt, exp, clamp, min, max, normalize, length, dot, cross, normalMap,
1415
remap, smoothstep, luminance, mx_rgbtohsv, mx_hsvtorgb,
@@ -400,7 +401,32 @@ class MaterialXNode {
400401

401402
} else if ( element === 'position' ) {
402403

403-
node = positionLocal;
404+
const space = this.getAttribute( 'space' );
405+
node = space === 'world' ? positionWorld : positionLocal;
406+
407+
} else if ( element === 'normal' ) {
408+
409+
const space = this.getAttribute( 'space' );
410+
node = space === 'world' ? normalWorld : normalLocal;
411+
412+
} else if ( element === 'tangent' ) {
413+
414+
const space = this.getAttribute( 'space' );
415+
node = space === 'world' ? tangentWorld : tangentLocal;
416+
417+
} else if ( element === 'texcoord' ) {
418+
419+
const indexNode = this.getChildByName( 'index' );
420+
const index = indexNode ? parseInt( indexNode.value ) : 0;
421+
422+
node = uv( index );
423+
424+
} else if ( element === 'geomcolor' ) {
425+
426+
const indexNode = this.getChildByName( 'index' );
427+
const index = indexNode ? parseInt( indexNode.value ) : 0;
428+
429+
node = vertexColor( index );
404430

405431
} else if ( element === 'tiledimage' ) {
406432

examples/jsm/nodes/Nodes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export { default as BitangentNode, bitangentGeometry, bitangentLocal, bitangentV
7373
export { default as BufferAttributeNode, bufferAttribute, dynamicBufferAttribute, instancedBufferAttribute, instancedDynamicBufferAttribute } from './accessors/BufferAttributeNode.js';
7474
export { default as BufferNode, buffer } from './accessors/BufferNode.js';
7575
export { default as CameraNode, cameraProjectionMatrix, cameraViewMatrix, cameraNormalMatrix, cameraWorldMatrix, cameraPosition, cameraNear, cameraFar, cameraLogDepth } from './accessors/CameraNode.js';
76+
export { default as VertexColorNode, vertexColor } from './accessors/VertexColorNode.js';
7677
export { default as CubeTextureNode, cubeTexture } from './accessors/CubeTextureNode.js';
7778
export { default as InstanceNode, instance } from './accessors/InstanceNode.js';
7879
export { default as MaterialNode, materialAlphaTest, materialColor, materialShininess, materialEmissive, materialOpacity, materialSpecularColor, materialSpecularStrength, materialReflectivity, materialRoughness, materialMetalness, materialNormal, materialClearcoat, materialClearcoatRoughness, materialClearcoatNormal, materialRotation, materialSheen, materialSheenRoughness, materialIridescence, materialIridescenceIOR, materialIridescenceThickness, materialLineScale, materialLineDashSize, materialLineGapSize, materialLineWidth, materialLineDashOffset, materialPointWidth } from './accessors/MaterialNode.js';
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { addNodeClass } from '../core/Node.js';
2+
import AttributeNode from '../core/AttributeNode.js';
3+
import { nodeObject } from '../shadernode/ShaderNode.js';
4+
import { Vector4 } from 'three';
5+
6+
class VertexColorNode extends AttributeNode {
7+
8+
constructor( index = 0 ) {
9+
10+
super( null, 'vec4' );
11+
12+
this.isVertexColorNode = true;
13+
14+
this.index = index;
15+
16+
}
17+
18+
getAttributeName( /*builder*/ ) {
19+
20+
const index = this.index;
21+
22+
return 'color' + ( index > 0 ? index : '' );
23+
24+
}
25+
26+
generate( builder ) {
27+
28+
const attributeName = this.getAttributeName( builder );
29+
const geometryAttribute = builder.hasGeometryAttribute( attributeName );
30+
31+
let result;
32+
33+
if ( geometryAttribute === true ) {
34+
35+
result = super.generate( builder );
36+
37+
} else {
38+
39+
// Vertex color fallback should be white
40+
result = builder.generateConst( this.nodeType, new Vector4( 1, 1, 1, 1 ) );
41+
42+
}
43+
44+
return result;
45+
46+
}
47+
48+
serialize( data ) {
49+
50+
super.serialize( data );
51+
52+
data.index = this.index;
53+
54+
}
55+
56+
deserialize( data ) {
57+
58+
super.deserialize( data );
59+
60+
this.index = data.index;
61+
62+
}
63+
64+
}
65+
66+
export default VertexColorNode;
67+
68+
export const vertexColor = ( ...params ) => nodeObject( new VertexColorNode( ...params ) );
69+
70+
addNodeClass( 'VertexColorNode', VertexColorNode );

examples/jsm/nodes/core/AttributeNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class AttributeNode extends Node {
9191

9292
} else {
9393

94-
console.warn( `AttributeNode: Attribute "${ attributeName }" not found.` );
94+
console.warn( `AttributeNode: Vertex attribute "${ attributeName }" not found on geometry.` );
9595

9696
return builder.generateConst( nodeType );
9797

0 commit comments

Comments
 (0)