Browse Source

Associated with #6012

David Catuhe 6 years ago
parent
commit
ce94789c2e

+ 5 - 3
src/Materials/Node/Blocks/addBlock.ts

@@ -13,9 +13,11 @@ export class AddBlock extends NodeMaterialBlock {
     public constructor(name: string) {
         super(name);
 
-        this.registerInput("left", NodeMaterialBlockConnectionPointTypes.Vector4OrColor4);
-        this.registerInput("right", NodeMaterialBlockConnectionPointTypes.Vector4OrColor4);
-        this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Vector4OrColor4);
+        this.registerInput("left", NodeMaterialBlockConnectionPointTypes.AutoDetect);
+        this.registerInput("right", NodeMaterialBlockConnectionPointTypes.AutoDetect);
+        this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
+
+        this._outputs[0]._typeConnectionSource = this._inputs[0];
     }
 
     /**

+ 1 - 0
src/Materials/Node/Blocks/index.ts

@@ -4,6 +4,7 @@ export * from "./Dual/index";
 export * from "./multiplyBlock";
 export * from "./addBlock";
 export * from "./clampBlock";
+export * from "./scaleBlock";
 export * from "./vector2TransformBlock";
 export * from "./vector3TransformBlock";
 export * from "./vector4TransformBlock";

+ 5 - 3
src/Materials/Node/Blocks/multiplyBlock.ts

@@ -13,9 +13,11 @@ export class MultiplyBlock extends NodeMaterialBlock {
     public constructor(name: string) {
         super(name);
 
-        this.registerInput("left", NodeMaterialBlockConnectionPointTypes.Vector4OrColor4);
-        this.registerInput("right", NodeMaterialBlockConnectionPointTypes.Vector4OrColor4);
-        this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Vector4OrColor4);
+        this.registerInput("left", NodeMaterialBlockConnectionPointTypes.AutoDetect);
+        this.registerInput("right", NodeMaterialBlockConnectionPointTypes.AutoDetect);
+        this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
+
+        this._outputs[0]._typeConnectionSource = this._inputs[0];
     }
 
     /**

+ 61 - 0
src/Materials/Node/Blocks/scaleBlock.ts

@@ -0,0 +1,61 @@
+import { NodeMaterialBlock } from '../nodeMaterialBlock';
+import { NodeMaterialBlockConnectionPointTypes } from '../nodeMaterialBlockConnectionPointTypes';
+import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
+import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
+/**
+ * Block used to scale a value
+ */
+export class ScaleBlock extends NodeMaterialBlock {
+    /**
+     * Creates a new ScaleBlock
+     * @param name defines the block name
+     */
+    public constructor(name: string) {
+        super(name);
+
+        this.registerInput("value", NodeMaterialBlockConnectionPointTypes.AutoDetect);
+        this.registerInput("scale", NodeMaterialBlockConnectionPointTypes.Float);
+        this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
+
+        this._outputs[0]._typeConnectionSource = this._inputs[0];
+    }
+
+    /**
+     * Gets the current class name
+     * @returns the class name
+     */
+    public getClassName() {
+        return "ScaleBlock";
+    }
+
+    /**
+     * Gets the value operand input component
+     */
+    public get value(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    /**
+     * Gets the scale operand input component
+     */
+    public get scale(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }
+
+    /**
+     * Gets the right operand input component
+     */
+    public get right(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }
+
+    protected _buildBlock(state: NodeMaterialBuildState) {
+        super._buildBlock(state);
+
+        let output = this._outputs[0];
+
+        state.compilationString += this._declareOutput(output, state) + ` = ${this.value.associatedVariableName} * ${this.scale.associatedVariableName};\r\n`;
+
+        return this;
+    }
+}

+ 1 - 1
src/Materials/Node/nodeMaterialBlock.ts

@@ -212,7 +212,7 @@ export class NodeMaterialBlock {
     public getFirstAvailableInput(forOutput: Nullable<NodeMaterialConnectionPoint> = null) {
         for (var input of this._inputs) {
             if (!input.isUniform && !input.isAttribute && !input.connectedPoint) {
-                if (!forOutput || (forOutput.type & input.type) !== 0) {
+                if (!forOutput || (forOutput.type & input.type) !== 0 || input.type === NodeMaterialBlockConnectionPointTypes.AutoDetect) {
                     return input;
                 }
             }

+ 20 - 2
src/Materials/Node/nodeMaterialBlockConnectionPoint.ts

@@ -27,12 +27,30 @@ export class NodeMaterialConnectionPoint {
     public _wellKnownValue: Nullable<NodeMaterialWellKnownValues> = null;
 
     /** @hidden */
+    public _typeConnectionSource: Nullable<NodeMaterialConnectionPoint> = null;
+
+    /** @hidden */
     public _needToEmitVarying = true;
 
+    private _type = NodeMaterialBlockConnectionPointTypes.Float;
     /**
      * Gets or sets the connection point type (default is float)
      */
-    public type: NodeMaterialBlockConnectionPointTypes = NodeMaterialBlockConnectionPointTypes.Float;
+    public get type(): NodeMaterialBlockConnectionPointTypes {
+        if (this._type === NodeMaterialBlockConnectionPointTypes.AutoDetect && this._connectedPoint) {
+            return this._connectedPoint.type;
+        }
+
+        if (this._type === NodeMaterialBlockConnectionPointTypes.BasedOnInput && this._typeConnectionSource) {
+            return this._typeConnectionSource.type;
+        }
+
+        return this._type;
+    }
+
+    public set type(value: NodeMaterialBlockConnectionPointTypes) {
+        this._type = value;
+    }
 
     /**
      * Gets or sets the connection point name
@@ -247,7 +265,7 @@ export class NodeMaterialConnectionPoint {
      * @returns the current connection point
      */
     public connectTo(connectionPoint: NodeMaterialConnectionPoint): NodeMaterialConnectionPoint {
-        if ((this.type & connectionPoint.type) === 0) {
+        if ((this.type & connectionPoint.type) === 0 && connectionPoint.type !== NodeMaterialBlockConnectionPointTypes.AutoDetect) {
             let fail = true;
             // Check swizzle
             if (this.swizzle) {

+ 4 - 0
src/Materials/Node/nodeMaterialBlockConnectionPointTypes.ts

@@ -32,4 +32,8 @@ export enum NodeMaterialBlockConnectionPointTypes {
     Color3OrColor4 = Color3 | Color4,
     /** Vector3 or Color3 */
     Vector3OrColor3OrVector4OrColor4 = Vector3 | Color3 | Vector4 | Color4,
+    /** Detect type based on connection */
+    AutoDetect = 1024,
+    /** Output type that will be defined by input type */
+    BasedOnInput = 2048
 }