简体   繁体   English

需要clojurescript相当于一个three.js javascript'loadTexture'语句

[英]Need clojurescript equivalent of a three.js javascript 'loadTexture' statement

I am trying to apply an image to a cube in a three.js (3js) project. 我正在尝试将图像应用于three.js(3js)项目中的多维数据集。 I am writing it in clojurescript (cljs). 我在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: 原始JavaScript语句如下所示:

var material = new THREE.MeshPhongMaterial({map:THREE.ImageUtils.loadTexture("images/webgl-logo-256.jpg")})

The best I can come up with in cljs is: 我能想出的最好的是cljs:

(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命令,我可以看到我假设应该具有文件名的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;
   # ...

Update: even after solving the problem (see answers below), this.map still showed as null in the type command output. 更新:即使解决了问题(参见下面的答案), this.map仍然在type命令输出中显示为null 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 (?) 因此,我认为type输出中的字段只是默认值,您不应期望它们反映所使用的实际值(?)

The part I'm most unsure about is the loadTexture command. 我最不确定的部分是loadTexture命令。 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? 有谁知道如何用clojurescript写这个语句?

(THREE.ImageUtils.loadTexture "images/webgl-logo-256.jpg") is not a valid ClojureScript function call (at least, with tree.js). (THREE.ImageUtils.loadTexture "images/webgl-logo-256.jpg")不是有效的ClojureScript函数调用(至少使用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 . 在ClojureScript中. is used for JS function calls (including methods) and .- for accessing object properties. 用于JS函数调用(包括方法)和.-用于访问对象属性。 More details are here . 更多细节在这里

Based on the suggestion from Jarlax, here is the final solution I came up with. 根据Jarlax的建议,这是我提出的最终解决方案。

I implemented it in two forms: one split into two expressions, and one combined. 我以两种形式实现它:一种分成两个表达式,一个组合。 Note: part of the problem was using 'MeshPhongMaterial' instead of 'MeshBasicMaterial'. 注意:部分问题是使用'MeshPhongMaterial'而不是'MeshBasicMaterial'。 I could not get the image to display when using 'MeshPhongMaterial', no matter what I did, thus I switched to using "MeshBasicMaterial". 无论我做什么,使用'MeshPhongMaterial'时都无法显示图像,因此我切换到使用“MeshBasicMaterial”。

Solution A (two lines): 解决方案A(两行):

(def mat-map (.loadTexture (.-ImageUtils js/THREE) "images/webgl-logo-256.jpg"))
(def material (js/THREE.MeshBasicMaterial. (clj->js {:map mat-map})))

Solution B (combined): 解决方案B(合并):

(def material (js/THREE.MeshBasicMaterial. (clj->js {:map (.loadTexture (.-ImageUtils js/THREE) "images/webgl-logo-256.jpg")})))

Note: you always need to prefix the THREE object with 'js/' eg "js/THREE", otherwise you get a compile error. 注意:您总是需要在'THs /'前加上'js /',例如“js / THREE”,否则会出现编译错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM