<!DOCTYPE html>

<html>

<head>
<title></title>

<script src="https://cdn.bootcss.com/three.js/r67/three.js"></script>
<script src="https://cdn.bootcss.com/stats.js/r10/Stats.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/dat-gui/0.7.3/dat.gui.js"></script>

    <style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body> <div id="Stats-output">
</div>
<div id="WebGL-output">
</div> <script type="text/javascript"> // 初始化
function init() { var stopMovingLight = false; var stats = initStats(); // 创建一个场景
var scene = new THREE.Scene(); // 创建一个照相机
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); // 创建一个渲染器
var renderer = new THREE.WebGLRenderer(); renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;
renderer.shadowMapType = THREE.PCFShadowMap; // 创建一个地板
var planeGeometry = new THREE.PlaneGeometry(60, 20, 1, 1);
var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true; // 给地板一个坐标
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 15;
plane.position.y = 0;
plane.position.z = 0; // 给场景添加一个地板
scene.add(plane); // 创建一个形状
var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff3333});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true; // 给这个正方体坐标
cube.position.x = -4;
cube.position.y = 3;
cube.position.z = 0; // 把正方体添加到场景中去
scene.add(cube); var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff});
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); // 正方体坐标位置
sphere.position.x = 20;
sphere.position.y = 0;
sphere.position.z = 2;
sphere.castShadow = true; // 把正方体添加到场景中
scene.add(sphere); // 创建位置和朝向
camera.position.x = -35;
camera.position.y = 30;
camera.position.z = 25;
camera.lookAt(new THREE.Vector3(10, 0, 0)); // 添加自然光
var ambiColor = "#1c1c1c";
var ambientLight = new THREE.AmbientLight(ambiColor);
scene.add(ambientLight); // 添加聚光灯
var spotLight0 = new THREE.SpotLight(0xcccccc);
spotLight0.position.set(-40, 30, -10);
spotLight0.lookAt(plane);
scene.add(spotLight0); var target = new THREE.Object3D();
target.position = new THREE.Vector3(5, 0, 0); //添加聚光灯
var pointColor = "#ffffff";
var spotLight = new THREE.SpotLight(pointColor);
spotLight.position.set(-40, 60, -10);
spotLight.castShadow = true;
spotLight.shadowCameraNear = 2;
spotLight.shadowCameraFar = 200;
spotLight.shadowCameraFov = 30;
spotLight.target = plane;
spotLight.distance = 0; //光的距离
spotLight.angle = 0.4; //光的强度 scene.add(spotLight); // 添加一个小的圆形 点光源
var sphereLight = new THREE.SphereGeometry(0.2);
var sphereLightMaterial = new THREE.MeshBasicMaterial({color: 0xac6c25});
var sphereLightMesh = new THREE.Mesh(sphereLight, sphereLightMaterial);
sphereLightMesh.castShadow = true; sphereLightMesh.position = new THREE.Vector3(3, 20, 3);
scene.add(sphereLightMesh); // 把渲染元素放到DOM元素里面去
document.getElementById("WebGL-output").appendChild(renderer.domElement); // call the render function
var step = 0; // used to determine the switch point for the light animation
var invert = 1;
var phase = 0; var controls = new function () {
this.rotationSpeed = 0.03;
this.bouncingSpeed = 0.03;
this.ambientColor = ambiColor;
this.pointColor = pointColor;
this.intensity = 1; //影子大小
this.distance = 0;
this.exponent = 30;
this.angle = 0.1;
this.debug = false; //是否调试
this.castShadow = true;
this.onlyShadow = false;
this.target = "Plane";
this.stopMovingLight = false; }; var gui = new dat.GUI();
gui.addColor(controls, 'ambientColor').onChange(function (e) {
ambientLight.color = new THREE.Color(e);
}); gui.addColor(controls, 'pointColor').onChange(function (e) {
spotLight.color = new THREE.Color(e);
}); gui.add(controls, 'angle', 0, Math.PI * 2).onChange(function (e) {
spotLight.angle = e;
}); gui.add(controls, 'intensity', 0, 5).onChange(function (e) {
spotLight.intensity = e;
}); gui.add(controls, 'distance', 0, 200).onChange(function (e) {
spotLight.distance = e;
}); gui.add(controls, 'exponent', 0, 100).onChange(function (e) {
spotLight.exponent = e;
}); gui.add(controls, 'debug').onChange(function (e) {
spotLight.shadowCameraVisible = e;
}); gui.add(controls, 'castShadow').onChange(function (e) {
spotLight.castShadow = e;
}); gui.add(controls, 'onlyShadow').onChange(function (e) {
spotLight.onlyShadow = e;
}); gui.add(controls, 'target', ['Plane', 'Sphere', 'Cube']).onChange(function (e) {
console.log(e);
switch (e) {
case "Plane":
spotLight.target = plane;
break;
case "Sphere":
spotLight.target = sphere;
break;
case "Cube":
spotLight.target = cube;
break;
} }); gui.add(controls, 'stopMovingLight').onChange(function (e) {
stopMovingLight = e;
}); render(); function render() {
stats.update();
// 旋转正方体
cube.rotation.x += controls.rotationSpeed;
cube.rotation.y += controls.rotationSpeed;
cube.rotation.z += controls.rotationSpeed; // 控制球体的移动
step += controls.bouncingSpeed;
sphere.position.x = 20 + ( 10 * (Math.cos(step)));
sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step))); // 移动光点
if (!stopMovingLight) {
if (phase > 2 * Math.PI) {
invert = invert * -1;
phase -= 2 * Math.PI;
} else {
phase += controls.rotationSpeed;
}
sphereLightMesh.position.z = +(7 * (Math.sin(phase)));
sphereLightMesh.position.x = +(14 * (Math.cos(phase)));
sphereLightMesh.position.y = 10; if (invert < 0) {
var pivot = 14;
sphereLightMesh.position.x = (invert * (sphereLightMesh.position.x - pivot)) + pivot;
} spotLight.position.copy(sphereLightMesh.position);
} requestAnimationFrame(render); renderer.render(scene, camera);
} function initStats() { var stats = new Stats(); stats.setMode(0); // 0: fps, 1: ms
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px'; document.getElementById("Stats-output").appendChild(stats.domElement); return stats;
}
} window.onload = init; </script>
</body>
</html>

14-THREE.JS 聚光灯的更多相关文章

  1. 2016.02.14 总结JS事件

    今天主要总结JS事件的基本知识以及使用技巧,并作出相应的DEMO.

  2. 14 (H5*) JS第4天 函数、作用域、预解析

    目录 1:函数的其他定义 2:函数作为参数 3:函数作为返回值 4:作用域 5:作用域链 6:预解析 7:预解析分段 复习 <script> /* * 复习: * 函数:把一些重复的代码封 ...

  3. 14.data.js

    dict_data = { "_id":1, name:"王五", age:55, gender:true } db.stu.insert(dict_data) ...

  4. arcgis api for js入门开发系列一arcgis api离线部署

    在我的GIS之家QQ群里,很多都是arcgis api for js开发的新手,他们一般都是GIS专业的学生,或者从计算机专业刚刚转向来的giser,他们难免会遇到各种webgis开发的简单问题,由于 ...

  5. [转]OC与JS的交互详解

    事情的起因还是因为项目需求驱动.折腾了两天,由于之前没有UIWebView与JS交互的经历,并且觉得这次在功能上有一定的创造性,特此留下一点文字,方便日后回顾. 我要实现这样一个需求:按照本地的CSS ...

  6. 关于JS

    首先推荐一个小插件:W3Cfuns前端开发工具箱 整理一些杂乱的知识点. 1,Dom用于操作html元素 2,window.location.reload();//刷新当前页********** 3, ...

  7. JS疑难点和GC原理

    js解析与序列化json数据(一)json.stringify()的基本用法: 对象有两个方法:stringify()和parse().在最简单的情况下,这两个方法分别用于把JavaScript对象序 ...

  8. Vue.js实例练习

    最近学习Vue.js感觉跟不上节奏了,Vue.js用起来很方便. 主要实现功能,能添加书的内容和删除.(用的Bootstrap的样式)demo链接 标题用了自定义组件,代码如下: components ...

  9. iOS之与JS交互通信

    随着苹果SDK的不断升级,越来越多的新特性增加了进来,本文主要讲述从iOS6至今,Native与JavaScript的交互方法 一.UIWebview && iframe && ...

随机推荐

  1. m进制转n进制

    http://www.cnblogs.com/pkuoliver/archive/2010/10/27/Convert-m-number-to-n-number.html 从这道题中可以看出,数论中存 ...

  2. nodejs开发解决方案

    1.2. 统一环境 开发环境 nvm nrm nodejs 0.10.38 node-inspector 部署环境 nvm nrm iojs 2.x pm2 nginx 异步流程控制:Promise是 ...

  3. 201703 ABAP面试题002

    转自: ABAP 面试问题及答案(一):数据库更新及更改 SAP Standard (转) 问题一:锁对象(Lock Object)和 FM(Function Module)激活锁定对象时,产生的 F ...

  4. HAProxy安装及简单配置

    一.HAProxy简介 代理的作用:web缓存(加速).反向代理.内容路由(根据流量及内容类型等将请求转发至特定服务器).转码器(将后端服务器的内容压缩后传输给client端).缓存的作用:减少冗余内 ...

  5. python read文件的r和rb的区别

    r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r':默认值,表示从文件读取数据. 'w':表示要向文件写入数据,并截断以前的内容 'a':表示要向文件 ...

  6. bootstrap模板

    一.bootstrap基本介绍 1.什么是bootstrap? bootstrap就是个前端快速开发的工具,该工具是个简单.直观.强悍的前端开发框架,让web开发更加迅速简单,同时也是个响应式布局,兼 ...

  7. vs2015 安卓相关配置

    vs2015的安卓相关配置百度不到,园子里也没人写.还是我没搜索到? 看来只能靠自己的英(pin)语(yin)能力一点点解决了 安装2015这个过程没啥可说的.都安装就OK了. 重要的就是选择安卓程序 ...

  8. MySQL数据文件介绍及存放位置

    怎样查看MySql数据库物理文件存放位置? 使用命令行查找: show global variables like '%datadir%'; 我查找的位置:C:\ProgramData\MySQL\M ...

  9. 自己动手编译Android源码(超详细)

    http://www.jianshu.com/p/367f0886e62b 在Android Studio代码调试一文中,简单的介绍了代码调试的一些技巧.现在我们来谈谈android源码编译的一些事. ...

  10. this 机制的四种规则

    江湖人称,谁调用 this,this 就指向谁. 那么 this 到底绑定或者引用的是哪个对象环境呢,以下便是四种规则 1. 默认绑定全局变量 function fn() { console.log( ...