webgpu_materials_toon.html 5.3 KB

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