* three.js/index5.html
class Stage {
constructor() {
this.renderParam = {
width: window.innerWidth,
height: window.innerHeight
};
this.cameraParam = {
fov: 20,
near: 0.1,
far: 100,
lookAt: new THREE.Vector3(0, 0, 0),
x: 0,
y: 0,
z: 10
};
this.scene = null;
this.camera = null;
this.renderer = null;
this.geometry = null;
this.material = null;
this.mesh = null;
this.isInitialized = false;
}
init() {
this._setScene();
this._setRender();
this._setCamera();
this.isInitialized = true;
}
_setScene() {
this.scene = new THREE.Scene();
}
_setRender() {
this.renderer = new THREE.WebGLRenderer({
canvas: document.getElementById('webgl-canvas'),
antialias: true,
alpha: true
});
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setClearColor(new THREE.Color(0x161616));
this.renderer.setSize(this.renderParam.width, this.renderParam.height);
}
_setCamera() {
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
if(!this.isInitialized){
this.camera = new THREE.PerspectiveCamera(
this.cameraParam.fov,
this.renderParam.width / this.renderParam.height,
this.cameraParam.near,
this.cameraParam.far
);
this.camera.position.set(
this.cameraParam.x,
this.cameraParam.y,
this.cameraParam.z
);
this.camera.lookAt(this.cameraParam.lookAt);
}
this.camera.aspect = windowWidth / windowHeight;
this.camera.updateProjectionMatrix();
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(windowWidth, windowHeight);
}
_render() {
this.renderer.render(this.scene, this.camera);
}
onResize() {
this._setCamera();
}
onRaf() {
this._render();
}
}
class Mesh {
constructor(stage) {
this.geometryParam = {
width: 4,
height: 2.5,
widthSegments: 20,
heightSegments: 20
}
this.uniforms = {
u_texture: {
type: "t",
value: new THREE.TextureLoader().load("img.jpg")
},
u_time: { type: "f", value: 1.0 }
};
this.stage = stage;
this.mesh = null;
}
init() {
this._setMesh();
}
_setMesh() {
const geometry = new THREE.PlaneBufferGeometry(this.geometryParam.width, this.geometryParam.height, this.geometryParam.widthSegments, this.geometryParam.heightSegments);
const material = new THREE.RawShaderMaterial({
vertexShader: document.querySelector("#js-vertex-shader").textContent,
fragmentShader: document.querySelector("#js-fragment-shader").textContent,
uniforms: this.uniforms
});
this.mesh = new THREE.Mesh(geometry, material);
this.stage.scene.add(this.mesh);
}
_render() {
this.uniforms.u_time.value += 0.02;
}
onRaf() {
this._render();
}
}
(() => {
const stage = new Stage();
const mesh = new Mesh(stage);
stage.init();
mesh.init();
window.addEventListener("resize", () => {
stage.onResize();
});
const _raf = () => {
window.requestAnimationFrame(() => {
stage.onRaf();
mesh.onRaf();
_raf();
});
};
_raf();
})();
What’s on our mind?
Collection of articles, design, site, and resources made by designers and publisher
@Menu View
ad