-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhellocube.js
More file actions
121 lines (89 loc) · 3.54 KB
/
Copy pathhellocube.js
File metadata and controls
121 lines (89 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r132/build/three.module.js';
import {OBJExporter} from 'https://threejsfundamentals.org/threejs/resources/threejs/r132/examples/jsm/exporters/OBJExporter.js';
import {OBJLoader} from 'https://threejsfundamentals.org/threejs/resources/threejs/r132/examples/jsm/loaders/OBJLoader.js';
import {MTLLoader} from 'https://threejsfundamentals.org/threejs/resources/threejs/r132/examples/jsm/loaders/MTLLoader.js';
import {OrbitControls} from 'https://threejsfundamentals.org/threejs/resources/threejs/r132/examples/jsm/controls/OrbitControls.js';
window.onload = function init() {
// 카메라 컨트롤: https://threejsfundamentals.org/threejs/lessons/kr/threejs-cameras.html
const canvas = document.getElementById( "gl-canvas" );
const renderer = new THREE.WebGLRenderer({canvas});
const fov = 75;
const aspect = 2; // the canvas default
const near = 0.01;
const far = 1000;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.x = 25
camera.position.y = 20
camera.position.z = 30
camera.lookAt(0,0,0) // 0,0,0을 향해서 봄
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0, 0);
controls.update();
controls.addEventListener('change', render);
const scene = new THREE.Scene();
{
const color = 0xFFFFFF;
const intensity = 0.7;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(10, 10, 10);
scene.add(light);
}
{
const color = 0xFFFFFF;
const intensity = 0.3;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(-10, 10, 10);
scene.add(light);
}
// OBJ, MTL 로더
const mtlLoader = new MTLLoader();
mtlLoader.load('4.mtl', (mtl) => {
mtl.preload();
const objLoader = new OBJLoader();
objLoader.setMaterials(mtl);
objLoader.load('4.obj', (mesh) => {
// 오브젝트 배치
scene.add(mesh);
// 오브젝트 Transition
mesh.rotation.y = -Math.PI/180*90
// mesh.scale.set(1,1,1)
// mesh.rotation.set(0,0,0)
mesh.position.set(10,0,0)
renderer.render(scene, camera);
});
});
const boxWidth = 10;
const boxHeight = 10;
const boxDepth = 10;
const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
function makeInstance(geometry, color, x, y, z) {
const material = new THREE.MeshPhongMaterial({color});
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
cube.position.x = x;
cube.position.y = y;
cube.position.z = z;
return cube;
}
const cubes = [
makeInstance(geometry, 0x44aa88, 0, 0, 0),
makeInstance(geometry, 0x8844aa, -10, 0, 0),
makeInstance(geometry, 0xaa8844, 10, 0, 0),
]
function render(time) {
renderer.render(scene, camera);
}
render(render);
// Obj 파일 출력
// https://stackoverflow.com/questions/19351419/exporting-threejs-scene-to-obj-format
document.getElementById("downloadBtn").addEventListener("click", () => {
var exporter = new OBJExporter();
var objCode = exporter.parse(scene);
// https://learngrid.tistory.com/12
var text = 'Text to File'
var link = document.getElementById("download")
link.download = 'test.obj';
var blob = new Blob([objCode], {type: 'text/plain'});
link.href = window.URL.createObjectURL(blob);
})
}