Performance testing, diamond-square terrain generation + canvas
authorggaren@apple.com <ggaren@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 23 May 2014 22:37:30 +0000 (22:37 +0000)
committerggaren@apple.com <ggaren@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 23 May 2014 22:37:30 +0000 (22:37 +0000)
https://bugs.webkit.org/show_bug.cgi?id=133054

Reviewed by Ryosuke Niwa.

This test was written by Hunter Loftis. It originally appeared on his
blog @ <http://www.playfuljs.com/realistic-terrain-in-130-lines/>.

I did a bit of editing for format and benchmark suitability.

* Canvas/terrain.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@169287 268f45cc-cd09-0410-ab3c-d52691b4dbfc

PerformanceTests/Canvas/terrain.html [new file with mode: 0644]
PerformanceTests/ChangeLog

diff --git a/PerformanceTests/Canvas/terrain.html b/PerformanceTests/Canvas/terrain.html
new file mode 100644 (file)
index 0000000..e74fb6f
--- /dev/null
@@ -0,0 +1,183 @@
+<!-- BSD License
+
+Copyright (c) 2014, Hunter Loftis.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. 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.
+
+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 HOLDER 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.
+ -->
+
+<html>
+    <head>
+        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
+        <title>Terrain Demo - PlayfulJS</title>
+        <script src="../resources/runner.js"></script>
+    </head>
+    <body>
+        <canvas id='display' style='background: #000'></canvas>
+        
+        <script>
+        Math.random = (function() {
+           var seed = 49734321;
+           return function() {
+               /* The Jenkins hash function:
+                   <http://en.wikipedia.org/wiki/Jenkins_hash_function>
+                   <http://burtleburtle.net/bob/hash/integer.html>
+                */
+               seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
+               seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
+               seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
+               seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
+               seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff;
+               seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
+               return (seed & 0xfffffff) / 0x10000000;
+           };
+        })();
+       
+        function Terrain(detail) {
+           this.size = Math.pow(2, detail) + 1;
+           this.max = this.size - 1;
+           this.map = new Float32Array(this.size * this.size);
+        }
+        
+        Terrain.prototype.get = function(x, y) {
+            if (x < 0 || x > this.max || y < 0 || y > this.max)
+                return -1;
+            return this.map[x + this.size * y];
+        };
+        
+        Terrain.prototype.set = function(x, y, val) {
+            this.map[x + this.size * y] = val;
+        };
+        
+        Terrain.prototype.generate = function(roughness) {
+            var self = this;
+            
+            this.set(0, 0, self.max);
+            this.set(this.max, 0, self.max / 2);
+            this.set(this.max, this.max, 0);
+            this.set(0, this.max, self.max / 2);
+            
+            divide(this.max);
+            
+            function divide(size) {
+                var x, y, half = size / 2;
+                var scale = roughness * size;
+                if (half < 1)
+                    return;
+                
+                for (y = half; y < self.max; y += size) {
+                    for (x = half; x < self.max; x += size) {
+                        square(x, y, half, Math.random() * scale * 2 - scale);
+                    }
+                }
+                for (y = 0; y <= self.max; y += half) {
+                    for (x = (y + half) % size; x <= self.max; x += size) {
+                        diamond(x, y, half, Math.random() * scale * 2 - scale);
+                    }
+                }
+                divide(size / 2);
+            }
+            
+            function average(values) {
+                var valid = values.filter(function(val) { return val !== -1; });
+                var total = valid.reduce(function(sum, val) { return sum + val; }, 0);
+                return total / valid.length;
+            }
+            
+            function square(x, y, size, offset) {
+                var ave = average([
+                    self.get(x - size, y - size),   // upper left
+                    self.get(x + size, y - size),   // upper right
+                    self.get(x + size, y + size),   // lower right
+                    self.get(x - size, y + size)    // lower left
+                ]);
+                self.set(x, y, ave + offset);
+            }
+            
+            function diamond(x, y, size, offset) {
+                var ave = average([
+                    self.get(x, y - size),      // top
+                    self.get(x + size, y),      // right
+                    self.get(x, y + size),      // bottom
+                    self.get(x - size, y)       // left
+                ]);
+                self.set(x, y, ave + offset);
+            }
+        };
+        
+        Terrain.prototype.draw = function(ctx, width, height) {
+            var self = this;
+            var waterVal = this.size * 0.3;
+            
+            for (var y = 0; y < this.size; y++) {
+                for (var x = 0; x < this.size; x++) {
+                    var val = this.get(x, y);
+                    var top = project(x, y, val);
+                    var bottom = project(x + 1, y, 0);
+                    var water = project(x, y, waterVal);
+                    var style = brightness(x, y, this.get(x + 1, y) - val);
+                    
+                    rect(top, bottom, style);
+                    rect(water, bottom, 'rgba(50, 150, 200, 0.15)');
+                }
+            }
+            
+            function rect(a, b, style) {
+                if (b.y < a.y)
+                    return;
+                ctx.fillStyle = style;
+                ctx.fillRect(a.x, a.y, b.x - a.x, b.y - a.y);
+            }
+            
+            function brightness(x, y, slope) {
+                if (y === self.max || x === self.max)
+                    return '#000';
+                var b = ~~(slope * 50) + 128;
+                return ['rgba(', b, ',', b, ',', b, ',1)'].join('');
+            }
+            
+            function iso(x, y) {
+                return {
+                    x: 0.5 * (self.size + x - y),
+                    y: 0.5 * (x + y)
+                };
+            }
+            
+            function project(flatX, flatY, flatZ) {
+                var point = iso(flatX, flatY);
+                var x0 = width * 0.5;
+                var y0 = height * 0.2;
+                var z = self.size * 0.5 - flatZ + point.y * 0.75;
+                var x = (point.x - self.size * 0.5) * 6;
+                var y = (self.size - point.y) * 0.005 + 1;
+                
+                return {
+                    x: x0 + x / y,
+                    y: y0 + z / y
+                };
+            }
+        };
+        
+        var display = document.getElementById('display');
+        var ctx = display.getContext('2d');
+        var width = display.width = 1024;
+        var height = display.height = 768;
+        
+        PerfTestRunner.measureTime({
+            run: function() {
+                var terrain = new Terrain(9);
+                terrain.generate(0.5);
+                ctx.clearRect(0, 0, width, height);
+                terrain.draw(ctx, width, height);
+            }
+        });
+        </script>
+    </body>
+</html>
index c37df70..5120816 100644 (file)
@@ -1,3 +1,17 @@
+2014-05-23  Geoffrey Garen  <ggaren@apple.com>
+
+        Performance testing, diamond-square terrain generation + canvas
+        https://bugs.webkit.org/show_bug.cgi?id=133054
+
+        Reviewed by Ryosuke Niwa.
+
+        This test was written by Hunter Loftis. It originally appeared on his
+        blog @ <http://www.playfuljs.com/realistic-terrain-in-130-lines/>.
+
+        I did a bit of editing for format and benchmark suitability.
+
+        * Canvas/terrain.html: Added.
+
 2014-05-13  Ryosuke Niwa  <rniwa@webkit.org>
 
         DYEBench should run 20 iterations in browser