From 2a8abb55311605bde66dd089d88d3871819b3422 Mon Sep 17 00:00:00 2001 From: "Mr.doob" Date: Fri, 10 Dec 2010 11:04:51 +0000 Subject: [PATCH] Implemented gero3 Rectangle optimisations. --- examples/geometry_terrain_gl.html | 4 +- examples/js/ImprovedNoise.js | 28 ++++++-------- src/core/Rectangle.js | 61 +++++++++++++++++++++++-------- src/renderers/CanvasRenderer.js | 6 +-- 4 files changed, 62 insertions(+), 37 deletions(-) diff --git a/examples/geometry_terrain_gl.html b/examples/geometry_terrain_gl.html index 5764586d8f..5b53239214 100644 --- a/examples/geometry_terrain_gl.html +++ b/examples/geometry_terrain_gl.html @@ -114,8 +114,8 @@ function generateHeight( width, height ) { - var size = width * height, data = new Float32Array( size ), perlin = new ImprovedNoise(), - quality = 1, z = Math.random() * 100; + var size = width * height, data = new Float32Array( size ), + perlin = new ImprovedNoise(), quality = 1, z = Math.random() * 100; for ( var i = 0; i < size; i ++ ) { diff --git a/examples/js/ImprovedNoise.js b/examples/js/ImprovedNoise.js index c20772bdc8..08246a6313 100644 --- a/examples/js/ImprovedNoise.js +++ b/examples/js/ImprovedNoise.js @@ -2,22 +2,18 @@ var ImprovedNoise = function () { - var data = [ - 151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10, - 23,190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87, - 174,20,125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,83,111,229,122,60,211, - 133,230,220,105,92,41,55,46,245,40,244,102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208, - 89,18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,250,124,123,5, - 202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,223,183,170,213,119, - 248,152,2,44,154,163,70,221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,224,232, - 178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249, - 14,239,107,49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,205, - 93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180]; - - var p = Int16Array ? new Int16Array( data.length ) : []; - data.forEach( function ( v, i ) { p[ i ] = v; } ); - - for ( var i = 0; i < 256; i ++ ) { + var p = [151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10, + 23,190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87, + 174,20,125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,83,111,229,122,60,211, + 133,230,220,105,92,41,55,46,245,40,244,102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208, + 89,18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,250,124,123,5, + 202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,223,183,170,213,119, + 248,152,2,44,154,163,70,221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,224,232, + 178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249, + 14,239,107,49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,205, + 93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180]; + + for (var i=0; i < 256 ; i++) { p[256+i] = p[i]; diff --git a/src/core/Rectangle.js b/src/core/Rectangle.js index 3d912ef03f..e9bf154217 100644 --- a/src/core/Rectangle.js +++ b/src/core/Rectangle.js @@ -81,16 +81,42 @@ THREE.Rectangle = function () { _left = x; _top = y; _right = x; _bottom = y; + resize(); + } else { - _left = Math.min( _left, x ); - _top = Math.min( _top, y ); - _right = Math.max( _right, x ); - _bottom = Math.max( _bottom, y ); + _left = _left < x ? _left : x; // Math.min( _left, x ); + _top = _top < y ? _top : y; // Math.min( _top, y ); + _right = _right > x ? _right : x; // Math.max( _right, x ); + _bottom = _bottom > y ? _bottom : y; // Math.max( _bottom, y ); + resize(); } - resize(); + }; + + this.add3Points = function ( x1, y1, x2, y2, x3, y3 ) { + + if (_isEmpty) { + + _isEmpty = false; + _left = x1 < x2 ? ( x1 < x3 ? x1 : x3 ) : ( x2 < x3 ? x2 : x3 ); + _top = y1 < y2 ? ( y1 < y3 ? y1 : y3 ) : ( y2 < y3 ? y2 : y3 ); + _right = x1 > x2 ? ( x1 > x3 ? x1 : x3 ) : ( x2 > x3 ? x2 : x3 ); + _bottom = y1 > y2 ? ( y1 > y3 ? y1 : y3 ) : ( y2 > y3 ? y2 : y3 ); + + resize(); + + } else { + + _left = x1 < x2 ? ( x1 < x3 ? ( x1 < _left ? x1 : _left ) : ( x3 < _left ? x3 : _left ) ) : ( x2 < x3 ? ( x2 < _left ? x2 : _left ) : ( x3 < _left ? x3 : _left ) ); + _top = y1 < y2 ? ( y1 < y3 ? ( y1 < _top ? y1 : _top ) : ( y3 < _top ? y3 : _top ) ) : ( y2 < y3 ? ( y2 < _top ? y2 : _top ) : ( y3 < _top ? y3 : _top ) ); + _right = x1 > x2 ? ( x1 > x3 ? ( x1 > _right ? x1 : _right ) : ( x3 > _right ? x3 : _right ) ) : ( x2 > x3 ? ( x2 > _right ? x2 : _right ) : ( x3 > _right ? x3 : _right ) ); + _bottom = y1 > y2 ? ( y1 > y3 ? ( y1 > _bottom ? y1 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) ) : ( y2 > y3 ? ( y2 > _bottom ? y2 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) ); + + resize(); + + }; }; @@ -102,16 +128,18 @@ THREE.Rectangle = function () { _left = r.getLeft(); _top = r.getTop(); _right = r.getRight(); _bottom = r.getBottom(); + resize(); + } else { - _left = Math.min(_left, r.getLeft()); - _top = Math.min(_top, r.getTop()); - _right = Math.max(_right, r.getRight()); - _bottom = Math.max(_bottom, r.getBottom()); + _left = _left < r.getLeft() ? _left : r.getLeft(); // Math.min(_left, r.getLeft() ); + _top = _top < r.getTop() ? _top : r.getTop(); // Math.min(_top, r.getTop() ); + _right = _right > r.getRight() ? _right : r.getRight(); // Math.max(_right, r.getRight() ); + _bottom = _bottom > r.getBottom() ? _bottom : r.getBottom(); // Math.max(_bottom, r.getBottom() ); - } + resize(); - resize(); + } }; @@ -126,10 +154,10 @@ THREE.Rectangle = function () { this.minSelf = function ( r ) { - _left = Math.max( _left, r.getLeft() ); - _top = Math.max( _top, r.getTop() ); - _right = Math.min( _right, r.getRight() ); - _bottom = Math.min( _bottom, r.getBottom() ); + _left = _left > r.getLeft() ? _left : r.getLeft(); // Math.max( _left, r.getLeft() ); + _top = _top > r.getTop() ? _top : r.getTop(); // Math.max( _top, r.getTop() ); + _right = _right < r.getRight() ? _right : r.getRight(); // Math.min( _right, r.getRight() ); + _bottom = _bottom < r.getBottom() ? _bottom : r.getBottom(); // Math.min( _bottom, r.getBottom() ); resize(); @@ -147,7 +175,8 @@ THREE.Rectangle = function () { // return this.contains( r.getLeft(), r.getTop() ) || this.contains( r.getRight(), r.getTop() ) || this.contains( r.getLeft(), r.getBottom() ) || this.contains( r.getRight(), r.getBottom() ); - return Math.min( _right, r.getRight() ) - Math.max( _left, r.getLeft() ) >= 0 && Math.min( _bottom, r.getBottom() ) - Math.max( _top, r.getTop() ) >= 0; + return Math.min( _right, r.getRight() ) - Math.max( _left, r.getLeft() ) >= 0 && + Math.min( _bottom, r.getBottom() ) - Math.max( _top, r.getTop() ) >= 0; }; diff --git a/src/renderers/CanvasRenderer.js b/src/renderers/CanvasRenderer.js index d977bfc33c..9a077df2fc 100644 --- a/src/renderers/CanvasRenderer.js +++ b/src/renderers/CanvasRenderer.js @@ -177,9 +177,9 @@ THREE.CanvasRenderer = function () { } - _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y ); - _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y ); - _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y ); + _bboxRect.add3Points( _v1.positionScreen.x, _v1.positionScreen.y, + _v2.positionScreen.x, _v2.positionScreen.y, + _v3.positionScreen.x, _v3.positionScreen.y ); if ( _clipRect.instersects( _bboxRect ) ) { -- GitLab