我正在尝试将图像应用于 three.js (3js) 项目中的立方体。我正在用 clojurescript (cljs) 编写它。为此,您需要从磁盘加载图像并将其应用于 Material 。原始的 JavaScript 语句如下所示:
var material = new THREE.MeshPhongMaterial({map:THREE.ImageUtils.loadTexture("images/webgl-logo-256.jpg")})
我能想到的最好的 cljs 是:
(def material (js/THREE.MeshPhongMaterial. (clj->js {:map (THREE.ImageUtils.loadTexture "images/webgl-logo-256.jpg")})))
这有效,因为它不会给我错误,但立方体没有应用图像,而是深蓝色。
使用 type
命令,我可以看到未设置 map
属性,我认为它应该具有文件名:
(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;
# ...
更新:即使在解决问题后(见下面的答案),this.map
仍然在 type
中显示为 null
命令输出。因此,我认为 type
输出中的字段只是默认值,您不应期望它们反射(reflect)正在使用的实际值 (?)
我最不确定的部分是 loadTexture 命令。我希望它写成这样:
(.-loadTexture THREE.ImageUtils "images/webgl-logo-256.jpg")
但这会返回:
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
有谁知道这个语句应该如何用 clojurescript 编写?
最佳答案
(THREE.ImageUtils.loadTexture "images/webgl-logo-256.jpg")
不是有效的 ClojureScript 函数调用(至少对于 tree.js)。这次尝试
(.-loadTexture THREE.ImageUtils "images/webgl-logo-256.jpg")
非常接近。应该改为:
(.loadTexture (.-ImageUtils THREE) "images/webgl-logo-256.jpg")
在 ClojureScript 中,.
用于 JS 函数调用(包括方法),.-
用于访问对象属性。更多详情here .
关于javascript - 需要相当于 three.js javascript 'loadTexture' 语句的 clojurescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28513636/