-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add gltf-model component and system. #2333
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
Changes from 1 commit
c2b5462
eb75294
e2ba0d3
4604c6f
7feb873
8ba7ebd
e4f239e
7b7f501
6beb60d
b2fc6de
53515cb
79ddddb
1979fed
3c3a3bd
1b16020
456cdaa
05a0f03
892a854
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Fixes #2261.
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| precision highp float; | ||
| varying vec3 v_normal; | ||
| uniform vec4 u_ambient; | ||
| uniform vec4 u_diffuse; | ||
| uniform vec4 u_emission; | ||
| uniform vec4 u_specular; | ||
| void main(void) { | ||
| vec3 normal = normalize(v_normal); | ||
| vec4 color = vec4(0., 0., 0., 0.); | ||
| vec4 diffuse = vec4(0., 0., 0., 1.); | ||
| vec4 emission; | ||
| vec4 ambient; | ||
| vec4 specular; | ||
| ambient = u_ambient; | ||
| diffuse = u_diffuse; | ||
| emission = u_emission; | ||
| specular = u_specular; | ||
| diffuse.xyz *= max(dot(normal,vec3(0.,0.,1.)), 0.); | ||
| color.xyz += diffuse.xyz; | ||
| color.xyz += emission.xyz; | ||
| color = vec4(color.rgb * diffuse.a, diffuse.a); | ||
| gl_FragColor = color; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| precision highp float; | ||
| attribute vec3 a_position; | ||
| attribute vec3 a_normal; | ||
| varying vec3 v_normal; | ||
| attribute vec4 a_joint; | ||
| attribute vec4 a_weight; | ||
| uniform mat4 u_jointMat[18]; | ||
| uniform mat3 u_normalMatrix; | ||
| uniform mat4 u_modelViewMatrix; | ||
| uniform mat4 u_projectionMatrix; | ||
| void main(void) { | ||
| mat4 skinMat = a_weight.x * u_jointMat[int(a_joint.x)]; | ||
| skinMat += a_weight.y * u_jointMat[int(a_joint.y)]; | ||
| skinMat += a_weight.z * u_jointMat[int(a_joint.z)]; | ||
| skinMat += a_weight.w * u_jointMat[int(a_joint.w)]; | ||
| vec4 pos = u_modelViewMatrix * skinMat * vec4(a_position,1.0); | ||
| v_normal = u_normalMatrix * mat3(skinMat)* a_normal; | ||
| gl_Position = u_projectionMatrix * pos; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| var registerComponent = require('../core/component').registerComponent; | ||
| var THREE = require('../lib/three'); | ||
|
|
||
| /** | ||
| * glTF model loader. | ||
| */ | ||
| module.exports.Component = registerComponent('gltf-model', { | ||
| schema: {type: 'model'}, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be multiprop to allow room for future props?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I slightly prefer multiprop, but can't actually think of any others we'd need. Maybe crossorigin. I'm expecting animation to be a separate component, but if not that would definitely need some properties. I've used
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, can just leave it as is |
||
|
|
||
| init: function () { | ||
| this.model = null; | ||
| this.loader = new THREE.GLTFLoader(); | ||
| }, | ||
|
|
||
| update: function () { | ||
| var self = this; | ||
| var el = this.el; | ||
| var src = this.data; | ||
|
|
||
| if (!src) { return; } | ||
|
|
||
| this.remove(); | ||
|
|
||
| this.loader.load(src, function (gltfModel) { | ||
|
||
| self.model = gltfModel.scene; | ||
| self.system.addModel(self.model); | ||
| el.setObject3D('mesh', self.model); | ||
| el.emit('model-loaded', {format: 'gltf', model: self.model}); | ||
| }); | ||
| }, | ||
|
|
||
| remove: function () { | ||
| if (!this.model) { return; } | ||
| this.el.removeObject3D('mesh'); | ||
| this.system.removeModel(this.model); | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| var registerSystem = require('../core/system').registerSystem; | ||
| var THREE = require('../lib/three'); | ||
|
|
||
| /** | ||
| * glTF model system. | ||
| */ | ||
| module.exports.System = registerSystem('gltf-model', { | ||
| init: function () { | ||
| this.models = []; | ||
| }, | ||
|
|
||
| /** | ||
| * Updates shaders for all glTF models in the system. | ||
| */ | ||
| tick: function () { | ||
| var sceneEl = this.sceneEl; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could test this code too
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| if (sceneEl.hasLoaded && this.models.length) { | ||
| THREE.GLTFLoader.Shaders.update(sceneEl.object3D, sceneEl.camera); | ||
| } | ||
| }, | ||
|
|
||
| /** | ||
| * Registers a glTF asset. | ||
| * @param {object} gltf Asset containing a scene and (optional) animations and cameras. | ||
| */ | ||
| addModel: function (gltf) { | ||
|
||
| this.models.push(gltf); | ||
| }, | ||
|
|
||
| /** | ||
| * Unregisters a glTF asset. | ||
| * @param {object} gltf Asset containing a scene and (optional) animations and cameras. | ||
| */ | ||
| removeModel: function (gltf) { | ||
| var models = this.models; | ||
| var index = models.indexOf(gltf); | ||
| if (index >= 0) { | ||
| models.splice(index, 1); | ||
| } | ||
| } | ||
| }); | ||
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.
Would be cool to add text labels under each model for the model format. And the scene could use a better background
<a-sky color="#FAFAFA">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.
Could also add the different format types in the page title & meta description
Models (glTF, COLLADA, OBJ)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.
scope creep alert 😉
Added the meta title/description, but I'm getting errors trying to do text.
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.
Is that on latest? We pushed some text patches switching to raw shader material.
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.
In the latest and greatest master, that warning has been eliminated by using RawShaderMaterial for the sdf and msdf text shaders. so... maybe needs rebase?
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.
Rebase did the trick. ✅