super-three 0.170.1 → 0.172.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (413) hide show
  1. package/README.md +1 -1
  2. package/build/three.cjs +157 -49
  3. package/build/three.core.js +104 -42
  4. package/build/three.core.min.js +1 -1
  5. package/build/three.module.js +53 -8
  6. package/build/three.module.min.js +1 -1
  7. package/build/three.tsl.js +10 -5
  8. package/build/three.tsl.min.js +1 -1
  9. package/build/three.webgpu.js +21191 -1008
  10. package/build/three.webgpu.min.js +1 -1
  11. package/build/three.webgpu.nodes.js +21189 -1008
  12. package/build/three.webgpu.nodes.min.js +1 -1
  13. package/examples/jsm/Addons.js +0 -5
  14. package/examples/jsm/animation/CCDIKSolver.js +3 -2
  15. package/examples/jsm/controls/ArcballControls.js +28 -19
  16. package/examples/jsm/controls/OrbitControls.js +5 -4
  17. package/examples/jsm/csm/CSMShadowNode.js +16 -11
  18. package/examples/jsm/curves/NURBSCurve.js +1 -1
  19. package/examples/jsm/curves/NURBSUtils.js +3 -0
  20. package/examples/jsm/effects/OutlineEffect.js +1 -1
  21. package/examples/jsm/exporters/GLTFExporter.js +11 -7
  22. package/examples/jsm/exporters/KTX2Exporter.js +1 -1
  23. package/examples/jsm/exporters/PLYExporter.js +2 -2
  24. package/examples/jsm/helpers/TextureHelperGPU.js +15 -5
  25. package/examples/jsm/helpers/VertexNormalsHelper.js +2 -0
  26. package/examples/jsm/lights/LightProbeGenerator.js +3 -3
  27. package/examples/jsm/lines/webgpu/LineSegments2.js +15 -6
  28. package/examples/jsm/loaders/3MFLoader.js +6 -3
  29. package/examples/jsm/loaders/BVHLoader.js +1 -1
  30. package/examples/jsm/loaders/ColladaLoader.js +5 -5
  31. package/examples/jsm/loaders/DDSLoader.js +42 -0
  32. package/examples/jsm/loaders/GCodeLoader.js +1 -1
  33. package/examples/jsm/loaders/GLTFLoader.js +22 -2
  34. package/examples/jsm/loaders/KTX2Loader.js +6 -1
  35. package/examples/jsm/loaders/KTXLoader.js +4 -4
  36. package/examples/jsm/loaders/LDrawLoader.js +1 -1
  37. package/examples/jsm/loaders/LottieLoader.js +1 -1
  38. package/examples/jsm/loaders/MTLLoader.js +1 -0
  39. package/examples/jsm/loaders/NRRDLoader.js +1 -1
  40. package/examples/jsm/loaders/PLYLoader.js +7 -7
  41. package/examples/jsm/loaders/PVRLoader.js +1 -1
  42. package/examples/jsm/loaders/SVGLoader.js +2 -2
  43. package/examples/jsm/loaders/TDSLoader.js +1 -1
  44. package/examples/jsm/loaders/VRMLLoader.js +7 -7
  45. package/examples/jsm/loaders/lwo/IFFParser.js +4 -4
  46. package/examples/jsm/materials/LDrawConditionalLineNodeMaterial.js +1 -1
  47. package/examples/jsm/math/ConvexHull.js +1 -1
  48. package/examples/jsm/math/OBB.js +17 -0
  49. package/examples/jsm/misc/ProgressiveLightMapGPU.js +10 -10
  50. package/examples/jsm/misc/Volume.js +20 -12
  51. package/examples/jsm/misc/VolumeSlice.js +2 -2
  52. package/examples/jsm/modifiers/CurveModifier.js +10 -8
  53. package/examples/jsm/modifiers/CurveModifierGPU.js +9 -7
  54. package/examples/jsm/modifiers/SimplifyModifier.js +2 -2
  55. package/examples/jsm/objects/GroundedSkybox.js +4 -4
  56. package/examples/jsm/objects/Sky.js +2 -2
  57. package/examples/jsm/objects/SkyMesh.js +3 -3
  58. package/examples/jsm/objects/Water2.js +1 -1
  59. package/examples/jsm/objects/Water2Mesh.js +2 -2
  60. package/examples/jsm/objects/WaterMesh.js +4 -4
  61. package/examples/jsm/postprocessing/BloomPass.js +2 -2
  62. package/examples/jsm/postprocessing/EffectComposer.js +1 -2
  63. package/examples/jsm/postprocessing/OutlinePass.js +4 -4
  64. package/examples/jsm/postprocessing/SAOPass.js +1 -2
  65. package/examples/jsm/postprocessing/SMAAPass.js +1 -3
  66. package/examples/jsm/postprocessing/SSAOPass.js +2 -4
  67. package/examples/jsm/postprocessing/SSRPass.js +2 -4
  68. package/examples/jsm/postprocessing/UnrealBloomPass.js +2 -2
  69. package/examples/jsm/renderers/SVGRenderer.js +6 -4
  70. package/examples/jsm/shaders/BokehShader2.js +1 -1
  71. package/examples/jsm/shaders/GodRaysShader.js +3 -3
  72. package/examples/jsm/shaders/SobelOperatorShader.js +1 -1
  73. package/examples/jsm/tsl/display/AfterImageNode.js +93 -4
  74. package/examples/jsm/tsl/display/AnaglyphPassNode.js +46 -2
  75. package/examples/jsm/tsl/display/AnamorphicNode.js +118 -5
  76. package/examples/jsm/tsl/display/BleachBypass.js +10 -0
  77. package/examples/jsm/tsl/display/BloomNode.js +233 -26
  78. package/examples/jsm/tsl/display/DenoiseNode.js +141 -25
  79. package/examples/jsm/tsl/display/DepthOfFieldNode.js +80 -1
  80. package/examples/jsm/tsl/display/DotScreenNode.js +50 -5
  81. package/examples/jsm/tsl/display/FXAANode.js +52 -3
  82. package/examples/jsm/tsl/display/FilmNode.js +47 -1
  83. package/examples/jsm/tsl/display/GTAONode.js +210 -14
  84. package/examples/jsm/tsl/display/GaussianBlurNode.js +152 -8
  85. package/examples/jsm/tsl/display/LensflareNode.js +131 -13
  86. package/examples/jsm/tsl/display/Lut3DNode.js +55 -1
  87. package/examples/jsm/tsl/display/MotionBlur.js +12 -2
  88. package/examples/jsm/tsl/display/OutlineNode.js +343 -26
  89. package/examples/jsm/tsl/display/ParallaxBarrierPassNode.js +36 -2
  90. package/examples/jsm/tsl/display/PixelationPassNode.js +142 -9
  91. package/examples/jsm/tsl/display/RGBShiftNode.js +49 -2
  92. package/examples/jsm/tsl/display/SMAANode.js +187 -46
  93. package/examples/jsm/tsl/display/SSAAPassNode.js +110 -25
  94. package/examples/jsm/tsl/display/SSRNode.js +194 -21
  95. package/examples/jsm/tsl/display/Sepia.js +9 -0
  96. package/examples/jsm/tsl/display/SobelOperatorNode.js +54 -4
  97. package/examples/jsm/tsl/display/StereoCompositePassNode.js +79 -3
  98. package/examples/jsm/tsl/display/StereoPassNode.js +41 -3
  99. package/examples/jsm/tsl/display/TRAAPassNode.js +116 -21
  100. package/examples/jsm/tsl/display/TransitionNode.js +71 -6
  101. package/examples/jsm/tsl/display/hashBlur.js +14 -3
  102. package/examples/jsm/tsl/lighting/TiledLightsNode.js +40 -34
  103. package/examples/jsm/utils/BufferGeometryUtils.js +1 -1
  104. package/examples/jsm/utils/CameraUtils.js +4 -1
  105. package/examples/jsm/utils/GeometryUtils.js +22 -19
  106. package/examples/jsm/utils/SceneUtils.js +2 -2
  107. package/examples/jsm/webxr/XRControllerModelFactory.js +6 -1
  108. package/examples/jsm/webxr/XREstimatedLight.js +1 -1
  109. package/package.json +8 -3
  110. package/src/Three.Core.js +2 -0
  111. package/src/Three.Legacy.js +0 -21
  112. package/src/Three.TSL.js +9 -4
  113. package/src/Three.WebGPU.Nodes.js +2 -2
  114. package/src/Three.WebGPU.js +2 -2
  115. package/src/animation/PropertyBinding.js +2 -2
  116. package/src/cameras/PerspectiveCamera.js +18 -2
  117. package/src/constants.js +1 -1
  118. package/src/core/RenderTarget.js +18 -0
  119. package/src/core/RenderTarget3D.js +22 -0
  120. package/src/core/RenderTargetArray.js +22 -0
  121. package/src/extras/Earcut.js +1 -1
  122. package/src/extras/PMREMGenerator.js +20 -0
  123. package/src/extras/TextureUtils.js +6 -0
  124. package/src/helpers/CameraHelper.js +16 -13
  125. package/src/loaders/nodes/NodeLoader.js +59 -0
  126. package/src/loaders/nodes/NodeMaterialLoader.js +45 -0
  127. package/src/loaders/nodes/NodeObjectLoader.js +61 -0
  128. package/src/materials/nodes/InstancedPointsNodeMaterial.js +72 -19
  129. package/src/materials/nodes/Line2NodeMaterial.js +128 -34
  130. package/src/materials/nodes/LineBasicNodeMaterial.js +17 -2
  131. package/src/materials/nodes/LineDashedNodeMaterial.js +78 -5
  132. package/src/materials/nodes/MeshBasicNodeMaterial.js +56 -0
  133. package/src/materials/nodes/MeshLambertNodeMaterial.js +35 -0
  134. package/src/materials/nodes/MeshMatcapNodeMaterial.js +22 -2
  135. package/src/materials/nodes/MeshNormalNodeMaterial.js +21 -2
  136. package/src/materials/nodes/MeshPhongNodeMaterial.js +64 -1
  137. package/src/materials/nodes/MeshPhysicalNodeMaterial.js +269 -0
  138. package/src/materials/nodes/MeshSSSNodeMaterial.js +97 -6
  139. package/src/materials/nodes/MeshStandardNodeMaterial.js +78 -0
  140. package/src/materials/nodes/MeshToonNodeMaterial.js +28 -0
  141. package/src/materials/nodes/NodeMaterial.js +474 -21
  142. package/src/materials/nodes/PointsNodeMaterial.js +22 -13
  143. package/src/materials/nodes/ShadowNodeMaterial.js +29 -0
  144. package/src/materials/nodes/SpriteNodeMaterial.js +77 -19
  145. package/src/materials/nodes/VolumeNodeMaterial.js +80 -4
  146. package/src/materials/nodes/manager/NodeMaterialObserver.js +87 -0
  147. package/src/nodes/Nodes.js +1 -6
  148. package/src/nodes/TSL.js +2 -3
  149. package/src/nodes/accessors/AccessorsUtils.js +27 -0
  150. package/src/nodes/accessors/Arrays.js +24 -5
  151. package/src/nodes/accessors/BatchNode.js +39 -3
  152. package/src/nodes/accessors/Bitangent.js +37 -0
  153. package/src/nodes/accessors/BufferAttributeNode.js +178 -5
  154. package/src/nodes/accessors/BufferNode.js +63 -0
  155. package/src/nodes/accessors/BuiltinNode.js +38 -0
  156. package/src/nodes/accessors/Camera.js +49 -0
  157. package/src/nodes/accessors/ClippingNode.js +71 -2
  158. package/src/nodes/accessors/CubeTextureNode.js +64 -0
  159. package/src/nodes/accessors/InstanceNode.js +85 -2
  160. package/src/nodes/accessors/InstancedMeshNode.js +28 -3
  161. package/src/nodes/accessors/InstancedPointsMaterialNode.js +13 -0
  162. package/src/nodes/accessors/Lights.js +50 -1
  163. package/src/nodes/accessors/MaterialNode.js +302 -8
  164. package/src/nodes/accessors/MaterialProperties.js +7 -0
  165. package/src/nodes/accessors/MaterialReferenceNode.js +53 -13
  166. package/src/nodes/accessors/ModelNode.js +100 -5
  167. package/src/nodes/accessors/ModelViewProjectionNode.js +10 -38
  168. package/src/nodes/accessors/MorphNode.js +48 -4
  169. package/src/nodes/accessors/Normal.js +59 -3
  170. package/src/nodes/accessors/Object3DNode.js +102 -1
  171. package/src/nodes/accessors/PointUVNode.js +26 -0
  172. package/src/nodes/accessors/Position.js +53 -4
  173. package/src/nodes/accessors/ReferenceBaseNode.js +188 -0
  174. package/src/nodes/accessors/ReferenceNode.js +202 -2
  175. package/src/nodes/accessors/ReflectVector.js +24 -0
  176. package/src/nodes/accessors/RendererReferenceNode.js +46 -1
  177. package/src/nodes/accessors/SceneNode.js +54 -0
  178. package/src/nodes/accessors/SkinningNode.js +127 -1
  179. package/src/nodes/accessors/StorageBufferNode.js +168 -4
  180. package/src/nodes/accessors/StorageTextureNode.js +118 -0
  181. package/src/nodes/accessors/Tangent.js +36 -0
  182. package/src/nodes/accessors/Texture3DNode.js +89 -5
  183. package/src/nodes/accessors/TextureBicubic.js +12 -2
  184. package/src/nodes/accessors/TextureNode.js +309 -10
  185. package/src/nodes/accessors/TextureSizeNode.js +42 -0
  186. package/src/nodes/accessors/UV.js +10 -1
  187. package/src/nodes/accessors/UniformArrayNode.js +223 -23
  188. package/src/nodes/accessors/UserDataNode.js +46 -0
  189. package/src/nodes/accessors/VelocityNode.js +82 -0
  190. package/src/nodes/accessors/VertexColorNode.js +39 -2
  191. package/src/nodes/code/CodeNode.js +95 -1
  192. package/src/nodes/code/ExpressionNode.js +28 -0
  193. package/src/nodes/code/FunctionCallNode.js +37 -0
  194. package/src/nodes/code/FunctionNode.js +45 -0
  195. package/src/nodes/code/ScriptableNode.js +223 -1
  196. package/src/nodes/code/ScriptableValueNode.js +85 -1
  197. package/src/nodes/core/AssignNode.js +48 -2
  198. package/src/nodes/core/AttributeNode.js +43 -0
  199. package/src/nodes/core/BypassNode.js +47 -2
  200. package/src/nodes/core/CacheNode.js +53 -2
  201. package/src/nodes/core/ConstNode.js +24 -0
  202. package/src/nodes/core/ContextNode.js +65 -0
  203. package/src/nodes/core/IndexNode.js +68 -7
  204. package/src/nodes/core/InputNode.js +48 -0
  205. package/src/nodes/core/LightingModel.js +60 -0
  206. package/src/nodes/core/MRTNode.js +66 -0
  207. package/src/nodes/core/Node.js +337 -12
  208. package/src/nodes/core/NodeAttribute.js +38 -0
  209. package/src/nodes/core/NodeBuilder.js +945 -15
  210. package/src/nodes/core/NodeCache.js +41 -2
  211. package/src/nodes/core/NodeCode.js +31 -0
  212. package/src/nodes/core/NodeFrame.js +123 -2
  213. package/src/nodes/core/NodeFunction.js +46 -0
  214. package/src/nodes/core/NodeFunctionInput.js +44 -0
  215. package/src/nodes/core/NodeParser.js +11 -0
  216. package/src/nodes/core/NodeUniform.js +52 -0
  217. package/src/nodes/core/NodeUtils.js +141 -0
  218. package/src/nodes/core/NodeVar.js +30 -0
  219. package/src/nodes/core/NodeVarying.js +28 -0
  220. package/src/nodes/core/OutputStructNode.js +33 -2
  221. package/src/nodes/core/ParameterNode.js +28 -0
  222. package/src/nodes/core/PropertyNode.js +232 -0
  223. package/src/nodes/core/StackNode.js +78 -0
  224. package/src/nodes/core/StructTypeNode.js +39 -1
  225. package/src/nodes/core/TempNode.js +30 -4
  226. package/src/nodes/core/UniformGroupNode.js +82 -7
  227. package/src/nodes/core/UniformNode.js +66 -0
  228. package/src/nodes/core/VarNode.js +52 -0
  229. package/src/nodes/core/VaryingNode.js +83 -2
  230. package/src/nodes/core/constants.js +36 -0
  231. package/src/nodes/display/BumpMapNode.js +37 -0
  232. package/src/nodes/display/ColorAdjustment.js +53 -11
  233. package/src/nodes/display/ColorSpaceFunctions.js +16 -0
  234. package/src/nodes/display/ColorSpaceNode.js +82 -0
  235. package/src/nodes/display/FrontFacingNode.js +29 -0
  236. package/src/nodes/display/NormalMapNode.js +43 -0
  237. package/src/nodes/display/PassNode.js +298 -7
  238. package/src/nodes/display/PosterizeNode.js +33 -0
  239. package/src/nodes/display/RenderOutputNode.js +66 -1
  240. package/src/nodes/display/ScreenNode.js +87 -0
  241. package/src/nodes/display/ToneMappingFunctions.js +58 -10
  242. package/src/nodes/display/ToneMappingNode.js +55 -2
  243. package/src/nodes/display/ToonOutlinePassNode.js +73 -0
  244. package/src/nodes/display/ViewportDepthNode.js +122 -8
  245. package/src/nodes/display/ViewportDepthTextureNode.js +23 -0
  246. package/src/nodes/display/ViewportSharedTextureNode.js +23 -0
  247. package/src/nodes/display/ViewportTextureNode.js +57 -0
  248. package/src/nodes/fog/Fog.js +93 -0
  249. package/src/nodes/functions/BasicLightingModel.js +24 -0
  250. package/src/nodes/functions/PhongLightingModel.js +34 -2
  251. package/src/nodes/functions/PhysicalLightingModel.js +167 -8
  252. package/src/nodes/functions/ShadowMaskModel.js +23 -0
  253. package/src/nodes/functions/ToonLightingModel.js +20 -0
  254. package/src/nodes/functions/material/getAlphaHashThreshold.js +1 -3
  255. package/src/nodes/functions/material/getParallaxCorrectNormal.js +16 -1
  256. package/src/nodes/geometry/RangeNode.js +54 -2
  257. package/src/nodes/gpgpu/AtomicFunctionNode.js +157 -1
  258. package/src/nodes/gpgpu/BarrierNode.js +47 -0
  259. package/src/nodes/gpgpu/ComputeBuiltinNode.js +126 -0
  260. package/src/nodes/gpgpu/ComputeNode.js +106 -2
  261. package/src/nodes/gpgpu/WorkgroupInfoNode.js +115 -6
  262. package/src/nodes/lighting/AONode.js +18 -0
  263. package/src/nodes/lighting/AmbientLightNode.js +10 -0
  264. package/src/nodes/lighting/AnalyticLightNode.js +97 -2
  265. package/src/nodes/lighting/BasicEnvironmentNode.js +19 -0
  266. package/src/nodes/lighting/BasicLightMapNode.js +17 -0
  267. package/src/nodes/lighting/DirectionalLightNode.js +10 -0
  268. package/src/nodes/lighting/EnvironmentNode.js +18 -0
  269. package/src/nodes/lighting/HemisphereLightNode.js +31 -0
  270. package/src/nodes/lighting/IESSpotLightNode.js +11 -0
  271. package/src/nodes/lighting/IrradianceNode.js +17 -0
  272. package/src/nodes/lighting/LightProbeNode.js +20 -0
  273. package/src/nodes/lighting/LightUtils.js +12 -0
  274. package/src/nodes/lighting/LightingContextNode.js +48 -0
  275. package/src/nodes/lighting/LightingNode.js +15 -6
  276. package/src/nodes/lighting/LightsNode.js +132 -1
  277. package/src/nodes/lighting/PointLightNode.js +32 -1
  278. package/src/nodes/lighting/PointShadowNode.js +54 -1
  279. package/src/nodes/lighting/RectAreaLightNode.js +44 -6
  280. package/src/nodes/lighting/ShadowBaseNode.js +93 -0
  281. package/src/nodes/lighting/ShadowNode.js +240 -34
  282. package/src/nodes/lighting/SpotLightNode.js +43 -0
  283. package/src/nodes/math/ConditionalNode.js +70 -3
  284. package/src/nodes/math/Hash.js +9 -0
  285. package/src/nodes/math/MathNode.js +614 -11
  286. package/src/nodes/math/MathUtils.js +45 -1
  287. package/src/nodes/math/OperatorNode.js +233 -0
  288. package/src/nodes/math/TriNoise3D.js +18 -7
  289. package/src/nodes/parsers/GLSLNodeFunction.js +16 -0
  290. package/src/nodes/parsers/GLSLNodeParser.js +11 -0
  291. package/src/nodes/pmrem/PMREMNode.js +133 -0
  292. package/src/nodes/pmrem/PMREMUtils.js +1 -1
  293. package/src/nodes/procedural/Checker.js +9 -0
  294. package/src/nodes/tsl/TSLBase.js +2 -2
  295. package/src/nodes/utils/ArrayElementNode.js +38 -2
  296. package/src/nodes/utils/ConvertNode.js +31 -0
  297. package/src/nodes/utils/CubeMapNode.js +78 -0
  298. package/src/nodes/utils/Discard.js +16 -0
  299. package/src/nodes/utils/EquirectUVNode.js +31 -1
  300. package/src/nodes/utils/FlipNode.js +38 -0
  301. package/src/nodes/utils/FunctionOverloadingNode.js +53 -1
  302. package/src/nodes/utils/JoinNode.js +25 -0
  303. package/src/nodes/utils/LoopNode.js +74 -1
  304. package/src/nodes/utils/MatcapUVNode.js +17 -0
  305. package/src/nodes/utils/MaxMipLevelNode.js +49 -0
  306. package/src/nodes/utils/Oscillators.js +33 -0
  307. package/src/nodes/utils/Packing.js +17 -0
  308. package/src/nodes/utils/PostProcessingUtils.js +28 -23
  309. package/src/nodes/utils/RTTNode.js +123 -2
  310. package/src/nodes/utils/ReflectorNode.js +174 -4
  311. package/src/nodes/utils/RemapNode.js +79 -0
  312. package/src/nodes/utils/RotateNode.js +41 -0
  313. package/src/nodes/utils/SetNode.js +44 -1
  314. package/src/nodes/utils/SplitNode.js +55 -3
  315. package/src/nodes/utils/SpriteSheetUVNode.js +46 -0
  316. package/src/nodes/utils/SpriteUtils.js +17 -0
  317. package/src/nodes/utils/StorageArrayElementNode.js +40 -0
  318. package/src/nodes/utils/Timer.js +19 -0
  319. package/src/nodes/utils/TriplanarTexturesNode.js +84 -0
  320. package/src/nodes/utils/UVUtils.js +20 -0
  321. package/src/nodes/utils/ViewportUtils.js +13 -0
  322. package/src/objects/BatchedMesh.js +1 -2
  323. package/src/objects/ClippingGroup.js +49 -0
  324. package/src/renderers/WebGLRenderer.js +23 -4
  325. package/src/renderers/common/Animation.js +63 -0
  326. package/src/renderers/common/Attributes.js +36 -0
  327. package/src/renderers/common/Backend.js +467 -52
  328. package/src/renderers/common/Background.js +34 -1
  329. package/src/renderers/common/BindGroup.js +43 -0
  330. package/src/renderers/common/Binding.js +35 -0
  331. package/src/renderers/common/Bindings.js +93 -1
  332. package/src/renderers/common/Buffer.js +49 -0
  333. package/src/renderers/common/BufferUtils.js +27 -0
  334. package/src/renderers/common/BundleGroup.js +57 -0
  335. package/src/renderers/common/ChainMap.js +38 -1
  336. package/src/renderers/common/ClippingContext.js +107 -14
  337. package/src/renderers/common/Color4.js +36 -0
  338. package/src/renderers/common/ComputePipeline.js +24 -0
  339. package/src/renderers/common/CubeRenderTarget.js +13 -0
  340. package/src/renderers/common/DataMap.js +37 -1
  341. package/src/renderers/common/Geometries.js +98 -0
  342. package/src/renderers/common/IndirectStorageBufferAttribute.js +25 -2
  343. package/src/renderers/common/Info.js +87 -0
  344. package/src/renderers/common/Lighting.js +26 -0
  345. package/src/renderers/common/Pipeline.js +22 -0
  346. package/src/renderers/common/Pipelines.js +148 -5
  347. package/src/renderers/common/PostProcessing.js +75 -3
  348. package/src/renderers/common/ProgrammableStage.js +60 -2
  349. package/src/renderers/common/QuadMesh.js +54 -5
  350. package/src/renderers/common/RenderBundle.js +14 -8
  351. package/src/renderers/common/RenderBundles.js +33 -10
  352. package/src/renderers/common/RenderContext.js +169 -4
  353. package/src/renderers/common/RenderContexts.js +42 -2
  354. package/src/renderers/common/RenderList.js +178 -0
  355. package/src/renderers/common/RenderLists.js +31 -0
  356. package/src/renderers/common/RenderObject.js +328 -4
  357. package/src/renderers/common/RenderObjects.js +101 -7
  358. package/src/renderers/common/RenderPipeline.js +24 -0
  359. package/src/renderers/common/Renderer.js +1096 -31
  360. package/src/renderers/common/RendererUtils.js +193 -0
  361. package/src/renderers/common/SampledTexture.js +121 -0
  362. package/src/renderers/common/Sampler.js +30 -0
  363. package/src/renderers/common/StorageBuffer.js +24 -0
  364. package/src/renderers/common/StorageBufferAttribute.js +31 -2
  365. package/src/renderers/common/StorageInstancedBufferAttribute.js +31 -2
  366. package/src/renderers/common/StorageTexture.js +38 -0
  367. package/src/renderers/common/Textures.js +83 -0
  368. package/src/renderers/common/Uniform.js +188 -2
  369. package/src/renderers/common/UniformBuffer.js +19 -0
  370. package/src/renderers/common/UniformsGroup.js +139 -2
  371. package/src/renderers/common/extras/PMREMGenerator.js +24 -3
  372. package/src/renderers/common/nodes/NodeBuilderState.js +95 -1
  373. package/src/renderers/common/nodes/NodeLibrary.js +89 -0
  374. package/src/renderers/common/nodes/NodeSampledTexture.js +91 -2
  375. package/src/renderers/common/nodes/NodeSampler.js +28 -0
  376. package/src/renderers/common/nodes/NodeStorageBuffer.js +35 -0
  377. package/src/renderers/common/nodes/NodeUniform.js +189 -0
  378. package/src/renderers/common/nodes/NodeUniformBuffer.js +29 -0
  379. package/src/renderers/common/nodes/NodeUniformsGroup.js +31 -18
  380. package/src/renderers/common/nodes/Nodes.js +327 -60
  381. package/src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js +2 -2
  382. package/src/renderers/shaders/ShaderChunk/transmission_pars_fragment.glsl.js +7 -7
  383. package/src/renderers/webgl-fallback/WebGLBackend.js +547 -29
  384. package/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js +0 -1
  385. package/src/renderers/webgl-fallback/utils/WebGLAttributeUtils.js +1 -1
  386. package/src/renderers/webgl-fallback/utils/WebGLTextureUtils.js +1 -1
  387. package/src/renderers/webgpu/WebGPUBackend.js +480 -49
  388. package/src/renderers/webgpu/WebGPURenderer.Nodes.js +34 -0
  389. package/src/renderers/webgpu/WebGPURenderer.js +35 -0
  390. package/src/renderers/webgpu/nodes/BasicNodeLibrary.js +20 -8
  391. package/src/renderers/webgpu/nodes/StandardNodeLibrary.js +21 -8
  392. package/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +59 -11
  393. package/src/renderers/webgpu/nodes/WGSLNodeFunction.js +17 -0
  394. package/src/renderers/webgpu/nodes/WGSLNodeParser.js +11 -0
  395. package/src/renderers/webgpu/utils/WebGPUAttributeUtils.js +10 -8
  396. package/src/renderers/webgpu/utils/WebGPUBindingUtils.js +9 -1
  397. package/src/renderers/webgpu/utils/WebGPUTextureUtils.js +5 -9
  398. package/src/renderers/webgpu/utils/WebGPUUtils.js +30 -0
  399. package/src/renderers/webxr/WebXRDepthSensing.js +1 -1
  400. package/src/renderers/webxr/WebXRManager.js +7 -1
  401. package/src/textures/Data3DTexture.js +2 -2
  402. package/src/textures/Texture.js +4 -0
  403. package/examples/jsm/animation/MMDAnimationHelper.js +0 -1209
  404. package/examples/jsm/animation/MMDPhysics.js +0 -1408
  405. package/examples/jsm/exporters/MMDExporter.js +0 -223
  406. package/examples/jsm/libs/mmdparser.module.js +0 -11530
  407. package/examples/jsm/loaders/MMDLoader.js +0 -2298
  408. package/examples/jsm/shaders/MMDToonShader.js +0 -134
  409. package/src/nodes/core/UniformGroup.js +0 -13
  410. package/src/nodes/fog/FogExp2Node.js +0 -35
  411. package/src/nodes/fog/FogNode.js +0 -50
  412. package/src/nodes/fog/FogRangeNode.js +0 -36
  413. package/src/renderers/common/PostProcessingUtils.js +0 -86
@@ -1,2298 +0,0 @@
1
- import {
2
- AddOperation,
3
- AnimationClip,
4
- Bone,
5
- BufferGeometry,
6
- Color,
7
- CustomBlending,
8
- TangentSpaceNormalMap,
9
- DoubleSide,
10
- DstAlphaFactor,
11
- Euler,
12
- FileLoader,
13
- Float32BufferAttribute,
14
- FrontSide,
15
- Interpolant,
16
- Loader,
17
- LoaderUtils,
18
- UniformsUtils,
19
- ShaderMaterial,
20
- MultiplyOperation,
21
- NearestFilter,
22
- NumberKeyframeTrack,
23
- OneMinusSrcAlphaFactor,
24
- Quaternion,
25
- QuaternionKeyframeTrack,
26
- RepeatWrapping,
27
- Skeleton,
28
- SkinnedMesh,
29
- SrcAlphaFactor,
30
- SRGBColorSpace,
31
- TextureLoader,
32
- Uint16BufferAttribute,
33
- Vector3,
34
- VectorKeyframeTrack,
35
- RGB_S3TC_DXT1_Format,
36
- RGB_PVRTC_4BPPV1_Format,
37
- RGB_PVRTC_2BPPV1_Format,
38
- RGB_ETC1_Format,
39
- RGB_ETC2_Format
40
- } from 'three';
41
- import { MMDToonShader } from '../shaders/MMDToonShader.js';
42
- import { TGALoader } from '../loaders/TGALoader.js';
43
- import { MMDParser } from '../libs/mmdparser.module.js';
44
-
45
- /**
46
- * Dependencies
47
- * - mmd-parser https://github.com/takahirox/mmd-parser
48
- * - TGALoader
49
- * - OutlineEffect
50
- *
51
- * MMDLoader creates Three.js Objects from MMD resources as
52
- * PMD, PMX, VMD, and VPD files.
53
- *
54
- * PMD/PMX is a model data format, VMD is a motion data format
55
- * VPD is a posing data format used in MMD(Miku Miku Dance).
56
- *
57
- * MMD official site
58
- * - https://sites.google.com/view/evpvp/
59
- *
60
- * PMD, VMD format (in Japanese)
61
- * - http://blog.goo.ne.jp/torisu_tetosuki/e/209ad341d3ece2b1b4df24abf619d6e4
62
- *
63
- * PMX format
64
- * - https://gist.github.com/felixjones/f8a06bd48f9da9a4539f
65
- *
66
- * TODO
67
- * - light motion in vmd support.
68
- * - SDEF support.
69
- * - uv/material/bone morphing support.
70
- * - more precise grant skinning support.
71
- * - shadow support.
72
- */
73
-
74
- /**
75
- * @param {THREE.LoadingManager} manager
76
- */
77
- class MMDLoader extends Loader {
78
-
79
- constructor( manager ) {
80
-
81
- super( manager );
82
-
83
- this.loader = new FileLoader( this.manager );
84
-
85
- this.parser = null; // lazy generation
86
- this.meshBuilder = new MeshBuilder( this.manager );
87
- this.animationBuilder = new AnimationBuilder();
88
-
89
- console.warn( 'THREE.MMDLoader: The module has been deprecated and will be removed with r172. Please migrate to https://github.com/takahirox/three-mmd-loader instead.' );
90
-
91
- }
92
-
93
- /**
94
- * @param {string} animationPath
95
- * @return {MMDLoader}
96
- */
97
- setAnimationPath( animationPath ) {
98
-
99
- this.animationPath = animationPath;
100
- return this;
101
-
102
- }
103
-
104
- // Load MMD assets as Three.js Object
105
-
106
- /**
107
- * Loads Model file (.pmd or .pmx) as a SkinnedMesh.
108
- *
109
- * @param {string} url - url to Model(.pmd or .pmx) file
110
- * @param {function} onLoad
111
- * @param {function} onProgress
112
- * @param {function} onError
113
- */
114
- load( url, onLoad, onProgress, onError ) {
115
-
116
- const builder = this.meshBuilder.setCrossOrigin( this.crossOrigin );
117
-
118
- // resource path
119
-
120
- let resourcePath;
121
-
122
- if ( this.resourcePath !== '' ) {
123
-
124
- resourcePath = this.resourcePath;
125
-
126
- } else if ( this.path !== '' ) {
127
-
128
- resourcePath = this.path;
129
-
130
- } else {
131
-
132
- resourcePath = LoaderUtils.extractUrlBase( url );
133
-
134
- }
135
-
136
- const parser = this._getParser();
137
- const extractModelExtension = this._extractModelExtension;
138
-
139
- this.loader
140
- .setMimeType( undefined )
141
- .setPath( this.path )
142
- .setResponseType( 'arraybuffer' )
143
- .setRequestHeader( this.requestHeader )
144
- .setWithCredentials( this.withCredentials )
145
- .load( url, function ( buffer ) {
146
-
147
- try {
148
-
149
- const modelExtension = extractModelExtension( buffer );
150
-
151
- if ( modelExtension !== 'pmd' && modelExtension !== 'pmx' ) {
152
-
153
- if ( onError ) onError( new Error( 'THREE.MMDLoader: Unknown model file extension .' + modelExtension + '.' ) );
154
-
155
- return;
156
-
157
- }
158
-
159
- const data = modelExtension === 'pmd' ? parser.parsePmd( buffer, true ) : parser.parsePmx( buffer, true );
160
-
161
- onLoad( builder.build( data, resourcePath, onProgress, onError ) );
162
-
163
- } catch ( e ) {
164
-
165
- if ( onError ) onError( e );
166
-
167
- }
168
-
169
- }, onProgress, onError );
170
-
171
- }
172
-
173
- /**
174
- * Loads Motion file(s) (.vmd) as a AnimationClip.
175
- * If two or more files are specified, they'll be merged.
176
- *
177
- * @param {string|Array<string>} url - url(s) to animation(.vmd) file(s)
178
- * @param {SkinnedMesh|THREE.Camera} object - tracks will be fitting to this object
179
- * @param {function} onLoad
180
- * @param {function} onProgress
181
- * @param {function} onError
182
- */
183
- loadAnimation( url, object, onLoad, onProgress, onError ) {
184
-
185
- const builder = this.animationBuilder;
186
-
187
- this.loadVMD( url, function ( vmd ) {
188
-
189
- onLoad( object.isCamera
190
- ? builder.buildCameraAnimation( vmd )
191
- : builder.build( vmd, object ) );
192
-
193
- }, onProgress, onError );
194
-
195
- }
196
-
197
- /**
198
- * Loads mode file and motion file(s) as an object containing
199
- * a SkinnedMesh and a AnimationClip.
200
- * Tracks of AnimationClip are fitting to the model.
201
- *
202
- * @param {string} modelUrl - url to Model(.pmd or .pmx) file
203
- * @param {string|Array{string}} vmdUrl - url(s) to animation(.vmd) file
204
- * @param {function} onLoad
205
- * @param {function} onProgress
206
- * @param {function} onError
207
- */
208
- loadWithAnimation( modelUrl, vmdUrl, onLoad, onProgress, onError ) {
209
-
210
- const scope = this;
211
-
212
- this.load( modelUrl, function ( mesh ) {
213
-
214
- scope.loadAnimation( vmdUrl, mesh, function ( animation ) {
215
-
216
- onLoad( {
217
- mesh: mesh,
218
- animation: animation
219
- } );
220
-
221
- }, onProgress, onError );
222
-
223
- }, onProgress, onError );
224
-
225
- }
226
-
227
- // Load MMD assets as Object data parsed by MMDParser
228
-
229
- /**
230
- * Loads .pmd file as an Object.
231
- *
232
- * @param {string} url - url to .pmd file
233
- * @param {function} onLoad
234
- * @param {function} onProgress
235
- * @param {function} onError
236
- */
237
- loadPMD( url, onLoad, onProgress, onError ) {
238
-
239
- const parser = this._getParser();
240
-
241
- this.loader
242
- .setMimeType( undefined )
243
- .setPath( this.path )
244
- .setResponseType( 'arraybuffer' )
245
- .setRequestHeader( this.requestHeader )
246
- .setWithCredentials( this.withCredentials )
247
- .load( url, function ( buffer ) {
248
-
249
- try {
250
-
251
- onLoad( parser.parsePmd( buffer, true ) );
252
-
253
- } catch ( e ) {
254
-
255
- if ( onError ) onError( e );
256
-
257
- }
258
-
259
- }, onProgress, onError );
260
-
261
- }
262
-
263
- /**
264
- * Loads .pmx file as an Object.
265
- *
266
- * @param {string} url - url to .pmx file
267
- * @param {function} onLoad
268
- * @param {function} onProgress
269
- * @param {function} onError
270
- */
271
- loadPMX( url, onLoad, onProgress, onError ) {
272
-
273
- const parser = this._getParser();
274
-
275
- this.loader
276
- .setMimeType( undefined )
277
- .setPath( this.path )
278
- .setResponseType( 'arraybuffer' )
279
- .setRequestHeader( this.requestHeader )
280
- .setWithCredentials( this.withCredentials )
281
- .load( url, function ( buffer ) {
282
-
283
- try {
284
-
285
- onLoad( parser.parsePmx( buffer, true ) );
286
-
287
- } catch ( e ) {
288
-
289
- if ( onError ) onError( e );
290
-
291
- }
292
-
293
- }, onProgress, onError );
294
-
295
- }
296
-
297
- /**
298
- * Loads .vmd file as an Object. If two or more files are specified
299
- * they'll be merged.
300
- *
301
- * @param {string|Array<string>} url - url(s) to .vmd file(s)
302
- * @param {function} onLoad
303
- * @param {function} onProgress
304
- * @param {function} onError
305
- */
306
- loadVMD( url, onLoad, onProgress, onError ) {
307
-
308
- const urls = Array.isArray( url ) ? url : [ url ];
309
-
310
- const vmds = [];
311
- const vmdNum = urls.length;
312
-
313
- const parser = this._getParser();
314
-
315
- this.loader
316
- .setMimeType( undefined )
317
- .setPath( this.animationPath )
318
- .setResponseType( 'arraybuffer' )
319
- .setRequestHeader( this.requestHeader )
320
- .setWithCredentials( this.withCredentials );
321
-
322
- for ( let i = 0, il = urls.length; i < il; i ++ ) {
323
-
324
- this.loader.load( urls[ i ], function ( buffer ) {
325
-
326
- try {
327
-
328
- vmds.push( parser.parseVmd( buffer, true ) );
329
-
330
- if ( vmds.length === vmdNum ) onLoad( parser.mergeVmds( vmds ) );
331
-
332
- } catch ( e ) {
333
-
334
- if ( onError ) onError( e );
335
-
336
- }
337
-
338
- }, onProgress, onError );
339
-
340
- }
341
-
342
- }
343
-
344
- /**
345
- * Loads .vpd file as an Object.
346
- *
347
- * @param {string} url - url to .vpd file
348
- * @param {boolean} isUnicode
349
- * @param {function} onLoad
350
- * @param {function} onProgress
351
- * @param {function} onError
352
- */
353
- loadVPD( url, isUnicode, onLoad, onProgress, onError ) {
354
-
355
- const parser = this._getParser();
356
-
357
- this.loader
358
- .setMimeType( isUnicode ? undefined : 'text/plain; charset=shift_jis' )
359
- .setPath( this.animationPath )
360
- .setResponseType( 'text' )
361
- .setRequestHeader( this.requestHeader )
362
- .setWithCredentials( this.withCredentials )
363
- .load( url, function ( text ) {
364
-
365
- try {
366
-
367
- onLoad( parser.parseVpd( text, true ) );
368
-
369
- } catch ( e ) {
370
-
371
- if ( onError ) onError( e );
372
-
373
- }
374
-
375
- }, onProgress, onError );
376
-
377
- }
378
-
379
- // private methods
380
-
381
- _extractModelExtension( buffer ) {
382
-
383
- const decoder = new TextDecoder( 'utf-8' );
384
- const bytes = new Uint8Array( buffer, 0, 3 );
385
- return decoder.decode( bytes ).toLowerCase();
386
-
387
- }
388
-
389
- _getParser() {
390
-
391
- if ( this.parser === null ) {
392
-
393
- this.parser = new MMDParser.Parser();
394
-
395
- }
396
-
397
- return this.parser;
398
-
399
- }
400
-
401
- }
402
-
403
- // Utilities
404
-
405
- /*
406
- * base64 encoded defalut toon textures toon00.bmp - toon10.bmp.
407
- * We don't need to request external toon image files.
408
- */
409
- const DEFAULT_TOON_TEXTURES = [
410
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=',
411
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAN0lEQVRYR+3WQREAMBACsZ5/bWiiMvgEBTt5cW37hjsBBAgQIECAwFwgyfYPCCBAgAABAgTWAh8aBHZBl14e8wAAAABJRU5ErkJggg==',
412
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAOUlEQVRYR+3WMREAMAwDsYY/yoDI7MLwIiP40+RJklfcCCBAgAABAgTqArfb/QMCCBAgQIAAgbbAB3z/e0F3js2cAAAAAElFTkSuQmCC',
413
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAN0lEQVRYR+3WQREAMBACsZ5/B5ilMvgEBTt5cW37hjsBBAgQIECAwFwgyfYPCCBAgAABAgTWAh81dWyx0gFwKAAAAABJRU5ErkJggg==',
414
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAOklEQVRYR+3WoREAMAwDsWb/UQtCy9wxTOQJ/oQ8SXKKGwEECBAgQIBAXeDt7f4BAQQIECBAgEBb4AOz8Hzx7WLY4wAAAABJRU5ErkJggg==',
415
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABPUlEQVRYR+1XwW7CMAy1+f9fZOMysSEOEweEOPRNdm3HbdOyIhAcklPrOs/PLy9RygBALxzcCDQFmgJNgaZAU6Ap0BR4PwX8gsRMVLssMRH5HcpzJEaWL7EVg9F1IHRlyqQohgVr4FGUlUcMJSjcUlDw0zvjeun70cLWmneoyf7NgBTQSniBTQQSuJAZsOnnaczjIMb5hCiuHKxokCrJfVnrctyZL0PkJAJe1HMil4nxeyi3Ypfn1kX51jpPvo/JeCNC4PhVdHdJw2XjBR8brF8PEIhNVn12AgP7uHsTBguBn53MUZCqv7Lp07Pn5k1Ro+uWmUNn7D+M57rtk7aG0Vo73xyF/fbFf0bPJjDXngnGocDTdFhygZjwUQrMNrDcmZlQT50VJ/g/UwNyHpu778+yW+/ksOz/BFo54P4AsUXMfRq7XWsAAAAASUVORK5CYII=',
416
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACMElEQVRYR+2Xv4pTQRTGf2dubhLdICiii2KnYKHVolhauKWPoGAnNr6BD6CvIVaihYuI2i1ia0BY0MZGRHQXjZj/mSPnnskfNWiWZUlzJ5k7M2cm833nO5Mziej2DWWJRUoCpQKlAntSQCqgw39/iUWAGmh37jrRnVsKlgpiqmkoGVABA7E57fvY+pJDdgKqF6HzFCSADkDq+F6AHABtQ+UMVE5D7zXod7fFNhTEckTbj5XQgHzNN+5tQvc5NG7C6BNkp6D3EmpXHDR+dQAjFLchW3VS9rlw3JBh+B7ys5Cf9z0GW1C/7P32AyBAOAz1q4jGliIH3YPuBnSfQX4OGreTIgEYQb/pBDtPnEQ4CivXYPAWBk13oHrB54yA9QuSn2H4AcKRpEILDt0BUzj+RLR1V5EqjD66NPRBVpLcQwjHoHYJOhsQv6U4mnzmrIXJCFr4LDwm/xBUoboG9XX4cc9VKdYoSA2yk5NQLJaKDUjTBoveG3Z2TElTxwjNK4M3LEZgUdDdruvcXzKBpStgp2NPiWi3ks9ZXxIoFVi+AvHLdc9TqtjL3/aYjpPlrzOcEnK62Szhimdd7xX232zFDTgtxezOu3WNMRLjiKgjtOhHVMd1loynVHvOgjuIIJMaELEqhJAV/RCSLbWTcfPFakFgFlALTRRvx+ok6Hlp/Q+v3fmx90bMyUzaEAhmM3KvHlXTL5DxnbGf/1M8RNNACLL5MNtPxP/mypJAqcDSFfgFhpYqWUzhTEAAAAAASUVORK5CYII=',
417
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=',
418
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=',
419
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=',
420
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII='
421
- ];
422
-
423
- const NON_ALPHA_CHANNEL_FORMATS = [
424
- RGB_S3TC_DXT1_Format,
425
- RGB_PVRTC_4BPPV1_Format,
426
- RGB_PVRTC_2BPPV1_Format,
427
- RGB_ETC1_Format,
428
- RGB_ETC2_Format
429
- ];
430
-
431
- // Builders. They build Three.js object from Object data parsed by MMDParser.
432
-
433
- /**
434
- * @param {THREE.LoadingManager} manager
435
- */
436
- class MeshBuilder {
437
-
438
- constructor( manager ) {
439
-
440
- this.crossOrigin = 'anonymous';
441
- this.geometryBuilder = new GeometryBuilder();
442
- this.materialBuilder = new MaterialBuilder( manager );
443
-
444
- }
445
-
446
- /**
447
- * @param {string} crossOrigin
448
- * @return {MeshBuilder}
449
- */
450
- setCrossOrigin( crossOrigin ) {
451
-
452
- this.crossOrigin = crossOrigin;
453
- return this;
454
-
455
- }
456
-
457
- /**
458
- * @param {Object} data - parsed PMD/PMX data
459
- * @param {string} resourcePath
460
- * @param {function} onProgress
461
- * @param {function} onError
462
- * @return {SkinnedMesh}
463
- */
464
- build( data, resourcePath, onProgress, onError ) {
465
-
466
- const geometry = this.geometryBuilder.build( data );
467
- const material = this.materialBuilder
468
- .setCrossOrigin( this.crossOrigin )
469
- .setResourcePath( resourcePath )
470
- .build( data, geometry, onProgress, onError );
471
-
472
- const mesh = new SkinnedMesh( geometry, material );
473
-
474
- const skeleton = new Skeleton( initBones( mesh ) );
475
- mesh.bind( skeleton );
476
-
477
- // console.log( mesh ); // for console debug
478
-
479
- return mesh;
480
-
481
- }
482
-
483
- }
484
-
485
- // TODO: Try to remove this function
486
-
487
- function initBones( mesh ) {
488
-
489
- const geometry = mesh.geometry;
490
-
491
- const bones = [];
492
-
493
- if ( geometry && geometry.bones !== undefined ) {
494
-
495
- // first, create array of 'Bone' objects from geometry data
496
-
497
- for ( let i = 0, il = geometry.bones.length; i < il; i ++ ) {
498
-
499
- const gbone = geometry.bones[ i ];
500
-
501
- // create new 'Bone' object
502
-
503
- const bone = new Bone();
504
- bones.push( bone );
505
-
506
- // apply values
507
-
508
- bone.name = gbone.name;
509
- bone.position.fromArray( gbone.pos );
510
- bone.quaternion.fromArray( gbone.rotq );
511
- if ( gbone.scl !== undefined ) bone.scale.fromArray( gbone.scl );
512
-
513
- }
514
-
515
- // second, create bone hierarchy
516
-
517
- for ( let i = 0, il = geometry.bones.length; i < il; i ++ ) {
518
-
519
- const gbone = geometry.bones[ i ];
520
-
521
- if ( ( gbone.parent !== - 1 ) && ( gbone.parent !== null ) && ( bones[ gbone.parent ] !== undefined ) ) {
522
-
523
- // subsequent bones in the hierarchy
524
-
525
- bones[ gbone.parent ].add( bones[ i ] );
526
-
527
- } else {
528
-
529
- // topmost bone, immediate child of the skinned mesh
530
-
531
- mesh.add( bones[ i ] );
532
-
533
- }
534
-
535
- }
536
-
537
- }
538
-
539
- // now the bones are part of the scene graph and children of the skinned mesh.
540
- // let's update the corresponding matrices
541
-
542
- mesh.updateMatrixWorld( true );
543
-
544
- return bones;
545
-
546
- }
547
-
548
- //
549
-
550
- class GeometryBuilder {
551
-
552
- /**
553
- * @param {Object} data - parsed PMD/PMX data
554
- * @return {BufferGeometry}
555
- */
556
- build( data ) {
557
-
558
- // for geometry
559
- const positions = [];
560
- const uvs = [];
561
- const normals = [];
562
-
563
- const indices = [];
564
-
565
- const groups = [];
566
-
567
- const bones = [];
568
- const skinIndices = [];
569
- const skinWeights = [];
570
-
571
- const morphTargets = [];
572
- const morphPositions = [];
573
-
574
- const iks = [];
575
- const grants = [];
576
-
577
- const rigidBodies = [];
578
- const constraints = [];
579
-
580
- // for work
581
- let offset = 0;
582
- const boneTypeTable = {};
583
-
584
- // positions, normals, uvs, skinIndices, skinWeights
585
-
586
- for ( let i = 0; i < data.metadata.vertexCount; i ++ ) {
587
-
588
- const v = data.vertices[ i ];
589
-
590
- for ( let j = 0, jl = v.position.length; j < jl; j ++ ) {
591
-
592
- positions.push( v.position[ j ] );
593
-
594
- }
595
-
596
- for ( let j = 0, jl = v.normal.length; j < jl; j ++ ) {
597
-
598
- normals.push( v.normal[ j ] );
599
-
600
- }
601
-
602
- for ( let j = 0, jl = v.uv.length; j < jl; j ++ ) {
603
-
604
- uvs.push( v.uv[ j ] );
605
-
606
- }
607
-
608
- for ( let j = 0; j < 4; j ++ ) {
609
-
610
- skinIndices.push( v.skinIndices.length - 1 >= j ? v.skinIndices[ j ] : 0.0 );
611
-
612
- }
613
-
614
- for ( let j = 0; j < 4; j ++ ) {
615
-
616
- skinWeights.push( v.skinWeights.length - 1 >= j ? v.skinWeights[ j ] : 0.0 );
617
-
618
- }
619
-
620
- }
621
-
622
- // indices
623
-
624
- for ( let i = 0; i < data.metadata.faceCount; i ++ ) {
625
-
626
- const face = data.faces[ i ];
627
-
628
- for ( let j = 0, jl = face.indices.length; j < jl; j ++ ) {
629
-
630
- indices.push( face.indices[ j ] );
631
-
632
- }
633
-
634
- }
635
-
636
- // groups
637
-
638
- for ( let i = 0; i < data.metadata.materialCount; i ++ ) {
639
-
640
- const material = data.materials[ i ];
641
-
642
- groups.push( {
643
- offset: offset * 3,
644
- count: material.faceCount * 3
645
- } );
646
-
647
- offset += material.faceCount;
648
-
649
- }
650
-
651
- // bones
652
-
653
- for ( let i = 0; i < data.metadata.rigidBodyCount; i ++ ) {
654
-
655
- const body = data.rigidBodies[ i ];
656
- let value = boneTypeTable[ body.boneIndex ];
657
-
658
- // keeps greater number if already value is set without any special reasons
659
- value = value === undefined ? body.type : Math.max( body.type, value );
660
-
661
- boneTypeTable[ body.boneIndex ] = value;
662
-
663
- }
664
-
665
- for ( let i = 0; i < data.metadata.boneCount; i ++ ) {
666
-
667
- const boneData = data.bones[ i ];
668
-
669
- const bone = {
670
- index: i,
671
- transformationClass: boneData.transformationClass,
672
- parent: boneData.parentIndex,
673
- name: boneData.name,
674
- pos: boneData.position.slice( 0, 3 ),
675
- rotq: [ 0, 0, 0, 1 ],
676
- scl: [ 1, 1, 1 ],
677
- rigidBodyType: boneTypeTable[ i ] !== undefined ? boneTypeTable[ i ] : - 1
678
- };
679
-
680
- if ( bone.parent !== - 1 ) {
681
-
682
- bone.pos[ 0 ] -= data.bones[ bone.parent ].position[ 0 ];
683
- bone.pos[ 1 ] -= data.bones[ bone.parent ].position[ 1 ];
684
- bone.pos[ 2 ] -= data.bones[ bone.parent ].position[ 2 ];
685
-
686
- }
687
-
688
- bones.push( bone );
689
-
690
- }
691
-
692
- // iks
693
-
694
- // TODO: remove duplicated codes between PMD and PMX
695
- if ( data.metadata.format === 'pmd' ) {
696
-
697
- for ( let i = 0; i < data.metadata.ikCount; i ++ ) {
698
-
699
- const ik = data.iks[ i ];
700
-
701
- const param = {
702
- target: ik.target,
703
- effector: ik.effector,
704
- iteration: ik.iteration,
705
- maxAngle: ik.maxAngle * 4,
706
- links: []
707
- };
708
-
709
- for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
710
-
711
- const link = {};
712
- link.index = ik.links[ j ].index;
713
- link.enabled = true;
714
-
715
- if ( data.bones[ link.index ].name.indexOf( 'ひざ' ) >= 0 ) {
716
-
717
- link.limitation = new Vector3( 1.0, 0.0, 0.0 );
718
-
719
- }
720
-
721
- param.links.push( link );
722
-
723
- }
724
-
725
- iks.push( param );
726
-
727
- }
728
-
729
- } else {
730
-
731
- for ( let i = 0; i < data.metadata.boneCount; i ++ ) {
732
-
733
- const ik = data.bones[ i ].ik;
734
-
735
- if ( ik === undefined ) continue;
736
-
737
- const param = {
738
- target: i,
739
- effector: ik.effector,
740
- iteration: ik.iteration,
741
- maxAngle: ik.maxAngle,
742
- links: []
743
- };
744
-
745
- for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
746
-
747
- const link = {};
748
- link.index = ik.links[ j ].index;
749
- link.enabled = true;
750
-
751
- if ( ik.links[ j ].angleLimitation === 1 ) {
752
-
753
- // Revert if rotationMin/Max doesn't work well
754
- // link.limitation = new Vector3( 1.0, 0.0, 0.0 );
755
-
756
- const rotationMin = ik.links[ j ].lowerLimitationAngle;
757
- const rotationMax = ik.links[ j ].upperLimitationAngle;
758
-
759
- // Convert Left to Right coordinate by myself because
760
- // MMDParser doesn't convert. It's a MMDParser's bug
761
-
762
- const tmp1 = - rotationMax[ 0 ];
763
- const tmp2 = - rotationMax[ 1 ];
764
- rotationMax[ 0 ] = - rotationMin[ 0 ];
765
- rotationMax[ 1 ] = - rotationMin[ 1 ];
766
- rotationMin[ 0 ] = tmp1;
767
- rotationMin[ 1 ] = tmp2;
768
-
769
- link.rotationMin = new Vector3().fromArray( rotationMin );
770
- link.rotationMax = new Vector3().fromArray( rotationMax );
771
-
772
- }
773
-
774
- param.links.push( link );
775
-
776
- }
777
-
778
- iks.push( param );
779
-
780
- // Save the reference even from bone data for efficiently
781
- // simulating PMX animation system
782
- bones[ i ].ik = param;
783
-
784
- }
785
-
786
- }
787
-
788
- // grants
789
-
790
- if ( data.metadata.format === 'pmx' ) {
791
-
792
- // bone index -> grant entry map
793
- const grantEntryMap = {};
794
-
795
- for ( let i = 0; i < data.metadata.boneCount; i ++ ) {
796
-
797
- const boneData = data.bones[ i ];
798
- const grant = boneData.grant;
799
-
800
- if ( grant === undefined ) continue;
801
-
802
- const param = {
803
- index: i,
804
- parentIndex: grant.parentIndex,
805
- ratio: grant.ratio,
806
- isLocal: grant.isLocal,
807
- affectRotation: grant.affectRotation,
808
- affectPosition: grant.affectPosition,
809
- transformationClass: boneData.transformationClass
810
- };
811
-
812
- grantEntryMap[ i ] = { parent: null, children: [], param: param, visited: false };
813
-
814
- }
815
-
816
- const rootEntry = { parent: null, children: [], param: null, visited: false };
817
-
818
- // Build a tree representing grant hierarchy
819
-
820
- for ( const boneIndex in grantEntryMap ) {
821
-
822
- const grantEntry = grantEntryMap[ boneIndex ];
823
- const parentGrantEntry = grantEntryMap[ grantEntry.parentIndex ] || rootEntry;
824
-
825
- grantEntry.parent = parentGrantEntry;
826
- parentGrantEntry.children.push( grantEntry );
827
-
828
- }
829
-
830
- // Sort grant parameters from parents to children because
831
- // grant uses parent's transform that parent's grant is already applied
832
- // so grant should be applied in order from parents to children
833
-
834
- function traverse( entry ) {
835
-
836
- if ( entry.param ) {
837
-
838
- grants.push( entry.param );
839
-
840
- // Save the reference even from bone data for efficiently
841
- // simulating PMX animation system
842
- bones[ entry.param.index ].grant = entry.param;
843
-
844
- }
845
-
846
- entry.visited = true;
847
-
848
- for ( let i = 0, il = entry.children.length; i < il; i ++ ) {
849
-
850
- const child = entry.children[ i ];
851
-
852
- // Cut off a loop if exists. (Is a grant loop invalid?)
853
- if ( ! child.visited ) traverse( child );
854
-
855
- }
856
-
857
- }
858
-
859
- traverse( rootEntry );
860
-
861
- }
862
-
863
- // morph
864
-
865
- function updateAttributes( attribute, morph, ratio ) {
866
-
867
- for ( let i = 0; i < morph.elementCount; i ++ ) {
868
-
869
- const element = morph.elements[ i ];
870
-
871
- let index;
872
-
873
- if ( data.metadata.format === 'pmd' ) {
874
-
875
- index = data.morphs[ 0 ].elements[ element.index ].index;
876
-
877
- } else {
878
-
879
- index = element.index;
880
-
881
- }
882
-
883
- attribute.array[ index * 3 + 0 ] += element.position[ 0 ] * ratio;
884
- attribute.array[ index * 3 + 1 ] += element.position[ 1 ] * ratio;
885
- attribute.array[ index * 3 + 2 ] += element.position[ 2 ] * ratio;
886
-
887
- }
888
-
889
- }
890
-
891
- for ( let i = 0; i < data.metadata.morphCount; i ++ ) {
892
-
893
- const morph = data.morphs[ i ];
894
- const params = { name: morph.name };
895
-
896
- const attribute = new Float32BufferAttribute( data.metadata.vertexCount * 3, 3 );
897
- attribute.name = morph.name;
898
-
899
- for ( let j = 0; j < data.metadata.vertexCount * 3; j ++ ) {
900
-
901
- attribute.array[ j ] = positions[ j ];
902
-
903
- }
904
-
905
- if ( data.metadata.format === 'pmd' ) {
906
-
907
- if ( i !== 0 ) {
908
-
909
- updateAttributes( attribute, morph, 1.0 );
910
-
911
- }
912
-
913
- } else {
914
-
915
- if ( morph.type === 0 ) { // group
916
-
917
- for ( let j = 0; j < morph.elementCount; j ++ ) {
918
-
919
- const morph2 = data.morphs[ morph.elements[ j ].index ];
920
- const ratio = morph.elements[ j ].ratio;
921
-
922
- if ( morph2.type === 1 ) {
923
-
924
- updateAttributes( attribute, morph2, ratio );
925
-
926
- } else {
927
-
928
- // TODO: implement
929
-
930
- }
931
-
932
- }
933
-
934
- } else if ( morph.type === 1 ) { // vertex
935
-
936
- updateAttributes( attribute, morph, 1.0 );
937
-
938
- } else if ( morph.type === 2 ) { // bone
939
-
940
- // TODO: implement
941
-
942
- } else if ( morph.type === 3 ) { // uv
943
-
944
- // TODO: implement
945
-
946
- } else if ( morph.type === 4 ) { // additional uv1
947
-
948
- // TODO: implement
949
-
950
- } else if ( morph.type === 5 ) { // additional uv2
951
-
952
- // TODO: implement
953
-
954
- } else if ( morph.type === 6 ) { // additional uv3
955
-
956
- // TODO: implement
957
-
958
- } else if ( morph.type === 7 ) { // additional uv4
959
-
960
- // TODO: implement
961
-
962
- } else if ( morph.type === 8 ) { // material
963
-
964
- // TODO: implement
965
-
966
- }
967
-
968
- }
969
-
970
- morphTargets.push( params );
971
- morphPositions.push( attribute );
972
-
973
- }
974
-
975
- // rigid bodies from rigidBodies field.
976
-
977
- for ( let i = 0; i < data.metadata.rigidBodyCount; i ++ ) {
978
-
979
- const rigidBody = data.rigidBodies[ i ];
980
- const params = {};
981
-
982
- for ( const key in rigidBody ) {
983
-
984
- params[ key ] = rigidBody[ key ];
985
-
986
- }
987
-
988
- /*
989
- * RigidBody position parameter in PMX seems global position
990
- * while the one in PMD seems offset from corresponding bone.
991
- * So unify being offset.
992
- */
993
- if ( data.metadata.format === 'pmx' ) {
994
-
995
- if ( params.boneIndex !== - 1 ) {
996
-
997
- const bone = data.bones[ params.boneIndex ];
998
- params.position[ 0 ] -= bone.position[ 0 ];
999
- params.position[ 1 ] -= bone.position[ 1 ];
1000
- params.position[ 2 ] -= bone.position[ 2 ];
1001
-
1002
- }
1003
-
1004
- }
1005
-
1006
- rigidBodies.push( params );
1007
-
1008
- }
1009
-
1010
- // constraints from constraints field.
1011
-
1012
- for ( let i = 0; i < data.metadata.constraintCount; i ++ ) {
1013
-
1014
- const constraint = data.constraints[ i ];
1015
- const params = {};
1016
-
1017
- for ( const key in constraint ) {
1018
-
1019
- params[ key ] = constraint[ key ];
1020
-
1021
- }
1022
-
1023
- const bodyA = rigidBodies[ params.rigidBodyIndex1 ];
1024
- const bodyB = rigidBodies[ params.rigidBodyIndex2 ];
1025
-
1026
- // Refer to http://www20.atpages.jp/katwat/wp/?p=4135
1027
- if ( bodyA.type !== 0 && bodyB.type === 2 ) {
1028
-
1029
- if ( bodyA.boneIndex !== - 1 && bodyB.boneIndex !== - 1 &&
1030
- data.bones[ bodyB.boneIndex ].parentIndex === bodyA.boneIndex ) {
1031
-
1032
- bodyB.type = 1;
1033
-
1034
- }
1035
-
1036
- }
1037
-
1038
- constraints.push( params );
1039
-
1040
- }
1041
-
1042
- // build BufferGeometry.
1043
-
1044
- const geometry = new BufferGeometry();
1045
-
1046
- geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
1047
- geometry.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
1048
- geometry.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
1049
- geometry.setAttribute( 'skinIndex', new Uint16BufferAttribute( skinIndices, 4 ) );
1050
- geometry.setAttribute( 'skinWeight', new Float32BufferAttribute( skinWeights, 4 ) );
1051
- geometry.setIndex( indices );
1052
-
1053
- for ( let i = 0, il = groups.length; i < il; i ++ ) {
1054
-
1055
- geometry.addGroup( groups[ i ].offset, groups[ i ].count, i );
1056
-
1057
- }
1058
-
1059
- geometry.bones = bones;
1060
-
1061
- geometry.morphTargets = morphTargets;
1062
- geometry.morphAttributes.position = morphPositions;
1063
- geometry.morphTargetsRelative = false;
1064
-
1065
- geometry.userData.MMD = {
1066
- bones: bones,
1067
- iks: iks,
1068
- grants: grants,
1069
- rigidBodies: rigidBodies,
1070
- constraints: constraints,
1071
- format: data.metadata.format
1072
- };
1073
-
1074
- geometry.computeBoundingSphere();
1075
-
1076
- return geometry;
1077
-
1078
- }
1079
-
1080
- }
1081
-
1082
- //
1083
-
1084
- /**
1085
- * @param {THREE.LoadingManager} manager
1086
- */
1087
- class MaterialBuilder {
1088
-
1089
- constructor( manager ) {
1090
-
1091
- this.manager = manager;
1092
-
1093
- this.textureLoader = new TextureLoader( this.manager );
1094
- this.tgaLoader = null; // lazy generation
1095
-
1096
- this.crossOrigin = 'anonymous';
1097
- this.resourcePath = undefined;
1098
-
1099
- }
1100
-
1101
- /**
1102
- * @param {string} crossOrigin
1103
- * @return {MaterialBuilder}
1104
- */
1105
- setCrossOrigin( crossOrigin ) {
1106
-
1107
- this.crossOrigin = crossOrigin;
1108
- return this;
1109
-
1110
- }
1111
-
1112
- /**
1113
- * @param {string} resourcePath
1114
- * @return {MaterialBuilder}
1115
- */
1116
- setResourcePath( resourcePath ) {
1117
-
1118
- this.resourcePath = resourcePath;
1119
- return this;
1120
-
1121
- }
1122
-
1123
- /**
1124
- * @param {Object} data - parsed PMD/PMX data
1125
- * @param {BufferGeometry} geometry - some properties are dependend on geometry
1126
- * @param {function} onProgress
1127
- * @param {function} onError
1128
- * @return {Array<MMDToonMaterial>}
1129
- */
1130
- build( data, geometry /*, onProgress, onError */ ) {
1131
-
1132
- const materials = [];
1133
-
1134
- const textures = {};
1135
-
1136
- this.textureLoader.setCrossOrigin( this.crossOrigin );
1137
-
1138
- // materials
1139
-
1140
- for ( let i = 0; i < data.metadata.materialCount; i ++ ) {
1141
-
1142
- const material = data.materials[ i ];
1143
-
1144
- const params = { userData: { MMD: {} } };
1145
-
1146
- if ( material.name !== undefined ) params.name = material.name;
1147
-
1148
- /*
1149
- * Color
1150
- *
1151
- * MMD MMDToonMaterial
1152
- * ambient - emissive * a
1153
- * (a = 1.0 without map texture or 0.2 with map texture)
1154
- *
1155
- * MMDToonMaterial doesn't have ambient. Set it to emissive instead.
1156
- * It'll be too bright if material has map texture so using coef 0.2.
1157
- */
1158
- params.diffuse = new Color().setRGB(
1159
- material.diffuse[ 0 ],
1160
- material.diffuse[ 1 ],
1161
- material.diffuse[ 2 ],
1162
- SRGBColorSpace
1163
- );
1164
- params.opacity = material.diffuse[ 3 ];
1165
- params.specular = new Color().setRGB( ...material.specular, SRGBColorSpace );
1166
- params.shininess = material.shininess;
1167
- params.emissive = new Color().setRGB( ...material.ambient, SRGBColorSpace );
1168
- params.transparent = params.opacity !== 1.0;
1169
-
1170
- //
1171
-
1172
- params.fog = true;
1173
-
1174
- // blend
1175
-
1176
- params.blending = CustomBlending;
1177
- params.blendSrc = SrcAlphaFactor;
1178
- params.blendDst = OneMinusSrcAlphaFactor;
1179
- params.blendSrcAlpha = SrcAlphaFactor;
1180
- params.blendDstAlpha = DstAlphaFactor;
1181
-
1182
- // side
1183
-
1184
- if ( data.metadata.format === 'pmx' && ( material.flag & 0x1 ) === 1 ) {
1185
-
1186
- params.side = DoubleSide;
1187
-
1188
- } else {
1189
-
1190
- params.side = params.opacity === 1.0 ? FrontSide : DoubleSide;
1191
-
1192
- }
1193
-
1194
- if ( data.metadata.format === 'pmd' ) {
1195
-
1196
- // map, matcap
1197
-
1198
- if ( material.fileName ) {
1199
-
1200
- const fileName = material.fileName;
1201
- const fileNames = fileName.split( '*' );
1202
-
1203
- // fileNames[ 0 ]: mapFileName
1204
- // fileNames[ 1 ]: matcapFileName( optional )
1205
-
1206
- params.map = this._loadTexture( fileNames[ 0 ], textures );
1207
-
1208
- if ( fileNames.length > 1 ) {
1209
-
1210
- const extension = fileNames[ 1 ].slice( - 4 ).toLowerCase();
1211
-
1212
- params.matcap = this._loadTexture(
1213
- fileNames[ 1 ],
1214
- textures
1215
- );
1216
-
1217
- params.matcapCombine = extension === '.sph'
1218
- ? MultiplyOperation
1219
- : AddOperation;
1220
-
1221
- }
1222
-
1223
- }
1224
-
1225
- // gradientMap
1226
-
1227
- const toonFileName = ( material.toonIndex === - 1 )
1228
- ? 'toon00.bmp'
1229
- : data.toonTextures[ material.toonIndex ].fileName;
1230
-
1231
- params.gradientMap = this._loadTexture(
1232
- toonFileName,
1233
- textures,
1234
- {
1235
- isToonTexture: true,
1236
- isDefaultToonTexture: this._isDefaultToonTexture( toonFileName )
1237
- }
1238
- );
1239
-
1240
- // parameters for OutlineEffect
1241
-
1242
- params.userData.outlineParameters = {
1243
- thickness: material.edgeFlag === 1 ? 0.003 : 0.0,
1244
- color: [ 0, 0, 0 ],
1245
- alpha: 1.0,
1246
- visible: material.edgeFlag === 1
1247
- };
1248
-
1249
- } else {
1250
-
1251
- // map
1252
-
1253
- if ( material.textureIndex !== - 1 ) {
1254
-
1255
- params.map = this._loadTexture( data.textures[ material.textureIndex ], textures );
1256
-
1257
- // Since PMX spec don't have standard to list map files except color map and env map,
1258
- // we need to save file name for further mapping, like matching normal map file names after model loaded.
1259
- // ref: https://gist.github.com/felixjones/f8a06bd48f9da9a4539f#texture
1260
- params.userData.MMD.mapFileName = data.textures[ material.textureIndex ];
1261
-
1262
- }
1263
-
1264
- // matcap TODO: support m.envFlag === 3
1265
-
1266
- if ( material.envTextureIndex !== - 1 && ( material.envFlag === 1 || material.envFlag == 2 ) ) {
1267
-
1268
- params.matcap = this._loadTexture(
1269
- data.textures[ material.envTextureIndex ],
1270
- textures
1271
- );
1272
-
1273
- // Same as color map above, keep file name in userData for further usage.
1274
- params.userData.MMD.matcapFileName = data.textures[ material.envTextureIndex ];
1275
-
1276
- params.matcapCombine = material.envFlag === 1
1277
- ? MultiplyOperation
1278
- : AddOperation;
1279
-
1280
- }
1281
-
1282
- // gradientMap
1283
-
1284
- let toonFileName, isDefaultToon;
1285
-
1286
- if ( material.toonIndex === - 1 || material.toonFlag !== 0 ) {
1287
-
1288
- toonFileName = 'toon' + ( '0' + ( material.toonIndex + 1 ) ).slice( - 2 ) + '.bmp';
1289
- isDefaultToon = true;
1290
-
1291
- } else {
1292
-
1293
- toonFileName = data.textures[ material.toonIndex ];
1294
- isDefaultToon = false;
1295
-
1296
- }
1297
-
1298
- params.gradientMap = this._loadTexture(
1299
- toonFileName,
1300
- textures,
1301
- {
1302
- isToonTexture: true,
1303
- isDefaultToonTexture: isDefaultToon
1304
- }
1305
- );
1306
-
1307
- // parameters for OutlineEffect
1308
- params.userData.outlineParameters = {
1309
- thickness: material.edgeSize / 300, // TODO: better calculation?
1310
- color: material.edgeColor.slice( 0, 3 ),
1311
- alpha: material.edgeColor[ 3 ],
1312
- visible: ( material.flag & 0x10 ) !== 0 && material.edgeSize > 0.0
1313
- };
1314
-
1315
- }
1316
-
1317
- if ( params.map !== undefined ) {
1318
-
1319
- if ( ! params.transparent ) {
1320
-
1321
- this._checkImageTransparency( params.map, geometry, i );
1322
-
1323
- }
1324
-
1325
- params.emissive.multiplyScalar( 0.2 );
1326
-
1327
- }
1328
-
1329
- materials.push( new MMDToonMaterial( params ) );
1330
-
1331
- }
1332
-
1333
- if ( data.metadata.format === 'pmx' ) {
1334
-
1335
- // set transparent true if alpha morph is defined.
1336
-
1337
- function checkAlphaMorph( elements, materials ) {
1338
-
1339
- for ( let i = 0, il = elements.length; i < il; i ++ ) {
1340
-
1341
- const element = elements[ i ];
1342
-
1343
- if ( element.index === - 1 ) continue;
1344
-
1345
- const material = materials[ element.index ];
1346
-
1347
- if ( material.opacity !== element.diffuse[ 3 ] ) {
1348
-
1349
- material.transparent = true;
1350
-
1351
- }
1352
-
1353
- }
1354
-
1355
- }
1356
-
1357
- for ( let i = 0, il = data.morphs.length; i < il; i ++ ) {
1358
-
1359
- const morph = data.morphs[ i ];
1360
- const elements = morph.elements;
1361
-
1362
- if ( morph.type === 0 ) {
1363
-
1364
- for ( let j = 0, jl = elements.length; j < jl; j ++ ) {
1365
-
1366
- const morph2 = data.morphs[ elements[ j ].index ];
1367
-
1368
- if ( morph2.type !== 8 ) continue;
1369
-
1370
- checkAlphaMorph( morph2.elements, materials );
1371
-
1372
- }
1373
-
1374
- } else if ( morph.type === 8 ) {
1375
-
1376
- checkAlphaMorph( elements, materials );
1377
-
1378
- }
1379
-
1380
- }
1381
-
1382
- }
1383
-
1384
- return materials;
1385
-
1386
- }
1387
-
1388
- // private methods
1389
-
1390
- _getTGALoader() {
1391
-
1392
- if ( this.tgaLoader === null ) {
1393
-
1394
- if ( TGALoader === undefined ) {
1395
-
1396
- throw new Error( 'THREE.MMDLoader: Import TGALoader' );
1397
-
1398
- }
1399
-
1400
- this.tgaLoader = new TGALoader( this.manager );
1401
-
1402
- }
1403
-
1404
- return this.tgaLoader;
1405
-
1406
- }
1407
-
1408
- _isDefaultToonTexture( name ) {
1409
-
1410
- if ( name.length !== 10 ) return false;
1411
-
1412
- return /toon(10|0[0-9])\.bmp/.test( name );
1413
-
1414
- }
1415
-
1416
- _loadTexture( filePath, textures, params, onProgress, onError ) {
1417
-
1418
- params = params || {};
1419
-
1420
- const scope = this;
1421
-
1422
- let fullPath;
1423
-
1424
- if ( params.isDefaultToonTexture === true ) {
1425
-
1426
- let index;
1427
-
1428
- try {
1429
-
1430
- index = parseInt( filePath.match( /toon([0-9]{2})\.bmp$/ )[ 1 ] );
1431
-
1432
- } catch ( e ) {
1433
-
1434
- console.warn( 'THREE.MMDLoader: ' + filePath + ' seems like a '
1435
- + 'not right default texture path. Using toon00.bmp instead.' );
1436
-
1437
- index = 0;
1438
-
1439
- }
1440
-
1441
- fullPath = DEFAULT_TOON_TEXTURES[ index ];
1442
-
1443
- } else {
1444
-
1445
- fullPath = this.resourcePath + filePath;
1446
-
1447
- }
1448
-
1449
- if ( textures[ fullPath ] !== undefined ) return textures[ fullPath ];
1450
-
1451
- let loader = this.manager.getHandler( fullPath );
1452
-
1453
- if ( loader === null ) {
1454
-
1455
- loader = ( filePath.slice( - 4 ).toLowerCase() === '.tga' )
1456
- ? this._getTGALoader()
1457
- : this.textureLoader;
1458
-
1459
- }
1460
-
1461
- const texture = loader.load( fullPath, function ( t ) {
1462
-
1463
- // MMD toon texture is Axis-Y oriented
1464
- // but Three.js gradient map is Axis-X oriented.
1465
- // So here replaces the toon texture image with the rotated one.
1466
- if ( params.isToonTexture === true ) {
1467
-
1468
- t.image = scope._getRotatedImage( t.image );
1469
-
1470
- t.magFilter = NearestFilter;
1471
- t.minFilter = NearestFilter;
1472
- t.generateMipmaps = false;
1473
-
1474
- }
1475
-
1476
- t.flipY = false;
1477
- t.wrapS = RepeatWrapping;
1478
- t.wrapT = RepeatWrapping;
1479
- t.colorSpace = SRGBColorSpace;
1480
-
1481
- for ( let i = 0; i < texture.readyCallbacks.length; i ++ ) {
1482
-
1483
- texture.readyCallbacks[ i ]( texture );
1484
-
1485
- }
1486
-
1487
- delete texture.readyCallbacks;
1488
-
1489
- }, onProgress, onError );
1490
-
1491
- texture.readyCallbacks = [];
1492
-
1493
- textures[ fullPath ] = texture;
1494
-
1495
- return texture;
1496
-
1497
- }
1498
-
1499
- _getRotatedImage( image ) {
1500
-
1501
- const canvas = document.createElement( 'canvas' );
1502
- const context = canvas.getContext( '2d' );
1503
-
1504
- const width = image.width;
1505
- const height = image.height;
1506
-
1507
- canvas.width = width;
1508
- canvas.height = height;
1509
-
1510
- context.clearRect( 0, 0, width, height );
1511
- context.translate( width / 2.0, height / 2.0 );
1512
- context.rotate( 0.5 * Math.PI ); // 90.0 * Math.PI / 180.0
1513
- context.translate( - width / 2.0, - height / 2.0 );
1514
- context.drawImage( image, 0, 0 );
1515
-
1516
- return context.getImageData( 0, 0, width, height );
1517
-
1518
- }
1519
-
1520
- // Check if the partial image area used by the texture is transparent.
1521
- _checkImageTransparency( map, geometry, groupIndex ) {
1522
-
1523
- map.readyCallbacks.push( function ( texture ) {
1524
-
1525
- // Is there any efficient ways?
1526
- function createImageData( image ) {
1527
-
1528
- const canvas = document.createElement( 'canvas' );
1529
- canvas.width = image.width;
1530
- canvas.height = image.height;
1531
-
1532
- const context = canvas.getContext( '2d' );
1533
- context.drawImage( image, 0, 0 );
1534
-
1535
- return context.getImageData( 0, 0, canvas.width, canvas.height );
1536
-
1537
- }
1538
-
1539
- function detectImageTransparency( image, uvs, indices ) {
1540
-
1541
- const width = image.width;
1542
- const height = image.height;
1543
- const data = image.data;
1544
- const threshold = 253;
1545
-
1546
- if ( data.length / ( width * height ) !== 4 ) return false;
1547
-
1548
- for ( let i = 0; i < indices.length; i += 3 ) {
1549
-
1550
- const centerUV = { x: 0.0, y: 0.0 };
1551
-
1552
- for ( let j = 0; j < 3; j ++ ) {
1553
-
1554
- const index = indices[ i * 3 + j ];
1555
- const uv = { x: uvs[ index * 2 + 0 ], y: uvs[ index * 2 + 1 ] };
1556
-
1557
- if ( getAlphaByUv( image, uv ) < threshold ) return true;
1558
-
1559
- centerUV.x += uv.x;
1560
- centerUV.y += uv.y;
1561
-
1562
- }
1563
-
1564
- centerUV.x /= 3;
1565
- centerUV.y /= 3;
1566
-
1567
- if ( getAlphaByUv( image, centerUV ) < threshold ) return true;
1568
-
1569
- }
1570
-
1571
- return false;
1572
-
1573
- }
1574
-
1575
- /*
1576
- * This method expects
1577
- * texture.flipY = false
1578
- * texture.wrapS = RepeatWrapping
1579
- * texture.wrapT = RepeatWrapping
1580
- * TODO: more precise
1581
- */
1582
- function getAlphaByUv( image, uv ) {
1583
-
1584
- const width = image.width;
1585
- const height = image.height;
1586
-
1587
- let x = Math.round( uv.x * width ) % width;
1588
- let y = Math.round( uv.y * height ) % height;
1589
-
1590
- if ( x < 0 ) x += width;
1591
- if ( y < 0 ) y += height;
1592
-
1593
- const index = y * width + x;
1594
-
1595
- return image.data[ index * 4 + 3 ];
1596
-
1597
- }
1598
-
1599
- if ( texture.isCompressedTexture === true ) {
1600
-
1601
- if ( NON_ALPHA_CHANNEL_FORMATS.includes( texture.format ) ) {
1602
-
1603
- map.transparent = false;
1604
-
1605
- } else {
1606
-
1607
- // any other way to check transparency of CompressedTexture?
1608
- map.transparent = true;
1609
-
1610
- }
1611
-
1612
- return;
1613
-
1614
- }
1615
-
1616
- const imageData = texture.image.data !== undefined
1617
- ? texture.image
1618
- : createImageData( texture.image );
1619
-
1620
- const group = geometry.groups[ groupIndex ];
1621
-
1622
- if ( detectImageTransparency(
1623
- imageData,
1624
- geometry.attributes.uv.array,
1625
- geometry.index.array.slice( group.start, group.start + group.count ) ) ) {
1626
-
1627
- map.transparent = true;
1628
-
1629
- }
1630
-
1631
- } );
1632
-
1633
- }
1634
-
1635
- }
1636
-
1637
- //
1638
-
1639
- class AnimationBuilder {
1640
-
1641
- /**
1642
- * @param {Object} vmd - parsed VMD data
1643
- * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
1644
- * @return {AnimationClip}
1645
- */
1646
- build( vmd, mesh ) {
1647
-
1648
- // combine skeletal and morph animations
1649
-
1650
- const tracks = this.buildSkeletalAnimation( vmd, mesh ).tracks;
1651
- const tracks2 = this.buildMorphAnimation( vmd, mesh ).tracks;
1652
-
1653
- for ( let i = 0, il = tracks2.length; i < il; i ++ ) {
1654
-
1655
- tracks.push( tracks2[ i ] );
1656
-
1657
- }
1658
-
1659
- return new AnimationClip( '', - 1, tracks );
1660
-
1661
- }
1662
-
1663
- /**
1664
- * @param {Object} vmd - parsed VMD data
1665
- * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
1666
- * @return {AnimationClip}
1667
- */
1668
- buildSkeletalAnimation( vmd, mesh ) {
1669
-
1670
- function pushInterpolation( array, interpolation, index ) {
1671
-
1672
- array.push( interpolation[ index + 0 ] / 127 ); // x1
1673
- array.push( interpolation[ index + 8 ] / 127 ); // x2
1674
- array.push( interpolation[ index + 4 ] / 127 ); // y1
1675
- array.push( interpolation[ index + 12 ] / 127 ); // y2
1676
-
1677
- }
1678
-
1679
- const tracks = [];
1680
-
1681
- const motions = {};
1682
- const bones = mesh.skeleton.bones;
1683
- const boneNameDictionary = {};
1684
-
1685
- for ( let i = 0, il = bones.length; i < il; i ++ ) {
1686
-
1687
- boneNameDictionary[ bones[ i ].name ] = true;
1688
-
1689
- }
1690
-
1691
- for ( let i = 0; i < vmd.metadata.motionCount; i ++ ) {
1692
-
1693
- const motion = vmd.motions[ i ];
1694
- const boneName = motion.boneName;
1695
-
1696
- if ( boneNameDictionary[ boneName ] === undefined ) continue;
1697
-
1698
- motions[ boneName ] = motions[ boneName ] || [];
1699
- motions[ boneName ].push( motion );
1700
-
1701
- }
1702
-
1703
- for ( const key in motions ) {
1704
-
1705
- const array = motions[ key ];
1706
-
1707
- array.sort( function ( a, b ) {
1708
-
1709
- return a.frameNum - b.frameNum;
1710
-
1711
- } );
1712
-
1713
- const times = [];
1714
- const positions = [];
1715
- const rotations = [];
1716
- const pInterpolations = [];
1717
- const rInterpolations = [];
1718
-
1719
- const basePosition = mesh.skeleton.getBoneByName( key ).position.toArray();
1720
-
1721
- for ( let i = 0, il = array.length; i < il; i ++ ) {
1722
-
1723
- const time = array[ i ].frameNum / 30;
1724
- const position = array[ i ].position;
1725
- const rotation = array[ i ].rotation;
1726
- const interpolation = array[ i ].interpolation;
1727
-
1728
- times.push( time );
1729
-
1730
- for ( let j = 0; j < 3; j ++ ) positions.push( basePosition[ j ] + position[ j ] );
1731
- for ( let j = 0; j < 4; j ++ ) rotations.push( rotation[ j ] );
1732
- for ( let j = 0; j < 3; j ++ ) pushInterpolation( pInterpolations, interpolation, j );
1733
-
1734
- pushInterpolation( rInterpolations, interpolation, 3 );
1735
-
1736
- }
1737
-
1738
- const targetName = '.bones[' + key + ']';
1739
-
1740
- tracks.push( this._createTrack( targetName + '.position', VectorKeyframeTrack, times, positions, pInterpolations ) );
1741
- tracks.push( this._createTrack( targetName + '.quaternion', QuaternionKeyframeTrack, times, rotations, rInterpolations ) );
1742
-
1743
- }
1744
-
1745
- return new AnimationClip( '', - 1, tracks );
1746
-
1747
- }
1748
-
1749
- /**
1750
- * @param {Object} vmd - parsed VMD data
1751
- * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
1752
- * @return {AnimationClip}
1753
- */
1754
- buildMorphAnimation( vmd, mesh ) {
1755
-
1756
- const tracks = [];
1757
-
1758
- const morphs = {};
1759
- const morphTargetDictionary = mesh.morphTargetDictionary;
1760
-
1761
- for ( let i = 0; i < vmd.metadata.morphCount; i ++ ) {
1762
-
1763
- const morph = vmd.morphs[ i ];
1764
- const morphName = morph.morphName;
1765
-
1766
- if ( morphTargetDictionary[ morphName ] === undefined ) continue;
1767
-
1768
- morphs[ morphName ] = morphs[ morphName ] || [];
1769
- morphs[ morphName ].push( morph );
1770
-
1771
- }
1772
-
1773
- for ( const key in morphs ) {
1774
-
1775
- const array = morphs[ key ];
1776
-
1777
- array.sort( function ( a, b ) {
1778
-
1779
- return a.frameNum - b.frameNum;
1780
-
1781
- } );
1782
-
1783
- const times = [];
1784
- const values = [];
1785
-
1786
- for ( let i = 0, il = array.length; i < il; i ++ ) {
1787
-
1788
- times.push( array[ i ].frameNum / 30 );
1789
- values.push( array[ i ].weight );
1790
-
1791
- }
1792
-
1793
- tracks.push( new NumberKeyframeTrack( '.morphTargetInfluences[' + morphTargetDictionary[ key ] + ']', times, values ) );
1794
-
1795
- }
1796
-
1797
- return new AnimationClip( '', - 1, tracks );
1798
-
1799
- }
1800
-
1801
- /**
1802
- * @param {Object} vmd - parsed VMD data
1803
- * @return {AnimationClip}
1804
- */
1805
- buildCameraAnimation( vmd ) {
1806
-
1807
- function pushVector3( array, vec ) {
1808
-
1809
- array.push( vec.x );
1810
- array.push( vec.y );
1811
- array.push( vec.z );
1812
-
1813
- }
1814
-
1815
- function pushQuaternion( array, q ) {
1816
-
1817
- array.push( q.x );
1818
- array.push( q.y );
1819
- array.push( q.z );
1820
- array.push( q.w );
1821
-
1822
- }
1823
-
1824
- function pushInterpolation( array, interpolation, index ) {
1825
-
1826
- array.push( interpolation[ index * 4 + 0 ] / 127 ); // x1
1827
- array.push( interpolation[ index * 4 + 1 ] / 127 ); // x2
1828
- array.push( interpolation[ index * 4 + 2 ] / 127 ); // y1
1829
- array.push( interpolation[ index * 4 + 3 ] / 127 ); // y2
1830
-
1831
- }
1832
-
1833
- const cameras = vmd.cameras === undefined ? [] : vmd.cameras.slice();
1834
-
1835
- cameras.sort( function ( a, b ) {
1836
-
1837
- return a.frameNum - b.frameNum;
1838
-
1839
- } );
1840
-
1841
- const times = [];
1842
- const centers = [];
1843
- const quaternions = [];
1844
- const positions = [];
1845
- const fovs = [];
1846
-
1847
- const cInterpolations = [];
1848
- const qInterpolations = [];
1849
- const pInterpolations = [];
1850
- const fInterpolations = [];
1851
-
1852
- const quaternion = new Quaternion();
1853
- const euler = new Euler();
1854
- const position = new Vector3();
1855
- const center = new Vector3();
1856
-
1857
- for ( let i = 0, il = cameras.length; i < il; i ++ ) {
1858
-
1859
- const motion = cameras[ i ];
1860
-
1861
- const time = motion.frameNum / 30;
1862
- const pos = motion.position;
1863
- const rot = motion.rotation;
1864
- const distance = motion.distance;
1865
- const fov = motion.fov;
1866
- const interpolation = motion.interpolation;
1867
-
1868
- times.push( time );
1869
-
1870
- position.set( 0, 0, - distance );
1871
- center.set( pos[ 0 ], pos[ 1 ], pos[ 2 ] );
1872
-
1873
- euler.set( - rot[ 0 ], - rot[ 1 ], - rot[ 2 ] );
1874
- quaternion.setFromEuler( euler );
1875
-
1876
- position.add( center );
1877
- position.applyQuaternion( quaternion );
1878
-
1879
- pushVector3( centers, center );
1880
- pushQuaternion( quaternions, quaternion );
1881
- pushVector3( positions, position );
1882
-
1883
- fovs.push( fov );
1884
-
1885
- for ( let j = 0; j < 3; j ++ ) {
1886
-
1887
- pushInterpolation( cInterpolations, interpolation, j );
1888
-
1889
- }
1890
-
1891
- pushInterpolation( qInterpolations, interpolation, 3 );
1892
-
1893
- // use the same parameter for x, y, z axis.
1894
- for ( let j = 0; j < 3; j ++ ) {
1895
-
1896
- pushInterpolation( pInterpolations, interpolation, 4 );
1897
-
1898
- }
1899
-
1900
- pushInterpolation( fInterpolations, interpolation, 5 );
1901
-
1902
- }
1903
-
1904
- const tracks = [];
1905
-
1906
- // I expect an object whose name 'target' exists under THREE.Camera
1907
- tracks.push( this._createTrack( 'target.position', VectorKeyframeTrack, times, centers, cInterpolations ) );
1908
-
1909
- tracks.push( this._createTrack( '.quaternion', QuaternionKeyframeTrack, times, quaternions, qInterpolations ) );
1910
- tracks.push( this._createTrack( '.position', VectorKeyframeTrack, times, positions, pInterpolations ) );
1911
- tracks.push( this._createTrack( '.fov', NumberKeyframeTrack, times, fovs, fInterpolations ) );
1912
-
1913
- return new AnimationClip( '', - 1, tracks );
1914
-
1915
- }
1916
-
1917
- // private method
1918
-
1919
- _createTrack( node, typedKeyframeTrack, times, values, interpolations ) {
1920
-
1921
- /*
1922
- * optimizes here not to let KeyframeTrackPrototype optimize
1923
- * because KeyframeTrackPrototype optimizes times and values but
1924
- * doesn't optimize interpolations.
1925
- */
1926
- if ( times.length > 2 ) {
1927
-
1928
- times = times.slice();
1929
- values = values.slice();
1930
- interpolations = interpolations.slice();
1931
-
1932
- const stride = values.length / times.length;
1933
- const interpolateStride = interpolations.length / times.length;
1934
-
1935
- let index = 1;
1936
-
1937
- for ( let aheadIndex = 2, endIndex = times.length; aheadIndex < endIndex; aheadIndex ++ ) {
1938
-
1939
- for ( let i = 0; i < stride; i ++ ) {
1940
-
1941
- if ( values[ index * stride + i ] !== values[ ( index - 1 ) * stride + i ] ||
1942
- values[ index * stride + i ] !== values[ aheadIndex * stride + i ] ) {
1943
-
1944
- index ++;
1945
- break;
1946
-
1947
- }
1948
-
1949
- }
1950
-
1951
- if ( aheadIndex > index ) {
1952
-
1953
- times[ index ] = times[ aheadIndex ];
1954
-
1955
- for ( let i = 0; i < stride; i ++ ) {
1956
-
1957
- values[ index * stride + i ] = values[ aheadIndex * stride + i ];
1958
-
1959
- }
1960
-
1961
- for ( let i = 0; i < interpolateStride; i ++ ) {
1962
-
1963
- interpolations[ index * interpolateStride + i ] = interpolations[ aheadIndex * interpolateStride + i ];
1964
-
1965
- }
1966
-
1967
- }
1968
-
1969
- }
1970
-
1971
- times.length = index + 1;
1972
- values.length = ( index + 1 ) * stride;
1973
- interpolations.length = ( index + 1 ) * interpolateStride;
1974
-
1975
- }
1976
-
1977
- const track = new typedKeyframeTrack( node, times, values );
1978
-
1979
- track.createInterpolant = function InterpolantFactoryMethodCubicBezier( result ) {
1980
-
1981
- return new CubicBezierInterpolation( this.times, this.values, this.getValueSize(), result, new Float32Array( interpolations ) );
1982
-
1983
- };
1984
-
1985
- return track;
1986
-
1987
- }
1988
-
1989
- }
1990
-
1991
- // interpolation
1992
-
1993
- class CubicBezierInterpolation extends Interpolant {
1994
-
1995
- constructor( parameterPositions, sampleValues, sampleSize, resultBuffer, params ) {
1996
-
1997
- super( parameterPositions, sampleValues, sampleSize, resultBuffer );
1998
-
1999
- this.interpolationParams = params;
2000
-
2001
- }
2002
-
2003
- interpolate_( i1, t0, t, t1 ) {
2004
-
2005
- const result = this.resultBuffer;
2006
- const values = this.sampleValues;
2007
- const stride = this.valueSize;
2008
- const params = this.interpolationParams;
2009
-
2010
- const offset1 = i1 * stride;
2011
- const offset0 = offset1 - stride;
2012
-
2013
- // No interpolation if next key frame is in one frame in 30fps.
2014
- // This is from MMD animation spec.
2015
- // '1.5' is for precision loss. times are Float32 in Three.js Animation system.
2016
- const weight1 = ( ( t1 - t0 ) < 1 / 30 * 1.5 ) ? 0.0 : ( t - t0 ) / ( t1 - t0 );
2017
-
2018
- if ( stride === 4 ) { // Quaternion
2019
-
2020
- const x1 = params[ i1 * 4 + 0 ];
2021
- const x2 = params[ i1 * 4 + 1 ];
2022
- const y1 = params[ i1 * 4 + 2 ];
2023
- const y2 = params[ i1 * 4 + 3 ];
2024
-
2025
- const ratio = this._calculate( x1, x2, y1, y2, weight1 );
2026
-
2027
- Quaternion.slerpFlat( result, 0, values, offset0, values, offset1, ratio );
2028
-
2029
- } else if ( stride === 3 ) { // Vector3
2030
-
2031
- for ( let i = 0; i !== stride; ++ i ) {
2032
-
2033
- const x1 = params[ i1 * 12 + i * 4 + 0 ];
2034
- const x2 = params[ i1 * 12 + i * 4 + 1 ];
2035
- const y1 = params[ i1 * 12 + i * 4 + 2 ];
2036
- const y2 = params[ i1 * 12 + i * 4 + 3 ];
2037
-
2038
- const ratio = this._calculate( x1, x2, y1, y2, weight1 );
2039
-
2040
- result[ i ] = values[ offset0 + i ] * ( 1 - ratio ) + values[ offset1 + i ] * ratio;
2041
-
2042
- }
2043
-
2044
- } else { // Number
2045
-
2046
- const x1 = params[ i1 * 4 + 0 ];
2047
- const x2 = params[ i1 * 4 + 1 ];
2048
- const y1 = params[ i1 * 4 + 2 ];
2049
- const y2 = params[ i1 * 4 + 3 ];
2050
-
2051
- const ratio = this._calculate( x1, x2, y1, y2, weight1 );
2052
-
2053
- result[ 0 ] = values[ offset0 ] * ( 1 - ratio ) + values[ offset1 ] * ratio;
2054
-
2055
- }
2056
-
2057
- return result;
2058
-
2059
- }
2060
-
2061
- _calculate( x1, x2, y1, y2, x ) {
2062
-
2063
- /*
2064
- * Cubic Bezier curves
2065
- * https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves
2066
- *
2067
- * B(t) = ( 1 - t ) ^ 3 * P0
2068
- * + 3 * ( 1 - t ) ^ 2 * t * P1
2069
- * + 3 * ( 1 - t ) * t^2 * P2
2070
- * + t ^ 3 * P3
2071
- * ( 0 <= t <= 1 )
2072
- *
2073
- * MMD uses Cubic Bezier curves for bone and camera animation interpolation.
2074
- * http://d.hatena.ne.jp/edvakf/20111016/1318716097
2075
- *
2076
- * x = ( 1 - t ) ^ 3 * x0
2077
- * + 3 * ( 1 - t ) ^ 2 * t * x1
2078
- * + 3 * ( 1 - t ) * t^2 * x2
2079
- * + t ^ 3 * x3
2080
- * y = ( 1 - t ) ^ 3 * y0
2081
- * + 3 * ( 1 - t ) ^ 2 * t * y1
2082
- * + 3 * ( 1 - t ) * t^2 * y2
2083
- * + t ^ 3 * y3
2084
- * ( x0 = 0, y0 = 0 )
2085
- * ( x3 = 1, y3 = 1 )
2086
- * ( 0 <= t, x1, x2, y1, y2 <= 1 )
2087
- *
2088
- * Here solves this equation with Bisection method,
2089
- * https://en.wikipedia.org/wiki/Bisection_method
2090
- * gets t, and then calculate y.
2091
- *
2092
- * f(t) = 3 * ( 1 - t ) ^ 2 * t * x1
2093
- * + 3 * ( 1 - t ) * t^2 * x2
2094
- * + t ^ 3 - x = 0
2095
- *
2096
- * (Another option: Newton's method
2097
- * https://en.wikipedia.org/wiki/Newton%27s_method)
2098
- */
2099
-
2100
- let c = 0.5;
2101
- let t = c;
2102
- let s = 1.0 - t;
2103
- const loop = 15;
2104
- const eps = 1e-5;
2105
- const math = Math;
2106
-
2107
- let sst3, stt3, ttt;
2108
-
2109
- for ( let i = 0; i < loop; i ++ ) {
2110
-
2111
- sst3 = 3.0 * s * s * t;
2112
- stt3 = 3.0 * s * t * t;
2113
- ttt = t * t * t;
2114
-
2115
- const ft = ( sst3 * x1 ) + ( stt3 * x2 ) + ( ttt ) - x;
2116
-
2117
- if ( math.abs( ft ) < eps ) break;
2118
-
2119
- c /= 2.0;
2120
-
2121
- t += ( ft < 0 ) ? c : - c;
2122
- s = 1.0 - t;
2123
-
2124
- }
2125
-
2126
- return ( sst3 * y1 ) + ( stt3 * y2 ) + ttt;
2127
-
2128
- }
2129
-
2130
- }
2131
-
2132
- class MMDToonMaterial extends ShaderMaterial {
2133
-
2134
- constructor( parameters ) {
2135
-
2136
- super();
2137
-
2138
- this.isMMDToonMaterial = true;
2139
-
2140
- this.type = 'MMDToonMaterial';
2141
-
2142
- this._matcapCombine = AddOperation;
2143
- this.emissiveIntensity = 1.0;
2144
- this.normalMapType = TangentSpaceNormalMap;
2145
-
2146
- this.combine = MultiplyOperation;
2147
-
2148
- this.wireframeLinecap = 'round';
2149
- this.wireframeLinejoin = 'round';
2150
-
2151
- this.flatShading = false;
2152
-
2153
- this.lights = true;
2154
-
2155
- this.vertexShader = MMDToonShader.vertexShader;
2156
- this.fragmentShader = MMDToonShader.fragmentShader;
2157
-
2158
- this.defines = Object.assign( {}, MMDToonShader.defines );
2159
- Object.defineProperty( this, 'matcapCombine', {
2160
-
2161
- get: function () {
2162
-
2163
- return this._matcapCombine;
2164
-
2165
- },
2166
-
2167
- set: function ( value ) {
2168
-
2169
- this._matcapCombine = value;
2170
-
2171
- switch ( value ) {
2172
-
2173
- case MultiplyOperation:
2174
- this.defines.MATCAP_BLENDING_MULTIPLY = true;
2175
- delete this.defines.MATCAP_BLENDING_ADD;
2176
- break;
2177
-
2178
- default:
2179
- case AddOperation:
2180
- this.defines.MATCAP_BLENDING_ADD = true;
2181
- delete this.defines.MATCAP_BLENDING_MULTIPLY;
2182
- break;
2183
-
2184
- }
2185
-
2186
- },
2187
-
2188
- } );
2189
-
2190
- this.uniforms = UniformsUtils.clone( MMDToonShader.uniforms );
2191
-
2192
- // merged from MeshToon/Phong/MatcapMaterial
2193
- const exposePropertyNames = [
2194
- 'specular',
2195
- 'opacity',
2196
- 'diffuse',
2197
-
2198
- 'map',
2199
- 'matcap',
2200
- 'gradientMap',
2201
-
2202
- 'lightMap',
2203
- 'lightMapIntensity',
2204
-
2205
- 'aoMap',
2206
- 'aoMapIntensity',
2207
-
2208
- 'emissive',
2209
- 'emissiveMap',
2210
-
2211
- 'bumpMap',
2212
- 'bumpScale',
2213
-
2214
- 'normalMap',
2215
- 'normalScale',
2216
-
2217
- 'displacemantBias',
2218
- 'displacemantMap',
2219
- 'displacemantScale',
2220
-
2221
- 'specularMap',
2222
-
2223
- 'alphaMap',
2224
-
2225
- 'reflectivity',
2226
- 'refractionRatio',
2227
- ];
2228
- for ( const propertyName of exposePropertyNames ) {
2229
-
2230
- Object.defineProperty( this, propertyName, {
2231
-
2232
- get: function () {
2233
-
2234
- return this.uniforms[ propertyName ].value;
2235
-
2236
- },
2237
-
2238
- set: function ( value ) {
2239
-
2240
- this.uniforms[ propertyName ].value = value;
2241
-
2242
- },
2243
-
2244
- } );
2245
-
2246
- }
2247
-
2248
- // Special path for shininess to handle zero shininess properly
2249
- this._shininess = 30;
2250
- Object.defineProperty( this, 'shininess', {
2251
-
2252
- get: function () {
2253
-
2254
- return this._shininess;
2255
-
2256
- },
2257
-
2258
- set: function ( value ) {
2259
-
2260
- this._shininess = value;
2261
- this.uniforms.shininess.value = Math.max( this._shininess, 1e-4 ); // To prevent pow( 0.0, 0.0 )
2262
-
2263
- },
2264
-
2265
- } );
2266
-
2267
- Object.defineProperty(
2268
- this,
2269
- 'color',
2270
- Object.getOwnPropertyDescriptor( this, 'diffuse' )
2271
- );
2272
-
2273
- this.setValues( parameters );
2274
-
2275
- }
2276
-
2277
- copy( source ) {
2278
-
2279
- super.copy( source );
2280
-
2281
- this.matcapCombine = source.matcapCombine;
2282
- this.emissiveIntensity = source.emissiveIntensity;
2283
- this.normalMapType = source.normalMapType;
2284
-
2285
- this.combine = source.combine;
2286
-
2287
- this.wireframeLinecap = source.wireframeLinecap;
2288
- this.wireframeLinejoin = source.wireframeLinejoin;
2289
-
2290
- this.flatShading = source.flatShading;
2291
-
2292
- return this;
2293
-
2294
- }
2295
-
2296
- }
2297
-
2298
- export { MMDLoader };