webgl_decals.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - decal splatter</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> - decal splatter<br/>
  13. click to shoot
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. import { GUI } from './jsm/libs/dat.gui.module.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  21. import { DecalGeometry } from './jsm/geometries/DecalGeometry.js';
  22. var container = document.getElementById( 'container' );
  23. var renderer, scene, camera, stats;
  24. var mesh;
  25. var raycaster;
  26. var line;
  27. var intersection = {
  28. intersects: false,
  29. point: new THREE.Vector3(),
  30. normal: new THREE.Vector3()
  31. };
  32. var mouse = new THREE.Vector2();
  33. var textureLoader = new THREE.TextureLoader();
  34. var decalDiffuse = textureLoader.load( 'textures/decal/decal-diffuse.png' );
  35. var decalNormal = textureLoader.load( 'textures/decal/decal-normal.jpg' );
  36. var decalMaterial = new THREE.MeshPhongMaterial( {
  37. specular: 0x444444,
  38. map: decalDiffuse,
  39. normalMap: decalNormal,
  40. normalScale: new THREE.Vector2( 1, 1 ),
  41. shininess: 30,
  42. transparent: true,
  43. depthTest: true,
  44. depthWrite: false,
  45. polygonOffset: true,
  46. polygonOffsetFactor: - 4,
  47. wireframe: false
  48. } );
  49. var decals = [];
  50. var mouseHelper;
  51. var position = new THREE.Vector3();
  52. var orientation = new THREE.Euler();
  53. var size = new THREE.Vector3( 10, 10, 10 );
  54. var params = {
  55. minScale: 10,
  56. maxScale: 20,
  57. rotate: true,
  58. clear: function () {
  59. removeDecals();
  60. }
  61. };
  62. window.addEventListener( 'load', init );
  63. function init() {
  64. renderer = new THREE.WebGLRenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. container.appendChild( renderer.domElement );
  68. stats = new Stats();
  69. container.appendChild( stats.dom );
  70. scene = new THREE.Scene();
  71. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  72. camera.position.z = 120;
  73. camera.target = new THREE.Vector3();
  74. var controls = new OrbitControls( camera, renderer.domElement );
  75. controls.minDistance = 50;
  76. controls.maxDistance = 200;
  77. scene.add( new THREE.AmbientLight( 0x443333 ) );
  78. var light = new THREE.DirectionalLight( 0xffddcc, 1 );
  79. light.position.set( 1, 0.75, 0.5 );
  80. scene.add( light );
  81. var light = new THREE.DirectionalLight( 0xccccff, 1 );
  82. light.position.set( - 1, 0.75, - 0.5 );
  83. scene.add( light );
  84. var geometry = new THREE.BufferGeometry();
  85. geometry.setFromPoints( [ new THREE.Vector3(), new THREE.Vector3() ] );
  86. line = new THREE.Line( geometry, new THREE.LineBasicMaterial() );
  87. scene.add( line );
  88. loadLeePerrySmith();
  89. raycaster = new THREE.Raycaster();
  90. mouseHelper = new THREE.Mesh( new THREE.BoxBufferGeometry( 1, 1, 10 ), new THREE.MeshNormalMaterial() );
  91. mouseHelper.visible = false;
  92. scene.add( mouseHelper );
  93. window.addEventListener( 'resize', onWindowResize, false );
  94. var moved = false;
  95. controls.addEventListener( 'change', function () {
  96. moved = true;
  97. } );
  98. window.addEventListener( 'mousedown', function () {
  99. moved = false;
  100. }, false );
  101. window.addEventListener( 'mouseup', function () {
  102. checkIntersection();
  103. if ( ! moved && intersection.intersects ) shoot();
  104. } );
  105. window.addEventListener( 'mousemove', onTouchMove );
  106. window.addEventListener( 'touchmove', onTouchMove );
  107. function onTouchMove( event ) {
  108. var x, y;
  109. if ( event.changedTouches ) {
  110. x = event.changedTouches[ 0 ].pageX;
  111. y = event.changedTouches[ 0 ].pageY;
  112. } else {
  113. x = event.clientX;
  114. y = event.clientY;
  115. }
  116. mouse.x = ( x / window.innerWidth ) * 2 - 1;
  117. mouse.y = - ( y / window.innerHeight ) * 2 + 1;
  118. checkIntersection();
  119. }
  120. function checkIntersection() {
  121. if ( ! mesh ) return;
  122. raycaster.setFromCamera( mouse, camera );
  123. var intersects = raycaster.intersectObjects( [ mesh ] );
  124. if ( intersects.length > 0 ) {
  125. var p = intersects[ 0 ].point;
  126. mouseHelper.position.copy( p );
  127. intersection.point.copy( p );
  128. var n = intersects[ 0 ].face.normal.clone();
  129. n.transformDirection( mesh.matrixWorld );
  130. n.multiplyScalar( 10 );
  131. n.add( intersects[ 0 ].point );
  132. intersection.normal.copy( intersects[ 0 ].face.normal );
  133. mouseHelper.lookAt( n );
  134. var positions = line.geometry.attributes.position;
  135. positions.setXYZ( 0, p.x, p.y, p.z );
  136. positions.setXYZ( 1, n.x, n.y, n.z );
  137. positions.needsUpdate = true;
  138. intersection.intersects = true;
  139. } else {
  140. intersection.intersects = false;
  141. }
  142. }
  143. var gui = new GUI();
  144. gui.add( params, 'minScale', 1, 30 );
  145. gui.add( params, 'maxScale', 1, 30 );
  146. gui.add( params, 'rotate' );
  147. gui.add( params, 'clear' );
  148. gui.open();
  149. onWindowResize();
  150. animate();
  151. }
  152. function loadLeePerrySmith() {
  153. var loader = new GLTFLoader();
  154. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  155. mesh = gltf.scene.children[ 0 ];
  156. mesh.material = new THREE.MeshPhongMaterial( {
  157. specular: 0x111111,
  158. map: textureLoader.load( 'models/gltf/LeePerrySmith/Map-COL.jpg' ),
  159. specularMap: textureLoader.load( 'models/gltf/LeePerrySmith/Map-SPEC.jpg' ),
  160. normalMap: textureLoader.load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg' ),
  161. shininess: 25
  162. } );
  163. scene.add( mesh );
  164. mesh.scale.set( 10, 10, 10 );
  165. } );
  166. }
  167. function shoot() {
  168. position.copy( intersection.point );
  169. orientation.copy( mouseHelper.rotation );
  170. if ( params.rotate ) orientation.z = Math.random() * 2 * Math.PI;
  171. var scale = params.minScale + Math.random() * ( params.maxScale - params.minScale );
  172. size.set( scale, scale, scale );
  173. var material = decalMaterial.clone();
  174. material.color.setHex( Math.random() * 0xffffff );
  175. var m = new THREE.Mesh( new DecalGeometry( mesh, position, orientation, size ), material );
  176. decals.push( m );
  177. scene.add( m );
  178. }
  179. function removeDecals() {
  180. decals.forEach( function ( d ) {
  181. scene.remove( d );
  182. } );
  183. decals = [];
  184. }
  185. function onWindowResize() {
  186. camera.aspect = window.innerWidth / window.innerHeight;
  187. camera.updateProjectionMatrix();
  188. renderer.setSize( window.innerWidth, window.innerHeight );
  189. }
  190. function animate() {
  191. requestAnimationFrame( animate );
  192. renderer.render( scene, camera );
  193. stats.update();
  194. }
  195. </script>
  196. </body>
  197. </html>