now my task is to load a 3d model from a MySQL database and use it in Three.js.
Here is what I have done,
i created a database like this
models{model_id int(4), model mediumblob};
I can successfully load the 3d models in JSON format to and from the database.
I know how to retrive the data from DB.
But my problem is - 'How do i make the JSON file that is loaded from DB to be used by Three.js?'
I load the Json file conventionally like this
// instantiate a loader
var loader = new THREE.JSONLoader();
// load a resource
loader.load(
// resource URL
'models/animated/monster/monster.js',
// Function when resource is loaded
function ( geometry, materials ) {
var material = new THREE.MultiMaterial( materials );
var object = new THREE.Mesh( geometry, material );
scene.add( object );
}
);
This is an example code from Three.js documentation.
The resource URL appears to be a file located physically in the root. But I can't figure it out in my head 'how to load the model obtained from Database to Three.js?'
I am not asking for any code.
Just guide me to load the file in Three.js
I am familiar with loading an simple image. But that is html oriented. This is Three.js oriented.
Looking at I need my PHP page to show my BLOB image from mysql database I would try taking the
:
<img src="image.php?id=<?php echo $image_id; ?>" />
line and using the src
attribute for the js loader so in the js (I'm assuming it's working on a web page via html) something like:
js:
// set up a path variable
var filed = 'model.php?id=<?php echo $sql_id; ?>'
// instantiate a loader
var loader = new THREE.JSONLoader();
// load a resource
loader.load(
// resource URL location
filed,
// Function when resource is loaded
function ( geometry, materials ) {
var material = new THREE.MultiMaterial( materials );
var object = new THREE.Mesh( geometry, material );
scene.add( object );
}
);
and then the php:
<?php
function load() { $sql = "SELECT model FROM ovf_items_cycles"; if(!$res = $_ENV['db']->query($sql)) { echo ' failed to operate'; } while($row = $res->fetch_assoc() ) { echo $row['pid'] . ' ' . $row['modeldata']; } }
?>
This is a sketch as my php is not so good. But ultimately it would be much easier if you could also store the path to the blob file in your sql and access that for the loader variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With