Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Added anisotropic roughness to mainline #17391

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 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
12 changes: 12 additions & 0 deletions examples/jsm/nodes/accessors/NormalNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function NormalNode( scope ) {
}

NormalNode.LOCAL = 'local';
NormalNode.BENT = 'bent';
NormalNode.WORLD = 'world';

NormalNode.prototype = Object.create( TempNode.prototype );
Expand Down Expand Up @@ -41,6 +42,17 @@ NormalNode.prototype.generate = function ( builder, output ) {

break;

case NormalNode.BENT:

if ( builder.isShader( 'fragment' ) ) {
if( builder.context.anisotropy ) result = 'getBentNormal( geometry, anisotropyFactor, roughnessFactor )';
DanielSturk marked this conversation as resolved.
Show resolved Hide resolved
else result = 'geometry.normal';
} else {
result = 'geometryNormal';
}

break;

case NormalNode.WORLD:

if ( builder.isShader( 'vertex' ) ) {
Expand Down
9 changes: 5 additions & 4 deletions examples/jsm/nodes/accessors/ReflectNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { TempNode } from '../core/TempNode.js';
import { PositionNode } from './PositionNode.js';
import { NormalNode } from './NormalNode.js';

function ReflectNode( scope ) {
function ReflectNode( scope, normal ) {

TempNode.call( this, 'v3' );

this.scope = scope || ReflectNode.CUBE;
this.normal = normal || null;

}

Expand Down Expand Up @@ -54,7 +55,7 @@ ReflectNode.prototype.generate = function ( builder, output ) {

case ReflectNode.VECTOR:

var viewNormalNode = builder.context.viewNormal || new NormalNode();
var viewNormalNode = this.normal || builder.context.viewNormal || new NormalNode();
var roughnessNode = builder.context.roughness;

var viewNormal = viewNormalNode.build( builder, 'v3' );
Expand Down Expand Up @@ -88,7 +89,7 @@ ReflectNode.prototype.generate = function ( builder, output ) {

case ReflectNode.CUBE:

var reflectVec = new ReflectNode( ReflectNode.VECTOR ).build( builder, 'v3' );
var reflectVec = new ReflectNode( ReflectNode.VECTOR, this.normal ).build( builder, 'v3' );

var code = 'vec3( -' + reflectVec + '.x, ' + reflectVec + '.yz )';

Expand All @@ -108,7 +109,7 @@ ReflectNode.prototype.generate = function ( builder, output ) {

case ReflectNode.SPHERE:

var reflectVec = new ReflectNode( ReflectNode.VECTOR ).build( builder, 'v3' );
var reflectVec = new ReflectNode( ReflectNode.VECTOR, this.normal ).build( builder, 'v3' );

var code = 'normalize( ( viewMatrix * vec4( ' + reflectVec + ', 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) ).xy * 0.5 + 0.5';

Expand Down
3 changes: 2 additions & 1 deletion examples/jsm/nodes/inputs/CubeTextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { InputNode } from '../core/InputNode.js';
import { ReflectNode } from '../accessors/ReflectNode.js';
import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
import { ExpressionNode } from '../core/ExpressionNode.js';
import { NormalNode } from '../accessors/NormalNode.js';

function CubeTextureNode( value, uv, bias ) {

InputNode.call( this, 'v4', { shared: true } );

this.value = value;
this.uv = uv || new ReflectNode();
this.uv = uv || new ReflectNode( ReflectNode.CUBE, new NormalNode( NormalNode.BENT ) );
this.bias = bias;

}
Expand Down
4 changes: 3 additions & 1 deletion examples/jsm/nodes/materials/StandardNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ NodeUtils.addShortcuts( StandardNodeMaterial.prototype, 'fragment', [
'environment',
'mask',
'position',
'sheen'
'sheen',
'anisotropy',
'anisotropyRotation'
] );

export { StandardNodeMaterial };
52 changes: 45 additions & 7 deletions examples/jsm/nodes/materials/nodes/StandardNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ExpressionNode } from '../../core/ExpressionNode.js';
import { ColorNode } from '../../inputs/ColorNode.js';
import { FloatNode } from '../../inputs/FloatNode.js';
import { SpecularMIPLevelNode } from '../../utils/SpecularMIPLevelNode.js';
import { NormalNode } from '../../accessors/NormalNode.js';

function StandardNode() {

Expand All @@ -35,7 +36,7 @@ StandardNode.prototype.build = function ( builder ) {

builder.define('STANDARD');

var useClearcoat = this.clearcoat || this.clearcoatRoughness || this.clearCoatNormal;
var useClearcoat = this.clearcoat || this.clearcoatRoughness || this.clearcoatNormal;

if( useClearcoat ){

Expand Down Expand Up @@ -70,12 +71,19 @@ StandardNode.prototype.build = function ( builder ) {
builder.addParsCode( [
"varying vec3 vViewPosition;",

"#ifndef FLAT_SHADED",
"#if !defined( FLAT_SHADED ) || defined( USE_TANGENT )",

" varying vec3 vNormal;",

"#endif",

"#ifdef USE_TANGENT",

" varying vec3 vTangent;",
" varying vec3 vBitangent;",

"#endif",

//"#include <encodings_pars_fragment>", // encoding functions
"#include <fog_pars_vertex>",
"#include <morphtarget_pars_vertex>",
Expand All @@ -93,12 +101,19 @@ StandardNode.prototype.build = function ( builder ) {
"#include <skinnormal_vertex>",
"#include <defaultnormal_vertex>",

"#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
"#if !defined( FLAT_SHADED ) || defined( USE_TANGENT )", // Normal computed with derivatives when FLAT_SHADED

" vNormal = normalize( transformedNormal );",

"#endif",

"#ifdef USE_TANGENT",

" vTangent = normalize( transformedTangent );",
" vBitangent = normalize( cross( vNormal, vTangent ) * tangent.w );",

"#endif",

"#include <begin_vertex>"
];

Expand Down Expand Up @@ -132,11 +147,18 @@ StandardNode.prototype.build = function ( builder ) {
var specularRoughness = new ExpressionNode('material.specularRoughness', 'f' );
var clearcoatRoughness = new ExpressionNode('material.clearcoatRoughness', 'f' );

if ( this.anisotropy ) this.anisotropy.analyze( builder );
if ( this.anisotropyRotation ) this.anisotropyRotation.analyze( builder );

var anisotropy = this.anisotropy ? this.anisotropy.flow( builder, 'f' ) : undefined;
var anisotropyRotation = anisotropy && this.anisotropyRotation ? this.anisotropyRotation.flow( builder, 'f' ) : undefined;

var contextEnvironment = {
roughness: specularRoughness,
bias: new SpecularMIPLevelNode( specularRoughness ),
viewNormal: new ExpressionNode('normal', 'v3'),
gamma: true
viewNormal: new ExpressionNode('normal', 'vec3'),
gamma: true,
anisotropy: !!anisotropy
};

var contextGammaOnly = {
Expand Down Expand Up @@ -236,25 +258,34 @@ StandardNode.prototype.build = function ( builder ) {
var clearcoatEnv = useClearcoat && environment ? this.environment.flow( builder, 'c', { cache: 'clearcoat', context: contextClearcoatEnvironment, slot: 'environment' } ) : undefined;

var sheen = this.sheen ? this.sheen.flow( builder, 'c' ) : undefined;
builder.requires.uv[0] = builder.requires.uv[0] || !!anisotropy; // if tangents aren't available

builder.requires.transparent = alpha !== undefined;

builder.addParsCode( [
"varying vec3 vViewPosition;",

"#ifndef FLAT_SHADED",
"#if !defined( FLAT_SHADED ) || defined( USE_TANGENT )",

" varying vec3 vNormal;",

"#endif",

"#ifdef USE_TANGENT",

" varying vec3 vTangent;",
" varying vec3 vBitangent;",

"#endif",

"#include <dithering_pars_fragment>",
"#include <fog_pars_fragment>",
"#include <bsdfs>",
"#include <lights_pars_begin>",
"#include <lights_physical_pars_fragment>",
"#include <shadowmap_pars_fragment>",
"#include <logdepthbuf_pars_fragment>"
"#include <logdepthbuf_pars_fragment>",
"#include <anisotropy_pars_fragment>"
].join( "\n" ) );

var output = [
Expand Down Expand Up @@ -356,6 +387,13 @@ StandardNode.prototype.build = function ( builder ) {

}

if ( anisotropy ) {

output.push( 'float anisotropyFactor = ' + anisotropy.result + ';' );
output.push( 'float anisotropyRotationFactor = ' + ( anisotropyRotation ? anisotropyRotation.result : '0.' ) + ';' );

}

if ( sheen ) {

output.push( 'material.sheenColor = ' + sheen.result + ';' );
Expand Down
Binary file added examples/textures/anisotropyRotation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/textures/carbon/carbon-fiber-tiny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/textures/checkers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading