96 Three.js 使用cubeCamera相机创建反光效果
生活随笔
收集整理的這篇文章主要介紹了
96 Three.js 使用cubeCamera相机创建反光效果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
案例查看地址:http://www.wjceo.com/blog/threejs/2018-05-03/159.html
通過案例可以看到,中間的球體不但可以和上一節一樣看到環境貼圖的相關紋理,連兩邊的模型都實現了反光的效果。這主要得益于cubeCamera的強大功能。
簡介
使用THREE.CubeCamera可以為場景中的所要渲染的物體創建快照,并使用這些快照創建CubeMap對象。但是需要確保攝像機放置在THREE.Mesh你所想顯示反射的位置上。
案例實現
- 首先,創建一個cubeCamera:
- 我們創建場景內所需要的內容,給scene添加背景,創建球體和球體旁邊的兩個模型,并給球體的環境貼圖賦值cubeCamera的紋理:
- 最后在render回調函數內更新獲取紋理:
注意,貼圖的模型最好和相機的位置相同,這樣獲取到的貼圖的效果最有代入感。
案例代碼
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Threejs使用CubeCamera創建反光效果</title><style type="text/css">html, body {margin: 0;height: 100%;}canvas {display: block;}</style> </head><body onload="draw();"> </body> <script src="https://cdn.bootcss.com/three.js/91/three.min.js"></script> <script src="/lib/js/controls/OrbitControls.js"></script> <script src="https://cdn.bootcss.com/stats.js/r17/Stats.min.js"></script> <script src="https://cdn.bootcss.com/dat-gui/0.7.1/dat.gui.min.js"></script> <script src="/lib/js/Detector.js"></script><script>var renderer, camera, scene, gui, light, stats, controls, material, cubeMesh, torusMesh, cubeCamera;function initRender() {renderer = new THREE.WebGLRenderer({antialias: true});renderer.setPixelRatio(window.devicePixelRatio);renderer.setSize(window.innerWidth, window.innerHeight);renderer.setClearColor(0xeeeeee);renderer.shadowMap.enabled = true;//告訴渲染器需要陰影效果document.body.appendChild(renderer.domElement);}function initCamera() {camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200);camera.position.set(0, 12, 15 );//CubeCamera(near:Number,far:Number,cubeResolution:Number)//近 - 近裁剪距離。//遠 - 裁剪距離//cubeResolution - 設置立方體邊緣的長度。//可以通過renderTarget對象獲取生成的立方體紋理。//創建一個獲取環境貼圖的cubeCameracubeCamera = new THREE.CubeCamera(0.1, 1000, 256);scene.add(cubeCamera);}function initScene() {//給場景添加天空盒子紋理var cubeTextureLoader = new THREE.CubeTextureLoader();cubeTextureLoader.setPath( '/lib/textures/cube/skybox/' );//六張圖片分別是朝前的(posz)、朝后的(negz)、朝上的(posy)、朝下的(negy)、朝右的(posx)和朝左的(negx)。var cubeTexture = cubeTextureLoader.load( ['px.jpg', 'nx.jpg','py.jpg', 'ny.jpg','pz.jpg', 'nz.jpg'] );scene = new THREE.Scene();scene.background = cubeTexture;}//初始化dat.GUI簡化試驗流程function initGui() {//聲明一個保存需求修改的相關數據的對象gui = {changeBg:function () {scene.background = new THREE.CubeTextureLoader().setPath( '/lib/textures/cube/pisa/' ).load( [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ] );sphereMaterial.envMap = scene.background;}};//var datGui = new dat.GUI();//將設置屬性添加到gui當中,gui.add(對象,屬性,最小值,最大值)}function initLight() {scene.add(new THREE.AmbientLight(0x444444));light = new THREE.DirectionalLight(0xffffff);light.position.set(0, 20, 20 );light.castShadow = true;light.shadow.camera.top = 10;light.shadow.camera.bottom = -10;light.shadow.camera.left = -10;light.shadow.camera.right = 10;//告訴平行光需要開啟陰影投射light.castShadow = true;scene.add(light);}function initModel() {//輔助工具var helper = new THREE.AxesHelper(50);scene.add(helper);//添加中間顯示的球體var geometry = new THREE.SphereBufferGeometry( 2, 100, 50 );//將cubeCamera的立方體紋理賦值給Material的envMapmaterial = new THREE.MeshBasicMaterial({envMap:cubeCamera.renderTarget.texture});var cubeMaterial = new THREE.MeshPhongMaterial({map:new THREE.TextureLoader().load("/lib/textures/disturb.jpg")});//添加球形var sphereMesh = new THREE.Mesh( geometry, material );scene.add( sphereMesh );//添加立方體cubeMesh = new THREE.Mesh(new THREE.CubeGeometry(2, 2, 2), cubeMaterial);cubeMesh.position.set(-5, 0, 0);scene.add(cubeMesh);//添加甜甜圈torusMesh = new THREE.Mesh(new THREE.TorusGeometry(2, 1, 16, 100), cubeMaterial);torusMesh.position.set(8, 0, 0);scene.add(torusMesh);}//初始化性能插件function initStats() {stats = new Stats();document.body.appendChild(stats.dom);}function initControls() {controls = new THREE.OrbitControls(camera, renderer.domElement);//設置控制器的中心點//controls.target.set( 0, 5, 0 );// 如果使用animate方法時,將此函數刪除//controls.addEventListener( 'change', render );// 使動畫循環使用時阻尼或自轉 意思是否有慣性controls.enableDamping = true;//動態阻尼系數 就是鼠標拖拽旋轉靈敏度//controls.dampingFactor = 0.25;//是否可以縮放controls.enableZoom = true;//是否自動旋轉controls.autoRotate = false;controls.autoRotateSpeed = 0.5;//設置相機距離原點的最遠距離controls.minDistance = 1;//設置相機距離原點的最遠距離controls.maxDistance = 2000;//是否開啟右鍵拖拽controls.enablePan = true;}function render() {//首先更新cubeCamera的相機cubeCamera.update( renderer, scene );//讓旁邊的兩個模型自動旋轉cubeMesh.rotation.x += 0.01;cubeMesh.rotation.y += 0.01;torusMesh.rotation.x += 0.01;torusMesh.rotation.y += 0.01;}//窗口變動觸發的函數function onWindowResize() {camera.aspect = window.innerWidth / window.innerHeight;camera.updateProjectionMatrix();renderer.setSize(window.innerWidth, window.innerHeight);}function animate() {//更新控制器render();//更新性能插件stats.update();controls.update();renderer.render(scene, camera);requestAnimationFrame(animate);}function draw() {//兼容性判斷if (!Detector.webgl) Detector.addGetWebGLMessage();initGui();initRender();initScene();initCamera();initLight();initModel();initControls();initStats();animate();window.onresize = onWindowResize;}</script> </html>總結
以上是生活随笔為你收集整理的96 Three.js 使用cubeCamera相机创建反光效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是哈希表?为什么要使用哈希表?哈希表
- 下一篇: 安川机器人编辑程序(一)