webgl_sprites_nodes.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - sprites nodes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Node-Based Sprites
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { NodeMaterialLoader, NodeMaterialLoaderUtils } from './jsm/loaders/NodeMaterialLoader.js';
  18. import {
  19. NodeFrame,
  20. SpriteNodeMaterial,
  21. MathNode,
  22. OperatorNode,
  23. TextureNode,
  24. Vector2Node,
  25. TimerNode,
  26. FunctionNode,
  27. FunctionCallNode,
  28. PositionNode,
  29. UVNode
  30. } from './jsm/nodes/Nodes.js';
  31. var container = document.getElementById( 'container' );
  32. var renderer, scene, camera, clock = new THREE.Clock(), fov = 50;
  33. var frame = new NodeFrame();
  34. var sprite1, sprite2, sprite3;
  35. var walkingManTexture, walkingManTextureURL;
  36. var library = {};
  37. var controls;
  38. window.addEventListener( 'load', init );
  39. function init() {
  40. //
  41. // Renderer / Controls
  42. //
  43. renderer = new THREE.WebGLRenderer( { antialias: true } );
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. container.appendChild( renderer.domElement );
  47. scene = new THREE.Scene();
  48. scene.fog = new THREE.Fog( 0x0000FF, 70, 150 );
  49. camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
  50. camera.position.z = 100;
  51. camera.target = new THREE.Vector3();
  52. controls = new OrbitControls( camera, renderer.domElement );
  53. controls.minDistance = 50;
  54. controls.maxDistance = 200;
  55. //
  56. // SpriteNode
  57. //
  58. // https://openclipart.org/detail/239883/walking-man-sprite-sheet
  59. walkingManTextureURL = "textures/WalkingManSpriteSheet.png";
  60. walkingManTexture = new THREE.TextureLoader().load( walkingManTextureURL );
  61. walkingManTexture.wrapS = walkingManTexture.wrapT = THREE.RepeatWrapping;
  62. library[ walkingManTextureURL ] = walkingManTexture;
  63. // horizontal sprite-sheet animator
  64. function createHorizontalSpriteSheetNode( hCount, speed ) {
  65. var speed = new Vector2Node( speed, 0 ); // frame per second
  66. var scale = new Vector2Node( 1 / hCount, 1 ); // 8 horizontal images in sprite-sheet
  67. var uvTimer = new OperatorNode(
  68. new TimerNode(),
  69. speed,
  70. OperatorNode.MUL
  71. );
  72. var uvIntegerTimer = new MathNode(
  73. uvTimer,
  74. MathNode.FLOOR
  75. );
  76. var uvFrameOffset = new OperatorNode(
  77. uvIntegerTimer,
  78. scale,
  79. OperatorNode.MUL
  80. );
  81. var uvScale = new OperatorNode(
  82. new UVNode(),
  83. scale,
  84. OperatorNode.MUL
  85. );
  86. var uvFrame = new OperatorNode(
  87. uvScale,
  88. uvFrameOffset,
  89. OperatorNode.ADD
  90. );
  91. return uvFrame;
  92. }
  93. // sprites
  94. var spriteWidth = 20,
  95. spriteHeight = 20;
  96. scene.add( sprite1 = new THREE.Sprite( new SpriteNodeMaterial() ) );
  97. sprite1.scale.x = spriteWidth;
  98. sprite1.scale.y = spriteHeight;
  99. sprite1.material.color = new TextureNode( walkingManTexture );
  100. sprite1.material.color.uv = createHorizontalSpriteSheetNode( 8, 10 );
  101. scene.add( sprite2 = new THREE.Sprite( new SpriteNodeMaterial() ) );
  102. sprite2.position.x = 30;
  103. sprite2.scale.x = spriteWidth;
  104. sprite2.scale.y = spriteHeight;
  105. sprite2.material.color = new TextureNode( walkingManTexture );
  106. sprite2.material.color.uv = createHorizontalSpriteSheetNode( 8, 30 );
  107. sprite2.material.color = new MathNode( sprite2.material.color, MathNode.INVERT );
  108. sprite2.material.spherical = false; // look at camera horizontally only, very used in vegetation
  109. // horizontal zigzag sprite
  110. sprite2.material.position = new OperatorNode(
  111. new OperatorNode(
  112. new MathNode( new TimerNode( 3 ), MathNode.SIN ), // 3 is speed (time scale)
  113. new Vector2Node( .3, 0 ), // horizontal scale (position)
  114. OperatorNode.MUL
  115. ),
  116. new PositionNode(),
  117. OperatorNode.ADD
  118. );
  119. var sineWaveFunction = new FunctionNode( [
  120. // https://stackoverflow.com/questions/36174431/how-to-make-a-wave-warp-effect-in-shader
  121. "vec2 sineWave(vec2 uv, vec2 phase) {",
  122. // wave distortion
  123. " float x = sin( 25.0*uv.y + 30.0*uv.x + 6.28*phase.x) * 0.01;",
  124. " float y = sin( 25.0*uv.y + 30.0*uv.x + 6.28*phase.y) * 0.03;",
  125. " return vec2(uv.x+x, uv.y+y);",
  126. "}"
  127. ].join( "\n" ) );
  128. scene.add( sprite3 = new THREE.Sprite( new SpriteNodeMaterial() ) );
  129. sprite3.position.x = - 30;
  130. sprite3.scale.x = spriteWidth;
  131. sprite3.scale.y = spriteHeight;
  132. sprite3.material.color = new TextureNode( walkingManTexture );
  133. sprite3.material.color.uv = new FunctionCallNode( sineWaveFunction, {
  134. "uv": createHorizontalSpriteSheetNode( 8, 10 ),
  135. "phase": new TimerNode()
  136. } );
  137. sprite3.material.fog = true;
  138. //
  139. // Test serialization
  140. //
  141. spriteToJSON( sprite1 );
  142. spriteToJSON( sprite2 );
  143. spriteToJSON( sprite3 );
  144. //
  145. // Events
  146. //
  147. window.addEventListener( 'resize', onWindowResize, false );
  148. onWindowResize();
  149. animate();
  150. }
  151. function spriteToJSON( sprite ) {
  152. // serialize
  153. var json = sprite.material.toJSON();
  154. // replace uuid to url (facilitates the load of textures using url otherside uuid)
  155. NodeMaterialLoaderUtils.replaceUUID( json, walkingManTexture, walkingManTextureURL );
  156. // deserialize
  157. var material = new NodeMaterialLoader( null, library ).parse( json );
  158. // replace material
  159. sprite.material.dispose();
  160. sprite.material = material;
  161. }
  162. function onWindowResize() {
  163. var width = window.innerWidth, height = window.innerHeight;
  164. camera.aspect = width / height;
  165. camera.updateProjectionMatrix();
  166. renderer.setSize( width, height );
  167. }
  168. function animate() {
  169. var delta = clock.getDelta();
  170. // update material animation and/or gpu calcs (pre-renderer)
  171. frame.update( delta )
  172. .updateNode( sprite1.material )
  173. .updateNode( sprite2.material )
  174. .updateNode( sprite3.material );
  175. // rotate sprite
  176. sprite3.rotation.z -= Math.PI * .005;
  177. renderer.render( scene, camera );
  178. requestAnimationFrame( animate );
  179. }
  180. </script>
  181. </body>
  182. </html>