-
Notifications
You must be signed in to change notification settings - Fork 73
Create GLTF component #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<script> | ||
import { onMount } from 'svelte'; | ||
|
||
import * as THREE from 'three'; | ||
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; | ||
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js'; | ||
import { KTX2Loader } from 'three/examples/jsm/loaders/KTX2Loader'; | ||
import { setup } from '../../utils/context.js'; | ||
import { transform } from '../../utils/object.js'; | ||
import * as defaults from '../../utils/defaults.js'; | ||
|
||
/** @type {string} */ | ||
export let path = null; | ||
/** @type {string} */ | ||
export let filename = null; | ||
export let useDraco = false; | ||
export let useKTX2 = false; | ||
|
||
export let position = defaults.position; | ||
export let rotation = defaults.rotation; | ||
export let scale = defaults.scale; | ||
export let castShadow = false; | ||
export let receiveShadow = false; | ||
export let frustumCulled = true; | ||
export let renderOrder = 0; | ||
|
||
/** @type {Object} */ | ||
let gltfScene = null; | ||
|
||
const { root, parent, self } = setup(new THREE.Object3D()); | ||
|
||
$: { | ||
if(gltfScene) { | ||
gltfScene.traverse( function( node ) { | ||
if ( node.isMesh ) { | ||
node.castShadow = castShadow; | ||
node.receiveShadow = receiveShadow; | ||
node.frustumCulled = frustumCulled; | ||
node.renderOrder = renderOrder; | ||
} | ||
} ); | ||
} | ||
|
||
transform(self, position, rotation, scale); | ||
root.invalidate(); | ||
} | ||
|
||
onMount(() => { | ||
|
||
const loader = new GLTFLoader() | ||
|
||
.setPath( path ) | ||
.setDRACOLoader( useDraco ? new DRACOLoader().setDecoderPath( '/loaders/draco/' ) : null ) | ||
.setKTX2Loader( useKTX2 ? new KTX2Loader() | ||
.setTranscoderPath( '/loaders/basis/' ) | ||
.detectSupport( root.renderer ) : null ) | ||
.load( filename, function ( gltf ) { | ||
gltfScene = gltf.scene; | ||
self.add(gltf.scene); | ||
} ); | ||
|
||
return () => {}; | ||
}); | ||
</script> | ||
|
||
<slot /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<script> | ||
import * as THREE from 'three'; | ||
import * as SC from 'svelte-cubed'; | ||
|
||
let cameraX = 0.0; | ||
|
||
SC.onFrame((elapsedTime, deltaTime) => { | ||
cameraX = Math.pow(Math.sin(elapsedTime/1000),3)*3; | ||
}); | ||
</script> | ||
|
||
<SC.Canvas | ||
antialias | ||
environment={new THREE.TextureLoader().load('/textures/kiara_1_dawn.jpeg', | ||
(tex) => { | ||
tex.mapping = THREE.EquirectangularReflectionMapping; | ||
tex.encoding = THREE.sRGBEncoding; | ||
})} | ||
background={new THREE.TextureLoader().load('/textures/kiara_1_dawn.jpeg', | ||
(tex) => { | ||
tex.mapping = THREE.EquirectangularReflectionMapping; | ||
tex.encoding = THREE.sRGBEncoding; | ||
})} | ||
shadows | ||
> | ||
|
||
<SC.Group position={[0, -1, 0]}> | ||
<SC.Mesh | ||
geometry={new THREE.PlaneGeometry(50, 50)} | ||
material={new THREE.MeshStandardMaterial({ color: 'burlywood' })} | ||
rotation={[-Math.PI / 2, 0, 0]} | ||
receiveShadow | ||
/> | ||
<SC.Primitive | ||
object={new THREE.GridHelper(50, 50, 'papayawhip', 'papayawhip')} | ||
position={[0, 0.001, 0]} | ||
/> | ||
</SC.Group> | ||
|
||
<SC.GLTF path="/gltf/" filename="DamagedHelmet.glb" | ||
position={[0, 0, 0]} | ||
castShadow | ||
receiveShadow /> | ||
<SC.GLTF path="/gltf/waterbottle/" filename="WaterBottle.gltf" | ||
position={[3, 0, 0]} | ||
useDraco | ||
castShadow | ||
receiveShadow /> | ||
<SC.GLTF path="/gltf/stainedglasslamp-KTX2/" filename="StainedGlassLamp.gltf" | ||
position={[-3, 0, 0]} | ||
useKTX2 | ||
castShadow | ||
receiveShadow /> | ||
|
||
<SC.PerspectiveCamera position={[1, 2, 6]} target={[0, 1, 0]} /> | ||
<SC.OrbitControls enableZoom={false} maxPolarAngle={Math.PI * 0.51} /> | ||
<SC.AmbientLight intensity={0.6} /> | ||
<SC.DirectionalLight intensity={0.6} position={[-2, 3, 2]} shadow={{ mapSize: [2048, 2048] }} /> | ||
</SC.Canvas> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"title": "glTF simple" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit: Maybe worth dispatching an event on load/change as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example of what that might look like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
At the top of the file before/after the props are exported, we could create an event dispatcher:
And then on line 77 where we have successfully loaded (or failed to load) the model, we could
dispatch("load", gltf);
ordispatch("error", err);
, etc...My thinking was that this could provide a convenient entry point for manipulating the materials in the scene, etc...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a consumer of the component, that would look something like
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that dispatching on change would provide any value to me, but the implementation would be similar, just within the reactive statement.