Skip to content

Commit

Permalink
Merge pull request #21208 from sunag/nodematerial-light
Browse files Browse the repository at this point in the history
WebGPU: NodeMaterial updates
  • Loading branch information
mrdoob committed Feb 7, 2021
2 parents 5191deb + da089ea commit 5e6fa8c
Show file tree
Hide file tree
Showing 16 changed files with 494 additions and 23 deletions.
3 changes: 2 additions & 1 deletion examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@
"webgpu": [
"webgpu_sandbox",
"webgpu_rtt",
"webgpu_compute"
"webgpu_compute",
"webgpu_materials"
],
"webaudio": [
"webaudio_orientation",
Expand Down
8 changes: 6 additions & 2 deletions examples/jsm/renderers/nodes/accessors/CameraNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ class CameraNode extends Node {

}

} else if ( inputNode === null || inputNode.isVector3Node !== true ) {
} else if ( scope === CameraNode.POSITION ) {

inputNode = new Vector3Node();
if ( inputNode === null || inputNode.isVector3Node !== true ) {

inputNode = new Vector3Node();

}

}

Expand Down
67 changes: 63 additions & 4 deletions examples/jsm/renderers/nodes/accessors/ModelNode.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,94 @@
import Node from '../core/Node.js';
import Matrix4Node from '../inputs/Matrix4Node.js';
import Matrix3Node from '../inputs/Matrix3Node.js';
import { NodeUpdateType } from '../core/constants.js';

class ModelNode extends Node {

static VIEW = 'view';
static NORMAL = 'normal';

constructor( scope = ModelNode.VIEW ) {

super( 'mat4' );
super();

this.scope = scope;

this.updateType = NodeUpdateType.Object;

this._inputNode = new Matrix4Node( null );
this._inputNode = null;

}

getType() {

const scope = this.scope;

if ( scope === ModelNode.VIEW ) {

return 'mat4';

} else if ( scope === ModelNode.NORMAL ) {

return 'mat3';

}

}

update( frame ) {

const object = frame.object;
const inputNode = this._inputNode;
const scope = this.scope;

if ( scope === ModelNode.VIEW ) {

inputNode.value = object.modelViewMatrix;

} else if ( scope === ModelNode.NORMAL ) {

inputNode.value = object.modelViewMatrix;
inputNode.value = object.normalMatrix;

}

}

generate( builder, output ) {

return this._inputNode.build( builder, output );
const nodeData = builder.getDataFromNode( this );

let inputNode = this._inputNode;

if ( nodeData.inputNode === undefined ) {

const scope = this.scope;

if ( scope === ModelNode.VIEW ) {

if ( inputNode === null || inputNode.isMatrix4Node !== true ) {

inputNode = new Matrix4Node( null );

}

} else if ( scope === ModelNode.NORMAL ) {

if ( inputNode === null || inputNode.isMatrix3Node !== true ) {

inputNode = new Matrix3Node( null );

}

}

this._inputNode = inputNode;

nodeData.inputNode = inputNode;

}

return inputNode.build( builder, output );

}

Expand Down
84 changes: 84 additions & 0 deletions examples/jsm/renderers/nodes/accessors/NormalNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Node from '../core/Node.js';
import AttributeNode from '../core/AttributeNode.js';
import VaryNode from '../core/VaryNode.js';
import ModelNode from '../accessors/ModelNode.js';
import CameraNode from '../accessors/CameraNode.js';
import OperatorNode from '../math/OperatorNode.js';
import MathNode from '../math/MathNode.js';

class NormalNode extends Node {

static LOCAL = 'local';
static WORLD = 'world';
static VIEW = 'view';

constructor( scope = NormalNode.LOCAL ) {

super( 'vec3' );

this.scope = scope;

}

generate( builder, output ) {

const type = this.getType( builder );
const nodeData = builder.getDataFromNode( this, builder.shaderStage );
const scope = this.scope;

let localNormalNode = nodeData.localNormalNode;

if ( localNormalNode === undefined ) {

localNormalNode = new AttributeNode( 'normal', 'vec3' );

nodeData.localNormalNode = localNormalNode;

}

let outputNode = localNormalNode;

if ( scope === NormalNode.VIEW ) {

let viewNormalNode = nodeData.viewNormalNode;

if ( viewNormalNode === undefined ) {

const unnormalizedWNNode = new OperatorNode( '*', new ModelNode( ModelNode.NORMAL ), localNormalNode );
const vertexNormalNode = new MathNode( MathNode.NORMALIZE, unnormalizedWNNode );

viewNormalNode = new MathNode( MathNode.NORMALIZE, new VaryNode( vertexNormalNode ) );

nodeData.viewNormalNode = viewNormalNode;

}

outputNode = viewNormalNode;

} else if ( scope === NormalNode.WORLD ) {

let worldNormalNode = nodeData.worldNormalNode;

if ( worldNormalNode === undefined ) {

const vertexNormalNode = new MathNode( MathNode.INVERSE_TRANSFORM_DIRETION, new NormalNode( NormalNode.VIEW ), new CameraNode( CameraNode.VIEW ) );

worldNormalNode = new VaryNode( vertexNormalNode );

nodeData.worldNormalNode = worldNormalNode;

}

outputNode = worldNormalNode;

}

const normalSnipped = outputNode.build( builder, type );

return builder.format( normalSnipped, type, output );

}

}

export default NormalNode;
2 changes: 1 addition & 1 deletion examples/jsm/renderers/nodes/accessors/PositionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AttributeNode from '../core/AttributeNode.js';

class PositionNode extends Node {

static LOCAL = 'position';
static LOCAL = 'local';

constructor( scope = PositionNode.POSITION ) {

Expand Down
3 changes: 2 additions & 1 deletion examples/jsm/renderers/nodes/core/AttributeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class AttributeNode extends Node {

if ( nodeVary === undefined ) {

nodeVary = builder.getVaryFromNode( this, attribute.type, attributeName );
nodeVary = builder.getVaryFromNode( this, attribute.type );
nodeVary.snippet = attributeName;

nodeData.nodeVary = nodeVary;

Expand Down
16 changes: 15 additions & 1 deletion examples/jsm/renderers/nodes/core/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,21 @@ class Node {

}

build( builder, output ) {
buildStage( builder, shaderStage, output = null ) {

const oldShaderStage = builder.shaderStage;

builder.shaderStage = shaderStage;

const snippet = this.build( builder, output );

builder.shaderStage = oldShaderStage;

return snippet;

}

build( builder, output = null ) {

builder.addNode( this );

Expand Down
4 changes: 2 additions & 2 deletions examples/jsm/renderers/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class NodeBuilder {

}

getVaryFromNode( node, type, value ) {
getVaryFromNode( node, type ) {

const nodeData = this.getDataFromNode( node );

Expand All @@ -209,7 +209,7 @@ class NodeBuilder {
const varys = this.varys;
const index = varys.length;

nodeVary = new NodeVary( 'nodeV' + index, type, value );
nodeVary = new NodeVary( 'nodeV' + index, type );

varys.push( nodeVary );

Expand Down
4 changes: 2 additions & 2 deletions examples/jsm/renderers/nodes/core/NodeVary.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class NodeVary {

constructor( name, type, value ) {
constructor( name, type, snippet = '' ) {

this.name = name;
this.type = type;
this.value = value;
this.snippet = snippet;

Object.defineProperty( this, 'isNodeVary', { value: true } );

Expand Down
8 changes: 6 additions & 2 deletions examples/jsm/renderers/nodes/core/VaryNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Node from './Node.js';
import { NodeShaderStage } from './constants.js';

class VaryNode extends Node {

Expand All @@ -22,9 +23,12 @@ class VaryNode extends Node {

const type = this.getType( builder );

const value = this.value.build( builder, type );
// force nodeVary.snippet work in vertex stage
const snippet = this.value.buildStage( builder, NodeShaderStage.Vertex, type );

const nodeVary = builder.getVaryFromNode( this, type );
nodeVary.snippet = snippet;

const nodeVary = builder.getVaryFromNode( this, type, value );
const propertyName = builder.getPropertyName( nodeVary );

return builder.format( propertyName, type, output );
Expand Down
5 changes: 5 additions & 0 deletions examples/jsm/renderers/nodes/core/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export const NodeShaderStage = {
Vertex: 'vertex',
Fragment: 'fragment'
};

export const NodeUpdateType = {
None: 'none',
Frame: 'frame',
Expand Down
Loading

0 comments on commit 5e6fa8c

Please sign in to comment.