TiltLoader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. DoubleSide,
  5. FileLoader,
  6. Group,
  7. Loader,
  8. Mesh,
  9. MeshBasicMaterial,
  10. RawShaderMaterial,
  11. TextureLoader,
  12. Quaternion,
  13. Vector3
  14. } from 'three';
  15. import * as fflate from '../libs/fflate.module.js';
  16. class TiltLoader extends Loader {
  17. load( url, onLoad, onProgress, onError ) {
  18. const scope = this;
  19. const loader = new FileLoader( this.manager );
  20. loader.setPath( this.path );
  21. loader.setResponseType( 'arraybuffer' );
  22. loader.setWithCredentials( this.withCredentials );
  23. loader.load( url, function ( buffer ) {
  24. try {
  25. onLoad( scope.parse( buffer ) );
  26. } catch ( e ) {
  27. if ( onError ) {
  28. onError( e );
  29. } else {
  30. console.error( e );
  31. }
  32. scope.manager.itemError( url );
  33. }
  34. }, onProgress, onError );
  35. }
  36. parse( buffer ) {
  37. const group = new Group();
  38. // https://docs.google.com/document/d/11ZsHozYn9FnWG7y3s3WAyKIACfbfwb4PbaS8cZ_xjvo/edit#
  39. const zip = fflate.unzipSync( new Uint8Array( buffer.slice( 16 ) ) );
  40. /*
  41. const thumbnail = zip[ 'thumbnail.png' ].buffer;
  42. const img = document.createElement( 'img' );
  43. img.src = URL.createObjectURL( new Blob( [ thumbnail ] ) );
  44. document.body.appendChild( img );
  45. */
  46. const metadata = JSON.parse( fflate.strFromU8( zip[ 'metadata.json' ] ) );
  47. /*
  48. const blob = new Blob( [ zip[ 'data.sketch' ].buffer ], { type: 'application/octet-stream' } );
  49. window.open( URL.createObjectURL( blob ) );
  50. */
  51. const data = new DataView( zip[ 'data.sketch' ].buffer );
  52. const num_strokes = data.getInt32( 16, true );
  53. const brushes = {};
  54. let offset = 20;
  55. for ( let i = 0; i < num_strokes; i ++ ) {
  56. const brush_index = data.getInt32( offset, true );
  57. const brush_color = [
  58. data.getFloat32( offset + 4, true ),
  59. data.getFloat32( offset + 8, true ),
  60. data.getFloat32( offset + 12, true ),
  61. data.getFloat32( offset + 16, true )
  62. ];
  63. const brush_size = data.getFloat32( offset + 20, true );
  64. const stroke_mask = data.getUint32( offset + 24, true );
  65. const controlpoint_mask = data.getUint32( offset + 28, true );
  66. let offset_stroke_mask = 0;
  67. let offset_controlpoint_mask = 0;
  68. for ( let j = 0; j < 4; j ++ ) {
  69. // TOFIX: I don't understand these masks yet
  70. const byte = 1 << j;
  71. if ( ( stroke_mask & byte ) > 0 ) offset_stroke_mask += 4;
  72. if ( ( controlpoint_mask & byte ) > 0 ) offset_controlpoint_mask += 4;
  73. }
  74. // console.log( { brush_index, brush_color, brush_size, stroke_mask, controlpoint_mask } );
  75. // console.log( offset_stroke_mask, offset_controlpoint_mask );
  76. offset = offset + 28 + offset_stroke_mask + 4; // TOFIX: This is wrong
  77. const num_control_points = data.getInt32( offset, true );
  78. // console.log( { num_control_points } );
  79. const positions = new Float32Array( num_control_points * 3 );
  80. const quaternions = new Float32Array( num_control_points * 4 );
  81. offset = offset + 4;
  82. for ( let j = 0, k = 0; j < positions.length; j += 3, k += 4 ) {
  83. positions[ j + 0 ] = data.getFloat32( offset + 0, true );
  84. positions[ j + 1 ] = data.getFloat32( offset + 4, true );
  85. positions[ j + 2 ] = data.getFloat32( offset + 8, true );
  86. quaternions[ k + 0 ] = data.getFloat32( offset + 12, true );
  87. quaternions[ k + 1 ] = data.getFloat32( offset + 16, true );
  88. quaternions[ k + 2 ] = data.getFloat32( offset + 20, true );
  89. quaternions[ k + 3 ] = data.getFloat32( offset + 24, true );
  90. offset = offset + 28 + offset_controlpoint_mask; // TOFIX: This is wrong
  91. }
  92. if ( brush_index in brushes === false ) {
  93. brushes[ brush_index ] = [];
  94. }
  95. brushes[ brush_index ].push( [ positions, quaternions, brush_size, brush_color ] );
  96. }
  97. for ( const brush_index in brushes ) {
  98. const geometry = new StrokeGeometry( brushes[ brush_index ] );
  99. const material = getMaterial( metadata.BrushIndex[ brush_index ] );
  100. group.add( new Mesh( geometry, material ) );
  101. }
  102. return group;
  103. }
  104. }
  105. class StrokeGeometry extends BufferGeometry {
  106. constructor( strokes ) {
  107. super();
  108. const vertices = [];
  109. const colors = [];
  110. const uvs = [];
  111. const position = new Vector3();
  112. const prevPosition = new Vector3();
  113. const quaternion = new Quaternion();
  114. const prevQuaternion = new Quaternion();
  115. const vector1 = new Vector3();
  116. const vector2 = new Vector3();
  117. const vector3 = new Vector3();
  118. const vector4 = new Vector3();
  119. // size = size / 2;
  120. for ( const k in strokes ) {
  121. const stroke = strokes[ k ];
  122. const positions = stroke[ 0 ];
  123. const quaternions = stroke[ 1 ];
  124. const size = stroke[ 2 ];
  125. const color = stroke[ 3 ];
  126. prevPosition.fromArray( positions, 0 );
  127. prevQuaternion.fromArray( quaternions, 0 );
  128. for ( let i = 3, j = 4, l = positions.length; i < l; i += 3, j += 4 ) {
  129. position.fromArray( positions, i );
  130. quaternion.fromArray( quaternions, j );
  131. vector1.set( - size, 0, 0 );
  132. vector1.applyQuaternion( quaternion );
  133. vector1.add( position );
  134. vector2.set( size, 0, 0 );
  135. vector2.applyQuaternion( quaternion );
  136. vector2.add( position );
  137. vector3.set( size, 0, 0 );
  138. vector3.applyQuaternion( prevQuaternion );
  139. vector3.add( prevPosition );
  140. vector4.set( - size, 0, 0 );
  141. vector4.applyQuaternion( prevQuaternion );
  142. vector4.add( prevPosition );
  143. vertices.push( vector1.x, vector1.y, - vector1.z );
  144. vertices.push( vector2.x, vector2.y, - vector2.z );
  145. vertices.push( vector4.x, vector4.y, - vector4.z );
  146. vertices.push( vector2.x, vector2.y, - vector2.z );
  147. vertices.push( vector3.x, vector3.y, - vector3.z );
  148. vertices.push( vector4.x, vector4.y, - vector4.z );
  149. prevPosition.copy( position );
  150. prevQuaternion.copy( quaternion );
  151. colors.push( ...color );
  152. colors.push( ...color );
  153. colors.push( ...color );
  154. colors.push( ...color );
  155. colors.push( ...color );
  156. colors.push( ...color );
  157. const p1 = i / l;
  158. const p2 = ( i - 3 ) / l;
  159. uvs.push( p1, 0 );
  160. uvs.push( p1, 1 );
  161. uvs.push( p2, 0 );
  162. uvs.push( p1, 1 );
  163. uvs.push( p2, 1 );
  164. uvs.push( p2, 0 );
  165. }
  166. }
  167. this.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  168. this.setAttribute( 'color', new BufferAttribute( new Float32Array( colors ), 4 ) );
  169. this.setAttribute( 'uv', new BufferAttribute( new Float32Array( uvs ), 2 ) );
  170. }
  171. }
  172. const BRUSH_LIST_ARRAY = {
  173. '89d104cd-d012-426b-b5b3-bbaee63ac43c': 'Bubbles',
  174. '700f3aa8-9a7c-2384-8b8a-ea028905dd8c': 'CelVinyl',
  175. '0f0ff7b2-a677-45eb-a7d6-0cd7206f4816': 'ChromaticWave',
  176. '1161af82-50cf-47db-9706-0c3576d43c43': 'CoarseBristles',
  177. '79168f10-6961-464a-8be1-57ed364c5600': 'CoarseBristlesSingleSided',
  178. '1caa6d7d-f015-3f54-3a4b-8b5354d39f81': 'Comet',
  179. 'c8313697-2563-47fc-832e-290f4c04b901': 'DiamondHull',
  180. '4391aaaa-df73-4396-9e33-31e4e4930b27': 'Disco',
  181. 'd1d991f2-e7a0-4cf1-b328-f57e915e6260': 'DotMarker',
  182. '6a1cf9f9-032c-45ec-9b1d-a6680bee30f7': 'Dots',
  183. '0d3889f3-3ede-470c-8af4-f44813306126': 'DoubleTaperedFlat',
  184. '0d3889f3-3ede-470c-8af4-de4813306126': 'DoubleTaperedMarker',
  185. 'd0262945-853c-4481-9cbd-88586bed93cb': 'DuctTape',
  186. '3ca16e2f-bdcd-4da2-8631-dcef342f40f1': 'DuctTapeSingleSided',
  187. 'f6e85de3-6dcc-4e7f-87fd-cee8c3d25d51': 'Electricity',
  188. '02ffb866-7fb2-4d15-b761-1012cefb1360': 'Embers',
  189. 'cb92b597-94ca-4255-b017-0e3f42f12f9e': 'Fire',
  190. '2d35bcf0-e4d8-452c-97b1-3311be063130': 'Flat',
  191. '55303bc4-c749-4a72-98d9-d23e68e76e18': 'FlatDeprecated',
  192. '280c0a7a-aad8-416c-a7d2-df63d129ca70': 'FlatSingleSided',
  193. 'cf019139-d41c-4eb0-a1d0-5cf54b0a42f3': 'Highlighter',
  194. '6a1cf9f9-032c-45ec-9b6e-a6680bee32e9': 'HyperGrid',
  195. 'dce872c2-7b49-4684-b59b-c45387949c5c': 'Hypercolor',
  196. 'e8ef32b1-baa8-460a-9c2c-9cf8506794f5': 'HypercolorSingleSided',
  197. '2f212815-f4d3-c1a4-681a-feeaf9c6dc37': 'Icing',
  198. 'f5c336cf-5108-4b40-ade9-c687504385ab': 'Ink',
  199. 'c0012095-3ffd-4040-8ee1-fc180d346eaa': 'InkSingleSided',
  200. '4a76a27a-44d8-4bfe-9a8c-713749a499b0': 'Leaves',
  201. 'ea19de07-d0c0-4484-9198-18489a3c1487': 'LeavesSingleSided',
  202. '2241cd32-8ba2-48a5-9ee7-2caef7e9ed62': 'Light',
  203. '4391aaaa-df81-4396-9e33-31e4e4930b27': 'LightWire',
  204. 'd381e0f5-3def-4a0d-8853-31e9200bcbda': 'Lofted',
  205. '429ed64a-4e97-4466-84d3-145a861ef684': 'Marker',
  206. '79348357-432d-4746-8e29-0e25c112e3aa': 'MatteHull',
  207. 'b2ffef01-eaaa-4ab5-aa64-95a2c4f5dbc6': 'NeonPulse',
  208. 'f72ec0e7-a844-4e38-82e3-140c44772699': 'OilPaint',
  209. 'c515dad7-4393-4681-81ad-162ef052241b': 'OilPaintSingleSided',
  210. 'f1114e2e-eb8d-4fde-915a-6e653b54e9f5': 'Paper',
  211. '759f1ebd-20cd-4720-8d41-234e0da63716': 'PaperSingleSided',
  212. 'e0abbc80-0f80-e854-4970-8924a0863dcc': 'Petal',
  213. 'c33714d1-b2f9-412e-bd50-1884c9d46336': 'Plasma',
  214. 'ad1ad437-76e2-450d-a23a-e17f8310b960': 'Rainbow',
  215. 'faaa4d44-fcfb-4177-96be-753ac0421ba3': 'ShinyHull',
  216. '70d79cca-b159-4f35-990c-f02193947fe8': 'Smoke',
  217. 'd902ed8b-d0d1-476c-a8de-878a79e3a34c': 'Snow',
  218. 'accb32f5-4509-454f-93f8-1df3fd31df1b': 'SoftHighlighter',
  219. 'cf7f0059-7aeb-53a4-2b67-c83d863a9ffa': 'Spikes',
  220. '8dc4a70c-d558-4efd-a5ed-d4e860f40dc3': 'Splatter',
  221. '7a1c8107-50c5-4b70-9a39-421576d6617e': 'SplatterSingleSided',
  222. '0eb4db27-3f82-408d-b5a1-19ebd7d5b711': 'Stars',
  223. '44bb800a-fbc3-4592-8426-94ecb05ddec3': 'Streamers',
  224. '0077f88c-d93a-42f3-b59b-b31c50cdb414': 'Taffy',
  225. 'b468c1fb-f254-41ed-8ec9-57030bc5660c': 'TaperedFlat',
  226. 'c8ccb53d-ae13-45ef-8afb-b730d81394eb': 'TaperedFlatSingleSided',
  227. 'd90c6ad8-af0f-4b54-b422-e0f92abe1b3c': 'TaperedMarker',
  228. '1a26b8c0-8a07-4f8a-9fac-d2ef36e0cad0': 'TaperedMarker_Flat',
  229. '75b32cf0-fdd6-4d89-a64b-e2a00b247b0f': 'ThickPaint',
  230. 'fdf0326a-c0d1-4fed-b101-9db0ff6d071f': 'ThickPaintSingleSided',
  231. '4391385a-df73-4396-9e33-31e4e4930b27': 'Toon',
  232. 'a8fea537-da7c-4d4b-817f-24f074725d6d': 'UnlitHull',
  233. 'd229d335-c334-495a-a801-660ac8a87360': 'VelvetInk',
  234. '10201aa3-ebc2-42d8-84b7-2e63f6eeb8ab': 'Waveform',
  235. 'b67c0e81-ce6d-40a8-aeb0-ef036b081aa3': 'WetPaint',
  236. 'dea67637-cd1a-27e4-c9b1-52f4bbcb84e5': 'WetPaintSingleSided',
  237. '5347acf0-a8e2-47b6-8346-30c70719d763': 'WigglyGraphite',
  238. 'e814fef1-97fd-7194-4a2f-50c2bb918be2': 'WigglyGraphiteSingleSided',
  239. '4391385a-cf83-4396-9e33-31e4e4930b27': 'Wire'
  240. };
  241. const common = {
  242. 'colors': {
  243. 'BloomColor': `
  244. vec3 BloomColor(vec3 color, float gain) {
  245. // Guarantee that there's at least a little bit of all 3 channels.
  246. // This makes fully-saturated strokes (which only have 2 non-zero
  247. // color channels) eventually clip to white rather than to a secondary.
  248. float cmin = length(color.rgb) * .05;
  249. color.rgb = max(color.rgb, vec3(cmin, cmin, cmin));
  250. // If we try to remove this pow() from .a, it brightens up
  251. // pressure-sensitive strokes; looks better as-is.
  252. color = pow(color, vec3(2.2));
  253. color.rgb *= 2. * exp(gain * 10.);
  254. return color;
  255. }
  256. `,
  257. 'LinearToSrgb': `
  258. vec3 LinearToSrgb(vec3 color) {
  259. // Approximation http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
  260. vec3 linearColor = color.rgb;
  261. vec3 S1 = sqrt(linearColor);
  262. vec3 S2 = sqrt(S1);
  263. vec3 S3 = sqrt(S2);
  264. color.rgb = 0.662002687 * S1 + 0.684122060 * S2 - 0.323583601 * S3 - 0.0225411470 * linearColor;
  265. return color;
  266. }
  267. `,
  268. 'hsv': `
  269. // uniform sampler2D lookupTex;
  270. vec4 lookup(vec4 textureColor) {
  271. return textureColor;
  272. }
  273. vec3 lookup(vec3 textureColor) {
  274. return textureColor;
  275. }
  276. vec3 hsv2rgb( vec3 hsv ) {
  277. vec3 rgb = clamp( abs(mod(hsv.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
  278. return hsv.z * mix( vec3(1.0), rgb, hsv.y);
  279. }
  280. vec3 rgb2hsv( vec3 rgb ) {
  281. vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  282. vec4 p = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));
  283. vec4 q = mix(vec4(p.xyw, rgb.r), vec4(rgb.r, p.yzx), step(p.x, rgb.r));
  284. float d = q.x - min(q.w, q.y);
  285. float e = 1.0e-10;
  286. return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
  287. }
  288. `,
  289. 'SrgbToLinear': `
  290. vec3 SrgbToLinear(vec3 color) {
  291. // Approximation http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
  292. vec3 sRGB = color.rgb;
  293. color.rgb = sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);
  294. return color;
  295. }
  296. `
  297. }
  298. };
  299. let shaders = null;
  300. function getShaders() {
  301. if ( shaders === null ) {
  302. const loader = new TextureLoader().setPath( './textures/tiltbrush/' );
  303. shaders = {
  304. 'Light': {
  305. uniforms: {
  306. mainTex: { value: loader.load( 'Light.webp' ) },
  307. alphaTest: { value: 0.067 },
  308. emission_gain: { value: 0.45 },
  309. alpha: { value: 1 },
  310. },
  311. vertexShader: `
  312. precision highp float;
  313. precision highp int;
  314. attribute vec2 uv;
  315. attribute vec4 color;
  316. attribute vec3 position;
  317. uniform mat4 modelMatrix;
  318. uniform mat4 modelViewMatrix;
  319. uniform mat4 projectionMatrix;
  320. uniform mat4 viewMatrix;
  321. uniform mat3 normalMatrix;
  322. uniform vec3 cameraPosition;
  323. varying vec2 vUv;
  324. varying vec3 vColor;
  325. ${ common.colors.LinearToSrgb }
  326. ${ common.colors.hsv }
  327. void main() {
  328. vUv = uv;
  329. vColor = lookup(color.rgb);
  330. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  331. gl_Position = projectionMatrix * mvPosition;
  332. }
  333. `,
  334. fragmentShader: `
  335. precision highp float;
  336. precision highp int;
  337. uniform float emission_gain;
  338. uniform sampler2D mainTex;
  339. uniform float alphaTest;
  340. varying vec2 vUv;
  341. varying vec3 vColor;
  342. ${ common.colors.BloomColor }
  343. ${ common.colors.SrgbToLinear }
  344. void main(){
  345. vec4 col = texture2D(mainTex, vUv);
  346. vec3 color = vColor;
  347. color = BloomColor(color, emission_gain);
  348. color = color * col.rgb;
  349. color = color * col.a;
  350. color = SrgbToLinear(color);
  351. gl_FragColor = vec4(color, 1.0);
  352. }
  353. `,
  354. side: 2,
  355. transparent: true,
  356. depthFunc: 2,
  357. depthWrite: true,
  358. depthTest: false,
  359. blending: 5,
  360. blendDst: 201,
  361. blendDstAlpha: 201,
  362. blendEquation: 100,
  363. blendEquationAlpha: 100,
  364. blendSrc: 201,
  365. blendSrcAlpha: 201,
  366. }
  367. };
  368. }
  369. return shaders;
  370. }
  371. function getMaterial( GUID ) {
  372. const name = BRUSH_LIST_ARRAY[ GUID ];
  373. switch ( name ) {
  374. case 'Light':
  375. return new RawShaderMaterial( getShaders().Light );
  376. default:
  377. return new MeshBasicMaterial( { vertexColors: true, side: DoubleSide } );
  378. }
  379. }
  380. export { TiltLoader };