73 - three.js 筆記 - 設定折射貼圖比率 refractionRatio 加環境貼圖實現反射效果
阿新 • • 發佈:2018-11-12
通過給物體設定環境貼圖envMap
可以實現虛假的反射效果,通過設定CubeCamera
可以實現動態的反光,設定材質的refractionRatio
屬性可以實現透明反射的效果。
1、示例
示例
http://ithanmang.com/threeJs/home/201809/20180907/03-refraction-texture.html
效果
2、實現步驟
2.1、refractionRatio
refractionRatio
:是物體對環境貼圖的折射率,值越接近0
的話,對貼圖的反射越強烈,越接近1
則月接近透明狀態,需要配合THREE.CubeRefractionMapping
THREE.EquirectangularRefractionMapping
一起使用。
2.2、設定環境貼圖和對映方式
let cubeTexture = new THREE.CubeTextureLoader().setPath('../../textures/cube/Bridge2/')
.load(
[
'posx.jpg',
'negx.jpg',
'posy.jpg',
'negy.jpg' ,
'posz.jpg',
'negz.jpg'
]
);
cubeTexture.mapping = THREE.CubeRefractionMapping;
scene.background = cubeTexture;
2.3、設定材質的對映率
envMap: cubeTexture,
refractionRatio:0.98,
reflectivity:0.9
首先,需要設定環境貼圖,然後設定設定率,設定貼圖的反射率
3、示例程式碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="../../../three.png">
<title>通過 Reflector 建立反光鏡</title>
<style>
body {
margin: 0;
overflow: hidden; /* 溢位隱藏 */
}
#loading {
position: fixed;
top: 50%;
left: 50%;
color: #FFFFFF;
font-size: 20px;
margin-top: -30px;
margin-left: -40px;
}
</style>
<script src="../../libs/build/three-r93.js"></script>
<script src="../../libs/examples/js/Detector.js"></script>
<script src="../../libs/examples/js/libs/dat.gui.min.js"></script>
<script src="../../libs/examples/js/libs/stats.min.js"></script>
<script src="../../libs/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<p id="loading">loading......</p>
<script>
let scene, camera, renderer, controls, guiControls;
let stats = initStats();
/* 場景 */
function initScene() {
scene = new THREE.Scene();
}
/* 相機 */
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.set(0, 0, 30);
camera.lookAt(new THREE.Vector3(0, 0, 0));
}
/* 渲染器 */
function initRender() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
}
/* 燈光 */
function initLight() {
scene.add(new THREE.AmbientLight(0x0c0c0c));
let spotLight1 = new THREE.SpotLight(0xffffff);
spotLight1.position.set(-400, -400, -400);
let spotLight2 = new THREE.SpotLight(0xffffff);
spotLight2.position.set(400, 400, 400);
scene.add(spotLight1);
scene.add(spotLight2);
}
/* 控制器 */
function initControls() {
/* 地圖控制元件 */
controls = new THREE.OrbitControls(camera, renderer.domElement);
/* 屬性引數 */
}
/* 除錯外掛 */
function initGui() {
guiControls = new function () {
};
let gui = new dat.GUI();
}
/* 場景中的內容 */
let cube;
function initContent() {
let cubeTexture = new THREE.CubeTextureLoader().setPath('../../textures/cube/Bridge2/')
.load(
[
'posx.jpg',
'negx.jpg',
'posy.jpg',
'negy.jpg',
'posz.jpg',
'negz.jpg'
]
);
cubeTexture.mapping = THREE.CubeRefractionMapping;
scene.background = cubeTexture;
let cubeMaterial1 = new THREE.MeshPhongMaterial({color: 0xccddff, envMap: cubeTexture, refractionRatio:0.98, reflectivity:0.9});
let cubeMaterial2 = new THREE.MeshPhongMaterial({color: 0xccfffd, envMap: cubeTexture, refractionRatio:0.98, reflectivity:0.9});
cube = new THREE.Mesh(new THREE.BoxGeometry(4, 4, 4), cubeMaterial1);
cube.material.color = new THREE.Color('0xffffff');
scene.add(cube);
let loader = new THREE.JSONLoader();
loader.load('../../models/json/Female02_slim.json', function (geometry) {
geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, -100, 0));
let mesh = new THREE.Mesh(geometry, cubeMaterial1);
mesh.translateX(7);
mesh.scale.set(0.1, 0.1, 0.1);
scene.add(mesh);
});
loader.load('../../models/json/Male02_dds.json', function (geometry) {
geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, -100, 0));
let mesh = new THREE.Mesh(geometry, cubeMaterial2);
mesh.translateX(-7);
mesh.scale.set(0.1, 0.1, 0.1);
scene.add(mesh);
removeLoading();
});
}
/* 移除載入元素 */
function removeLoading() {
document.getElementById('loading').style.display = 'none';
}
/* 效能外掛 */
function initStats() {
let stats = new Stats();
document.body.appendChild(stats.domElement);
return stats;
}
/* 視窗變動觸發 */
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
/* 資料更新 */
function update() {
stats.update();
controls.update();
if (cube) {
cube.rotateX(0.01);
cube.rotateY(0.01);
cube.rotateZ(0.01);
}
}
/* 初始化 */
function init() {
initScene();
initCamera();
initRender();
initLight();
initControls();
initContent();
initGui();
/* 監聽事件 */
window.addEventListener('resize', onWindowResize, false);
}
/* 迴圈渲染 */
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
update();
}
/* 初始載入 */
(function () {
console.log("three init start...");
init();
animate();
console.log("three init end...");
})();
</script>
</body>
</html>