質問するログイン新規登録

質問編集履歴

1

変更したから

2020/12/18 00:57

投稿

takahashi-one
takahashi-one

スコア120

title CHANGED
@@ -1,1 +1,1 @@
1
- D3jsのcall登録した関数削除したい
1
+ ThreejsTHREE.BoxGeometryの1面にだけTexture貼るには
body CHANGED
@@ -1,22 +1,75 @@
1
- 下記のような要素があるとします。
2
- ```html
3
- <div id="example"></div>
4
- ```
5
- この要素にD3jsのcallで下記のようにd3.drag()を登録しました。
6
- 登録た関数はexampleあるsvg要素ドラックで大きくしたり小さくしたりするものです
1
+ 下記なのですが、canvasをtextureに使用ています。BoxGeometry6面すべてtextureが表示されていますが、これ前面(1面)のみにしたのですがどうすればいいでしょうか?
7
- 登録する前はexampleをドラックするとブラウザがスクロールしてました。
8
- ```d3js
2
+ ```threejs
3
+ <!DOCTYPE html>
4
+ <head>
9
- d3.select("#example").call(
5
+ <meta charset="utf-8">
6
+ <script src="https://d3js.org/d3.v5.min.js"></script>
7
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/105/three.min.js"></script>
8
+ </head>
10
- d3.drag()
9
+ <body>
10
+ <script>
11
- .on("start", dragstarted)
11
+ var camera, scene, renderer, geometry, texture, mesh;
12
- .on("drag", dragged)
13
- .on("end", dragended)
14
- );
15
- ```
16
- そして大きくしたり小さくしたりした後に、
17
- 元の動作のドラックでブラウザがスクロールに戻したいのですが、どうすればいいでしょうか?
18
12
 
13
+ function makeCanvas(){
14
+ const width = 200;
15
+ const height = 200;
16
+ const canvas = d3.create("canvas")
17
+ .attr("width", width)
18
+ .attr("height", height)
19
+ .attr("style","background-color:#ffd700")
19
- 下記のようにしてdragイベントをnullにしたらdragは呼ばれなくなりますが、元の動作には戻りません。
20
+ const context = canvas.node().getContext("2d");
21
+ context.fillStyle = "#000000";
22
+ context.fillRect(0, 0, width, height);
23
+ context.fillStyle = "#ffd700";
24
+ context.fillRect(1, 1, width-2, height-2);
25
+ context.textAlign = "center";
26
+ context.textBaseline = "middle";
27
+ context.fillStyle = "#C19F4F";
28
+ context.font = "bold 20px 'MS ゴシック'";
29
+ context.fillText("999.9", width/2, height/2-40, width);
30
+ context.font = "bold 50px 'MS ゴシック'";
20
- d3.select("#example").on(".drag", null);
31
+ context.fillText("GOLD", width/2, height/2, width);
21
32
 
33
+ return canvas.node();
34
+ }
35
+
36
+ function init() {
37
+ var width = window.innerWidth, height = window.innerHeight;
38
+
39
+ renderer = new THREE.WebGLRenderer();
40
+ renderer.setSize(width, height);
41
+ document.body.appendChild(renderer.domElement);
42
+
43
+ scene = new THREE.Scene();
44
+
45
+ camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000);
46
+ camera.position.z = 400;
47
+ camera.position.y = 200;
48
+ camera.position.x = 10;
49
+ camera.lookAt(scene.position);
50
+
51
+ scene.add(camera);
52
+
53
+ texture = new THREE.Texture(makeCanvas());
22
- 元の動作のドラックでブラウザがスクロールに戻すにはどうすればいいでしょうか?
54
+ texture.minFilter = THREE.LinearFilter;
55
+ var material = new THREE.MeshBasicMaterial({ map: texture});
56
+ geometry = new THREE.BoxGeometry( 200, 200, 200 );
57
+ mesh = new THREE.Mesh( geometry, material );
58
+ scene.add( mesh );
59
+
60
+ animate();
61
+ }
62
+
63
+ function animate() {
64
+ requestAnimationFrame(animate);
65
+ texture.needsUpdate = true;
66
+ mesh.rotation.y += 0.01;
67
+ renderer.render(scene, camera);
68
+ }
69
+
70
+ window.onload = init;
71
+
72
+ </script>
73
+ </body>
74
+ </html>
75
+ ```