From 6d59518149cd5b2ce4bdd83f30a5f5068be14157 Mon Sep 17 00:00:00 2001 From: Eric Levin Date: Mon, 8 Jun 2015 14:02:38 -0700 Subject: [PATCH] added meditative ocean landscape and common utilities file --- examples/example/dynamicLandscape.js | 89 ++++++++++++++++++++++++++++ examples/utilities.js | 56 +++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 examples/example/dynamicLandscape.js create mode 100644 examples/utilities.js diff --git a/examples/example/dynamicLandscape.js b/examples/example/dynamicLandscape.js new file mode 100644 index 0000000000..ad247963fd --- /dev/null +++ b/examples/example/dynamicLandscape.js @@ -0,0 +1,89 @@ + +// dynamicLandscape.js +// examples +// +// Created by Eric Levin on June 8 +// Copyright 2015 High Fidelity, Inc. +// +// Meditative ocean landscape +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html + +HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; +Script.include(HIFI_PUBLIC_BUCKET + 'scripts/utilities.js') + +var NUM_ROWS = 10; +var CUBE_SIZE = 1; +var cubes = []; +var cubesSettings = []; +var time = 0; + +var OMEGA = 2.0 * Math.PI/8; +var RANGE = CUBE_SIZE/2; + +var center = Vec3.sum(MyAvatar.position, Vec3.multiply(CUBE_SIZE* 10, Quat.getFront(Camera.getOrientation()))); + + +for (var x = 0, rowIndex = 0; x < NUM_ROWS * CUBE_SIZE; x += CUBE_SIZE, rowIndex++) { + for (var z = 0, columnIndex = 0; z < NUM_ROWS * CUBE_SIZE; z += CUBE_SIZE, columnIndex++) { + + var baseHeight = map( columnIndex + 1, 1, NUM_ROWS, -CUBE_SIZE * 2, -CUBE_SIZE); + var relativePosition = { + x: x, + y: baseHeight, + z: z + }; + var position = Vec3.sum(center, relativePosition); + cubes.push(Entities.addEntity({ + type: 'Box', + position: position, + dimensions: { + x: CUBE_SIZE, + y: CUBE_SIZE, + z: CUBE_SIZE + } + })); + + var phase = map( (columnIndex + 1) * (rowIndex + 1), 2, NUM_ROWS * NUM_ROWS, Math.PI * 2, Math.PI * 4); + cubesSettings.push({ + baseHeight: center.y + baseHeight, + phase: phase + }) + } +} + +function update(deleteTime) { + time += deleteTime; + for (var i = 0; i < cubes.length; i++) { + var phase = cubesSettings[i].phase; + var props = Entities.getEntityProperties(cubes[i]); + var newHeight = Math.sin(time * OMEGA + phase) / 2.0; + var hue = map(newHeight, -.5, .5, 0.5, 0.7); + var light = map(newHeight, -.5, .5, 0.4, 0.6) + newHeight = cubesSettings[i].baseHeight + (newHeight * RANGE); + var newVelocityY = Math.cos(time * OMEGA + phase) / 2.0 * RANGE * OMEGA; + + var newPosition = props.position; + var newVelocity = props.velocity; + + newPosition.y = newHeight; + newVelocity = newVelocityY; + Entities.editEntity( cubes[i], { + position: newPosition, + velocity: props.velocity, + color: hslToRgb({hue: hue, sat: 0.7, light: light}) + }); + } +} + +function cleanup() { + cubes.forEach(function(cube) { + Entities.deleteEntity(cube); + }) +} + +Script.update.connect(update); +Script.scriptEnding.connect(cleanup) + + diff --git a/examples/utilities.js b/examples/utilities.js new file mode 100644 index 0000000000..3844e23e14 --- /dev/null +++ b/examples/utilities.js @@ -0,0 +1,56 @@ +// utilities.js +// examples +// +// Created by Eric Levin on June 8 +// Copyright 2015 High Fidelity, Inc. +// +// Common utilities +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html + +function hslToRgb(hslColor) { + var h = hslColor.hue; + var s = hslColor.sat; + var l = hslColor.light; + var r, g, b; + + if (s == 0) { + r = g = b = l; // achromatic + } else { + var hue2rgb = function hue2rgb(p, q, t) { + if (t < 0) t += 1; + if (t > 1) t -= 1; + if (t < 1 / 6) return p + (q - p) * 6 * t; + if (t < 1 / 2) return q; + if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; + return p; + } + + var q = l < 0.5 ? l * (1 + s) : l + s - l * s; + var p = 2 * l - q; + r = hue2rgb(p, q, h + 1 / 3); + g = hue2rgb(p, q, h); + b = hue2rgb(p, q, h - 1 / 3); + } + + return { + red: Math.round(r * 255), + green: Math.round(g * 255), + blue: Math.round(b * 255) + }; + +} + +function map(value, min1, max1, min2, max2) { + return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); +} + +function randFloat(low, high) { + return low + Math.random() * (high - low); +} + + +function randInt(low, high) { + return Math.floor(randFloat(low, high)); +} \ No newline at end of file