Skip to content

Commit 22298c5

Browse files
author
Nikos M
committed
0.3.6, displaceMap modifier, optimizatins / edits
1 parent ade8c09 commit 22298c5

12 files changed

Lines changed: 272 additions & 68 deletions

File tree

build/MOD3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/mod3.bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/mod3.three.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/Three/DisplaceMap.html

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<!DOCTYPE html>
2+
<html lang="en"><head>
3+
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
4+
<title>DisplaceMap demo and three.js</title>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
7+
<style>
8+
body
9+
{
10+
background-color:#121212;
11+
}
12+
</style>
13+
<script type="text/javascript" src="./three.min.js"></script>
14+
<script type="text/javascript" src="../../build/mod3.bundle.js"></script>
15+
<script type="text/javascript" src="../js/Tween.js"></script>
16+
<script type="text/javascript" src="../js/RequestAnimationFrame.js"></script>
17+
</head>
18+
<body>
19+
<div id="container" style="position:relative;margin:0 auto;padding:0;z-index:0;cursor:pointer;"></div>
20+
<script>
21+
var container;
22+
var camera, scene, renderer, projector;
23+
var multx=0.5*Math.PI;
24+
var multy=-Math.PI;
25+
26+
var cube;
27+
28+
var targetRotationY = 0;
29+
var targetRotationOnMouseDownY = 0;
30+
var targetRotationX = 0;
31+
var targetRotationOnMouseDownX = 0;
32+
var rad=500;
33+
var mouse={x:0,y:0};
34+
var mouseX = 0;
35+
var mouseXOnMouseDown = 0;
36+
var mouseY = 0;
37+
var mouseYOnMouseDown = 0;
38+
var mstack,mod;
39+
var windowHalfX = window.innerWidth / 2;
40+
var windowHalfY = window.innerHeight / 2;
41+
var w,h,w2,h2;
42+
43+
init();
44+
animate();
45+
46+
47+
function init() {
48+
49+
container=document.getElementById('container');
50+
w=window.innerWidth;
51+
h=window.innerHeight;
52+
w2=w/2;
53+
h2=h/2;
54+
container.style.width=w+"px";
55+
container.style.height=h+"px";
56+
container.style.marginTop=0.5*(window.innerHeight-h)+'px';
57+
58+
scene = new THREE.Scene();
59+
60+
camera = new THREE.PerspectiveCamera( 70, w / h, 1, 1000 );
61+
camera.position.z = rad;
62+
scene.add( camera );
63+
64+
projector = new THREE.Projector();
65+
66+
// Cube Cube
67+
var materials=[];
68+
for (var mii=0;mii<6;mii++)
69+
{
70+
var mat=new THREE.MeshBasicMaterial( { color: 0xffffff } );
71+
materials.push( mat );
72+
}
73+
cube =new THREE.Mesh( new THREE.CubeGeometry( 400, 400, 1, 20, 20, 20 ), new THREE.MeshFaceMaterial(materials) );
74+
scene.add( cube );
75+
renderer = new THREE.CanvasRenderer();
76+
renderer.setSize( w, h );
77+
78+
container.appendChild( renderer.domElement );
79+
80+
container.addEventListener( 'mousedown', onDocumentMouseDown, false );
81+
82+
var displacemap = document.createElement('canvas'),
83+
ctx = displacemap.getContext('2d'), grd, displaceData,
84+
dW = 100, dH = 100
85+
;
86+
87+
// create a displace map image
88+
displacemap.width = dW;
89+
displacemap.height = dH;
90+
displacemap.style.width = dW+'px';
91+
displacemap.style.height = dH+'px';
92+
ctx.createImageData(dW, dH);
93+
ctx.fillStyle="rgb(128,128,128)";
94+
ctx.fillRect(0,0, dW, dH);
95+
// create radial gradient
96+
grd = ctx.createRadialGradient(dW/2, dH/2, 0, dW/2, dH/2, dW/2);
97+
grd.addColorStop(1, "#ffffff"); // neutral
98+
grd.addColorStop(0, "#808080"); // white
99+
ctx.fillStyle = grd;
100+
ctx.beginPath();
101+
ctx.arc(dW/2,dH/2,dW/2,0,Math.PI*2,true);
102+
ctx.fill();
103+
displaceData = ctx.getImageData(0, 0, dW, dH);
104+
105+
// display the map also
106+
displacemap.style.position = 'absolute';
107+
displacemap.style.top = 0;
108+
displacemap.style.left = 0;
109+
displacemap.style.zIndex = 10;
110+
container.appendChild( displacemap );
111+
112+
mstack=new MOD3.ModifierStack(MOD3.LibraryThree,cube);
113+
mod=new MOD3.DisplaceMap( displaceData.data, displaceData.width, displaceData.height );
114+
mod.offset = 128;
115+
mstack.addModifier( mod );
116+
var tobj={force:-1.5};
117+
new TWEEN.Tween(tobj).to({force:1.5}, 5000).onUpdate(function(){
118+
mod.force=this.force;
119+
mstack.apply();
120+
}).start();
121+
}
122+
123+
function onDocumentMouseDown( event ) {
124+
event.preventDefault();
125+
mouseX=(( event.clientX / w ) * 2 - 1);
126+
targetRotationY=mouseX;
127+
mouseY=(( event.clientY / h ) * 2 - 1);
128+
targetRotationX=mouseY;
129+
container.addEventListener( 'mousemove', onDocumentMouseMove, false );
130+
container.addEventListener( 'mouseup', onDocumentMouseUp, false );
131+
container.addEventListener( 'mouseout', onDocumentMouseOut, false );
132+
}
133+
134+
135+
function onDocumentMouseMove( event ) {
136+
137+
/*mouseX = event.clientX - w2;
138+
mouseY = event.clientY - h2;
139+
140+
targetRotationY = targetRotationOnMouseDownY + ( mouseX - mouseXOnMouseDown ) * 0.02;
141+
targetRotationX = targetRotationOnMouseDownX + ( mouseY - mouseYOnMouseDown ) * 0.02;*/
142+
//var target=
143+
//mouse_path.push(e.seenas.ray);
144+
mouseX=(( event.clientX / w ) * 2 - 1);
145+
targetRotationY=mouseX;
146+
mouseY=(( event.clientY / h ) * 2 - 1);
147+
targetRotationX=mouseY;
148+
}
149+
150+
function onDocumentMouseUp( event ) {
151+
container.removeEventListener( 'mousemove', onDocumentMouseMove, false );
152+
container.removeEventListener( 'mouseup', onDocumentMouseUp, false );
153+
container.removeEventListener( 'mouseout', onDocumentMouseOut, false );
154+
}
155+
156+
157+
function onDocumentMouseOut( event ) {
158+
159+
container.removeEventListener( 'mousemove', onDocumentMouseMove, false );
160+
container.removeEventListener( 'mouseup', onDocumentMouseUp, false );
161+
container.removeEventListener( 'mouseout', onDocumentMouseOut, false );
162+
}
163+
//
164+
165+
function animate() {
166+
167+
requestAnimationFrame( animate );
168+
169+
camera.position.x = rad * Math.sin( targetRotationY*multy ) * Math.cos( targetRotationX*multx );
170+
camera.position.y = rad * Math.sin( targetRotationX*multx );
171+
camera.position.z = rad * Math.cos( targetRotationY*multy ) * Math.cos( targetRotationX*multx );
172+
camera.lookAt(scene.position);
173+
TWEEN.update();
174+
renderer.render( scene, camera );
175+
}
176+
</script>
177+
</body></html>

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Also the API architecture is setup for more modifiers to be added (even custom o
5858
* Bloat
5959
* Break
6060
* Perlin ( MOD3 v.0.3.4 )
61-
* DisplaceMap ( MOD3 v.0.3.5, in progress )
61+
* DisplaceMap ( MOD3 v.0.3.5 )
6262

6363

6464
###TODO
@@ -77,6 +77,7 @@ Also the API architecture is setup for more modifiers to be added (even custom o
7777

7878

7979
###Changelog
80+
* 0.3.5 DisplaceMap modifier added, edits / optimizations
8081
* 0.3.4 update classy.js, code refactor / optimizations, Perlin modifier added
8182
* 0.3.2-0.3.3 update buildtools, api-reference, classy.js, Three.js revision (r66)
8283
* ver 0.3.1 update buildtools, api-reference, use classy.js for OOP

src/modifiers/Bend.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@
117117
min = this.min,
118118
mid = this.mid,
119119
m1 = this.m1,
120-
m2 = this.m2;
120+
m2 = this.m2,
121121

122-
var distance = origin + width * offset,
122+
distance = origin + width * offset,
123123
radius = width / PI / force,
124124
bendAngle = doublePI * (width / (radius * doublePI)),
125125
v, vmax, vmid, vmin, np, p, fa, op, ow, np2,
126126
invwidth = 1.0/width
127-
;
127+
;
128128

129129
// optimize loop using while counting down instead of up
130130
while ( --vc >= 0 )

src/modifiers/Break.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949

5050
apply: function( ) {
5151
var mod = this.mod, vs = mod.vertices, vc = vs.length,
52-
offset = this.offset, range = this.range, angle = this.angle, bv = this.bv, bvxyz=bv.xyz;
53-
var pv, npv, v, c, rm;
52+
offset = this.offset, range = this.range, angle = this.angle, bv = this.bv, bvxyz = bv.xyz,
53+
pv, npv, v, c, rm;
5454

5555
pv = new Vector3([0, 0, -(mod.minZ + mod.depth * offset)]);
5656
npv = pv.negate( );

src/modifiers/DisplaceMap.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,36 @@
2222

2323
(function(MOD3, undef){
2424

25-
var round = Math.round, ModConstant = MOD3.ModConstant, NONE = ModConstant.NONE,
25+
var ModConstant = MOD3.ModConstant, NONE = ModConstant.NONE,
2626
X = ModConstant.X, Y = ModConstant.Y, Z = ModConstant.Z
2727
;
2828

29-
// IN PROGRESS
3029
var DisplaceMap = MOD3.DisplaceMap = MOD3.Class ( MOD3.Modifier, {
3130

32-
constructor: function( b, w, h, f ) {
31+
constructor: function( bmp, w, h, f ) {
3332
this.$super('constructor');
3433
this.name = 'DisplaceMap';
35-
this.setBitmap( b, w, h );
36-
this.force = f || 1;
37-
this.offset = 0x80;
34+
if ( arguments.length >= 3 )
35+
{
36+
this.setBitmap( bmp, w, h );
37+
this.force = f || 1;
38+
}
39+
else
40+
{
41+
this.force = bmp || 1;
42+
}
43+
this.offset = 0x7F;
3844
this.axes = X | Y | Z;
3945
},
4046

4147
width: null,
4248
height: null,
43-
bitmap: null,
49+
bitmapData: null,
4450
force: 1,
45-
offset: 0x80,
51+
offset: 0x7F,
4652

4753
dispose: function( ) {
48-
this.bitmap = null;
54+
this.bitmapData = null;
4955
this.width = null;
5056
this.height = null;
5157
this.force = null;
@@ -55,35 +61,33 @@
5561
return this;
5662
},
5763

58-
setBitmap: function( b, w, h ) {
59-
this.bitmap = b || null;
64+
setBitmap: function( bmpData, w, h ) {
65+
this.bitmapData = bmpData || null;
6066
this.width = w || 0;
6167
this.height = h || 0;
6268
return this;
6369
},
6470

65-
getUVPixel: function( u, v ) {
66-
var w = this.width, h = this.height,
67-
x = round( (w - 1) * u ),
68-
y = round( (h - 1) * v )
69-
;
70-
return this.bitmap[ (y % h)*w + x % w ];
71-
},
72-
7371
apply: function( ) {
7472
var vs = this.mod.vertices, vc = vs.length, axes = this.axes,
75-
force = this.force, offset = this.offset, v, uv, xyz;
73+
w = this.width, h = this.height, bmp = this.bitmapData,
74+
force = this.force, offset = this.offset, v, uv, x, y, xyz;
7675

76+
if ( !axes || !bmp ) return this;
77+
7778
// optimize loop using while counting down instead of up
7879
while ( --vc >= 0 )
7980
{
8081
v = vs[ vc ];
8182
xyz = v.getXYZ( );
82-
uv = this.getUVPixel( v.ratio[ 0 ]/* X */, v.ratio[ 2 ]/* Z */ );
8383

84-
if ( axes & X ) xyz[ 0 ] += ((uv >> 16 & 0xff) - offset) * force;
85-
if ( axes & Y ) xyz[ 1 ] += ((uv >> 8 & 0xff) - offset) * force;
86-
if ( axes & Z ) xyz[ 2 ] += ((uv & 0xff) - offset) * force;
84+
x = ~~( (w - 1) * v.ratio[ 0 ]/* X */ );
85+
y = ~~( (h - 1) * v.ratio[ 2 ]/* Z */ );
86+
uv = ( y * w + x ) << 2;
87+
88+
if ( axes & X ) xyz[ 0 ] += ((bmp[ uv ] >> 16 & 0xff) - offset) * force;
89+
if ( axes & Y ) xyz[ 1 ] += ((bmp[ uv+1 ] >> 8 & 0xff) - offset) * force;
90+
if ( axes & Z ) xyz[ 2 ] += ((bmp[ uv+2 ] & 0xff) - offset) * force;
8791

8892
v.setXYZ( xyz );
8993
}

src/modifiers/Noise.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
vs = mod.vertices, vc = vs.length, force = this.force, halfforce = 0.5*force,
5757
maxAxis = mod.maxAxis, v, r, p, rp, xyz;
5858

59+
if ( !axes || !force ) return this;
5960
// optimize loop using while counting down instead of up
6061
while ( --vc >= 0 )
6162
{

0 commit comments

Comments
 (0)