-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathviewer.php
245 lines (207 loc) · 9.42 KB
/
viewer.php
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/*
Plugin Name: 3d Model Viewer
Plugin URI: http://turneremanager.com
Description: Model viewer using three.js for CAD files
Author: Matthew M. Emma & Robert Carmosino
Version: 0.4
Author URI: http://www.turneremanager.com
Credits: va3c - http://va3c.github.io/
*/
/*-------------------------------------------------------*/
/* Enqueue scripts
/*-------------------------------------------------------*/
$WPModelViewer = new ModelViewer();
class ModelViewer {
protected $turl;
protected $tkey;
protected $murl;
protected $mloc;
protected $mwidth;
protected $mheight;
public function __construct() {
add_action( 'wp_enqueue_scripts', array($this, 'model_scripts'), 10, 0 );
add_action( 'wp_footer', array($this, 'model_fscript'));
add_shortcode('model', array($this, 'model_shortcode'));
}
public function model_scripts() {
wp_register_script('Detector', plugins_url('assets/Detector.js', __FILE__), false);
wp_register_script('Three', plugins_url('assets/three.min.js', __FILE__), false);
wp_register_script('TrackballControls', plugins_url('assets/TrackballControls.js', __FILE__), false);
wp_register_script('Stats', plugins_url('assets/stats.min.js', __FILE__), false);
wp_register_script('jama-materials', plugins_url('assets/jama-materials.js', __FILE__), false);
wp_register_script('jama-materials-data', plugins_url('assets/jama-materials-data.js', __FILE__), false);
wp_enqueue_script('Detector');
wp_enqueue_script('Three');
wp_enqueue_script('TrackballControls');
wp_enqueue_script('Stats');
wp_enqueue_script('jama-materials');
wp_enqueue_script('jama-materials-data');
}
public function strLength($str,$len){
$length = strlen($str);
if($length > $len){
return substr($str,0,$len).'...';
}else{
return $str;
}
}
public function model_shortcode( $atts ) {
extract( shortcode_atts( array(
'url' => plugins_url('assets/rac_basic_sample_project.rvt.js', __FILE__),
'loc' => 'New York',
'width' => '800',
'height' => '600'
), $atts, 'model' ) );
$filestring = self::strLength(basename($url, ".js"),10);
$this->tkey = 'em_mdl-'.$filestring;
if ( $this->turl == '' ) {
$this->turl = $url;
set_transient( $tkey, $this->turl, 60 * 60 * 24 );
}
$this->turl = get_transient( $tkey );
$this->murl = $url;
$this->mloc = $loc;
$this->mwidth = $width;
$this->mheight = $height;
$v = '';
$v .= '<div id="threejs" style="position: relative; width: '.$width.'; height: '.$height.'; border: 2px solid black">';
$v .= '<div class="controls" style="background: #cccccc; text-align: center; font-weight: bold; padding: 10px; position: absolute; bottom: 0; width: 100%; zIndex: 100">';
/* check if fontawesome exists */
if (wp_style_is( 'fontawesome' )) {
$v .= '<div class="controls-left" style="float: left; width: 15%"><i class="fa fa-undo fa-2x"></i><br><strong>Rotate [Left Button]</strong></div>
<div class="controls-center" style="float: left; width: 15%"><i class="fa fa-search-plus fa-2x"></i><br><strong>Zoom [Mouse Wheel]</strong></div>
<div class="controls-right" style="float: left; width: 15%"><i class="fa fa-arrows fa-2x"></i><br><strong>Pan [Right Button]</strong></div>';
} else {
$v .= '<div class="controls-left" style="float: left; width: 15%">Left Click<br>to Rotate.</div>
<div class="controls-center" style="float: left; width: 15%">Mouse Wheel<br>to Zoom.</div>
<div class="controls-right" style="float: left; width: 15%">Right Click<br>to Pan.</div>';
}
/* check if bootstrap exists */
if (wp_style_is( 'bootstrap' )) {
$v .= '<div class="controls-load" style="float: left; width: 25%">
<button id="control-load" type="button" class="btn btn-primary btn-lg" onclick="loadFile(\''.$url.'\')">Load Model</button>
<div class="progress" id="progress" style="visibility: hidden; display: none"><div class="progress-bar progress-bar-striped active" id="progressbar" role="progressbar" style="width: 0%"><span class="sr-only">Loading</span></div></div>
</div>
<div class="controls-download" style="float: left; width: 30%"><a id="control-download" type="button" class="btn btn-primary btn-lg" href="'.$url.'" download>Download Model</a></div>';
} else {
$v .= '<div class="controls-load" style="float: left; width: 25%"><button id="control-load" type="button" onclick="loadFile(\''.$this->turl.'\')">Load Model</button></div>
<div class="controls-download" style="float: left; width: 30%"><a id="control-download" type="button" href="'.$url.'" download>Download Model</a></div>';
}
$v .= '</div></div>';
return $v;
}
public function model_fscript() {
?>
<script id="modelviewer">
var renderer, stats, scene, camera, controls;
init();
animate();
function init() {
var elem, geometry, material, mesh;
if ( ! Detector.webgl ) {
renderer = new THREE.CanvasRenderer( { alpha: 1, antialias: true, clearColor: 0xffffff } );
} else {
renderer = new THREE.WebGLRenderer( { alpha: 1, antialias: true, clearColor: 0xffffff } );
}
renderer.setSize( <?php echo $this->mwidth; ?>, <?php echo $this->mheight; ?> );
renderer.shadowMapEnabled = true;
stats = new Stats();
stats.domElement.style.cssText = "position: absolute; right: 0; top: 0; zIndex: 100;";
elem = document.getElementById( "threejs" );
elem.appendChild( renderer.domElement );
elem.appendChild( stats.domElement );
camera = new THREE.PerspectiveCamera( 40, (<?php echo $this->mwidth; ?>/<?php echo $this->mheight; ?>), 1, 100000 );
camera.position.set( 15000, 15000, 15000 );
controls = new THREE.TrackballControls( camera, renderer.domElement );
scene = new THREE.Scene();
light = new THREE.AmbientLight( 0x888888 );
scene.add( light );
light = new THREE.PointLight( 0xffffff, 0.5 );
scene.add( light );
light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 10000, 10000, 10000 ).normalize();
scene.add( light );
}
function loadFile( file ) {
renderer.setSize( document.getElementById("threejs").clientWidth, document.getElementById("threejs").clientHeight );
document.getElementById("control-load").style.visibility = "hidden";
document.getElementById("control-load").style.display = "none";
document.getElementById("progress").style.removeProperty("visibility");
document.getElementById("progress").style.removeProperty("display");
var data = requestFile(file);
document.getElementById("progressbar").style.width = "25%";
data = JSON.parse(data);
document.getElementById("progressbar").style.width = "50%";
if ( data.metadata === undefined ) { // 2.0
data.metadata = { type: "Geometry" };
}
if ( data.metadata.type === undefined ) { // 3.0
data.metadata.type = "Geometry";
}
if ( data.metadata.version === undefined ) {
data.metadata.version = data.metadata.formatVersion;
}
if ( data.metadata.type.toLowerCase() === "geometry" ) {
console.log( "found geometry" );
loader = new THREE.JSONLoader();
contents = loader.parse( data );
geometry = contents.geometry;
if ( contents.materials !== undefined ) {
console.log( "found geometry", contents.materials );
if ( contents.materials.length > 1 ) {
material = new THREE.MeshFaceMaterial( contents.materials );
} else {
material = contents.materials[ 0 ];
}
} else {
material = JAMA.materials.NormalSmooth.set();
}
var mesh = new THREE.Mesh( geometry, material );
document.getElementById("progressbar").style.width = "75%";
scene.add( mesh );
document.getElementById("progressbar").style.width = "100%";
} else if ( data.metadata.type.toLowerCase() === "object" ) {
loader = new THREE.ObjectLoader();
contents = loader.parse( data );
document.getElementById("progressbar").style.width = "75%";
if ( contents instanceof THREE.Scene ) {
console.log( "found scene" );
scene.add( contents );
document.getElementById("progressbar").style.width = "100%";
} else {
console.log( "found object", contents );
scene.add( contents );
document.getElementById("progressbar").style.width = "100%";
}
} else if ( data.metadata.type.toLowerCase() === "scene" ) {
console.log( "found deprecated");
// DEPRECATED
var loader = new THREE.SceneLoader();
loader.load( bundle.src, function ( contents ) {
document.getElementById("progressbar").style.width = "75%";
scene.add( contents );
document.getElementById("progressbar").style.width = "100%";
}, "" );
} else {
console.log( "found a whoopsie");
document.getElementById("progressbar").style.width = "100%";
}
}
function requestFile ( fname ) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.crossOrigin = "Anonymous";
xmlhttp.open( "GET", fname, false );
xmlhttp.send( null );
return xmlhttp.responseText;
}
function animate() {
requestAnimationFrame( animate );
controls.update();
renderer.render( scene, camera );
stats.update();
}
</script>
<?php
}
}