1. 程式人生 > >74 - three.js 筆記 - 通過透明渲染設定背景圖片

74 - three.js 筆記 - 通過透明渲染設定背景圖片

1、示例

示例
http://ithanmang.com/threeJs/home/201809/20180910/01-canvas-background-image.html
效果
這裡寫圖片描述

2、實現步驟

2.1、背景圖
這裡寫圖片描述
2.2、設定背景
此處是給body元素設定了背景圖

body {
    margin: 0;
    overflow: hidden; /* 溢位隱藏 */
    background: url("../../images/bgc-map.jpg") center no-repeat;
}

設定背景圖自適應,否則使用手機開啟的時候不會撐滿整個螢幕

body {
    margin
: 0
; overflow: hidden; /* 溢位隱藏 */ background: url("../../images/bgc-map.jpg") center no-repeat; -webkit-background-size: cover; background-size: cover; }

設定setClearAlpha值,為背景透明度

/* 渲染器 */
function initRender() {

    renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
    renderer.setSize
(window.innerWidth, window.innerHeight); renderer.setClearAlpha(0.8); document.body.appendChild(renderer.domElement); }

setClearAlpha ( alpha : Float ) : null
設定alpha的值,範圍在0 - 1.0之間

3、示例程式碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta
name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="icon" href="../../../three.png"> <title>設定渲染背景</title> <style> body { margin: 0; overflow: hidden; /* 溢位隱藏 */ background: url("../../images/bgc-map.jpg") center no-repeat; -webkit-background-size: cover; background-size: cover; } #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, 10, 120); camera.lookAt(new THREE.Vector3(0, 0, 0)); } /* 渲染器 */ function initRender() { renderer = new THREE.WebGLRenderer({antialias: true, alpha: true}); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setClearAlpha(0.8); 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 () { this.specular = sphere.material.specular.getStyle(); this.shininess = sphere.material.shininess; this.clearAlpha = renderer.getClearAlpha(); this.clearColor = renderer.getClearColor().getStyle(); }; let gui = new dat.GUI(); gui.addColor(guiControls, 'specular').onChange(function (e) { sphere.material.specular.setStyle(e); }); gui.add(guiControls,'shininess', 0, 100).onChange(function (e) { sphere.material.shininess = e; }); gui.add(guiControls, 'clearAlpha', 0, 1).onChange(function (e) { renderer.setClearAlpha(e); }); gui.addColor(guiControls, 'clearColor').onChange(function (e) { renderer.setClearColor(new THREE.Color(e), renderer.getClearAlpha()); }); } /* 場景中的內容 */ let sphere; function initContent() { let material = new THREE.MeshPhongMaterial(); material.map = new THREE.TextureLoader().load('../../textures/planets/Earth.png'); material.normalMap = new THREE.TextureLoader().load('../../textures/planets/EarthNormal.png'); material.specularMap = new THREE.TextureLoader().load('../../textures/planets/EarthSpec.png', function () { removeLoading(); }); material.specular = new THREE.Color(0x3366ff); material.shininess = 2; let sphereGeometry = new THREE.SphereGeometry(40, 50, 50); sphere = new THREE.Mesh(sphereGeometry, material); scene.add(sphere); } /* 移除載入元素 */ 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 (sphere) { sphere.rotateY(0.001); } } /* 初始化 */ function init() { if (!Detector.webgl) Detector.addGetWebGLMessage(); 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>