webgpu_materials_toon.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - toon material</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="example.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  13. <div class="title-wrapper">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Toon Material</span>
  15. </div>
  16. <small>
  17. Toon material and outline post-processing effect.
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three/webgpu';
  32. import { toonOutlinePass } from 'three/tsl';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { FontLoader } from 'three/addons/loaders/FontLoader.js';
  36. import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
  37. let container;
  38. let camera, scene, renderer, postProcessing;
  39. let particleLight;
  40. const loader = new FontLoader();
  41. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  42. init( font );
  43. } );
  44. function init( font ) {
  45. container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2500 );
  48. camera.position.set( 0.0, 400, 400 * 3.5 );
  49. //
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x444488 );
  52. //
  53. renderer = new THREE.WebGPURenderer( { antialias: true } );
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. renderer.setAnimationLoop( render );
  57. renderer.inspector = new Inspector();
  58. container.appendChild( renderer.domElement );
  59. //
  60. postProcessing = new THREE.PostProcessing( renderer );
  61. postProcessing.outputNode = toonOutlinePass( scene, camera );
  62. // Materials
  63. const cubeWidth = 400;
  64. const numberOfSpheresPerSide = 5;
  65. const sphereRadius = ( cubeWidth / numberOfSpheresPerSide ) * 0.8 * 0.5;
  66. const stepSize = 1.0 / numberOfSpheresPerSide;
  67. const geometry = new THREE.SphereGeometry( sphereRadius, 32, 16 );
  68. for ( let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  69. const colors = new Uint8Array( alphaIndex + 2 );
  70. for ( let c = 0; c <= colors.length; c ++ ) {
  71. colors[ c ] = ( c / colors.length ) * 256;
  72. }
  73. const gradientMap = new THREE.DataTexture( colors, colors.length, 1, THREE.RedFormat );
  74. gradientMap.needsUpdate = true;
  75. for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
  76. for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  77. // basic monochromatic energy preservation
  78. const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
  79. const material = new THREE.MeshToonNodeMaterial( {
  80. color: diffuseColor,
  81. gradientMap: gradientMap
  82. } );
  83. const mesh = new THREE.Mesh( geometry, material );
  84. mesh.position.x = alpha * 400 - 200;
  85. mesh.position.y = beta * 400 - 200;
  86. mesh.position.z = gamma * 400 - 200;
  87. scene.add( mesh );
  88. }
  89. }
  90. }
  91. function addLabel( name, location ) {
  92. const textGeo = new TextGeometry( name, {
  93. font: font,
  94. size: 20,
  95. depth: 1,
  96. curveSegments: 1
  97. } );
  98. const textMaterial = new THREE.MeshBasicNodeMaterial();
  99. const textMesh = new THREE.Mesh( textGeo, textMaterial );
  100. textMesh.position.copy( location );
  101. scene.add( textMesh );
  102. }
  103. addLabel( '-gradientMap', new THREE.Vector3( - 350, 0, 0 ) );
  104. addLabel( '+gradientMap', new THREE.Vector3( 350, 0, 0 ) );
  105. addLabel( '-diffuse', new THREE.Vector3( 0, 0, - 300 ) );
  106. addLabel( '+diffuse', new THREE.Vector3( 0, 0, 300 ) );
  107. particleLight = new THREE.Mesh(
  108. new THREE.SphereGeometry( 4, 8, 8 ),
  109. new THREE.MeshBasicNodeMaterial( { color: 0xffffff } )
  110. );
  111. scene.add( particleLight );
  112. // Lights
  113. scene.add( new THREE.AmbientLight( 0xc1c1c1, 3 ) );
  114. const pointLight = new THREE.PointLight( 0xffffff, 2, 800, 0 );
  115. particleLight.add( pointLight );
  116. //
  117. const controls = new OrbitControls( camera, renderer.domElement );
  118. controls.minDistance = 200;
  119. controls.maxDistance = 2000;
  120. window.addEventListener( 'resize', onWindowResize );
  121. }
  122. function onWindowResize() {
  123. camera.aspect = window.innerWidth / window.innerHeight;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. }
  127. //
  128. function render() {
  129. const timer = Date.now() * 0.00025;
  130. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  131. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  132. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  133. postProcessing.render();
  134. }
  135. </script>
  136. </body>
  137. </html>
粤ICP备19079148号