-
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
Open
snagy
wants to merge
3
commits into
Rich-Harris:main
Choose a base branch
from
snagy:gltf-comp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<script context="module"> | ||
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'; | ||
|
||
/** @type {GLTFLoader} */ | ||
let loader = null; | ||
/** @type {DRACOLoader} */ | ||
let dracoLoader = null; | ||
/** @type {KTX2Loader} */ | ||
let ktxLoader = null; | ||
</script> | ||
|
||
<script> | ||
import { onMount } from 'svelte'; | ||
|
||
import * as THREE from 'three'; | ||
import { setup } from '../../utils/context.js'; | ||
import { transform } from '../../utils/object.js'; | ||
import * as defaults from '../../utils/defaults.js'; | ||
|
||
/** @type {string} */ | ||
export let path = ''; | ||
/** @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 {THREE.Group} */ | ||
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(() => { | ||
if (loader === null) { | ||
loader = new GLTFLoader() | ||
} | ||
|
||
if (useDraco && dracoLoader === null) { | ||
dracoLoader = new DRACOLoader().setDecoderPath( '/loaders/draco/' ); | ||
loader.setDRACOLoader(dracoLoader); | ||
} | ||
|
||
if (useKTX2 && ktxLoader === null) { | ||
ktxLoader = new KTX2Loader() | ||
.setTranscoderPath( '/loaders/basis/' ) | ||
.detectSupport( root.renderer ); | ||
loader.setKTX2Loader(ktxLoader); | ||
} | ||
|
||
loader.setPath( path ) | ||
.load( filename, function ( gltf ) { | ||
gltfScene = gltf.scene; | ||
self.add(gltf.scene); | ||
} ); | ||
|
||
return () => {}; | ||
}); | ||
</script> | ||
|
||
<slot /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
</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 filename="/gltf/CesiumMan.glb" | ||
position={[-6, 0, 0]} | ||
castShadow | ||
receiveShadow /> | ||
|
||
<SC.GLTF filename="/gltf/waterbottle/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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"title": "glTF simple" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.