Skip to content

Commit

Permalink
added loading 3d models example
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno-Bells committed Oct 25, 2022
1 parent b5a77c6 commit 85f3091
Show file tree
Hide file tree
Showing 16 changed files with 1,570 additions and 32 deletions.
3 changes: 2 additions & 1 deletion examples/files.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"webgl": [
"creating_3d_text"
"creating_3d_text",
"loading_3d_models_NissanZProto"
],
"webgl / nodes": [

Expand Down
4 changes: 2 additions & 2 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8">
<title>PyWeb3D examples</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="shortcut icon" href="../docs/files/favicon_white.ico" media="(prefers-color-scheme: dark)"/>
<link rel="shortcut icon" href="../docs/files/favicon.ico" media="(prefers-color-scheme: light)" />
<link rel="shortcut icon" href="../docs/logo/pyweb3d_logo.ico" media="(prefers-color-scheme: dark)"/>
<link rel="shortcut icon" href="../docs/logo/pyweb3d_logo.ico" media="(prefers-color-scheme: light)" />
<link rel="stylesheet" type="text/css" href="../docs/files/main.css">
</head>
<body>
Expand Down
123 changes: 123 additions & 0 deletions examples/loading_3d_models_NissanZProto.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PyWeb3D webgl - GLTFloader</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.js"></script>
<script src="https://unpkg.com/[email protected]/build/three.js"></script>
<script src="https://www.pyweb3d.org/pyweb3d/v1.0.0/pyweb3d.brython.js"></script>
<link rel="stylesheet" href="main.css">
<style>
body { margin: 0; }
</style>
</head>
<body onload="brython(1)">
<script src="../jsm/loaders/GLTFLoader.js"></script>
<script src="../jsm/controls/OrbitControls.js"></script>
<script src="../jsm/loaders/RGBELoader.js"></script>

<div id="info">
<a href="https://pyweb3d.org" target="_blank" rel="noopener">PyWeb3D</a> car materials<br/>
Nissan Z Proto model by <a href="https://sketchfab.com/3d-models/nissan-z-proto-db9a5f7ae9d84a0fb01cac96afe3e69c" target="_blank" rel="noopener">Lexyc16</a>
<br><br>
</div>

<script type="text/python">
from browser import document, window
from pyweb3d.pyweb3d import *

GLTFLoader = window.THREE.GLTFLoader.new
OrbitControls = window.THREE.OrbitControls.new
RGBELoader = window.THREE.RGBELoader.new

#Variables for setup

camera = None
renderer = None
scene = None
car = None

def init():
global camera
global renderer
global scene
global car

#Create scene
scene = Scene()
scene.background = Color( 0x333333 )
scene.environment = RGBELoader().load( 'textures/venice_sunset_1k.hdr' )
scene.environment.mapping = EquirectangularReflectionMapping
scene.fog = Fog( 0x333333, 10, 15 )

grid = GridHelper( 20, 40, 0xffffff, 0xffffff )
grid.material.opacity = 0.2
grid.material.depthWrite = False
grid.material.transparent = True
scene.add( grid )

fov = 45
aspect = window.innerWidth / window.innerHeight
near = 1
far = 1500

#Camera setup
camera = PerspectiveCamera(fov, aspect, near, far)
camera.position.x = 8
camera.position.y = 1
camera.position.z = 10

ambientLight = AmbientLight( 'white', 2 )
scene.add( ambientLight )

DirectLight = DirectionalLight( 'white', 3.5 )
DirectLight.position.set( 100, 100, 100 )
scene.add( DirectLight )

hemiLight = HemisphereLight('white', 'darkslategrey', 5)
scene.add(hemiLight)

#Renderer
renderer = WebGLRenderer( { 'antialias': True } )
renderer.setPixelRatio( window.devicePixelRatio )
renderer.setSize( window.innerWidth, window.innerHeight )
document.body.appendChild( renderer.domElement )

controls = OrbitControls(camera, renderer.domElement)

#Load Model
loader = GLTFLoader()

def loadGLTF(gltf):
car = gltf.scene
car.scale.setScalar( 1.5 )
# center
box = Box3().setFromObject( car )
center = box.getCenter( Vector3() )
car.position.x += ( car.position.x - center.x )
car.position.y += ( car.position.y - center.y + 1.4)
car.position.z += ( car.position.z - center.z )

scene.add( car )
animate(0)

loader.load( 'models/Nissan_Z_Proto/scene.gltf', loadGLTF)

def onWindowResize(resize):

camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()

renderer.setSize( window.innerWidth, window.innerHeight )

window.addEventListener( 'resize', onWindowResize )

def animate(time):
window.requestAnimationFrame( animate )
renderer.render( scene, camera )

init()
</script>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/models/Nissan_Z_Proto/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Model Information:
* title: Nissan Z Proto
* source: https://sketchfab.com/3d-models/nissan-z-proto-db9a5f7ae9d84a0fb01cac96afe3e69c
* author: Lexyc16 (https://sketchfab.com/Lexyc16)

Model License:
* license type: CC-BY-4.0 (http://creativecommons.org/licenses/by/4.0/)
* requirements: Author must be credited. Commercial use is allowed.

If you use this 3D model in your project be sure to copy paste this credit wherever you share it:
This work is based on "Nissan Z Proto" (https://sketchfab.com/3d-models/nissan-z-proto-db9a5f7ae9d84a0fb01cac96afe3e69c) by Lexyc16 (https://sketchfab.com/Lexyc16) licensed under CC-BY-4.0 (http://creativecommons.org/licenses/by/4.0/)
Binary file added examples/models/Nissan_Z_Proto/scene.bin
Binary file not shown.
Loading

0 comments on commit 85f3091

Please sign in to comment.