|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>MathBox - Example: Texture map on surface.</title> |
| 6 | + |
| 7 | + <!-- |
| 8 | + This example shows how to apply a texture map to a surface. |
| 9 | + --> |
| 10 | + |
| 11 | + <style type="text/css" media="screen"> |
| 12 | + html, body { height: 100%; } |
| 13 | + body { margin: 0; padding: 0 } |
| 14 | + canvas { display: block } |
| 15 | + </style> |
| 16 | + |
| 17 | + <script type="text/javascript" charset="utf-8" src="../vendor/domready.js"></script> |
| 18 | + <script type="text/javascript" charset="utf-8" src="../vendor/console-extras/dist/console-extras.min.js"></script> |
| 19 | + <script type="text/javascript" charset="utf-8" src="../build/MathBox-bundle.js"></script> |
| 20 | + |
| 21 | + <script type="text/javascript" charset="utf-8"> |
| 22 | + DomReady.ready(function () { |
| 23 | + if (location.href.match(/^file:/)) { |
| 24 | + document.getElementById('info').style.opacity = 1; |
| 25 | + document.getElementById('info').innerHTML = "Sorry. This example does not work when accessed using file://. Please use an http:// host and try again."; |
| 26 | + } |
| 27 | + }); |
| 28 | + </script> |
| 29 | + |
| 30 | + <style type="text/css" media="screen"> |
| 31 | + #info { |
| 32 | + position: absolute; |
| 33 | + left: 50%; |
| 34 | + bottom: 50px; |
| 35 | + z-index: 20; |
| 36 | + |
| 37 | + width: 300px; |
| 38 | + margin-left: -150px; |
| 39 | + |
| 40 | + padding: 25px; |
| 41 | + background: rgba(0, 0, 0, .3); |
| 42 | + color: #fff; |
| 43 | + |
| 44 | + font-family: "Lucida Grande", sans-serif; |
| 45 | + font-size: 16px; |
| 46 | + text-align: center; |
| 47 | + |
| 48 | + border-radius: 3px; |
| 49 | + text-shadow: 0px 1px 0px rgba(0, 0, 0, .4); |
| 50 | + |
| 51 | + opacity: 0; |
| 52 | + } |
| 53 | + |
| 54 | + #info kbd { |
| 55 | + background: #ccc; |
| 56 | + box-shadow: 0px 1px 1px rgba(0, 0, 0, .3); |
| 57 | + border-radius: 3px; |
| 58 | + padding: 3px; |
| 59 | + margin: 3px; |
| 60 | + |
| 61 | + font-family: inherit; |
| 62 | + } |
| 63 | + </style> |
| 64 | + |
| 65 | + <script type="text/javascript" charset="utf-8"> |
| 66 | + /** |
| 67 | + * Bootstrap |
| 68 | + */ |
| 69 | + DomReady.ready(function() { |
| 70 | + ThreeBox.preload([ |
| 71 | + '../shaders/snippets.glsl.html', |
| 72 | + '../resources/texture.png', |
| 73 | + ], function (assets) { |
| 74 | + |
| 75 | + // MathBox boilerplate |
| 76 | + var mathbox = window.mathbox = mathBox({ |
| 77 | + cameraControls: true, |
| 78 | + cursor: true, |
| 79 | + controlClass: ThreeBox.OrbitControls, |
| 80 | + elementResize: true, |
| 81 | + fullscreen: true, |
| 82 | + screenshot: true, |
| 83 | + stats: false, |
| 84 | + scale: 1, |
| 85 | + }).start(); |
| 86 | + |
| 87 | + // Viewport camera/setup |
| 88 | + mathbox |
| 89 | + // Polar viewport |
| 90 | + .viewport({ |
| 91 | + type: 'cartesian', |
| 92 | + }) |
| 93 | + .camera({ |
| 94 | + orbit: 4, |
| 95 | + phi: τ/4-.8, |
| 96 | + theta: 1.1, |
| 97 | + }) |
| 98 | + .transition(300) |
| 99 | + |
| 100 | + // Surface function |
| 101 | + .surface({ |
| 102 | + n: [192, 192], |
| 103 | + color: 0xffffff, |
| 104 | + shaded: true, |
| 105 | + map: assets['texture'], // Asset name is the filename without extension |
| 106 | + mapOpacity: 1, |
| 107 | + domain: [[-1.5, 1.5], [-1.5, 1.5]], |
| 108 | + n: [48, 32], |
| 109 | + expression: surfaceFunc, |
| 110 | + }) |
| 111 | + |
| 112 | + // Animate viewport between polar and cartesian |
| 113 | + mathbox.world().loop().hookPreRender(function () { |
| 114 | + var t = +new Date() * .0003; |
| 115 | + mathbox.set('viewport', { polar: Math.sin(t) * .5 + .5 }); |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + </script> |
| 120 | + |
| 121 | + <script type="text/javascript" charset="utf-8"> |
| 122 | + /** |
| 123 | + * Custom helpers |
| 124 | + */ |
| 125 | + |
| 126 | + // Clock that starts as soon as it is first called (per id). |
| 127 | + var clocks = {}; |
| 128 | + window.clock = function (id) { |
| 129 | + if (!clocks[id]) clocks[id] = +new Date(); |
| 130 | + return (+new Date() - clocks[id]) * .001; |
| 131 | + } |
| 132 | + |
| 133 | + // Arbitrary function |
| 134 | + function surfaceFunc(x, y) { |
| 135 | + |
| 136 | + return [x, -.15 + (Math.cos(x*3.41+Math.sin(y*1.9)) + Math.cos(y*3.7-Math.cos(x*4.1)))*.25, y]; |
| 137 | + } |
| 138 | + |
| 139 | + </script> |
| 140 | + |
| 141 | +</head> |
| 142 | +<body> |
| 143 | + <div id="info"></div> |
| 144 | +</body> |
| 145 | +</html> |
0 commit comments