ThreeJS - 3D Bar and Progress Bar

[code]
import * as THREE from "https://cdn.skypack.dev/three@0.136.0";
import {OrbitControls} from "https://cdn.skypack.dev/three@0.136.0/examples/jsm/controls/OrbitControls";
import {createNoise2D} from "https://cdn.skypack.dev/simplex-noise@4.0.0";
console.clear();
let scene = new THREE.Scene();
scene.background = new THREE.Color(0x7f7f7f);
let camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 1, 1000);
camera.position.set(-5, 10, 10).setLength(20);
let renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
window.addEventListener("resize", event => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
})
let controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
let light = new THREE.DirectionalLight(0xffffff, 0.5);
light.position.set(0.75, 0.5, 1);
scene.add(light, new THREE.AmbientLight(0xffffff, 0.5));
scene.add(new THREE.GridHelper());
class ProgressBar extends THREE.Sprite{
constructor(_progress){
super();
this.scale.set(2, 0.1);
this.material = new THREE.SpriteMaterial({
onBeforeCompile: shader => {
shader.uniforms.progress = _progress;
shader.fragmentShader = `
#define ss(a, b, c) smoothstep(a, b, c)
uniform float progress;
${shader.fragmentShader}
`.replace(
`outgoingLight = diffuseColor.rgb;`,
`outgoingLight = diffuseColor.rgb;
vec3 backColor = mix(vec3(0), vec3(0, 0.5, 0), progress);
float pb = step(progress, vUv.x);
outgoingLight.rgb = mix(vec3(0, 1, 0), backColor, pb);
`
);
console.log(shader.fragmentShader);
}
});
this.material.defines = {"USE_UV" : ""};
this.center.set(0.5, 0);
}
}
class ProgressObject extends THREE.Object3D{
constructor(height){
const rand = Math.random() * 100;
const noise = createNoise2D();
super();
let _progress = {value: 0.5};
let og = new THREE.BoxGeometry().translate(0, 0.5, 0);
let om = new THREE.MeshLambertMaterial();
let o = new THREE.Mesh(og, om);
o.scale.y = height;
let lg = new THREE.EdgesGeometry(new THREE.BoxGeometry());
lg.translate(0, 0.5, 0);
lg.scale(1, height, 1);
let lm = new THREE.LineBasicMaterial({color: new THREE.Color(Math.random() * 0xffffff).multiplyScalar(0.5).addScalar(0.5)});
let l = new THREE.LineSegments(lg, lm);
let pbar = new ProgressBar(_progress);
pbar.position.y = height * 1.1;
this.add(o, l, pbar);
this.update = val => {
_progress.value = noise(val, 0.25) * 0.5 + 0.5;
o.scale.y = height * _progress.value;
}
}
}
let tmu = THREE.MathUtils;
let POs = new Array(5).fill().map(o => {
let po = new ProgressObject(tmu.randInt(2, 5));
po.position.x = tmu.randInt(-5, 4) + 0.5;
po.position.z = tmu.randInt(-5, 4) + 0.5;
scene.add(po);
return po
})
let clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
controls.update();
let t = clock.getElapsedTime() * 0.5;
POs.forEach(po => {po.update(t)});
renderer.render(scene, camera);
});
[/code]
댓글 1개
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4886 | PHP | 7년 전 | 2392 | ||
| 4885 | PHP | 7년 전 | 2708 | ||
| 4884 | 기타 | 7년 전 | 3085 | ||
| 4883 | PHP | 7년 전 | 2367 | ||
| 4882 | PHP | 7년 전 | 3706 | ||
| 4881 | PHP | 7년 전 | 3344 | ||
| 4880 | JavaScript | 7년 전 | 3343 | ||
| 4879 | JavaScript | 7년 전 | 3064 | ||
| 4878 | PHP | 7년 전 | 5454 | ||
| 4877 | PHP |
welcome
|
7년 전 | 2916 | |
| 4876 | OS | 7년 전 | 2939 | ||
| 4875 | 기타 | 7년 전 | 2391 | ||
| 4874 | PHP |
|
7년 전 | 2753 | |
| 4873 | 웹서버 |
black
|
7년 전 | 2361 | |
| 4872 | PHP |
|
7년 전 | 3578 | |
| 4871 | 기타 | 7년 전 | 3352 | ||
| 4870 | 기타 | 7년 전 | 5656 | ||
| 4869 | 기타 | 7년 전 | 4226 | ||
| 4868 | JavaScript | 7년 전 | 4583 | ||
| 4867 | PHP |
|
7년 전 | 3922 | |
| 4866 | JavaScript | 7년 전 | 4445 | ||
| 4865 | PHP | 7년 전 | 5629 | ||
| 4864 | jQuery | 7년 전 | 2593 | ||
| 4863 | jQuery |
루돌프사슴코
|
7년 전 | 3993 | |
| 4862 | JavaScript | 7년 전 | 7805 | ||
| 4861 | 기타 | 7년 전 | 2263 | ||
| 4860 | 웹서버 | 7년 전 | 2501 | ||
| 4859 | 기타 | 7년 전 | 6953 | ||
| 4858 | 기타 | 7년 전 | 5120 | ||
| 4857 | 웹서버 | 7년 전 | 6195 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기