diff options
author | gman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-13 19:33:26 +0000 |
---|---|---|
committer | gman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-13 19:33:26 +0000 |
commit | 9bdbe9f7444baa8e26034f4fcd0f8138e50125ec (patch) | |
tree | a1a9817b509ca31f1619eced2bcb4555a220c128 /o3d | |
parent | e6e47fcb0b73a2f44f4ce7b4dfab83032458199d (diff) | |
download | chromium_src-9bdbe9f7444baa8e26034f4fcd0f8138e50125ec.zip chromium_src-9bdbe9f7444baa8e26034f4fcd0f8138e50125ec.tar.gz chromium_src-9bdbe9f7444baa8e26034f4fcd0f8138e50125ec.tar.bz2 |
Added o3djs.material.createCheckerMaterial.
It seems pretty useful for lots of samples.
Added o3djs.effect.createCheckerEffect
to support that.
Also put pseudoRandom into math since so many
samples use it. Will consider switching samples
to use it.
Review URL: http://codereview.chromium.org/149509
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20510 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d')
-rw-r--r-- | o3d/samples/billboards.html | 21 | ||||
-rw-r--r-- | o3d/samples/o3djs/effect.js | 83 | ||||
-rw-r--r-- | o3d/samples/o3djs/material.js | 41 | ||||
-rw-r--r-- | o3d/samples/o3djs/math.js | 37 | ||||
-rw-r--r-- | o3d/samples/shaders/green-blue-checker.shader | 31 | ||||
-rw-r--r-- | o3d/tests/selenium/tests/math-test.html | 19 |
6 files changed, 215 insertions, 17 deletions
diff --git a/o3d/samples/billboards.html b/o3d/samples/billboards.html index f6ce244..38b00dd 100644 --- a/o3d/samples/billboards.html +++ b/o3d/samples/billboards.html @@ -57,7 +57,7 @@ o3djs.require('o3djs.loader'); // init() once the page has finished loading. // unload() when the page is unloaded. window.onload = init; -window.onunload= unload; +window.onunload = unload; // global variables var g_o3dElement; @@ -78,16 +78,6 @@ var g_randSeed = 0; var g_textures = []; /** - * Returns a deterministic pseudorandom number bewteen 0 and 1 - * @return {number} a random number between 0 and 1 - */ -function pseudoRandom() { - var range = Math.pow(2, 32); - - return (g_randSeed = (134775813 * g_randSeed + 1) % range) / range; -} - -/** * Creates the client area. */ function init() { @@ -147,9 +137,8 @@ function initStep3() { g_viewInfo, [0.2, 1, 0.2, 1]); // green - var checkerMaterial = o3djs.material.createMaterialFromFile( - g_pack, 'shaders/green-blue-checker.shader', - g_viewInfo.performanceDrawList); + var checkerMaterial = o3djs.material.createCheckerMaterial(g_pack, + g_viewInfo); var billboardMaterial = o3djs.material.createMaterialFromFile( g_pack, 'shaders/billboard.shader', g_viewInfo.zOrderedDrawList); @@ -204,9 +193,9 @@ function initStep3() { for (var ii = 0; ii < 30; ++ii) { transform = g_pack.createObject('Transform'); transform.parent = g_root; - transform.translate((pseudoRandom() - 0.5) * 90, + transform.translate((g_math.pseudoRandom() - 0.5) * 90, 0, - (pseudoRandom() - 0.5) * 90); + (g_math.pseudoRandom() - 0.5) * 90); transform.addShape(cylinderShape); var topTransform = g_pack.createObject('Transform'); diff --git a/o3d/samples/o3djs/effect.js b/o3d/samples/o3djs/effect.js index 3031cee..2a12520 100644 --- a/o3d/samples/o3djs/effect.js +++ b/o3d/samples/o3djs/effect.js @@ -50,6 +50,71 @@ o3djs.require('o3djs.io'); o3djs.effect = o3djs.effect || {}; /** + * The name of standard 2 color checker effect. + * @type {string} + */ +o3djs.effect.TWO_COLOR_CHECKER_EFFECT_NAME = + 'o3djs.effect.twoColorCheckerEffect'; + +/** + * An effect string for a 2 color checker effect. + * @type {string} + */ +o3djs.effect.TWO_COLOR_CHECKER_FXSTRING = '' + + 'float4x4 worldViewProjection : WORLDVIEWPROJECTION;\n' + + 'float4x4 worldInverseTranspose : WORLDINVERSETRANSPOSE;\n' + + 'float4x4 world : WORLD;\n' + + 'float4 color1;\n' + + 'float4 color2;\n' + + 'float checkSize;\n' + + 'float3 lightWorldPos;\n' + + 'float3 lightColor;\n' + + '\n' + + 'struct VertexShaderInput {\n' + + ' float4 position : POSITION;\n' + + ' float4 normal : NORMAL;\n' + + ' float2 texcoord : TEXCOORD0;\n' + + '};\n' + + '\n' + + 'struct PixelShaderInput {\n' + + ' float4 position : POSITION;\n' + + ' float2 texcoord : TEXCOORD0;\n' + + ' float3 normal : TEXCOORD1;\n' + + ' float3 worldPosition : TEXCOORD2;\n' + + '};\n' + + '\n' + + 'float4 checker(float2 uv) {\n' + + ' float fmodResult = fmod(floor(checkSize * uv.x) + \n' + + ' floor(checkSize * uv.y), 2.0);\n' + + ' return (fmodResult < 1) ? color1 : color2;\n' + + '}\n' + + '\n' + + 'PixelShaderInput vertexShaderFunction(VertexShaderInput input) {\n' + + ' PixelShaderInput output;\n' + + '\n' + + ' output.position = mul(input.position, worldViewProjection);\n' + + ' output.normal = mul(input.normal, worldInverseTranspose).xyz;\n' + + ' output.worldPosition = mul(input.position, world).xyz;\n' + + ' output.texcoord = input.texcoord;\n' + + ' return output;\n' + + '}\n' + + '\n' + + 'float4 pixelShaderFunction(PixelShaderInput input): COLOR {\n' + + ' float3 surfaceToLight = \n' + + ' normalize(lightWorldPos - input.worldPosition);\n' + + ' float3 worldNormal = normalize(input.normal);\n' + + ' float4 check = checker(input.texcoord);\n' + + ' float4 directionalIntensity = \n' + + ' saturate(dot(worldNormal, surfaceToLight));\n' + + ' float4 outColor = directionalIntensity * check;\n' + + ' return float4(outColor.rgb, check.a);\n' + + '}\n' + + '\n' + + '// #o3d VertexShaderEntryPoint vertexShaderFunction\n' + + '// #o3d PixelShaderEntryPoint pixelShaderFunction\n' + + '// #o3d MatrixLoadOrder RowMajor\n'; + +/** * The name of the parameter on a material if it's a collada standard * material. * @@ -770,3 +835,21 @@ o3djs.effect.createUniformParameters = function(pack, effect, paramObject) { } }; +/** + * Creates an effect that draws a 2 color procedural checker pattern. + * @param {!o3d.Pack} pack The pack to create the effect in. If the pack + * already has an effect with the same name that effect will be returned. + * @return {!o3d.Effect} The effect. + */ +o3djs.effect.createCheckerEffect = function(pack) { + var effects = pack.getObjects(o3djs.effect.TWO_COLOR_CHECKER_EFFECT_NAME, + 'o3d.Effect'); + if (effects.length > 0) { + return effects[0]; + } + + var effect = pack.createObject('Effect'); + effect.loadFromFXString(o3djs.effect.TWO_COLOR_CHECKER_FXSTRING); + effect.name = o3djs.effect.TWO_COLOR_CHECKER_EFFECT_NAME; + return effect; +}; diff --git a/o3d/samples/o3djs/material.js b/o3d/samples/o3djs/material.js index db1449d..8ef180c 100644 --- a/o3d/samples/o3djs/material.js +++ b/o3d/samples/o3djs/material.js @@ -337,6 +337,47 @@ o3djs.material.createBasicMaterial = function(pack, }; /** + * This function creates 2 color procedureal texture material. + * + * @see o3djs.material.createBasicMaterial + * + * @param {!o3d.Pack} pack Pack to manage created objects. + * @param {!o3djs.rendergraph.ViewInfo} viewInfo as returned from + * o3djs.rendergraph.createBasicView. + * @param {!o3djs.math.Vector4} opt_color1 a color in the format [r, g, b, a]. + * Defaults to a medium blue-green. + * @param {!o3djs.math.Vector4} opt_color2 a color in the format [r, g, b, a]. + * Defaults to a light blue-green. + * @param {boolean} opt_transparent Whether or not the material is transparent. + * Defaults to false. + * @param {number} opt_checkSize Defaults to 10 units. + * @return {!o3d.Material} The created material. + */ +o3djs.material.createCheckerMaterial = function(pack, + viewInfo, + opt_color1, + opt_color2, + opt_transparent, + opt_checkSize) { + opt_color1 = opt_color1 || [0.4, 0.5, 0.5, 1]; + opt_color2 = opt_color2 || [0.4, 0.8, 0.8, 1]; + opt_checkSize = opt_checkSize || 10; + + var effect = o3djs.effect.createCheckerEffect(pack); + var material = pack.createObject('Material'); + material.effect = effect; + material.drawList = opt_transparent ? viewInfo.zOrderedDrawList : + viewInfo.performanceDrawList; + o3djs.effect.createUniformParameters(pack, effect, material); + + material.getParam('color1').value = opt_color1; + material.getParam('color2').value = opt_color2; + material.getParam('checkSize').value = opt_checkSize; + + return material; +}; + +/** * Creates a material for an effect loaded from a file. * If the effect has already been loaded in the pack it will be reused. * @param {!o3d.Pack} pack Pack to create effect in. diff --git a/o3d/samples/o3djs/math.js b/o3d/samples/o3djs/math.js index ee0853b..e3d32d4 100644 --- a/o3d/samples/o3djs/math.js +++ b/o3d/samples/o3djs/math.js @@ -108,29 +108,48 @@ o3djs.provide('o3djs.math'); o3djs.math = o3djs.math || {}; /** + * A random seed for the pseudoRandom function. + * @private + * @type {number} + */ +o3djs.math.randomSeed_ = 0; + +/** + * A constant for the pseudoRandom function + * @private + * @type {number} + */ +o3djs.math.RANDOM_RANGE_ = Math.pow(2, 32); + +/** * Functions which deal with 4-by-4 transformation matrices are kept in their * own namespsace. + * @namespace */ o3djs.math.matrix4 = o3djs.math.matrix4 || {}; /** * Functions that are specifically row major are kept in their own namespace. + * @namespace */ o3djs.math.rowMajor = o3djs.math.rowMajor || {}; /** * Functions that are specifically column major are kept in their own namespace. + * @namespace */ o3djs.math.columnMajor = o3djs.math.columnMajor || {}; /** * Functions that do error checking are stored in their own namespace. + * @namespace */ o3djs.math.errorCheck = o3djs.math.errorCheck || {}; /** * Functions that do no error checking and have a separate version that does in * o3djs.math.errorCheck are stored in their own namespace. + * @namespace */ o3djs.math.errorCheckFree = o3djs.math.errorCheckFree || {}; @@ -189,6 +208,24 @@ o3djs.math.Matrix4 = goog.typedef; o3djs.math.Matrix = goog.typedef; /** + * Returns a deterministic pseudorandom number between 0 and 1 + * @return {number} a random number between 0 and 1 + */ +o3djs.math.pseudoRandom = function() { + var math = o3djs.math; + return (math.randomSeed_ = + (134775813 * math.randomSeed_ + 1) % + math.RANDOM_RANGE_) / math.RANDOM_RANGE_; +}; + +/** + * Resets the pseudoRandom function sequence. + */ +o3djs.math.resetPseudoRandom = function() { + o3djs.math.randomSeed_ = 0; +}; + +/** * Converts degrees to radians. * @param {number} degrees A value in degrees. * @return {number} the value in radians. diff --git a/o3d/samples/shaders/green-blue-checker.shader b/o3d/samples/shaders/green-blue-checker.shader index 02b5296..41c2436 100644 --- a/o3d/samples/shaders/green-blue-checker.shader +++ b/o3d/samples/shaders/green-blue-checker.shader @@ -1,4 +1,33 @@ -// @@REWRITE(insert shader-copyright) +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ // The 4x4 world view projection matrix. float4x4 worldViewProjection : WORLDVIEWPROJECTION; float4x4 worldInverseTranspose : WORLDINVERSETRANSPOSE; diff --git a/o3d/tests/selenium/tests/math-test.html b/o3d/tests/selenium/tests/math-test.html index f31fbd6..399e432 100644 --- a/o3d/tests/selenium/tests/math-test.html +++ b/o3d/tests/selenium/tests/math-test.html @@ -56,6 +56,25 @@ var isInternetExplorer = navigator.userAgent.indexOf('Internet Explorer' != -1); var g_suite = {}; +g_suite.testPseudoRandom = function() { + var values = []; + g_math.resetPseudoRandom(); + for (var ii = 0; ii < 100; ++ii) { + value = g_math.pseudoRandom(); + g_test.assertTrue(value >= 0); + g_test.assertTrue(value <= 1); + for (var jj = 0; jj < ii - 1; ++jj) { + g_test.assertTrue(value !== values[jj]); + } + values.push(value); + } + g_math.resetPseudoRandom(); + for (var ii = 0; ii < values.length; ++ii) { + value = g_math.pseudoRandom(); + g_test.assertEquals(value, values[ii]); + } +}; + g_suite.testDegToRad = function() { g_test.assertEquals(Math.PI, g_math.degToRad(180)); }; |