David Catuhe 6 سال پیش
والد
کامیت
1be0e50418

+ 4 - 4
src/Engines/Processors/shaderCodeArithmeticTestNode.ts

@@ -7,11 +7,11 @@ export class ShaderCodeArithmeticTestNode extends ShaderCodeNode {
     testValue: string;
     testValue: string;
     child: ShaderCodeNode;
     child: ShaderCodeNode;
 
 
-    getNextNode(preprocessors: {[key: string]: string}) {
+    isValid(preprocessors: { [key: string]: string }) {
         let value = preprocessors[this.define];
         let value = preprocessors[this.define];
 
 
         if (value === undefined) {
         if (value === undefined) {
-            return this.next;
+            return false;
         }
         }
 
 
         let condition = false;
         let condition = false;
@@ -25,9 +25,9 @@ export class ShaderCodeArithmeticTestNode extends ShaderCodeNode {
         }
         }
 
 
         if (condition) {
         if (condition) {
-            return this.child;
+            return true;
         }
         }
 
 
-        return this.next;
+        return false;
     }
     }
 }
 }

+ 15 - 0
src/Engines/Processors/shaderCodeConditionNode.ts

@@ -0,0 +1,15 @@
+import { ShaderCodeNode } from './shaderCodeNode';
+
+/** @hidden */
+export class ShaderCodeConditionNode extends ShaderCodeNode {
+    if: ShaderCodeNode;
+    else: ShaderCodeNode;
+
+    process(preprocessors: { [key: string]: string }) {
+        if (this.if.isValid(preprocessors)) {
+            return this.if.process(preprocessors);
+        }
+
+        return this.else.process(preprocessors);
+    }
+}

+ 2 - 7
src/Engines/Processors/shaderCodeDefineTestNode.ts

@@ -3,13 +3,8 @@ import { ShaderCodeNode } from './shaderCodeNode';
 /** @hidden */
 /** @hidden */
 export class ShaderCodeDefineTestNode extends ShaderCodeNode {
 export class ShaderCodeDefineTestNode extends ShaderCodeNode {
     define: string;
     define: string;
-    child: ShaderCodeNode;
 
 
-    getNextNode(preprocessors: {[key: string]: string}) {
-        if (preprocessors[this.define] !== undefined) {
-            return this.child;
-        } else {
-            return this.next;
-        }
+    isValid(preprocessors: { [key: string]: string }) {
+        return preprocessors[this.define] !== undefined;
     }
     }
 }
 }

+ 16 - 4
src/Engines/Processors/shaderCodeNode.ts

@@ -2,10 +2,22 @@
 /** @hidden */
 /** @hidden */
 export class ShaderCodeNode {
 export class ShaderCodeNode {
     line: string;
     line: string;
-    next: ShaderCodeNode;
-    parent?: ShaderCodeNode;
+    children: ShaderCodeNode[] = [];
 
 
-    getNextNode(preprocessors: {[key: string]: string}) {
-        return this.next;
+    isValid(preprocessors: { [key: string]: string }): boolean {
+        return true;
+    }
+
+    process(preprocessors: { [key: string]: string }): string {
+        let result = "";
+        if (this.isValid(preprocessors)) {
+            result += this.line + "\r\n";
+        }
+
+        this.children.forEach(child => {
+            result += child.process(preprocessors);
+        });
+
+        return result;
     }
     }
 }
 }

+ 23 - 20
src/Engines/Processors/shaderProcessor.ts

@@ -3,6 +3,7 @@ import { ShaderCodeNode } from './shaderCodeNode';
 import { ShaderCodeCursor } from './shaderCodeCursor';
 import { ShaderCodeCursor } from './shaderCodeCursor';
 import { ShaderCodeArithmeticTestNode } from './shaderCodeArithmeticTestNode';
 import { ShaderCodeArithmeticTestNode } from './shaderCodeArithmeticTestNode';
 import { ShaderCodeDefineTestNode } from './shaderCodeDefineTestNode';
 import { ShaderCodeDefineTestNode } from './shaderCodeDefineTestNode';
+import { Nullable } from '../../types';
 
 
 /** @hidden */
 /** @hidden */
 interface ProcessingOptions {
 interface ProcessingOptions {
@@ -43,7 +44,7 @@ export class ShaderProcessor {
         return source;
         return source;
     }
     }
 
 
-    private static _EvaluatePreProcessors(sourceCode: string, preprocessors: {[key: string]: string}): string {
+    private static _EvaluatePreProcessors(sourceCode: string, preprocessors: { [key: string]: string }): string {
         const rootNode = new ShaderCodeNode();
         const rootNode = new ShaderCodeNode();
         let cursor = new ShaderCodeCursor();
         let cursor = new ShaderCodeCursor();
 
 
@@ -54,7 +55,7 @@ export class ShaderProcessor {
         // Decompose
         // Decompose
         while (!cursor.eof) {
         while (!cursor.eof) {
             let line = cursor.currentLine;
             let line = cursor.currentLine;
-            if (line[0] === "#") {
+            if (line[0] === "#" && line[1] !== "d") {
                 let first6 = line.substring(0, 6).toLowerCase();
                 let first6 = line.substring(0, 6).toLowerCase();
                 if (first6 === "#ifdef") {
                 if (first6 === "#ifdef") {
                     let newNode = new ShaderCodeDefineTestNode();
                     let newNode = new ShaderCodeDefineTestNode();
@@ -67,10 +68,25 @@ export class ShaderProcessor {
                         cursor.currentNode.next = newNode;
                         cursor.currentNode.next = newNode;
                     }
                     }
                     cursor.currentNode = newNode.child;
                     cursor.currentNode = newNode.child;
-                } else if (first6 === "#endif") {
+                } else if (line.substring(0, 5).toLowerCase() === "#else") {
+                    let ifNode = cursor.parentNode as ShaderCodeDefineTestNode;
+                    cursor.parentNode = cursor.parentNode!.parent;
+
+                    let newNode = new ShaderCodeDefineTestNode();
+                    newNode.define = ifNode.define;
+                    newNode.not = true;
+                    newNode.parent = cursor.parentNode;
+                    newNode.child = new ShaderCodeNode();
+
+                    cursor.parentNode = newNode;
+                    if (cursor.currentNode) {
+                        cursor.currentNode.next = newNode;
+                    }
+                    cursor.currentNode = newNode.child;
                     cursor.currentNode = cursor.parentNode;
                     cursor.currentNode = cursor.parentNode;
                     cursor.parentNode = cursor.parentNode!.parent;
                     cursor.parentNode = cursor.parentNode!.parent;
-                }else if (line.substring(0, 3).toLowerCase() === "#if") {
+                } else if (first6 === "#endif") {
+                } else if (line.substring(0, 3).toLowerCase() === "#if") {
                     let newNode = new ShaderCodeArithmeticTestNode();
                     let newNode = new ShaderCodeArithmeticTestNode();
                     let regex = /(.+)(.)(.+)/;
                     let regex = /(.+)(.)(.+)/;
                     let matches = regex.exec(line.substring(3).trim())!;
                     let matches = regex.exec(line.substring(3).trim())!;
@@ -89,7 +105,6 @@ export class ShaderProcessor {
             } else {
             } else {
                 let newNode = new ShaderCodeNode();
                 let newNode = new ShaderCodeNode();
                 newNode.line = line;
                 newNode.line = line;
-                newNode.parent = cursor.parentNode;
 
 
                 if (cursor.currentNode) {
                 if (cursor.currentNode) {
                     cursor.currentNode.next = newNode;
                     cursor.currentNode.next = newNode;
@@ -101,19 +116,7 @@ export class ShaderProcessor {
         }
         }
 
 
         // Recompose
         // Recompose
-
-        let currentNode = rootNode;
-        let processedCode = "";
-
-        while (currentNode) {
-            if (currentNode.line) {
-                processedCode += currentNode.line + "\r\n";
-            }
-
-            currentNode = currentNode.getNextNode(preprocessors);
-        }
-
-        return processedCode;
+        return rootNode.process(preprocessors);
     }
     }
 
 
     private static _ProcessShaderConversion(sourceCode: string, options: ProcessingOptions): string {
     private static _ProcessShaderConversion(sourceCode: string, options: ProcessingOptions): string {
@@ -131,8 +134,8 @@ export class ShaderProcessor {
 
 
         let defines = options.defines.split("\n");
         let defines = options.defines.split("\n");
 
 
-        let preprocessors: {[key: string]: string} = {};
-        
+        let preprocessors: { [key: string]: string } = {};
+
         for (var define of defines) {
         for (var define of defines) {
             let keyValue = define.replace("#define", "").trim();
             let keyValue = define.replace("#define", "").trim();
             let split = keyValue.split(" ");
             let split = keyValue.split(" ");