I am trying to apply an image to a cube in a three.js (3js) project. I am writing it in clojurescript (cljs). In order to do this, you need to load the image off disk and apply it to the material. The original JavaScript statement looks like:
var material = new THREE.MeshPhongMaterial({map:THREE.ImageUtils.loadTexture("images/webgl-logo-256.jpg")})
The best I can come up with in cljs is:
(def material (js/THREE.MeshPhongMaterial. (clj->js {:map (THREE.ImageUtils.loadTexture "images/webgl-logo-256.jpg")})))
This works, in the sense that it doesn't give me an error, but the cube doesn't have the image applied and instead is dark blue.
Using the type command, I can see that the map property, which I assume should have the filename, is not set:
(type material)
function(parameters) {
THREE.Material.call( this );
this.type = 'MeshPhongMaterial';
this.color = new THREE.Color( 0xffffff ); // diffuse
this.ambient = new THREE.Color( 0xffffff );
this.emissive = new THREE.Color( 0x000000 );
this.specular = new THREE.Color( 0x111111 );
this.shininess = 30;
this.metal = false;
this.wrapAround = false;
this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
this.map = null;
# ...
Update: even after solving the problem (see answers below), this.map still showed as null in the type command output. Thus, I think the fields in a type output are just default values, and you should not expect them to reflect the actual values being used (?)
The part I'm most unsure about is the loadTexture command. I expected it to be written something like:
(.-loadTexture THREE.ImageUtils "images/webgl-logo-256.jpg")
But this returns:
clojure.lang.ExceptionInfo: Unknown dot form of (. THREE.ImageUtils -loadTexture ("images/webgl-logo-256.jpg")) with classification [:cljs.analyzer/expr :cljs.analyzer/property :cljs.analyzer/expr] at line 1 {:tag :cljs/analysis-error, :file "", :line 1, :column 1}
nil
Does anyone know how this statement should be written in clojurescript?
(THREE.ImageUtils.loadTexture "images/webgl-logo-256.jpg") is not a valid ClojureScript function call (at least, with tree.js). This attempt
(.-loadTexture THREE.ImageUtils "images/webgl-logo-256.jpg")
was pretty close. It should be changed to:
(.loadTexture (.-ImageUtils THREE) "images/webgl-logo-256.jpg")
In ClojureScript . is used for JS function calls (including methods) and .- for accessing object properties. More details are here.
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