Jelajahi Sumber

Inline Constants

sebavan 5 tahun lalu
induk
melakukan
144f187d8b

+ 58 - 0
Tools/Gulp/helpers/gulp-processConstants.js

@@ -0,0 +1,58 @@
+// Dependencies.
+var through = require('through2');
+var PluginError = require('plugin-error');
+const fs = require('fs');
+const babylonConstants = require(__dirname + '/../../../dist/preview release/babylon.max').Constants;
+
+
+/**
+ * Replace all constants by their inlined values.
+ */
+function processConstants(sourceCode) {
+    var regexImport = /import { Constants } from .*;/g;
+    sourceCode = sourceCode.replace(regexImport, "");
+
+    var regexConstant = /(?<![_0-9a-zA-Z])Constants\.([_0-9a-zA-Z]*)/g;
+    var match = regexConstant.exec(sourceCode);
+    var constantList = [];
+    while (match) {
+        var constantName = match[1];
+        if (constantName && constantName.length > 1) {
+            constantList.push(constantName);
+        }
+        match = regexConstant.exec(sourceCode);
+    }
+
+    for (var constant of constantList) {
+        var value = babylonConstants[constant];
+        var regex = new RegExp(`(?<![_0-9a-zA-Z])Constants\.${constant}(?![_0-9a-zA-Z])`, "g");
+        sourceCode = sourceCode.replace(regex, value);
+    }
+
+    return sourceCode;
+}
+
+/**
+ * Replaces all constants by their inlined values.
+ */
+function main() {
+    return through.obj(function (file, enc, cb) {
+            if (file.isNull()) {
+                cb(null, file);
+                return;
+            }
+            if (file.isStream()) {
+                cb(new PluginError("Process Constants", "Streaming not supported."));
+            }
+
+            let data = file.contents.toString();
+            data = processConstants(data);
+
+            // Go to disk.
+            fs.writeFileSync(file.path, data);
+
+            return cb();
+        });
+}
+
+module.exports = main;

+ 1 - 1
Tools/Gulp/helpers/gulp-processImportsToEs6.js

@@ -26,7 +26,7 @@ function main(replacements) {
                 return;
             }
             if (file.isStream()) {
-                cb(new PluginError("Process Shader", "Streaming not supported."));
+                cb(new PluginError("Process Imports", "Streaming not supported."));
             }
 
             let data = file.contents.toString();

+ 18 - 3
Tools/Gulp/tasks/gulpTasks-librariesES6.js

@@ -8,6 +8,7 @@ var concat = require('gulp-concat');
 // Gulp Helpers
 var rmDir = require("../../NodeHelpers/rmDir");
 var processImports = require("../helpers/gulp-processImportsToEs6");
+var processConstants = require("../helpers/gulp-processConstants");
 var processLooseDeclarations = require("../helpers/gulp-processLooseDeclarationsEs6");
 var uncommentShaders = require('../helpers/gulp-removeShaderComments');
 var processShaders = require("../helpers/gulp-processShaders");
@@ -55,6 +56,7 @@ var source = function(settings) {
 var dep = function(settings) {
     const copyPaths = []
     // Add tsconfig rules.
+    copyPaths.push(path.join(config.computed.rootFolder, "/dist/preview release/babylon.max.js"));
     copyPaths.push(path.join(config.computed.rootFolder, "tsconfigRules.json"));
 
     const tsconfig = require(settings.computed.tsConfigPath);
@@ -82,7 +84,7 @@ var dep = function(settings) {
 /**
  * Adapt Sources import paths.
  */
-var modifySources = function(settings) {
+var modifySourcesImports = function(settings) {
     const tsconfig = require(settings.computed.tsConfigPath);
 
     var replacements = [];
@@ -113,6 +115,18 @@ var modifySources = function(settings) {
 }
 
 /**
+ * Inline Constants in sources.
+ */
+var modifySourcesConstants = function(settings) {
+    if (settings.isCore) {
+        return gulp.src([settings.computed.sourceES6Directory + "/**/*.ts", 
+            settings.computed.sourceES6Directory + "/**/*.tsx"])
+            .pipe(processConstants());
+    }
+    return Promise.resolve();
+}
+
+/**
  * Adapt TS Config Paths.
  */
 var modifyTsConfig = function(settings, cb) {
@@ -261,7 +275,8 @@ function buildES6Library(settings, module) {
     }
     var copySource = function() { return source(settings); };
     var dependencies = function() { return dep(settings); };
-    var adaptSourceImportPaths = function() { return modifySources(settings); };
+    var adaptSourceImportPaths = function() { return modifySourcesImports(settings); };
+    var adaptSourceConstants = function() { return modifySourcesConstants(settings); };
     var adaptTsConfigImportPaths = function(cb) { return modifyTsConfig(settings, cb); };
 
     // Build with ts or webpack
@@ -280,7 +295,7 @@ function buildES6Library(settings, module) {
         ];
     }
 
-    tasks.push(...cleanAndShaderTasks, copySource, dependencies, adaptSourceImportPaths, adaptTsConfigImportPaths, ...buildSteps);
+    tasks.push(...cleanAndShaderTasks, copySource, dependencies, adaptSourceImportPaths, adaptSourceConstants, adaptTsConfigImportPaths, ...buildSteps);
 
     return gulp.series.apply(this, tasks);
 }

+ 0 - 4
src/Engines/constants.ts

@@ -412,10 +412,6 @@ export class Constants {
      * Special billboard mode where the particle will be biilboard to the camera but rotated to align with direction
      */
     public static readonly PARTICLES_BILLBOARDMODE_STRETCHED = 8;
-    /**
-     * Gets or sets base Assets URL
-     */
-    public static PARTICLES_BaseAssetsUrl = "https://assets.babylonjs.com/particles";
 
     /** Default culling strategy : this is an exclusion test and it's the more accurate.
      *  Test order :

+ 1 - 2
src/Particles/particleHelper.ts

@@ -10,7 +10,6 @@ import { IParticleSystem } from "./IParticleSystem";
 import { GPUParticleSystem } from "./gpuParticleSystem";
 import { ParticleSystemSet } from "./particleSystemSet";
 import { ParticleSystem } from "./particleSystem";
-import { Constants } from "../Engines/constants";
 /**
  * This class is made for on one-liner static method to help creating particle system set.
  */
@@ -18,7 +17,7 @@ export class ParticleHelper {
     /**
      * Gets or sets base Assets URL
      */
-    public static BaseAssetsUrl = Constants.PARTICLES_BaseAssetsUrl;
+    public static BaseAssetsUrl = ParticleSystemSet.BaseAssetsUrl;
 
     /**
      * Create a default particle system that you can tweak

+ 6 - 2
src/Particles/particleSystemSet.ts

@@ -9,7 +9,6 @@ import { EngineStore } from "../Engines/engineStore";
 import { ParticleSystem } from "../Particles/particleSystem";
 import { Scene, IDisposable } from "../scene";
 import { StandardMaterial } from "../Materials/standardMaterial";
-import { Constants } from "../Engines/constants";
 
 /** Internal class used to store shapes for emitters */
 class ParticleSystemSetEmitterCreationOptions {
@@ -22,6 +21,11 @@ class ParticleSystemSetEmitterCreationOptions {
  * Represents a set of particle systems working together to create a specific effect
  */
 export class ParticleSystemSet implements IDisposable {
+    /**
+     * Gets or sets base Assets URL
+     */
+    public static BaseAssetsUrl = "https://assets.babylonjs.com/particles";
+
     private _emitterCreationOptions: ParticleSystemSetEmitterCreationOptions;
     private _emitterNode: Nullable<TransformNode>;
 
@@ -125,7 +129,7 @@ export class ParticleSystemSet implements IDisposable {
      */
     public static Parse(data: any, scene: Scene, gpu = false): ParticleSystemSet {
         var result = new ParticleSystemSet();
-        var rootUrl = Constants.PARTICLES_BaseAssetsUrl + "/textures/";
+        var rootUrl = this.BaseAssetsUrl + "/textures/";
 
         scene = scene || EngineStore.LastCreatedScene;