summaryrefslogtreecommitdiffstats
path: root/o3d/samples/o3djs
diff options
context:
space:
mode:
Diffstat (limited to 'o3d/samples/o3djs')
-rw-r--r--o3d/samples/o3djs/effect.js83
-rw-r--r--o3d/samples/o3djs/material.js41
-rw-r--r--o3d/samples/o3djs/math.js37
3 files changed, 161 insertions, 0 deletions
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.