webgl_particles_general.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <script src="../build/three.js"></script>
  6. <script src="js/Detector.js"></script>
  7. <script src="js/libs/stats.min.js"></script>
  8. <script src="js/controls/OrbitControls.js"></script>
  9. <script src="js/loaders/OBJLoader.js"></script>
  10. <script src='js/libs/dat.gui.min.js'></script>
  11. <script src="js/particles_general/Atlas.js"></script>
  12. <script src="js/particles_general/Particles.js"></script>
  13. <script src="js/particles_general/ParticleSystem.js"></script>
  14. <script src="js/particles_general/ParticleSystemFrameset.js"></script>
  15. <script src="js/particles_general/ParticleModifiers.js"></script>
  16. <script src="js/particles_general/ParticleSystemUtil.js"></script>
  17. <style>
  18. body {
  19. font-family: Monospace;
  20. background-color: #000;
  21. color: #fff;
  22. margin: 0px;
  23. overflow: hidden;
  24. }
  25. #info {
  26. position: absolute;
  27. top: 10px;
  28. width: 100%;
  29. text-align: center;
  30. z-index: 50;
  31. display:block;
  32. }
  33. #info a { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  34. </style>
  35. <title>Three.js Particle System</title>
  36. </head>
  37. <body>
  38. <div id="info">
  39. <a href="http://threejs.org" target="_blank">three.js</a> - General Purpose Particle System by <a href="https://github.com/mkkellogg">mkkellogg</a>
  40. </div>
  41. <div id="renderingContainer" style="position: absolute; left:0px; top:0px"></div>
  42. <script>
  43. var ParticleSystemIDs = Object.freeze(
  44. {
  45. Smoke1: 1,
  46. Smoke2: 2,
  47. Flame: 3,
  48. FlameEmbers: 4
  49. } );
  50. var ParticleEnvironmentIDs = Object.freeze(
  51. {
  52. Campfire: 1
  53. } );
  54. var rendererContainer;
  55. var screenWidth, screenHeight;
  56. var pointLight, ambientLight;
  57. var particleSystems, loadingManager;
  58. var scene, camera, renderer, controls, stats, clock;
  59. var currentEnvironmentID;
  60. var smokeActive, smokeType;
  61. var particleSystemsParent;
  62. window.addEventListener( "load", function load( event ) {
  63. window.removeEventListener( "load", load, false );
  64. init();
  65. }, false );
  66. function init() {
  67. clock = new THREE.Clock();
  68. getScreenDimensions();
  69. initScene();
  70. initGUI();
  71. initListeners();
  72. initLights();
  73. ParticleSystemUtil.initializeLoadingManager();
  74. initSceneGeometry( function() {
  75. initParticleSystems();
  76. startParticleSystemEnvironment ( ParticleEnvironmentIDs.Campfire );
  77. initRenderer();
  78. initControls();
  79. initStats();
  80. animate();
  81. } );
  82. }
  83. function initParticleSystems() {
  84. particleSystems = {};
  85. initializeFlameSystem();
  86. initializeSmokeSystem();
  87. }
  88. function initializeSmokeSystem() {
  89. var _TPSV = THREE.Particles.SingularVector;
  90. smokeType = ParticleSystemIDs.Smoke1;
  91. var smoke1Atlas = new THREE.Atlas( THREE.ImageUtils.loadTexture( 'textures/campfire/smokeparticle.png' ), true );
  92. var smoke2Atlas = THREE.Atlas.createGridAtlas( THREE.ImageUtils.loadTexture( 'textures/campfire/smokeparticles.png' ), 0.0, 1.0, 1.0, 0.0, 4.0, 4.0, false, true );
  93. var altVertexShader = [
  94. THREE.Particles.ParticleSystem.Shader.VertexVars,
  95. "varying vec4 vPosition;",
  96. THREE.Particles.ParticleSystem.Shader.ParticleVertexQuadPositionFunction,
  97. "void main()",
  98. "{",
  99. "vColor = customColor;",
  100. "vUV = uv;",
  101. "vec4 quadPos = getQuadPosition();",
  102. "vPosition = viewMatrix * quadPos;",
  103. "gl_Position = projectionMatrix * vPosition;",
  104. "}"
  105. ].join( "\n" );
  106. var altFragmentShader = [
  107. THREE.Particles.ParticleSystem.Shader.FragmentVars,
  108. "varying vec4 vPosition;",
  109. THREE.ShaderChunk[ "common" ],
  110. THREE.ShaderChunk[ "bsdfs" ],
  111. THREE.ShaderChunk[ "lights_pars" ],
  112. "void main()",
  113. "{",
  114. "vec4 textureColor = texture2D( texture, vUV );",
  115. "vec4 viewPosition = -vPosition;",
  116. "vec3 outgoingLight = vec3( 0.0 );",
  117. "vec4 diffuseColor = vColor * textureColor;",
  118. "vec3 totalDiffuseLight = vec3( 0.0 );",
  119. "#if NUM_POINT_LIGHTS > 0",
  120. "for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
  121. "vec3 lightColor = pointLights[ i ].color;",
  122. "vec3 lightPosition = pointLights[ i ].position;",
  123. "vec3 lVector = lightPosition + viewPosition.xyz;",
  124. "vec3 lightDir = normalize( lVector );",
  125. "float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
  126. "totalDiffuseLight += lightColor * attenuation;",
  127. "}",
  128. "#endif",
  129. "gl_FragColor = diffuseColor * vec4( totalDiffuseLight, 1.0 );",
  130. "}"
  131. ].join( "\n" );
  132. var customUniforms1 = THREE.UniformsUtils.merge( [ THREE.UniformsLib[ 'lights' ], THREE.UniformsLib[ 'ambient' ] ] );
  133. var altMaterial1 = THREE.Particles.ParticleSystem.createMaterial( altVertexShader, altFragmentShader, customUniforms1 );
  134. altMaterial1.lights = true;
  135. altMaterial1.blending = THREE.CustomBlending;
  136. altMaterial1.blendSrc = THREE.SrcAlphaFactor;
  137. altMaterial1.blendDst = THREE.OneMinusSrcAlphaFactor;
  138. altMaterial1.blendEquation = THREE.AddEquation;
  139. altMaterial1.uniforms.texture.value = smoke1Atlas.getTexture();
  140. var altMaterial2 = altMaterial1.clone();
  141. altMaterial2.uniforms.texture.value = smoke2Atlas.getTexture();
  142. var particleSystemParams1 = {
  143. material: altMaterial1,
  144. zSort: true,
  145. particleAtlas : smoke1Atlas,
  146. particleReleaseRate : 100,
  147. particleLifeSpan : 3.0,
  148. lifespan : 0
  149. };
  150. var particleSystemParams2 = {
  151. material: altMaterial2,
  152. zSort: true,
  153. particleAtlas : smoke2Atlas,
  154. particleReleaseRate : 100,
  155. particleLifeSpan : 3.0,
  156. lifespan : 0
  157. };
  158. var particleSystem1 = new THREE.Particles.ParticleSystem();
  159. particleSystem1.initialize( camera, particleSystemParams1 );
  160. var particleSystem2 = new THREE.Particles.ParticleSystem();
  161. particleSystem2.initialize( camera, particleSystemParams2 );
  162. var positionModifier = new THREE.Particles.RandomModifier(
  163. {
  164. offset: new THREE.Vector3( 0, 0, 0 ),
  165. range: new THREE.Vector3( 10, 0, 10 ),
  166. rangeEdgeClamp: false,
  167. rangeType: THREE.Particles.RangeType.Sphere
  168. } );
  169. var velocityModifier = new THREE.Particles.RandomModifier(
  170. {
  171. offset: new THREE.Vector3( 0, 75, 0 ),
  172. range: new THREE.Vector3( 5, 30, 5 ),
  173. rangeEdgeClamp: false,
  174. rangeType: THREE.Particles.RangeType.Sphere
  175. } );
  176. var accelerationModifier = new THREE.Particles.RandomModifier(
  177. {
  178. offset: new THREE.Vector3( 0, - 22, 0 ),
  179. range: new THREE.Vector3( 35, 20, 35 ),
  180. rangeEdgeClamp: false,
  181. rangeType: THREE.Particles.RangeType.Cube
  182. } );
  183. var rotationModifier = new THREE.Particles.RandomModifier(
  184. {
  185. offset: new THREE.Particles.SingularVector( 0 ),
  186. range: new THREE.Particles.SingularVector( 360 )
  187. } );
  188. var rotationalSpeedModifier = new THREE.Particles.RandomModifier(
  189. {
  190. offset: new THREE.Particles.SingularVector( 50 ),
  191. range: new THREE.Particles.SingularVector( 400 )
  192. } );
  193. var atlas1Modifier = new THREE.Particles.EvenIntervalIndexModifier ( 1 );
  194. var sizeModifier = new THREE.Particles.FrameSetModifier(
  195. new THREE.Particles.FrameSet(
  196. [ 0, 3 ],
  197. [ new THREE.Vector2( 10, 10 ),
  198. new THREE.Vector2( 40, 40 ) ],
  199. false )
  200. );
  201. var alphaModifier = new THREE.Particles.FrameSetModifier(
  202. new THREE.Particles.FrameSet(
  203. [ 0, 1.0, 2.0, 3.0 ],
  204. [ new _TPSV( 0.0 ), new _TPSV( 0.1 ), new _TPSV( 0.075 ), new _TPSV( 0.0 ) ],
  205. true
  206. ) );
  207. var colorModifier = new THREE.Particles.FrameSetModifier(
  208. new THREE.Particles.FrameSet(
  209. [ 0.0, 1.5, 3 ],
  210. [ new THREE.Vector3( 0.1, 0.1, 0.1 ),
  211. new THREE.Vector3( 0.35, 0.35, 0.35 ),
  212. new THREE.Vector3( 0.7, 0.7, 0.7 ) ],
  213. false )
  214. );
  215. particleSystem1.bindInitializer( 'position', positionModifier );
  216. particleSystem1.bindInitializer( 'velocity', velocityModifier );
  217. particleSystem1.bindInitializer( 'acceleration', accelerationModifier );
  218. particleSystem1.bindInitializer( 'rotation', rotationModifier );
  219. particleSystem1.bindInitializer( 'rotationalSpeed', rotationalSpeedModifier );
  220. particleSystem1.bindModifier( 'atlas', atlas1Modifier );
  221. particleSystem1.bindModifier( 'size', sizeModifier );
  222. particleSystem1.bindModifier( 'alpha', alphaModifier );
  223. particleSystem1.bindModifier( 'color', colorModifier );
  224. var atlas2Modifier = new THREE.Particles.EvenIntervalIndexModifier ( 16 );
  225. particleSystem2.bindInitializer( 'position', positionModifier );
  226. particleSystem2.bindInitializer( 'velocity', velocityModifier );
  227. particleSystem2.bindInitializer( 'acceleration', accelerationModifier );
  228. particleSystem2.bindInitializer( 'rotation', rotationModifier );
  229. particleSystem2.bindInitializer( 'rotationalSpeed', rotationalSpeedModifier );
  230. particleSystem2.bindModifier( 'atlas', atlas2Modifier );
  231. particleSystem2.bindModifier( 'size', sizeModifier );
  232. particleSystem2.bindModifier( 'alpha', alphaModifier );
  233. particleSystem2.bindModifier( 'color', colorModifier );
  234. particleSystems[ ParticleSystemIDs.Smoke1 ] = particleSystem1;
  235. particleSystems[ ParticleSystemIDs.Smoke2 ] = particleSystem2;
  236. particleSystemsParent.add ( particleSystems[ ParticleSystemIDs.Smoke1 ] );
  237. particleSystemsParent.add ( particleSystems[ ParticleSystemIDs.Smoke2 ] );
  238. }
  239. function initializeFlameSystem() {
  240. var _TPSV = THREE.Particles.SingularVector;
  241. // ---------------------
  242. // flame particle system
  243. // ---------------------
  244. var flameMaterial = THREE.Particles.ParticleSystem.createMaterial();
  245. flameMaterial.blending = THREE.AdditiveBlending;
  246. var particleSystemParams = {
  247. material: flameMaterial,
  248. particleAtlas : THREE.Atlas.createGridAtlas( THREE.ImageUtils.loadTexture( 'textures/campfire/fireloop3.jpg' ), 0.0, 1.0, 1.0, 0.0, 8.0, 8.0, false, true ),
  249. particleReleaseRate : 3,
  250. particleLifeSpan : 3,
  251. lifespan : 0
  252. };
  253. var particleSystem = new THREE.Particles.ParticleSystem();
  254. particleSystem.initialize( camera, particleSystemParams );
  255. particleSystem.bindModifier( "atlas", new THREE.Particles.EvenIntervalIndexModifier ( 64 ) );
  256. particleSystem.bindModifier( "size", new THREE.Particles.FrameSetModifier(
  257. new THREE.Particles.FrameSet(
  258. [ 0, 3 ],
  259. [ new THREE.Vector3( 20, 25 ),
  260. new THREE.Vector3( 20, 25 ) ],
  261. false )
  262. ) );
  263. particleSystem.bindModifier( "alpha", new THREE.Particles.FrameSetModifier(
  264. new THREE.Particles.FrameSet(
  265. [ 0, 0.2, 1.2, 2.0, 3 ],
  266. [ new _TPSV( 0 ), new _TPSV( .3 ), new _TPSV( 1 ), new _TPSV( 1 ), new _TPSV( 0 ) ],
  267. true )
  268. ) );
  269. particleSystem.bindModifier( "color", new THREE.Particles.FrameSetModifier(
  270. new THREE.Particles.FrameSet(
  271. [ 0, 3 ],
  272. [ new THREE.Vector3( 1.4, 1.4, 1.4 ),
  273. new THREE.Vector3( 1.4, 1.4, 1.4 ) ],
  274. false )
  275. ) );
  276. particleSystem.bindInitializer( 'position', new THREE.Particles.RandomModifier(
  277. {
  278. isScalar: false,
  279. offset: new THREE.Vector3( 0, 0, 0 ),
  280. range: new THREE.Vector3( 0, 0, 0 ),
  281. rangeEdgeClamp: false,
  282. rangeType: THREE.Particles.RangeType.Sphere
  283. } ) );
  284. particleSystem.bindInitializer( 'velocity', new THREE.Particles.RandomModifier(
  285. {
  286. isScalar: false,
  287. offset: new THREE.Vector3( 0, 25, 0 ),
  288. range: new THREE.Vector3( 10, 2, 10 ),
  289. rangeEdgeClamp: false,
  290. rangeType: THREE.Particles.RangeType.Sphere
  291. } ) );
  292. particleSystems[ ParticleSystemIDs.Flame ] = particleSystem;
  293. particleSystemsParent.add ( particleSystems[ ParticleSystemIDs.Flame ] );
  294. // ---------------------
  295. // flame embers particle system
  296. // ---------------------
  297. var emberMaterial = THREE.Particles.ParticleSystem.createMaterial();
  298. emberMaterial.blending = THREE.AdditiveBlending;
  299. particleSystemParams = {
  300. material: emberMaterial,
  301. particleAtlas : new THREE.Atlas( THREE.ImageUtils.loadTexture( 'textures/campfire/Puff.png' ), true ),
  302. particleReleaseRate : 18,
  303. particleLifeSpan : 3,
  304. lifespan : 0
  305. };
  306. particleSystem = new THREE.Particles.ParticleSystem();
  307. particleSystem.initialize( camera, particleSystemParams );
  308. particleSystem.bindModifier( "atlas", new THREE.Particles.EvenIntervalIndexModifier ( 1 ) );
  309. particleSystem.bindModifier( 'size', new THREE.Particles.RandomModifier(
  310. {
  311. isScalar: false,
  312. offset: new THREE.Vector3( .25, .25, 0.0 ),
  313. range: new THREE.Vector3( 0.05, 0.05, 0.0 ),
  314. rangeEdgeClamp: false,
  315. rangeType: THREE.Particles.RangeType.Sphere,
  316. runOnce: true
  317. } ) );
  318. particleSystem.bindModifier( "alpha", new THREE.Particles.FrameSetModifier(
  319. new THREE.Particles.FrameSet(
  320. [ 0, 0.2, 1.2, 2.0, 3 ],
  321. [ new _TPSV( 0 ), new _TPSV( 1 ), new _TPSV( 1 ), new _TPSV( 1 ), new _TPSV( 0 ) ],
  322. true )
  323. ) );
  324. particleSystem.bindModifier( "color", new THREE.Particles.FrameSetModifier(
  325. new THREE.Particles.FrameSet(
  326. [ 0, 2, 3 ],
  327. [ new THREE.Vector3( 1.3, 1.3, 0 ),
  328. new THREE.Vector3( .75, .4, .4 ),
  329. new THREE.Vector3( .6, .6, .6 ) ],
  330. false )
  331. ) );
  332. particleSystem.bindInitializer( 'position', new THREE.Particles.RandomModifier(
  333. {
  334. isScalar: false,
  335. offset: new THREE.Vector3( 0, 7, 0 ),
  336. range: new THREE.Vector3( 3, 0, 3 ),
  337. rangeEdgeClamp: false,
  338. rangeType: THREE.Particles.RangeType.Sphere
  339. } ) );
  340. particleSystem.bindInitializer( 'velocity', new THREE.Particles.RandomModifier(
  341. {
  342. isScalar: false,
  343. offset: new THREE.Vector3( 0, 25, 0 ),
  344. range: new THREE.Vector3( 15, 25, 15 ),
  345. rangeEdgeClamp: true,
  346. rangeType: THREE.Particles.RangeType.Sphere
  347. } ) );
  348. particleSystem.bindModifier( 'acceleration', new THREE.Particles.RandomModifier(
  349. {
  350. isScalar: false,
  351. offset: new THREE.Vector3( 0, 15, 0 ),
  352. range: new THREE.Vector3( 180, 280, 180 ),
  353. rangeEdgeClamp: true,
  354. rangeType: THREE.Particles.RangeType.Sphere
  355. } ) );
  356. particleSystems[ ParticleSystemIDs.FlameEmbers ] = particleSystem;
  357. particleSystemsParent.add( particleSystems[ ParticleSystemIDs.FlameEmbers ] );
  358. }
  359. function initGUI() {
  360. gui = new dat.GUI();
  361. parameters =
  362. {
  363. smoke: function() {
  364. smokeActive = ! smokeActive; updateSmokeType();
  365. },
  366. smokeType: null,
  367. embers: function() {
  368. toggleParticleSystem( ParticleSystemIDs.FlameEmbers );
  369. },
  370. flame: function() {
  371. toggleParticleSystem( ParticleSystemIDs.Flame );
  372. },
  373. };
  374. gui.add( parameters, 'smokeType', { Basic: ParticleSystemIDs.Smoke1, Animated: ParticleSystemIDs.Smoke2 } ).name( "Smoke type" ).onChange( function() {
  375. smokeType = parameters.smokeType;
  376. updateSmokeType();
  377. } );
  378. gui.add( parameters, 'smoke' ).name( "Toggle smoke" );
  379. gui.add( parameters, 'embers' ).name( "Toggle embers" );
  380. gui.add( parameters, 'flame' ).name( "Toggle flame" );
  381. gui.open();
  382. gui.domElement.parentNode.style.zIndex = 100;
  383. }
  384. function initListeners() {
  385. window.addEventListener( 'resize', onWindowResize, false );
  386. }
  387. function initRenderer() {
  388. renderer = new THREE.WebGLRenderer();
  389. renderer.setSize( screenWidth, screenHeight );
  390. renderer.setClearColor( 0x000000 );
  391. renderer.shadowMap.enabled = true;
  392. renderer.shadowMap.type = THREE.BasicShadowMap;
  393. rendererContainer = document.getElementById( 'renderingContainer' );
  394. rendererContainer.appendChild( renderer.domElement );
  395. }
  396. function initLights() {
  397. ambientLight = new THREE.AmbientLight( 0x101010 );
  398. scene.add( ambientLight );
  399. pointLight = new THREE.PointLight( 0xffffff, 2, 1000, 1 );
  400. pointLight.position.set( 0, 40, 0 );
  401. pointLight.castShadow = true;
  402. pointLight.shadowCameraNear = 1;
  403. pointLight.shadowCameraFar = 1000;
  404. pointLight.shadowDarkness = .8;
  405. // pointLight.shadowCameraVisible = true;
  406. pointLight.shadowMapWidth = 4096;
  407. pointLight.shadowMapHeight = 2048;
  408. pointLight.shadowBias = - 0.5;
  409. scene.add( pointLight );
  410. }
  411. function initSceneGeometry( onFinished ) {
  412. var loadedCount = 0;
  413. var targetLoadCount = 3;
  414. var onFinishedCalled = false;
  415. function incrementAndCheckLoadComplete() {
  416. loadedCount ++;
  417. if ( ! onFinishedCalled && loadedCount >= targetLoadCount && onFinished ) {
  418. onFinishedCalled = true;
  419. onFinished();
  420. }
  421. }
  422. // ---------------------
  423. // create ground
  424. // ---------------------
  425. var groundTexture = new THREE.ImageUtils.loadTexture( 'textures/campfire/grass1.jpg' );
  426. groundTexture.wrapS = THREE.RepeatWrapping;
  427. groundTexture.wrapT = THREE.RepeatWrapping;
  428. groundTexture.repeat.set( 10, 10 );
  429. var groundMaterial = new THREE.MeshLambertMaterial( {
  430. color: 0xffffff,
  431. shading: THREE.SmoothShading,
  432. map: groundTexture,
  433. vertexColors: THREE.NoColors,
  434. side: THREE.BackSide
  435. } );
  436. var groundGeometry = new THREE.PlaneGeometry( 1000, 1000, 30, 30 );
  437. var groundMesh = new THREE.Mesh( groundGeometry, groundMaterial );
  438. groundMesh.position.y = 0;
  439. groundMesh.rotation.x = Math.PI / 2.0;
  440. groundMesh.receiveShadow = true;
  441. scene.add( groundMesh );
  442. // ---------------------
  443. // load campfire
  444. // ---------------------
  445. var campFireMaterial = new THREE.MeshLambertMaterial( {
  446. color: 0xffffff,
  447. shading: THREE.SmoothShading,
  448. vertexColors: THREE.NoColors,
  449. side: THREE.FrontSide
  450. } );
  451. ParticleSystemUtil.loadObj( 'models/campfire/campfire.obj', 'models/campfire/campfire_texture.png', campFireMaterial,
  452. function( mesh ) {
  453. mesh.castShadow = true;
  454. mesh.receiveShadow = false;
  455. },
  456. function( object ) {
  457. object.position.set( 0, 0, 0 );
  458. object.scale.set( 7, 7, 7 );
  459. scene.add( object );
  460. incrementAndCheckLoadComplete();
  461. }
  462. );
  463. // ---------------------
  464. // load rocks
  465. // ---------------------
  466. var rockMaterial = new THREE.MeshLambertMaterial( {
  467. color: 0xffffff,
  468. shading: THREE.SmoothShading,
  469. vertexColors: THREE.NoColors,
  470. side: THREE.FrontSide
  471. } );
  472. ParticleSystemUtil.loadObj( 'models/campfire/brownrock.obj', 'models/campfire/brownrock.png', rockMaterial,
  473. function( mesh ) {
  474. mesh.castShadow = true;
  475. mesh.receiveShadow = true;
  476. },
  477. function( object ) {
  478. object.position.set( - 70, 0, 0 );
  479. object.scale.set( .55, .55, .55 );
  480. scene.add( object );
  481. var rockObject2 = object.clone();
  482. rockObject2.rotation.z = - Math.PI / 4;
  483. rockObject2.rotation.x = Math.PI / 2;
  484. rockObject2.position.set( - 55, - 1, 25 );
  485. rockObject2.scale.set( .35, .35, .35 );
  486. scene.add( rockObject2 );
  487. var rockObject3 = object.clone();
  488. rockObject3.rotation.z = Math.PI / 4;
  489. rockObject3.rotation.x = Math.PI / 2;
  490. rockObject3.position.set( 45, 10, 45 );
  491. rockObject3.scale.set( .65, .65, .85 );
  492. scene.add( rockObject3 );
  493. incrementAndCheckLoadComplete();
  494. }
  495. );
  496. // ---------------------
  497. // load trees
  498. // ---------------------
  499. var treeMaterial = new THREE.MeshLambertMaterial( {
  500. color: 0xffffff,
  501. shading: THREE.SmoothShading,
  502. vertexColors: THREE.NoColors,
  503. side: THREE.FrontSide
  504. } );
  505. ParticleSystemUtil.loadObj( 'models/campfire/pinetree_doubleface.obj', 'models/campfire/pinetree.jpg', treeMaterial,
  506. function( mesh ) {
  507. mesh.castShadow = true;
  508. mesh.receiveShadow = true;
  509. },
  510. function( object ) {
  511. object.rotation.z = Math.PI / 64;
  512. object.rotation.x = Math.PI / 64;
  513. object.position.set( - 20, - 1, - 80 );
  514. object.scale.set( 1.155, 1.155, 1.155 );
  515. scene.add( object );
  516. var treeObject2 = object.clone();
  517. treeObject2.rotation.z = - Math.PI / 16;
  518. treeObject2.rotation.x = Math.PI / 32;
  519. treeObject2.position.set( 15, - 1, - 80 );
  520. treeObject2.scale.set( .855, .855, .855 );
  521. scene.add( treeObject2 );
  522. incrementAndCheckLoadComplete();
  523. }
  524. );
  525. particleSystemsParent = new THREE.Object3D();
  526. particleSystemsParent.position.set( 0, 0, 0 );
  527. particleSystemsParent.matrixAutoUpdate = true;
  528. scene.add( particleSystemsParent );
  529. }
  530. function initScene() {
  531. scene = new THREE.Scene();
  532. camera = new THREE.PerspectiveCamera( 45, 1.0, 2, 2000 );
  533. scene.add( camera );
  534. resetCamera();
  535. }
  536. function initStats() {
  537. stats = new Stats();
  538. stats.domElement.style.position = 'absolute';
  539. stats.domElement.style.bottom = '0px';
  540. stats.domElement.style.zIndex = 100;
  541. rendererContainer.appendChild( stats.domElement );
  542. }
  543. function initControls() {
  544. controls = new THREE.OrbitControls( camera, renderer.domElement );
  545. controls.target.set( 0, 0, 0 );
  546. controls.update();
  547. }
  548. function onWindowResize() {
  549. getScreenDimensions();
  550. renderer.setSize( screenWidth, screenHeight );
  551. resetCamera();
  552. }
  553. var flickerPointLight = ( function() {
  554. var lastAdjuster;
  555. return function flickerPointLight() {
  556. var adjuster = ( Math.random() - 0.5 );
  557. if ( lastAdjuster ) {
  558. diff = ( adjuster - lastAdjuster ) * .2;
  559. adjuster = lastAdjuster + diff;
  560. }
  561. var intensity = 4;
  562. intensity += adjuster * 4;
  563. pointLight.intensity = intensity;
  564. pointLight.distance = adjuster * 50 + 200;
  565. pointLight.decay = adjuster * 5 + 3;
  566. lastAdjuster = adjuster;
  567. }
  568. } )();
  569. function updateSmokeType() {
  570. particleSystems[ ParticleSystemIDs.Smoke1 ].deactivate();
  571. particleSystems[ ParticleSystemIDs.Smoke2 ].deactivate();
  572. if ( smokeActive ) {
  573. particleSystems[ smokeType ].activate();
  574. }
  575. }
  576. function toggleParticleSystem( id ) {
  577. if ( particleSystems[ id ] ) {
  578. if ( particleSystems[ id ].isActive ) {
  579. particleSystems[ id ].deactivate();
  580. } else {
  581. particleSystems[ id ].activate();
  582. }
  583. }
  584. }
  585. function startParticleSystemEnvironment( id ) {
  586. resetCamera();
  587. Object.keys( particleSystems ).forEach( function( key ) {
  588. var system = particleSystems[ key ];
  589. system.deactivate();
  590. } );
  591. currentEnvironmentID = id;
  592. if ( id == ParticleEnvironmentIDs.Campfire ) {
  593. smokeActive = true;
  594. particleSystems[ ParticleSystemIDs.Flame ].activate();
  595. particleSystems[ ParticleSystemIDs.FlameEmbers ].activate();
  596. updateSmokeType();
  597. pointLight.distance = 300;
  598. pointLight.intensity = 6;
  599. pointLight.color.setRGB( 1, .8, .4 );
  600. pointLight.decay = 2;
  601. pointLight.position.set( 0, 40, 0 );
  602. ambientLight.color.setRGB( .08, .08, .08 );
  603. } else {
  604. return;
  605. }
  606. }
  607. function getScreenDimensions() {
  608. screenWidth = window.innerWidth;
  609. screenHeight = window.innerHeight;
  610. }
  611. function resetCamera() {
  612. getScreenDimensions();
  613. camera.aspect = screenWidth / screenHeight;
  614. camera.updateProjectionMatrix();
  615. camera.position.set( 0, 200, 400 );
  616. camera.lookAt( scene.position );
  617. }
  618. function updateParticleSystems() {
  619. var deltaTime = clock.getDelta();
  620. Object.keys( particleSystems ).forEach( function( key ) {
  621. var system = particleSystems[ key ];
  622. if ( system.isActive ) {
  623. system.update( deltaTime );
  624. }
  625. } );
  626. if ( currentEnvironmentID == ParticleEnvironmentIDs.Campfire ) {
  627. flickerPointLight();
  628. }
  629. }
  630. function animate() {
  631. requestAnimationFrame( animate );
  632. update();
  633. render();
  634. }
  635. function update() {
  636. var time = performance.now() * 0.001;
  637. //particleSystemsParent.position.x = Math.sin( time ) * 49;
  638. //particleSystemsParent.position.z = Math.sin( time * 1.2 ) * 49;
  639. controls.update();
  640. stats.update();
  641. updateParticleSystems();
  642. }
  643. function render() {
  644. renderer.render( scene, camera );
  645. }
  646. </script>
  647. </body>
  648. </html>