Repository navigation
Expand file tree
/
Copy pathbinary.html
More file actions
225 lines (155 loc) · 5.6 KB
/
Copy pathbinary.html
File metadata and controls
225 lines (155 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - node.js - OBJ converter</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
html, body{
padding:0;
margin:0;
}
body {
background:#fff;
overflow:hidden;
font-family:georgia;
text-align:center;
}
h1 { }
a { color:darkblue }
canvas { pointer-events:none; z-index:10; }
#d { text-align:center; margin:1em 0 -9.2em 0; z-index:0; position:absolute; display:block; left: 0; right: 0; }
.button { background:#000; color:#fff; padding:0.2em 0.5em; cursor:pointer }
.inactive { background:#999; color:#eee }
</style>
</head>
<body>
<div id="d">
<h1><a href="https://npmjs.org/package/three-obj">three-obj</a> binary test</h1>
<p>Module by <a href="http://makesites.org">Makesites</a> - Model by <a href="http://blog.dreamway.co">Dreamway</a>.
</div>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.min.js"></script>
<script type="text/javascript" src="//threejs.org/examples/js/loaders/BinaryLoader.js"></script>
<script type="text/javascript">
var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight;
var FLOOR = -250;
var container;
var camera, scene;
var canvasRenderer, webglRenderer;
var mesh, zmesh, geometry;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var render_gl = 1;
var has_gl = 0;
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
camera.position.z = 300;
scene = new THREE.Scene();
// GROUND
var x = document.createElement( "canvas" );
var xc = x.getContext("2d");
x.width = x.height = 128;
xc.fillStyle = "#fff";
xc.fillRect(0, 0, 128, 128);
xc.fillStyle = "#000";
xc.fillRect(0, 0, 64, 64);
xc.fillStyle = "#999";
xc.fillRect(32, 32, 32, 32);
xc.fillStyle = "#000";
xc.fillRect(64, 64, 64, 64);
xc.fillStyle = "#555";
xc.fillRect(96, 96, 32, 32);
var xm = new THREE.MeshBasicMaterial( { map: new THREE.Texture( x, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping ) } );
//var xm = new THREE.MeshBasicMaterial( { map: new THREE.Texture( x ) } );
xm.map.needsUpdate = true;
xm.map.repeat.set( 10, 10 );
geometry = new THREE.PlaneBufferGeometry( 100, 100, 15, 10 );
mesh = new THREE.Mesh( geometry, xm );
mesh.position.set( 0, FLOOR, 0 );
mesh.rotation.x = - Math.PI / 2;
mesh.scale.set( 10, 10, 10 );
scene.add( mesh );
// LIGHTS
var ambient = new THREE.AmbientLight( 0x221100 );
scene.add( ambient );
var directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set( 0, -70, 100 ).normalize();
scene.add( directionalLight );
// RENDERER
try {
webglRenderer = new THREE.WebGLRenderer({ alpha: true });
webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
webglRenderer.domElement.style.position = "relative";
container.appendChild( webglRenderer.domElement );
has_gl = 1;
}
catch (e) {
}
var loader = new THREE.BinaryLoader();
loader.load( "binary/palm.js", function ( geometry, materials ) {
// set opacity
for( var i in materials ){
// limit to only the leaves?
materials[i].transparent = true;
}
createScene( geometry, materials, 0, FLOOR, 50, 105 )
});
}
function createScene( geometry, materials, x, y, z, b ) {
zmesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
zmesh.position.set( x, y, z );
zmesh.scale.set( 3, 3, 3 );
scene.add( zmesh );
createMaterialsPalette( materials, 100, b );
}
function createMaterialsPalette( materials, size, bottom ) {
for ( var i = 0; i < materials.length; i ++ ) {
// material
mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( size, size ), materials[i] );
mesh.position.x = i * (size + 5) - ( ( materials.length - 1 )* ( size + 5 )/2);
mesh.position.y = FLOOR + size/2 + bottom;
mesh.position.z = -100;
scene.add( mesh );
// number
var x = document.createElement( "canvas" );
var xc = x.getContext( "2d" );
x.width = x.height = 128;
xc.shadowColor = "#000";
xc.shadowBlur = 7;
xc.fillStyle = "orange";
xc.font = "50pt arial bold";
xc.fillText( i, 10, 64 );
var xm = new THREE.MeshBasicMaterial( { map: new THREE.Texture( x ), transparent: true } );
xm.map.needsUpdate = true;
mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( size, size ), xm );
mesh.position.x = i * ( size + 5 ) - ( ( materials.length - 1 )* ( size + 5 )/2);
mesh.position.y = FLOOR + size/2 + bottom;
mesh.position.z = -99;
scene.add( mesh );
}
}
function onDocumentMouseMove(event) {
mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );
}
//
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
camera.position.x += ( mouseX - camera.position.x ) * .02;
camera.position.y += ( - mouseY - camera.position.y ) * .02;
camera.lookAt( scene.position );
webglRenderer.render( scene, camera );
}
</script>
</body>
</html>