2
0

webgl_particles_general.html 22 KB

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