Created
October 9, 2018 18:47
-
-
Save prestomation/393fc19771e4452e0d0545a0f1268d27 to your computer and use it in GitHub Desktop.
Randomly generate a height map, create a Surface from the heightmap, and add it to the world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Paste this script into a script component to use | |
const start = Date.now(); | |
const nLin = 100; | |
const nCol = 30; | |
function getMatrix() { | |
const matrix = []; | |
const m = Math.cos((Date.now() - start) / 1000 * 0.05); | |
const a = m * 0.2; | |
const b = m * 0.2; | |
// var c = m * 0.4; | |
for (let i = 0; i < nLin; i++) { | |
const row = []; | |
if (reverse) { | |
matrix.unshift(row); | |
} else { | |
matrix.push(row); | |
} | |
for (let j = 0; j < nCol; j++) { | |
const value = Math.sin(i * a) * Math.sin(j * b) + 1; | |
row.push(value); | |
} | |
} | |
return matrix; | |
} | |
function setup(args, ctx) { | |
// This matrix could come from a web call. | |
const matrix = getMatrix(); | |
const meshData = sumerian.Surface.createFromHeightMap(matrix, 1, 1, 1); | |
const entity = ctx.world.createEntity(meshData, new sumerian.Material(sumerian.ShaderLib.textured)).addToWorld(); | |
// We need to keep track of our entity... | |
ctx.entityData.newEntity = entity; | |
} | |
function cleanup(args, ctx) { | |
//..so that we clean it up at the end | |
ctx.entityData.newEntity.removeFromWorld(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment