diff options
Diffstat (limited to 'o3d/samples/o3djs')
-rw-r--r-- | o3d/samples/o3djs/manipulators.js | 32 | ||||
-rw-r--r-- | o3d/samples/o3djs/material.js | 68 | ||||
-rw-r--r-- | o3d/samples/o3djs/rendergraph.js | 24 |
3 files changed, 83 insertions, 41 deletions
diff --git a/o3d/samples/o3djs/manipulators.js b/o3d/samples/o3djs/manipulators.js index 74cc79e..7bea7a5 100644 --- a/o3d/samples/o3djs/manipulators.js +++ b/o3d/samples/o3djs/manipulators.js @@ -495,14 +495,14 @@ o3djs.manipulators.Manager = function(pack, * @type {!o3d.Material} */ this.defaultMaterial = - this.createPhongMaterial_(o3djs.manipulators.DEFAULT_COLOR); + this.createMaterial_(o3djs.manipulators.DEFAULT_COLOR); /** * The material used for manipulators when they are highlighted. * (TODO(simonrad): This is not currently used; only defaultMaterial is used. Remove this?) * @type {!o3d.Material} */ this.highlightedMaterial = - this.createPhongMaterial_(o3djs.manipulators.HIGHLIGHTED_COLOR); + this.createMaterial_(o3djs.manipulators.HIGHLIGHTED_COLOR); /** * A map from the manip's parent Transform clientId to the manip. @@ -532,31 +532,17 @@ o3djs.manipulators.Manager = function(pack, } /** - * Creates a phong material based on the given single color. + * Creates a material based on the given single color. * @private * @param {!o3djs.math.Vector4} baseColor A vector with 4 entries, the * R,G,B, and A components of a color. * @return {!o3d.Material} A phong material whose overall pigment is baseColor. */ -o3djs.manipulators.Manager.prototype.createPhongMaterial_ = +o3djs.manipulators.Manager.prototype.createMaterial_ = function(baseColor) { // Create a new, empty Material object. - var material = this.pack.createObject('Material'); - - o3djs.effect.attachStandardShader( - this.pack, material, this.lightPosition, 'phong'); - - material.drawList = this.drawList; - - // Assign parameters to the phong material. - material.getParam('emissive').value = [0, 0, 0, 1]; - material.getParam('ambient').value = - o3djs.math.mulScalarVector(0.1, baseColor); - material.getParam('diffuse').value = baseColor; - material.getParam('specular').value = [.2, .2, .2, 1]; - material.getParam('shininess').value = 20; - - return material; + return o3djs.material.createConstantMaterialEx( + this.pack, this.drawList, baseColor); } /** @@ -1182,7 +1168,7 @@ o3djs.manipulators.Translate1 = function(manager) { * @private * @type {!o3d.ParamFloat4} */ - this.colorParam_ = this.getTransform().createParam('diffuse', 'ParamFloat4'); + this.colorParam_ = this.getTransform().createParam('emissive', 'ParamFloat4'); this.clearHighlight(); /** @@ -1293,7 +1279,7 @@ o3djs.manipulators.Translate2 = function(manager) { * @private * @type {!o3d.ParamFloat4} */ - this.colorParam_ = this.getTransform().createParam('diffuse', 'ParamFloat4'); + this.colorParam_ = this.getTransform().createParam('emissive', 'ParamFloat4'); this.clearHighlight(); /** @@ -1406,7 +1392,7 @@ o3djs.manipulators.Rotate1 = function(manager) { * @private * @type {!o3d.ParamFloat4} */ - this.colorParam_ = this.getTransform().createParam('diffuse', 'ParamFloat4'); + this.colorParam_ = this.getTransform().createParam('emissive', 'ParamFloat4'); this.clearHighlight(); /** diff --git a/o3d/samples/o3djs/material.js b/o3d/samples/o3djs/material.js index a2aa67c..7ba3c8e 100644 --- a/o3d/samples/o3djs/material.js +++ b/o3d/samples/o3djs/material.js @@ -200,6 +200,30 @@ o3djs.material.prepareMaterials = function(pack, }; /** + * Builds a standard effect for a given material. + * If the material already has an effect, none is created. + * @param {!o3d.Pack} pack Pack to manage created objects. + * @param {!o3d.Material} material The material for which to create an + * effect. + * @param {string} effectType Type of effect to create ('phong', 'lambert', + * 'constant'). + * + * @see o3djs.effect.attachStandardShader + */ +o3djs.material.attachStandardEffectEx = function(pack, + material, + effectType) { + if (!material.effect) { + if (!o3djs.effect.attachStandardShader(pack, + material, + [0, 0, 0], + effectType)) { + throw 'Could not attach a standard effect'; + } + } +}; + +/** * Builds a standard effect for a given material. The position of the * default light is set to the view position. If the material already has * an effect, none is created. @@ -223,14 +247,13 @@ o3djs.material.attachStandardEffect = function(pack, o3djs.math.inverse(viewInfo.drawContext.view)); if (!o3djs.effect.attachStandardShader(pack, material, - lightPos, + lightPos, // TODO(gman): remove this effectType)) { throw 'Could not attach a standard effect'; } } }; - /** * Prepares all the materials in the given pack by setting their * drawList. @@ -341,21 +364,16 @@ o3djs.material.createBasicMaterial = function(pack, * useful for debugging shapes and 2d UI elements. * * @param {!o3d.Pack} pack Pack to manage created objects. - * @param {!o3djs.rendergraph.ViewInfo} viewInfo as returned from - * o3djs.rendergraph.createBasicView. + * @param {!o3d.DrawList} drawList The DrawList for the material. * @param {(!o3djs.math.Vector4|!o3d.Texture)} colorOrTexture Either a color in * the format [r, g, b, a] or an O3D texture. - * @param {boolean} opt_transparent Whether or not the material is transparent. - * Defaults to false. * @return {!o3d.Material} The created material. */ -o3djs.material.createConstantMaterial = function(pack, - viewInfo, - colorOrTexture, - opt_transparent) { +o3djs.material.createConstantMaterialEx = function(pack, + drawList, + colorOrTexture) { var material = pack.createObject('Material'); - material.drawList = opt_transparent ? viewInfo.zOrderedDrawList : - viewInfo.performanceDrawList; + material.drawList = drawList; // If it has a length assume it's a color, otherwise assume it's a texture. if (colorOrTexture.length) { @@ -367,12 +385,36 @@ o3djs.material.createConstantMaterial = function(pack, sampler.texture = colorOrTexture; } - o3djs.material.attachStandardEffect(pack, material, viewInfo, 'constant'); + o3djs.material.attachStandardEffectEx(pack, material, 'constant'); return material; }; /** + * This function creates a constant material. No lighting. It is especially + * useful for debugging shapes and 2d UI elements. + * + * @param {!o3d.Pack} pack Pack to manage created objects. + * @param {!o3djs.rendergraph.ViewInfo} viewInfo as returned from + * o3djs.rendergraph.createBasicView. + * @param {(!o3djs.math.Vector4|!o3d.Texture)} colorOrTexture Either a color in + * the format [r, g, b, a] or an O3D texture. + * @param {boolean} opt_transparent Whether or not the material is transparent. + * Defaults to false. + * @return {!o3d.Material} The created material. + */ +o3djs.material.createConstantMaterial = function(pack, + viewInfo, + colorOrTexture, + opt_transparent) { + return o3djs.material.createConstantMaterialEx( + pack, + opt_transparent ? viewInfo.zOrderedDrawList : + viewInfo.performanceDrawList, + colorOrTexture) +}; + +/** * This function creates 2 color procedureal texture material. * * @see o3djs.material.createBasicMaterial diff --git a/o3d/samples/o3djs/rendergraph.js b/o3d/samples/o3djs/rendergraph.js index 5587623..2457004 100644 --- a/o3d/samples/o3djs/rendergraph.js +++ b/o3d/samples/o3djs/rendergraph.js @@ -61,6 +61,8 @@ o3djs.rendergraph = o3djs.rendergraph || {}; * use for performanceDrawPass. * @param {!o3d.DrawList} opt_zOrderedDrawList Optional DrawList to * use for zOrderedDrawPass. + * @param {!o3d.DrawContext} opt_drawContext Optional DrawContext to + * use. If not passed in one is created. * @return {!o3djs.rendergraph.ViewInfo} A ViewInfo object with info about * everything created. */ @@ -71,7 +73,8 @@ o3djs.rendergraph.createView = function(pack, opt_priority, opt_viewport, opt_performanceDrawList, - opt_zOrderedDrawList) { + opt_zOrderedDrawList, + opt_drawContext) { return new o3djs.rendergraph.ViewInfo(pack, treeRoot, opt_parent, @@ -79,7 +82,8 @@ o3djs.rendergraph.createView = function(pack, opt_priority, opt_viewport, opt_performanceDrawList, - opt_zOrderedDrawList); + opt_zOrderedDrawList, + opt_drawContext); }; /** @@ -164,6 +168,8 @@ o3djs.rendergraph.createExtraView = function(viewInfo, * performanceDrawPass. * @param {!o3d.DrawList} opt_zOrderedDrawList DrawList to use for * zOrderedDrawPass. + * @param {!o3d.DrawContext} opt_drawContext Optional DrawContext to + * use. If not passed in one is created. */ o3djs.rendergraph.ViewInfo = function(pack, treeRoot, @@ -172,7 +178,8 @@ o3djs.rendergraph.ViewInfo = function(pack, opt_priority, opt_viewport, opt_performanceDrawList, - opt_zOrderedDrawList) { + opt_zOrderedDrawList, + opt_drawContext) { var that = this; var clearColor = opt_clearColor || [0.5, 0.5, 0.5, 1.0]; var viewPriority = opt_priority || 0; @@ -240,7 +247,7 @@ o3djs.rendergraph.ViewInfo = function(pack, this.clearBuffer = clearBuffer; // Create DrawContext. - var drawContext = pack.createObject('DrawContext'); + var drawContext = opt_drawContext || pack.createObject('DrawContext'); /** * The DrawContext used by this ViewInfo. @@ -371,6 +378,13 @@ o3djs.rendergraph.ViewInfo = function(pack, * @type {!o3d.DrawPass} */ this.zOrderedDrawPass = zOrderedDrawPassInfo.drawPass; + + /** + * A flag whether or not we created the DrawContext for this DrawPassInfo. + * @private + * @type {boolean} + */ + this.ownDrawContext_ = opt_drawContext ? false : true; }; /** @@ -395,7 +409,7 @@ o3djs.rendergraph.ViewInfo.prototype.destroy = function( // Remove everything we created from the pack. this.pack.removeObject(this.viewport); this.pack.removeObject(this.clearBuffer); - if (opt_destroyDrawContext) { + if (opt_destroyDrawContext && this.ownDrawContext_) { this.pack.removeObject(this.drawContext); } this.pack.removeObject(this.treeTraversal); |