From a0d23354802ebc6b437698acb4b18d3395d47cd1 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Fri, 16 Mar 2012 12:26:30 -0700 Subject: Conversion to JSON based file IO for canvas2D and WebGL rendering --- js/document/html-document.js | 43 +++-- js/lib/drawing/world.js | 128 +++++++++++++++ js/lib/geom/circle.js | 72 ++++++++- js/lib/geom/geom-obj.js | 79 +++++++++ js/lib/geom/line.js | 53 ++++++- js/lib/geom/rectangle.js | 87 ++++++++-- js/lib/rdge/materials/bump-metal-material.js | 44 +++++ js/lib/rdge/materials/flat-material.js | 21 +++ js/lib/rdge/materials/linear-gradient-material.js | 53 +++++++ js/lib/rdge/materials/pulse-material.js | 32 ++++ js/lib/rdge/materials/radial-blur-material.js | 36 +++++ js/lib/rdge/materials/radial-gradient-material.js | 54 ++++++- js/lib/rdge/materials/taper-material.js | 27 ++++ js/lib/rdge/materials/twist-vert-material.js | 28 ++++ js/lib/rdge/materials/uber-material.js | 185 +++++++++++++++++++++- js/models/materials-model.js | 32 +++- 16 files changed, 923 insertions(+), 51 deletions(-) (limited to 'js') diff --git a/js/document/html-document.js b/js/document/html-document.js index d4db6e2f..bf03e38b 100755 --- a/js/document/html-document.js +++ b/js/document/html-document.js @@ -194,11 +194,7 @@ exports.HTMLDocument = Montage.create(TextDocument, { // if (elt) { this._glData = []; - //if (path) { - //this.collectGLData(elt, this._glData, path); - //} else { - this.collectGLData(elt, this._glData ); - //} + this.collectGLData(elt, this._glData ); } else { this._glData = null } @@ -220,25 +216,26 @@ exports.HTMLDocument = Montage.create(TextDocument, { // /* var importStr = value[i]; - var startIndex = importStr.indexOf( "id: " ); - if (startIndex >= 0) { - var endIndex = importStr.indexOf( "\n", startIndex ); - if (endIndex > 0) { - var id = importStr.substring( startIndex+4, endIndex ); - if (id) { - var canvas = this.findCanvasWithID( id, elt ); - if (canvas) { - if (!canvas.elementModel) { - NJUtils.makeElementModel(canvas, "Canvas", "shape", true); + var jObj = JSON.parse( importStr ); + if (jObj) + { + var id = jObj.id; + if (id) + { + var canvas = this.findCanvasWithID( id, elt ); + if (canvas) { + if (!canvas.elementModel) { + NJUtils.makeElementModel(canvas, "Canvas", "shape", true); + } + if (canvas.elementModel) { + if (canvas.elementModel.shapeModel.GLWorld) { + canvas.elementModel.shapeModel.GLWorld.clearTree(); } - if (canvas.elementModel) { - if (canvas.elementModel.shapeModel.GLWorld) { - canvas.elementModel.shapeModel.GLWorld.clearTree(); - } - var index = importStr.indexOf( "webGL: " ); - var useWebGL = (index >= 0) + if (jObj) + { + var useWebGL = jObj.webGL; var world = new GLWorld( canvas, useWebGL ); - world.import( importStr ); + world.importJSON( jObj ); this.buildShapeModel( canvas.elementModel, world ); } } @@ -382,7 +379,7 @@ exports.HTMLDocument = Montage.create(TextDocument, { { if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) { - var data = elt.elementModel.shapeModel.GLWorld.export(); + var data = elt.elementModel.shapeModel.GLWorld.exportJSON(); dataArray.push( data ); } diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js index 44c9e37d..9e502c3e 100755 --- a/js/lib/drawing/world.js +++ b/js/lib/drawing/world.js @@ -727,6 +727,57 @@ World.prototype.getShapeFromPoint = function( offsetX, offsetY ) { } }; +World.prototype.exportJSON = function() +{ + // world properties + var worldObj = + { + 'version' : 1.1, + 'id' : this.getCanvas().getAttribute( "data-RDGE-id" ), + 'fov' : this._fov, + 'zNear' : this._zNear, + 'zFar' : this._zFar, + 'viewDist' : this._viewDist, + 'webGL' : this._useWebGL + }; + + // RDGE scenegraph + if (this._useWebGL) + worldObj.scenedata = this.myScene.exportJSON(); + + // object data + var strArray = []; + this.exportObjectsJSON( this._geomRoot, worldObj ); + + // convert the object to a string + var jStr = JSON.stringify( worldObj ); + + // the RDGE export function corrupts the data. + // rebuild the tree + var root = this._rootNode; + root.children = new Array(); + if (worldObj.children && (worldObj.children.length === 1)) + this.importObjectsJSON( worldObj.children[0] ); + + return jStr; +} + +World.prototype.exportObjectsJSON = function( obj, parentObj ) +{ + if (!obj) return; + + var jObj = obj.exportJSON(); + if (!parentObj.children) parentObj.children = []; + parentObj.children.push( jObj ); + + if (obj.getChild()) { + this.exportObjects( obj.getChild (), jObj ); + } + + if (obj.getNext()) + this.exportObjects( obj.getNext(), parentObj ); +} + World.prototype.export = function() { var exportStr = "GLWorld 1.0\n"; @@ -808,6 +859,83 @@ World.prototype.findTransformNodeByMaterial = function( materialNode, trNode ) return rtnNode; }; +World.prototype.importJSON = function( jObj ) +{ + if (jObj.webGL) + { + // start RDGE + rdgeStarted = true; + var id = this._canvas.getAttribute( "data-RDGE-id" ); + this._canvas.rdgeid = id; + g_Engine.registerCanvas(this._canvas, this); + RDGEStart( this._canvas ); + this._canvas.task.stop() + } + + // import the objects + // there should be exactly one child of the parent object + if (jObj.children && (jObj.children.length === 1)) + this.importObjectsJSON( jObj.children[0] ); + else + throw new Error ("unrecoverable canvas import error - inconsistent root object: " + jObj.children ); + + if (!this._useWebGL) + { + // render using canvas 2D + this.render(); + } +} + +World.prototype.importObjectsJSON = function( jObj, parentGeomObj ) +{ + // read the next object + var gObj = this.importObjectJSON( jObj, parentGeomObj ); + + // determine if we have children + if (jObj.children) + { + var nKids = ojObjbj.chilodren.length; + for (var i=0; i */ var MaterialsModel = require("js/models/materials-model").MaterialsModel; +/* var FlatMaterial = require("js/lib/rdge/materials/flat-material").FlatMaterial; var LinearGradientMaterial = require("js/lib/rdge/materials/linear-gradient-material").LinearGradientMaterial; var RadialGradientMaterial = require("js/lib/rdge/materials/radial-gradient-material").RadialGradientMaterial; @@ -25,6 +26,7 @@ var TwistMaterial = require("js/lib/rdge/materials/twist-material").TwistMateria var JuliaMaterial = require("js/lib/rdge/materials/julia-material").JuliaMaterial; var KeleidoscopeMaterial = require("js/lib/rdge/materials/keleidoscope-material").KeleidoscopeMaterial; var MandelMaterial = require("js/lib/rdge/materials/mandel-material").MandelMaterial; +*/ /////////////////////////////////////////////////////////////////////// // Class GLGeomObj @@ -225,6 +227,83 @@ var GeomObj = function GLGeomObj() { return fillMaterial; }; + this.exportMaterialsJSON = function() + { + var jObj; + if (this._materialArray && this._materialNodeArray) + { + var nMats = this._materialArray.length; + if (nMats > 0) + { + var arr = []; + + for (var i=0; i 0) + { + this._ubershaderCaps.lighting = + { + 'light0' : this._lights[0], + 'light1' : this._lights[1], + 'light2' : this._lights[2], + 'light3' : this._lights[3] + } + } + } + + var diffuseMap = jObj['diffuseMap']; + if(diffuseMap) + this.setProperty( "diffuseMap", diffuseMap ); + + var normalMap = jObj['normalMap']; + if(normalMap) + this.setProperty( "normalMap", normalMap ); + + var specularMap = jObj['specularMap']; + if(specularMap) + this.setProperty( "specularMap", specularMap ); + + var environmentMap = jObj['environmentMap']; + if(environmentMap) + { + this.setProperty( "environmentMap", environmentMap ); + this.setProperty( "environmentAmount", jObj['environmentAmount'] ); + } + + this.rebuildShader(); + } + + this.exportJSON = function() + { + // we will be needing the world. Make sure it is there + var world = this.getWorld(); + if (!world) + throw new Error( "no world in material.export, " + this.getName() ); + + // every material needs the base type and instance name + var caps = this._ubershaderCaps; + var jObj = + { + 'material' : this.getShaderName(), + 'name' : this.getName() + }; + + // export the material properties + if (typeof caps.material != 'undefined') + { + jObj.materialProps = + { + 'ambientColor' : this._ambientColor, + 'diffuseColor' : this._diffuseColor, + 'specularColor' : this._specularColor, + 'specularPower' : this._specularPower + }; + + } + + if (typeof caps.lighting != 'undefined') + { + var lightArray = []; + for (var i=0; i 0) + jObj.lights = lightArray; + } + + if(typeof caps.diffuseMap != 'undefined') + jObj['diffuseMap'] = caps.diffuseMap.texture; + + if(typeof caps.normalMap != 'undefined') + jObj['normalMap'] = caps.normalMap.texture; + + if(typeof caps.specularMap != 'undefined') + jObj['specularMap'] = caps.specularMap.texture; + + if(typeof caps.environmentMap != 'undefined') + { + jObj['environmentMap'] = caps.environmentMap.texture; + jObj['environmentAmount'] = caps.environmentMap.envReflection; + } + + return jObj; + } + + this.export = function() { // every material needs the base type and instance name @@ -539,10 +720,6 @@ var UberMaterial = function UberMaterial() { } } -// this._diffuseMapOb = { 'texture' : 'assets/images/rocky-diffuse.jpg', 'wrap' : 'REPEAT' }; -// this._normalMapOb = { 'texture' : 'assets/images/rocky-normal.jpg', 'wrap' : 'REPEAT' }; -// this._specularMapOb = { 'texture' : 'assets/images/rocky-spec.jpg', 'wrap' : 'REPEAT' }; -// this._environmentMapOb = { 'texture' : 'assets/images/silver.png', 'wrap' : 'CLAMP', 'envReflection' : this._environmentAmount }; var world = this.getWorld(); if (!world) throw new Error( "no world in material.export, " + this.getName() ); diff --git a/js/models/materials-model.js b/js/models/materials-model.js index d8fb4016..a45b0c72 100755 --- a/js/models/materials-model.js +++ b/js/models/materials-model.js @@ -115,7 +115,31 @@ exports.MaterialsModel = Montage.create(Component, { return this._materials[index]; } } - }, + }, + + getMaterialByShader: + { + value: function( shaderName ) + { + var index = this.getIndexOfMaterialByShader( shaderName ); + if (index >= 0) + return this._materials[index]; + } + }, + + getIndexOfMaterialByShader: { + value: function (shaderName) { + var len = this._materials.length; + for(var i=0; i