Vom Anderen PC aus hoch gespielt

This commit is contained in:
ChK
2026-02-01 13:40:05 +01:00
commit 60b1b7591c
1088 changed files with 452896 additions and 0 deletions

View File

@@ -0,0 +1,162 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { varying } from '../core/VaryingNode.js';
import { property } from '../core/PropertyNode.js';
import { attribute } from '../core/AttributeNode.js';
import { cameraProjectionMatrix } from '../accessors/CameraNode.js';
import { materialColor, materialPointWidth } from '../accessors/MaterialNode.js'; // or should this be a property, instead?
import { modelViewMatrix } from '../accessors/ModelNode.js';
import { positionGeometry } from '../accessors/PositionNode.js';
import { smoothstep } from '../math/MathNode.js';
import { tslFn, vec2, vec4 } from '../shadernode/ShaderNode.js';
import { uv } from '../accessors/UVNode.js';
import { viewport } from '../display/ViewportNode.js';
import { PointsMaterial } from 'three';
const defaultValues = new PointsMaterial();
class InstancedPointsNodeMaterial extends NodeMaterial {
constructor( params = {} ) {
super();
this.normals = false;
this.lights = false;
this.useAlphaToCoverage = true;
this.useColor = params.vertexColors;
this.pointWidth = 1;
this.pointColorNode = null;
this.setDefaultValues( defaultValues );
this.setupShaders();
this.setValues( params );
}
setupShaders() {
const useAlphaToCoverage = this.alphaToCoverage;
const useColor = this.useColor;
this.vertexNode = tslFn( () => {
//vUv = uv;
varying( vec2(), 'vUv' ).assign( uv() ); // @TODO: Analyze other way to do this
const instancePosition = attribute( 'instancePosition' );
// camera space
const mvPos = property( 'vec4', 'mvPos' );
mvPos.assign( modelViewMatrix.mul( vec4( instancePosition, 1.0 ) ) );
const aspect = viewport.z.div( viewport.w );
// clip space
const clipPos = cameraProjectionMatrix.mul( mvPos );
// offset in ndc space
const offset = property( 'vec2', 'offset' );
offset.assign( positionGeometry.xy );
offset.assign( offset.mul( materialPointWidth ) );
offset.assign( offset.div( viewport.z ) );
offset.y.assign( offset.y.mul( aspect ) );
// back to clip space
offset.assign( offset.mul( clipPos.w ) );
//clipPos.xy += offset;
clipPos.assign( clipPos.add( vec4( offset, 0, 0 ) ) );
return clipPos;
//vec4 mvPosition = mvPos; // this was used for somethihng...
} )();
this.colorNode = tslFn( () => {
const vUv = varying( vec2(), 'vUv' );
// force assignment into correct place in flow
const alpha = property( 'float', 'alpha' );
alpha.assign( 1 );
const a = vUv.x;
const b = vUv.y;
const len2 = a.mul( a ).add( b.mul( b ) );
if ( useAlphaToCoverage ) {
// force assignment out of following 'if' statement - to avoid uniform control flow errors
const dlen = property( 'float', 'dlen' );
dlen.assign( len2.fwidth() );
alpha.assign( smoothstep( dlen.oneMinus(), dlen.add( 1 ), len2 ).oneMinus() );
} else {
len2.greaterThan( 1.0 ).discard();
}
let pointColorNode;
if ( this.pointColorNode ) {
pointColorNode = this.pointColorNode;
} else {
if ( useColor ) {
const instanceColor = attribute( 'instanceColor' );
pointColorNode = instanceColor.mul( materialColor );
} else {
pointColorNode = materialColor;
}
}
return vec4( pointColorNode, alpha );
} )();
this.needsUpdate = true;
}
get alphaToCoverage() {
return this.useAlphaToCoverage;
}
set alphaToCoverage( value ) {
if ( this.useAlphaToCoverage !== value ) {
this.useAlphaToCoverage = value;
this.setupShaders();
}
}
}
export default InstancedPointsNodeMaterial;
addNodeMaterial( 'InstancedPointsNodeMaterial', InstancedPointsNodeMaterial );

View File

@@ -0,0 +1,448 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { temp } from '../core/VarNode.js';
import { varying } from '../core/VaryingNode.js';
import { property } from '../core/PropertyNode.js';
import { attribute } from '../core/AttributeNode.js';
import { cameraProjectionMatrix } from '../accessors/CameraNode.js';
import { materialColor, materialLineScale, materialLineDashSize, materialLineGapSize, materialLineDashOffset, materialLineWidth } from '../accessors/MaterialNode.js';
import { modelViewMatrix } from '../accessors/ModelNode.js';
import { positionGeometry } from '../accessors/PositionNode.js';
import { mix, smoothstep } from '../math/MathNode.js';
import { tslFn, float, vec2, vec3, vec4, If } from '../shadernode/ShaderNode.js';
import { uv } from '../accessors/UVNode.js';
import { viewport } from '../display/ViewportNode.js';
import { dashSize, gapSize } from '../core/PropertyNode.js';
import { LineDashedMaterial } from 'three';
const defaultValues = new LineDashedMaterial();
class Line2NodeMaterial extends NodeMaterial {
constructor( params = {} ) {
super();
this.normals = false;
this.lights = false;
this.setDefaultValues( defaultValues );
this.useAlphaToCoverage = true;
this.useColor = params.vertexColors;
this.useDash = params.dashed;
this.useWorldUnits = false;
this.dashOffset = 0;
this.lineWidth = 1;
this.lineColorNode = null;
this.offsetNode = null;
this.dashScaleNode = null;
this.dashSizeNode = null;
this.gapSizeNode = null;
this.setupShaders();
this.setValues( params );
}
setupShaders() {
const useAlphaToCoverage = this.alphaToCoverage;
const useColor = this.useColor;
const useDash = this.dashed;
const useWorldUnits = this.worldUnits;
const trimSegment = tslFn( ( { start, end } ) => {
const a = cameraProjectionMatrix.element( 2 ).element( 2 ); // 3nd entry in 3th column
const b = cameraProjectionMatrix.element( 3 ).element( 2 ); // 3nd entry in 4th column
const nearEstimate = b.mul( -0.5 ).div( a );
const alpha = nearEstimate.sub( start.z ).div( end.z.sub( start.z ) );
return vec4( mix( start.xyz, end.xyz, alpha ), end.w );
} );
this.vertexNode = tslFn( () => {
varying( vec2(), 'vUv' ).assign( uv() ); // @TODO: Analyze other way to do this
const instanceStart = attribute( 'instanceStart' );
const instanceEnd = attribute( 'instanceEnd' );
// camera space
const start = property( 'vec4', 'start' );
const end = property( 'vec4', 'end' );
start.assign( modelViewMatrix.mul( vec4( instanceStart, 1.0 ) ) ); // force assignment into correct place in flow
end.assign( modelViewMatrix.mul( vec4( instanceEnd, 1.0 ) ) );
if ( useWorldUnits ) {
varying( vec3(), 'worldStart' ).assign( start.xyz );
varying( vec3(), 'worldEnd' ).assign( end.xyz );
}
const aspect = viewport.z.div( viewport.w );
// special case for perspective projection, and segments that terminate either in, or behind, the camera plane
// clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
// but we need to perform ndc-space calculations in the shader, so we must address this issue directly
// perhaps there is a more elegant solution -- WestLangley
const perspective = cameraProjectionMatrix.element( 2 ).element( 3 ).equal( -1.0 ); // 4th entry in the 3rd column
If( perspective, () => {
If( start.z.lessThan( 0.0 ).and( end.z.greaterThan( 0.0 ) ), () => {
end.assign( trimSegment( { start: start, end: end } ) );
} ).elseif( end.z.lessThan( 0.0 ).and( start.z.greaterThanEqual( 0.0 ) ), () => {
start.assign( trimSegment( { start: end, end: start } ) );
} );
} );
// clip space
const clipStart = cameraProjectionMatrix.mul( start );
const clipEnd = cameraProjectionMatrix.mul( end );
// ndc space
const ndcStart = clipStart.xyz.div( clipStart.w );
const ndcEnd = clipEnd.xyz.div( clipEnd.w );
// direction
const dir = ndcEnd.xy.sub( ndcStart.xy ).temp();
// account for clip-space aspect ratio
dir.x.assign( dir.x.mul( aspect ) );
dir.assign( dir.normalize() );
const clip = temp( vec4() );
if ( useWorldUnits ) {
// get the offset direction as perpendicular to the view vector
const worldDir = end.xyz.sub( start.xyz ).normalize();
const offset = positionGeometry.y.lessThan( 0.5 ).cond(
start.xyz.cross( worldDir ).normalize(),
end.xyz.cross( worldDir ).normalize()
);
// sign flip
offset.assign( positionGeometry.x.lessThan( 0.0 ).cond( offset.negate(), offset ) );
const forwardOffset = worldDir.dot( vec3( 0.0, 0.0, 1.0 ) );
// don't extend the line if we're rendering dashes because we
// won't be rendering the endcaps
if ( ! useDash ) {
// extend the line bounds to encompass endcaps
start.assign( start.sub( vec4( worldDir.mul( materialLineWidth ).mul( 0.5 ), 0 ) ) );
end.assign( end.add( vec4( worldDir.mul( materialLineWidth ).mul( 0.5 ), 0 ) ) );
// shift the position of the quad so it hugs the forward edge of the line
offset.assign( offset.sub( vec3( dir.mul( forwardOffset ), 0 ) ) );
offset.z.assign( offset.z.add( 0.5 ) );
}
// endcaps
If( positionGeometry.y.greaterThan( 1.0 ).or( positionGeometry.y.lessThan( 0.0 ) ), () => {
offset.assign( offset.add( vec3( dir.mul( 2.0 ).mul( forwardOffset ), 0 ) ) );
} );
// adjust for linewidth
offset.assign( offset.mul( materialLineWidth ).mul( 0.5 ) );
// set the world position
const worldPos = varying( vec4(), 'worldPos' );
worldPos.assign( positionGeometry.y.lessThan( 0.5 ).cond( start, end ) );
worldPos.assign( worldPos.add( vec4( offset, 0 ) ) );
// project the worldpos
clip.assign( cameraProjectionMatrix.mul( worldPos ) );
// shift the depth of the projected points so the line
// segments overlap neatly
const clipPose = temp( vec3() );
clipPose.assign( positionGeometry.y.lessThan( 0.5 ).cond( ndcStart, ndcEnd ) );
clip.z.assign( clipPose.z.mul( clip.w ) );
} else {
const offset = property( 'vec2', 'offset' );
offset.assign( vec2( dir.y, dir.x.negate() ) );
// undo aspect ratio adjustment
dir.x.assign( dir.x.div( aspect ) );
offset.x.assign( offset.x.div( aspect ) );
// sign flip
offset.assign( positionGeometry.x.lessThan( 0.0 ).cond( offset.negate(), offset ) );
// endcaps
If( positionGeometry.y.lessThan( 0.0 ), () => {
offset.assign( offset.sub( dir ) );
} ).elseif( positionGeometry.y.greaterThan( 1.0 ), () => {
offset.assign( offset.add( dir ) );
} );
// adjust for linewidth
offset.assign( offset.mul( materialLineWidth ) );
// adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
offset.assign( offset.div( viewport.w ) );
// select end
clip.assign( positionGeometry.y.lessThan( 0.5 ).cond( clipStart, clipEnd ) );
// back to clip space
offset.assign( offset.mul( clip.w ) );
clip.assign( clip.add( vec4( offset, 0, 0 ) ) );
}
return clip;
} )();
const closestLineToLine = tslFn( ( { p1, p2, p3, p4 } ) => {
const p13 = p1.sub( p3 );
const p43 = p4.sub( p3 );
const p21 = p2.sub( p1 );
const d1343 = p13.dot( p43 );
const d4321 = p43.dot( p21 );
const d1321 = p13.dot( p21 );
const d4343 = p43.dot( p43 );
const d2121 = p21.dot( p21 );
const denom = d2121.mul( d4343 ).sub( d4321.mul( d4321 ) );
const numer = d1343.mul( d4321 ).sub( d1321.mul( d4343 ) );
const mua = numer.div( denom ).clamp();
const mub = d1343.add( d4321.mul( mua ) ).div( d4343 ).clamp();
return vec2( mua, mub );
} );
this.colorNode = tslFn( () => {
const vUv = varying( vec2(), 'vUv' );
if ( useDash ) {
const offsetNode = this.offsetNode ? float( this.offsetNodeNode ) : materialLineDashOffset;
const dashScaleNode = this.dashScaleNode ? float( this.dashScaleNode ) : materialLineScale;
const dashSizeNode = this.dashSizeNode ? float( this.dashSizeNode ) : materialLineDashSize;
const gapSizeNode = this.dashSizeNode ? float( this.dashGapNode ) : materialLineGapSize;
dashSize.assign( dashSizeNode );
gapSize.assign( gapSizeNode );
const instanceDistanceStart = attribute( 'instanceDistanceStart' );
const instanceDistanceEnd = attribute( 'instanceDistanceEnd' );
const lineDistance = positionGeometry.y.lessThan( 0.5 ).cond( dashScaleNode.mul( instanceDistanceStart ), materialLineScale.mul( instanceDistanceEnd ) );
const vLineDistance = varying( lineDistance.add( materialLineDashOffset ) );
const vLineDistanceOffset = offsetNode ? vLineDistance.add( offsetNode ) : vLineDistance;
vUv.y.lessThan( - 1.0 ).or( vUv.y.greaterThan( 1.0 ) ).discard(); // discard endcaps
vLineDistanceOffset.mod( dashSize.add( gapSize ) ).greaterThan( dashSize ).discard(); // todo - FIX
}
// force assignment into correct place in flow
const alpha = property( 'float', 'alpha' );
alpha.assign( 1 );
if ( useWorldUnits ) {
const worldStart = varying( vec3(), 'worldStart' );
const worldEnd = varying( vec3(), 'worldEnd' );
// Find the closest points on the view ray and the line segment
const rayEnd = varying( vec4(), 'worldPos' ).xyz.normalize().mul( 1e5 );
const lineDir = worldEnd.sub( worldStart );
const params = closestLineToLine( { p1: worldStart, p2: worldEnd, p3: vec3( 0.0, 0.0, 0.0 ), p4: rayEnd } );
const p1 = worldStart.add( lineDir.mul( params.x ) );
const p2 = rayEnd.mul( params.y );
const delta = p1.sub( p2 );
const len = delta.length();
const norm = len.div( materialLineWidth );
if ( ! useDash ) {
if ( useAlphaToCoverage ) {
const dnorm = norm.fwidth();
alpha.assign( smoothstep( dnorm.negate().add( 0.5 ), dnorm.add( 0.5 ), norm ).oneMinus() );
} else {
norm.greaterThan( 0.5 ).discard();
}
}
} else {
// round endcaps
if ( useAlphaToCoverage ) {
const a = vUv.x;
const b = vUv.y.greaterThan( 0.0 ).cond( vUv.y.sub( 1.0 ), vUv.y.add( 1.0 ) );
const len2 = a.mul( a ).add( b.mul( b ) );
// force assignment out of following 'if' statement - to avoid uniform control flow errors
const dlen = property( 'float', 'dlen' );
dlen.assign( len2.fwidth() );
If( vUv.y.abs().greaterThan( 1.0 ), () => {
alpha.assign( smoothstep( dlen.oneMinus(), dlen.add( 1 ), len2 ).oneMinus() );
} );
} else {
If( vUv.y.abs().greaterThan( 1.0 ), () => {
const a = vUv.x;
const b = vUv.y.greaterThan( 0.0 ).cond( vUv.y.sub( 1.0 ), vUv.y.add( 1.0 ) );
const len2 = a.mul( a ).add( b.mul( b ) );
len2.greaterThan( 1.0 ).discard();
} );
}
}
let lineColorNode;
if ( this.lineColorNode ) {
lineColorNode = this.lineColorNode;
} else {
if ( useColor ) {
const instanceColorStart = attribute( 'instanceColorStart' );
const instanceColorEnd = attribute( 'instanceColorEnd' );
const instanceColor = positionGeometry.y.lessThan( 0.5 ).cond( instanceColorStart, instanceColorEnd );
lineColorNode = instanceColor.mul( materialColor );
} else {
lineColorNode = materialColor;
}
}
return vec4( lineColorNode, alpha );
} )();
this.needsUpdate = true;
}
get worldUnits() {
return this.useWorldUnits;
}
set worldUnits( value ) {
if ( this.useWorldUnits !== value ) {
this.useWorldUnits = value;
this.setupShaders();
}
}
get dashed() {
return this.useDash;
}
set dashed( value ) {
if ( this.useDash !== value ) {
this.useDash = value;
this.setupShaders();
}
}
get alphaToCoverage() {
return this.useAlphaToCoverage;
}
set alphaToCoverage( value ) {
if ( this.useAlphaToCoverage !== value ) {
this.useAlphaToCoverage = value;
this.setupShaders();
}
}
}
export default Line2NodeMaterial;
addNodeMaterial( 'Line2NodeMaterial', Line2NodeMaterial );

View File

@@ -0,0 +1,28 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { LineBasicMaterial } from 'three';
const defaultValues = new LineBasicMaterial();
class LineBasicNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isLineBasicNodeMaterial = true;
this.lights = false;
this.normals = false;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
}
export default LineBasicNodeMaterial;
addNodeMaterial( 'LineBasicNodeMaterial', LineBasicNodeMaterial );

View File

@@ -0,0 +1,54 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { attribute } from '../core/AttributeNode.js';
import { varying } from '../core/VaryingNode.js';
import { materialLineDashSize, materialLineGapSize, materialLineScale } from '../accessors/MaterialNode.js';
import { dashSize, gapSize } from '../core/PropertyNode.js';
import { float } from '../shadernode/ShaderNode.js';
import { LineDashedMaterial } from 'three';
const defaultValues = new LineDashedMaterial();
class LineDashedNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isLineDashedNodeMaterial = true;
this.lights = false;
this.normals = false;
this.setDefaultValues( defaultValues );
this.offsetNode = null;
this.dashScaleNode = null;
this.dashSizeNode = null;
this.gapSizeNode = null;
this.setValues( parameters );
}
setupVariants() {
const offsetNode = this.offsetNode;
const dashScaleNode = this.dashScaleNode ? float( this.dashScaleNode ) : materialLineScale;
const dashSizeNode = this.dashSizeNode ? float( this.dashSizeNode ) : materialLineDashSize;
const gapSizeNode = this.dashSizeNode ? float( this.dashGapNode ) : materialLineGapSize;
dashSize.assign( dashSizeNode );
gapSize.assign( gapSizeNode );
const vLineDistance = varying( attribute( 'lineDistance' ).mul( dashScaleNode ) );
const vLineDistanceOffset = offsetNode ? vLineDistance.add( offsetNode ) : vLineDistance;
vLineDistanceOffset.mod( dashSize.add( gapSize ) ).greaterThan( dashSize ).discard();
}
}
export default LineDashedNodeMaterial;
addNodeMaterial( 'LineDashedNodeMaterial', LineDashedNodeMaterial );

View File

@@ -0,0 +1,15 @@
// @TODO: We can simplify "export { default as SomeNode, other, exports } from '...'" to just "export * from '...'" if we will use only named exports
export { default as NodeMaterial, addNodeMaterial, createNodeMaterialFromType } from './NodeMaterial.js';
export { default as InstancedPointsNodeMaterial } from './InstancedPointsNodeMaterial.js';
export { default as LineBasicNodeMaterial } from './LineBasicNodeMaterial.js';
export { default as LineDashedNodeMaterial } from './LineDashedNodeMaterial.js';
export { default as Line2NodeMaterial } from './Line2NodeMaterial.js';
export { default as MeshNormalNodeMaterial } from './MeshNormalNodeMaterial.js';
export { default as MeshBasicNodeMaterial } from './MeshBasicNodeMaterial.js';
export { default as MeshLambertNodeMaterial } from './MeshLambertNodeMaterial.js';
export { default as MeshPhongNodeMaterial } from './MeshPhongNodeMaterial.js';
export { default as MeshStandardNodeMaterial } from './MeshStandardNodeMaterial.js';
export { default as MeshPhysicalNodeMaterial } from './MeshPhysicalNodeMaterial.js';
export { default as PointsNodeMaterial } from './PointsNodeMaterial.js';
export { default as SpriteNodeMaterial } from './SpriteNodeMaterial.js';

View File

@@ -0,0 +1,27 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { MeshBasicMaterial } from 'three';
const defaultValues = new MeshBasicMaterial();
class MeshBasicNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isMeshBasicNodeMaterial = true;
this.lights = false;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
}
export default MeshBasicNodeMaterial;
addNodeMaterial( 'MeshBasicNodeMaterial', MeshBasicNodeMaterial );

View File

@@ -0,0 +1,34 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import PhongLightingModel from '../functions/PhongLightingModel.js';
import { MeshLambertMaterial } from 'three';
const defaultValues = new MeshLambertMaterial();
class MeshLambertNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isMeshLambertNodeMaterial = true;
this.lights = true;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
setupLightingModel( /*builder*/ ) {
return new PhongLightingModel( false ); // ( specular ) -> force lambert
}
}
export default MeshLambertNodeMaterial;
addNodeMaterial( 'MeshLambertNodeMaterial', MeshLambertNodeMaterial );

View File

@@ -0,0 +1,40 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { diffuseColor } from '../core/PropertyNode.js';
import { directionToColor } from '../utils/PackingNode.js';
import { materialOpacity } from '../accessors/MaterialNode.js';
import { transformedNormalView } from '../accessors/NormalNode.js';
import { float, vec4 } from '../shadernode/ShaderNode.js';
import { MeshNormalMaterial } from 'three';
const defaultValues = new MeshNormalMaterial();
class MeshNormalNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isMeshNormalNodeMaterial = true;
this.colorSpace = false;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
setupDiffuseColor() {
const opacityNode = this.opacityNode ? float( this.opacityNode ) : materialOpacity;
diffuseColor.assign( vec4( directionToColor( transformedNormalView ), opacityNode ) );
}
}
export default MeshNormalNodeMaterial;
addNodeMaterial( 'MeshNormalNodeMaterial', MeshNormalNodeMaterial );

View File

@@ -0,0 +1,65 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { shininess, specularColor } from '../core/PropertyNode.js';
import { materialShininess, materialSpecularColor } from '../accessors/MaterialNode.js';
import { float } from '../shadernode/ShaderNode.js';
import PhongLightingModel from '../functions/PhongLightingModel.js';
import { MeshPhongMaterial } from 'three';
const defaultValues = new MeshPhongMaterial();
class MeshPhongNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isMeshPhongNodeMaterial = true;
this.lights = true;
this.shininessNode = null;
this.specularNode = null;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
setupLightingModel( /*builder*/ ) {
return new PhongLightingModel();
}
setupVariants() {
// SHININESS
const shininessNode = ( this.shininessNode ? float( this.shininessNode ) : materialShininess ).max( 1e-4 ); // to prevent pow( 0.0, 0.0 )
shininess.assign( shininessNode );
// SPECULAR COLOR
const specularNode = this.specularNode || materialSpecularColor;
specularColor.assign( specularNode );
}
copy( source ) {
this.shininessNode = source.shininessNode;
this.specularNode = source.specularNode;
return super.copy( source );
}
}
export default MeshPhongNodeMaterial;
addNodeMaterial( 'MeshPhongNodeMaterial', MeshPhongNodeMaterial );

View File

@@ -0,0 +1,155 @@
import { addNodeMaterial } from './NodeMaterial.js';
import { transformedClearcoatNormalView } from '../accessors/NormalNode.js';
import { clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness } from '../core/PropertyNode.js';
import { materialClearcoat, materialClearcoatRoughness, materialClearcoatNormal, materialSheen, materialSheenRoughness, materialIridescence, materialIridescenceIOR, materialIridescenceThickness } from '../accessors/MaterialNode.js';
import { float, vec3 } from '../shadernode/ShaderNode.js';
import PhysicalLightingModel from '../functions/PhysicalLightingModel.js';
import MeshStandardNodeMaterial from './MeshStandardNodeMaterial.js';
import { MeshPhysicalMaterial } from 'three';
const defaultValues = new MeshPhysicalMaterial();
class MeshPhysicalNodeMaterial extends MeshStandardNodeMaterial {
constructor( parameters ) {
super();
this.isMeshPhysicalNodeMaterial = true;
this.clearcoatNode = null;
this.clearcoatRoughnessNode = null;
this.clearcoatNormalNode = null;
this.sheenNode = null;
this.sheenRoughnessNode = null;
this.iridescenceNode = null;
this.iridescenceIORNode = null;
this.iridescenceThicknessNode = null;
this.specularIntensityNode = null;
this.specularColorNode = null;
this.transmissionNode = null;
this.thicknessNode = null;
this.attenuationDistanceNode = null;
this.attenuationColorNode = null;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
get useClearcoat() {
return this.clearcoat > 0 || this.clearcoatNode !== null;
}
get useIridescence() {
return this.iridescence > 0 || this.iridescenceNode !== null;
}
get useSheen() {
return this.sheen > 0 || this.sheenNode !== null;
}
setupLightingModel( /*builder*/ ) {
return new PhysicalLightingModel( this.useClearcoat, this.useSheen, this.useIridescence );
}
setupVariants( builder ) {
super.setupVariants( builder );
// CLEARCOAT
if ( this.useClearcoat ) {
const clearcoatNode = this.clearcoatNode ? float( this.clearcoatNode ) : materialClearcoat;
const clearcoatRoughnessNode = this.clearcoatRoughnessNode ? float( this.clearcoatRoughnessNode ) : materialClearcoatRoughness;
clearcoat.assign( clearcoatNode );
clearcoatRoughness.assign( clearcoatRoughnessNode );
}
// SHEEN
if ( this.useSheen ) {
const sheenNode = this.sheenNode ? vec3( this.sheenNode ) : materialSheen;
const sheenRoughnessNode = this.sheenRoughnessNode ? float( this.sheenRoughnessNode ) : materialSheenRoughness;
sheen.assign( sheenNode );
sheenRoughness.assign( sheenRoughnessNode );
}
// IRIDESCENCE
if ( this.useIridescence ) {
const iridescenceNode = this.iridescenceNode ? float( this.iridescenceNode ) : materialIridescence;
const iridescenceIORNode = this.iridescenceIORNode ? float( this.iridescenceIORNode ) : materialIridescenceIOR;
const iridescenceThicknessNode = this.iridescenceThicknessNode ? float( this.iridescenceThicknessNode ) : materialIridescenceThickness;
iridescence.assign( iridescenceNode );
iridescenceIOR.assign( iridescenceIORNode );
iridescenceThickness.assign( iridescenceThicknessNode );
}
}
setupNormal( builder ) {
super.setupNormal( builder );
// CLEARCOAT NORMAL
const clearcoatNormalNode = this.clearcoatNormalNode ? vec3( this.clearcoatNormalNode ) : materialClearcoatNormal;
transformedClearcoatNormalView.assign( clearcoatNormalNode );
}
copy( source ) {
this.clearcoatNode = source.clearcoatNode;
this.clearcoatRoughnessNode = source.clearcoatRoughnessNode;
this.clearcoatNormalNode = source.clearcoatNormalNode;
this.sheenNode = source.sheenNode;
this.sheenRoughnessNode = source.sheenRoughnessNode;
this.iridescenceNode = source.iridescenceNode;
this.iridescenceIORNode = source.iridescenceIORNode;
this.iridescenceThicknessNode = source.iridescenceThicknessNode;
this.specularIntensityNode = source.specularIntensityNode;
this.specularColorNode = source.specularColorNode;
this.transmissionNode = source.transmissionNode;
this.thicknessNode = source.thicknessNode;
this.attenuationDistanceNode = source.attenuationDistanceNode;
this.attenuationColorNode = source.attenuationColorNode;
return super.copy( source );
}
}
export default MeshPhysicalNodeMaterial;
addNodeMaterial( 'MeshPhysicalNodeMaterial', MeshPhysicalNodeMaterial );

View File

@@ -0,0 +1,80 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { diffuseColor, metalness, roughness, specularColor } from '../core/PropertyNode.js';
import { mix } from '../math/MathNode.js';
import { materialRoughness, materialMetalness } from '../accessors/MaterialNode.js';
import getRoughness from '../functions/material/getRoughness.js';
import PhysicalLightingModel from '../functions/PhysicalLightingModel.js';
import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
import { MeshStandardMaterial } from 'three';
const defaultValues = new MeshStandardMaterial();
class MeshStandardNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isMeshStandardNodeMaterial = true;
this.emissiveNode = null;
this.metalnessNode = null;
this.roughnessNode = null;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
setupLightingModel( /*builder*/ ) {
return new PhysicalLightingModel();
}
setupVariants() {
// METALNESS
const metalnessNode = this.metalnessNode ? float( this.metalnessNode ) : materialMetalness;
metalness.assign( metalnessNode );
// ROUGHNESS
let roughnessNode = this.roughnessNode ? float( this.roughnessNode ) : materialRoughness;
roughnessNode = getRoughness( { roughness: roughnessNode } );
roughness.assign( roughnessNode );
// SPECULAR COLOR
const specularColorNode = mix( vec3( 0.04 ), diffuseColor.rgb, metalnessNode );
specularColor.assign( specularColorNode );
// DIFFUSE COLOR
diffuseColor.assign( vec4( diffuseColor.rgb.mul( metalnessNode.oneMinus() ), diffuseColor.a ) );
}
copy( source ) {
this.emissiveNode = source.emissiveNode;
this.metalnessNode = source.metalnessNode;
this.roughnessNode = source.roughnessNode;
return super.copy( source );
}
}
export default MeshStandardNodeMaterial;
addNodeMaterial( 'MeshStandardNodeMaterial', MeshStandardNodeMaterial );

View File

@@ -0,0 +1,543 @@
import { Material, ShaderMaterial, NoColorSpace, LinearSRGBColorSpace } from 'three';
import { getNodeChildren, getCacheKey } from '../core/NodeUtils.js';
import { attribute } from '../core/AttributeNode.js';
import { output, diffuseColor } from '../core/PropertyNode.js';
import { materialAlphaTest, materialColor, materialOpacity, materialEmissive, materialNormal } from '../accessors/MaterialNode.js';
import { modelViewProjection } from '../accessors/ModelViewProjectionNode.js';
import { transformedNormalView } from '../accessors/NormalNode.js';
import { instance } from '../accessors/InstanceNode.js';
import { positionLocal, positionView } from '../accessors/PositionNode.js';
import { skinning } from '../accessors/SkinningNode.js';
import { morph } from '../accessors/MorphNode.js';
import { texture } from '../accessors/TextureNode.js';
import { cubeTexture } from '../accessors/CubeTextureNode.js';
import { lightsWithoutWrap } from '../lighting/LightsNode.js';
import { mix } from '../math/MathNode.js';
import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
import AONode from '../lighting/AONode.js';
import { lightingContext } from '../lighting/LightingContextNode.js';
import EnvironmentNode from '../lighting/EnvironmentNode.js';
const NodeMaterials = new Map();
class NodeMaterial extends ShaderMaterial {
constructor() {
super();
this.isNodeMaterial = true;
this.type = this.constructor.type;
this.forceSinglePass = false;
this.unlit = this.constructor === NodeMaterial.prototype.constructor; // Extended materials are not unlit by default
this.fog = true;
this.lights = true;
this.normals = true;
this.colorSpace = true;
this.lightsNode = null;
this.envNode = null;
this.colorNode = null;
this.normalNode = null;
this.opacityNode = null;
this.backdropNode = null;
this.backdropAlphaNode = null;
this.alphaTestNode = null;
this.positionNode = null;
this.outputNode = null; // @TODO: Rename to fragmentNode
this.vertexNode = null;
}
customProgramCacheKey() {
return this.type + getCacheKey( this );
}
build( builder ) {
this.setup( builder );
}
setup( builder ) {
// < VERTEX STAGE >
builder.addStack();
builder.stack.outputNode = this.setupPosition( builder );
builder.addFlow( 'vertex', builder.removeStack() );
// < FRAGMENT STAGE >
builder.addStack();
let outputNode;
if ( this.unlit === false ) {
if ( this.normals === true ) this.setupNormal( builder );
this.setupDiffuseColor( builder );
this.setupVariants( builder );
const outgoingLightNode = this.setupLighting( builder );
outputNode = this.setupOutput( builder, vec4( outgoingLightNode, diffuseColor.a ) );
// OUTPUT NODE
output.assign( outputNode );
//
if ( this.outputNode !== null ) outputNode = this.outputNode;
} else {
outputNode = this.setupOutput( builder, this.outputNode || vec4( 0, 0, 0, 1 ) );
}
builder.stack.outputNode = outputNode;
builder.addFlow( 'fragment', builder.removeStack() );
}
setupPosition( builder ) {
const object = builder.object;
const geometry = object.geometry;
builder.addStack();
if ( geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color ) {
morph( object ).append();
}
if ( object.isSkinnedMesh === true ) {
skinning( object ).append();
}
if ( ( object.instanceMatrix && object.instanceMatrix.isInstancedBufferAttribute === true ) && builder.isAvailable( 'instance' ) === true ) {
instance( object ).append();
}
if ( this.positionNode !== null ) {
positionLocal.assign( this.positionNode );
}
builder.context.vertex = builder.removeStack();
return this.vertexNode || modelViewProjection();
}
setupDiffuseColor( { geometry } ) {
let colorNode = this.colorNode ? vec4( this.colorNode ) : materialColor;
// VERTEX COLORS
if ( this.vertexColors === true && geometry.hasAttribute( 'color' ) ) {
colorNode = vec4( colorNode.xyz.mul( attribute( 'color' ) ), colorNode.a );
}
// COLOR
diffuseColor.assign( colorNode );
// OPACITY
const opacityNode = this.opacityNode ? float( this.opacityNode ) : materialOpacity;
diffuseColor.a.assign( diffuseColor.a.mul( opacityNode ) );
// ALPHA TEST
if ( this.alphaTestNode !== null || this.alphaTest > 0 ) {
const alphaTestNode = this.alphaTestNode !== null ? float( this.alphaTestNode ) : materialAlphaTest;
diffuseColor.a.lessThanEqual( alphaTestNode ).discard();
}
}
setupVariants( /*builder*/ ) {
// Interface function.
}
setupNormal() {
// NORMAL VIEW
if ( this.flatShading === true ) {
const normalNode = positionView.dFdx().cross( positionView.dFdy() ).normalize();
transformedNormalView.assign( normalNode );
} else {
const normalNode = this.normalNode ? vec3( this.normalNode ) : materialNormal;
transformedNormalView.assign( normalNode );
}
}
getEnvNode( builder ) {
let node = null;
if ( this.envNode ) {
node = this.envNode;
} else if ( this.envMap ) {
node = this.envMap.isCubeTexture ? cubeTexture( this.envMap ) : texture( this.envMap );
} else if ( builder.environmentNode ) {
node = builder.environmentNode;
}
return node;
}
setupLights( builder ) {
const envNode = this.getEnvNode( builder );
//
const materialLightsNode = [];
if ( envNode ) {
materialLightsNode.push( new EnvironmentNode( envNode ) );
}
if ( builder.material.aoMap ) {
materialLightsNode.push( new AONode( texture( builder.material.aoMap ) ) );
}
let lightsNode = this.lightsNode || builder.lightsNode;
if ( materialLightsNode.length > 0 ) {
lightsNode = lightsWithoutWrap( [ ...lightsNode.lightNodes, ...materialLightsNode ] );
}
return lightsNode;
}
setupLightingModel( /*builder*/ ) {
// Interface function.
}
setupLighting( builder ) {
const { material } = builder;
const { backdropNode, backdropAlphaNode, emissiveNode } = this;
// OUTGOING LIGHT
const lights = this.lights === true || this.lightsNode !== null;
const lightsNode = lights ? this.setupLights( builder ) : null;
let outgoingLightNode = diffuseColor.rgb;
if ( lightsNode && lightsNode.hasLight !== false ) {
const lightingModel = this.setupLightingModel( builder );
outgoingLightNode = lightingContext( lightsNode, lightingModel, backdropNode, backdropAlphaNode );
} else if ( backdropNode !== null ) {
outgoingLightNode = vec3( backdropAlphaNode !== null ? mix( outgoingLightNode, backdropNode, backdropAlphaNode ) : backdropNode );
}
// EMISSIVE
if ( ( emissiveNode && emissiveNode.isNode === true ) || ( material.emissive && material.emissive.isColor === true ) ) {
outgoingLightNode = outgoingLightNode.add( vec3( emissiveNode ? emissiveNode : materialEmissive ) );
}
return outgoingLightNode;
}
setupOutput( builder, outputNode ) {
const renderer = builder.renderer;
// TONE MAPPING
const toneMappingNode = builder.toneMappingNode;
if ( toneMappingNode ) {
outputNode = vec4( toneMappingNode.context( { color: outputNode.rgb } ), outputNode.a );
}
// FOG
if ( this.fog === true ) {
const fogNode = builder.fogNode;
if ( fogNode ) outputNode = vec4( fogNode.mixAssign( outputNode.rgb ), outputNode.a );
}
// ENCODING
if ( this.colorSpace === true ) {
const renderTarget = renderer.getRenderTarget();
let outputColorSpace;
if ( renderTarget !== null ) {
if ( Array.isArray( renderTarget.texture ) ) {
outputColorSpace = renderTarget.texture[ 0 ].colorSpace;
} else {
outputColorSpace = renderTarget.texture.colorSpace;
}
} else {
outputColorSpace = renderer.outputColorSpace;
}
if ( outputColorSpace !== LinearSRGBColorSpace && outputColorSpace !== NoColorSpace ) {
outputNode = outputNode.linearToColorSpace( outputColorSpace );
}
}
return outputNode;
}
setDefaultValues( material ) {
// This approach is to reuse the native refreshUniforms*
// and turn available the use of features like transmission and environment in core
for ( const property in material ) {
const value = material[ property ];
if ( this[ property ] === undefined ) {
this[ property ] = value;
if ( value && value.clone ) this[ property ] = value.clone();
}
}
Object.assign( this.defines, material.defines );
const descriptors = Object.getOwnPropertyDescriptors( material.constructor.prototype );
for ( const key in descriptors ) {
if ( Object.getOwnPropertyDescriptor( this.constructor.prototype, key ) === undefined &&
descriptors[ key ].get !== undefined ) {
Object.defineProperty( this.constructor.prototype, key, descriptors[ key ] );
}
}
}
toJSON( meta ) {
const isRoot = ( meta === undefined || typeof meta === 'string' );
if ( isRoot ) {
meta = {
textures: {},
images: {},
nodes: {}
};
}
const data = Material.prototype.toJSON.call( this, meta );
const nodeChildren = getNodeChildren( this );
data.inputNodes = {};
for ( const { property, childNode } of nodeChildren ) {
data.inputNodes[ property ] = childNode.toJSON( meta ).uuid;
}
// TODO: Copied from Object3D.toJSON
function extractFromCache( cache ) {
const values = [];
for ( const key in cache ) {
const data = cache[ key ];
delete data.metadata;
values.push( data );
}
return values;
}
if ( isRoot ) {
const textures = extractFromCache( meta.textures );
const images = extractFromCache( meta.images );
const nodes = extractFromCache( meta.nodes );
if ( textures.length > 0 ) data.textures = textures;
if ( images.length > 0 ) data.images = images;
if ( nodes.length > 0 ) data.nodes = nodes;
}
return data;
}
copy( source ) {
this.lightsNode = source.lightsNode;
this.envNode = source.envNode;
this.colorNode = source.colorNode;
this.normalNode = source.normalNode;
this.opacityNode = source.opacityNode;
this.backdropNode = source.backdropNode;
this.backdropAlphaNode = source.backdropAlphaNode;
this.alphaTestNode = source.alphaTestNode;
this.positionNode = source.positionNode;
this.outputNode = source.outputNode;
this.vertexNode = source.vertexNode;
return super.copy( source );
}
static fromMaterial( material ) {
if ( material.isNodeMaterial === true ) { // is already a node material
return material;
}
const type = material.type.replace( 'Material', 'NodeMaterial' );
const nodeMaterial = createNodeMaterialFromType( type );
if ( nodeMaterial === undefined ) {
throw new Error( `NodeMaterial: Material "${ material.type }" is not compatible.` );
}
for ( const key in material ) {
nodeMaterial[ key ] = material[ key ];
}
return nodeMaterial;
}
}
export default NodeMaterial;
export function addNodeMaterial( type, nodeMaterial ) {
if ( typeof nodeMaterial !== 'function' || ! type ) throw new Error( `Node material ${ type } is not a class` );
if ( NodeMaterials.has( type ) ) throw new Error( `Redefinition of node material ${ type }` );
NodeMaterials.set( type, nodeMaterial );
nodeMaterial.type = type;
}
export function createNodeMaterialFromType( type ) {
const Material = NodeMaterials.get( type );
if ( Material !== undefined ) {
return new Material();
}
}
addNodeMaterial( 'NodeMaterial', NodeMaterial );

View File

@@ -0,0 +1,49 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { PointsMaterial } from 'three';
const defaultValues = new PointsMaterial();
class PointsNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isPointsNodeMaterial = true;
this.lights = false;
this.normals = false;
this.transparent = true;
this.colorNode = null;
this.opacityNode = null;
this.alphaTestNode = null;
this.lightNode = null;
this.sizeNode = null;
this.positionNode = null;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
copy( source ) {
this.sizeNode = source.sizeNode;
return super.copy( source );
}
}
export default PointsNodeMaterial;
addNodeMaterial( 'PointsNodeMaterial', PointsNodeMaterial );

View File

@@ -0,0 +1,103 @@
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { uniform } from '../core/UniformNode.js';
import { cameraProjectionMatrix } from '../accessors/CameraNode.js';
import { materialRotation } from '../accessors/MaterialNode.js';
import { modelViewMatrix, modelWorldMatrix } from '../accessors/ModelNode.js';
import { positionLocal } from '../accessors/PositionNode.js';
import { float, vec2, vec3, vec4 } from '../shadernode/ShaderNode.js';
import { SpriteMaterial } from 'three';
const defaultValues = new SpriteMaterial();
class SpriteNodeMaterial extends NodeMaterial {
constructor( parameters ) {
super();
this.isSpriteNodeMaterial = true;
this.lights = false;
this.normals = false;
this.colorNode = null;
this.opacityNode = null;
this.alphaTestNode = null;
this.lightNode = null;
this.positionNode = null;
this.rotationNode = null;
this.scaleNode = null;
this.setDefaultValues( defaultValues );
this.setValues( parameters );
}
setupPosition( { object, context } ) {
// < VERTEX STAGE >
const { positionNode, rotationNode, scaleNode } = this;
const vertex = positionLocal;
let mvPosition = modelViewMatrix.mul( vec3( positionNode || 0 ) );
let scale = vec2( modelWorldMatrix[ 0 ].xyz.length(), modelWorldMatrix[ 1 ].xyz.length() );
if ( scaleNode !== null ) {
scale = scale.mul( scaleNode );
}
let alignedPosition = vertex.xy;
if ( object.center && object.center.isVector2 === true ) {
alignedPosition = alignedPosition.sub( uniform( object.center ).sub( 0.5 ) );
}
alignedPosition = alignedPosition.mul( scale );
const rotation = float( rotationNode || materialRotation );
const cosAngle = rotation.cos();
const sinAngle = rotation.sin();
const rotatedPosition = vec2( // @TODO: Maybe we can create mat2 and write something like rotationMatrix.mul( alignedPosition )?
vec2( cosAngle, sinAngle.negate() ).dot( alignedPosition ),
vec2( sinAngle, cosAngle ).dot( alignedPosition )
);
mvPosition = vec4( mvPosition.xy.add( rotatedPosition ), mvPosition.zw );
const modelViewProjection = cameraProjectionMatrix.mul( mvPosition );
context.vertex = vertex;
return modelViewProjection;
}
copy( source ) {
this.positionNode = source.positionNode;
this.rotationNode = source.rotationNode;
this.scaleNode = source.scaleNode;
return super.copy( source );
}
}
export default SpriteNodeMaterial;
addNodeMaterial( 'SpriteNodeMaterial', SpriteNodeMaterial );