Browse Source

NodeMaterials: Introduce a new RawNodeMaterial class

Signed-off-by: martinRenou <[email protected]>
martinRenou 5 years ago
parent
commit
7e7d07cfb8

+ 2 - 0
examples/jsm/nodes/Nodes.d.ts

@@ -85,6 +85,7 @@ export * from './effects/LuminanceNode';
 // material nodes
 
 export * from './materials/nodes/RawNode';
+export * from './materials/nodes/SimpleNode';
 export * from './materials/nodes/SpriteNode';
 export * from './materials/nodes/PhongNode';
 export * from './materials/nodes/StandardNode';
@@ -93,6 +94,7 @@ export * from './materials/nodes/MeshStandardNode';
 // materials
 
 export * from './materials/NodeMaterial';
+export * from './materials/RawNodeMaterial';
 export * from './materials/SpriteNodeMaterial';
 export * from './materials/PhongNodeMaterial';
 export * from './materials/StandardNodeMaterial';

+ 2 - 0
examples/jsm/nodes/Nodes.js

@@ -88,6 +88,7 @@ export { LuminanceNode } from './effects/LuminanceNode.js';
 // material nodes
 
 export { RawNode } from './materials/nodes/RawNode.js';
+export { SimpleNode } from './materials/nodes/SimpleNode.js';
 export { SpriteNode } from './materials/nodes/SpriteNode.js';
 export { PhongNode } from './materials/nodes/PhongNode.js';
 export { StandardNode } from './materials/nodes/StandardNode.js';
@@ -96,6 +97,7 @@ export { MeshStandardNode } from './materials/nodes/MeshStandardNode.js';
 // materials
 
 export { NodeMaterial } from './materials/NodeMaterial.js';
+export { RawNodeMaterial } from './materials/RawNodeMaterial.js';
 export { SpriteNodeMaterial } from './materials/SpriteNodeMaterial.js';
 export { PhongNodeMaterial } from './materials/PhongNodeMaterial.js';
 export { StandardNodeMaterial } from './materials/StandardNodeMaterial.js';

+ 12 - 0
examples/jsm/nodes/materials/RawNodeMaterial.d.ts

@@ -0,0 +1,12 @@
+import { Node } from '../core/Node';
+import { NodeMaterial } from './NodeMaterial';
+
+export class RawNodeMaterial extends NodeMaterial {
+
+  constructor();
+
+  color: Node;
+  alpha: Node;
+  position: Node;
+
+}

+ 28 - 0
examples/jsm/nodes/materials/RawNodeMaterial.js

@@ -0,0 +1,28 @@
+/**
+ * @author sunag / http://www.sunag.com.br/
+ */
+
+import { SimpleNode } from './nodes/SimpleNode.js';
+import { NodeMaterial } from './NodeMaterial.js';
+import { NodeUtils } from '../core/NodeUtils.js';
+
+function RawNodeMaterial() {
+
+  var node = new SimpleNode();
+
+  NodeMaterial.call( this, node, node );
+
+  this.type = "RawNodeMaterial";
+
+}
+
+RawNodeMaterial.prototype = Object.create( NodeMaterial.prototype );
+RawNodeMaterial.prototype.constructor = RawNodeMaterial;
+
+NodeUtils.addShortcuts( RawNodeMaterial.prototype, 'fragment', [
+  'color',
+  'alpha',
+  'position'
+] );
+
+export { RawNodeMaterial };

+ 16 - 0
examples/jsm/nodes/materials/nodes/SimpleNode.d.ts

@@ -0,0 +1,16 @@
+import { NodeBuilder } from '../../core/NodeBuilder';
+import { Node } from '../../core/Node';
+
+export class SimpleNode extends Node {
+
+  constructor();
+
+  position: Node;
+  color: Node;
+  alpha: Node;
+  nodeType: string;
+
+  build( builder: NodeBuilder ): string;
+  copy( source: SimpleNode ): this;
+
+}

+ 111 - 0
examples/jsm/nodes/materials/nodes/SimpleNode.js

@@ -0,0 +1,111 @@
+/**
+ * @author sunag / http://www.sunag.com.br/
+ */
+
+import { Node } from '../../core/Node.js';
+
+function SimpleNode() {
+
+  Node.call( this );
+
+  this.color = new ColorNode( 0xFFFFFF );
+
+}
+
+SimpleNode.prototype = Object.create( Node.prototype );
+SimpleNode.prototype.constructor = SimpleNode;
+SimpleNode.prototype.nodeType = "Simple";
+
+SimpleNode.prototype.generate = function ( builder ) {
+
+  var code;
+
+  if (builder.isShader('vertex')) {
+
+    var position = this.position ? this.position.analyzeAndFlow( builder, 'v3', { cache: 'position' } ) : undefined;
+
+    var output = [
+      "vec3 transformed = position;"
+    ];
+
+    if ( position ) {
+      output.push(
+        position.code,
+        position.result ? "gl_Position = " + position.result + ";" : ''
+      );
+    } else {
+      output.push( "gl_Position = projectionMatrix * modelViewMatrix * vec4(transformed, 1.0)" );
+    }
+
+    code = output.join("\n");
+  } else {
+    // Analyze all nodes to reuse generate codes
+    this.color.analyze( builder, { slot: 'color' } );
+
+    if ( this.alpha ) this.alpha.analyze( builder );
+
+    // Build code
+    var color = this.color.flow(builder, 'c', { slot: 'color' });
+    var alpha = this.alpha ? this.alpha.flow( builder, 'f' ) : undefined;
+
+    builder.requires.transparent = alpha !== undefined;
+
+    var output = [
+      color.code,
+    ];
+
+    if ( alpha ) {
+      output.push(
+        alpha.code,
+        '#ifdef ALPHATEST',
+
+        ' if ( ' + alpha.result + ' <= ALPHATEST ) discard;',
+
+        '#endif'
+      );
+    }
+
+    if ( alpha ) {
+      output.push( "gl_FragColor = vec4(" + color.result + ", " + alpha.result + " );" );
+    } else {
+      output.push( "gl_FragColor = vec4(" + color.result + ", 1.0 );" );
+    }
+
+    code = output.join( "\n" );
+  }
+
+  return code;
+
+};
+
+SimpleNode.prototype.copy = function ( source ) {
+
+  Node.prototype.copy.call( this, source );
+
+  this.position = source.position;
+  this.color = source.color;
+  this.alpha = source.alpha;
+
+  return this;
+
+};
+
+SimpleNode.prototype.toJSON = function ( meta ) {
+
+  var data = this.getJSONNode( meta );
+
+  if ( ! data ) {
+
+    data = this.createJSONNode( meta );
+
+    data.position = this.position.toJSON( meta ).uuid;
+    data.color = this.color.toJSON( meta ).uuid;
+    data.alpha = this.alpha.toJSON( meta ).uuid;
+
+  }
+
+  return data;
+
+};
+
+export { SimpleNode };