2017-10-06 19:31
cd
yarn start
app/main.js
Scene
div
Entity
Entity
primitive
a-cylinder
rotation
height
Scene
app/intialize.js
CTRL + ALT + I
import { h, Component } from 'preact'
import { Entity, Scene } from 'aframe-react'
// Color palette to use for later
const COLORS = ['#D92B6A', '#9564F2', '#FFCF59']
class App extends Component {
constructor() {
super()
// We'll use this state later on in the tutorial
this.state = {
colorIndex: 0,
spherePosition: { x: 0.0, y: 4, z: -10.0 }
}
}
render() {
return (
<Scene
environment={{
preset: 'starry',
seed: 2,
lightPosition: { x: 0.0, y: 0.03, z: -0.5 },
fog: 0.8,
ground: 'canyon',
groundYScale: 6.31,
groundTexture: 'walkernoise',
groundColor: '#8a7f8a',
grid: 'none'
}}
>
</Scene>
)
}
}
Entity
a-camera
<Entity primitive="a-camera" look-controls>
<Entity
primitive="a-cursor"
cursor={{ fuse: false }}
material={{ color: 'white', shader: 'flat', opacity: 0.75 }}
geometry={{ radiusInner: 0.005, radiusOuter: 0.007 }}
/>
</Entity>
a-octahedron
<Entity
primitive="a-octahedron"
detail={2}
radius={2}
position={this.state.spherePosition}
color="#FAFAF1"
/>
<Entity
primitive="a-light"
type="directional"
color="#FFF"
intensity={1}
position={{ x: 2.5, y: 0.0, z: 0.0 }}
/>
app/components/aframe-custom.js
AFRAME
init
AFRAME.registerComponent('lowpoly', {
schema: {
// Here we define our properties, their types and default values
color: { type: 'string', default: '#FFF' },
nodes: { type: 'boolean', default: false },
opacity: { type: 'number', default: 1.0 },
wireframe: { type: 'boolean', default: false }
},
init: function() {
// This block gets executed when the component gets initialized.
// Then we can use our properties like so:
console.log('The color of our component is ', this.data.color)
}
}
init
init: function() {
// Get the ref of the object to which the component is attached
const obj = this.el.getObject3D('mesh')
// Grab the reference to the main WebGL scene
const scene = document.querySelector('a-scene').object3D
// Modify the color of the material
obj.material = new THREE.MeshPhongMaterial({
color: this.data.color,
shading: THREE.FlatShading
})
// Define the geometry for the outer wireframe
const frameGeom = new THREE.OctahedronGeometry(2.5, 2)
// Define the material for it
const frameMat = new THREE.MeshPhongMaterial({
color: '#FFFFFF',
opacity: this.data.opacity,
transparent: true,
wireframe: true
})
// The final mesh is a composition of the geometry and the material
const icosFrame = new THREE.Mesh(frameGeom, frameMat)
// Set the position of the mesh to the position of the sphere
const { x, y, z } = obj.position
icosFrame.position.set(0.0, 4, -10.0)
// If the wireframe prop is set to true, then we attach the new object
if (this.data.wireframe) {
scene.add(icosFrame)
}
// If the nodes attribute is set to true
if (this.data.nodes) {
let spheres = new THREE.Group()
let vertices = icosFrame.geometry.vertices
// Traverse the vertices of the wireframe and attach small spheres
for (var i in vertices) {
// Create a basic sphere
let geometry = new THREE.SphereGeometry(0.045, 16, 16)
let material = new THREE.MeshBasicMaterial({
color: '#FFFFFF',
opacity: this.data.opacity,
shading: THREE.FlatShading,
transparent: true
})
let sphere = new THREE.Mesh(geometry, material)
// Reposition them correctly
sphere.position.set(
vertices[i].x,
vertices[i].y + 4,
vertices[i].z + -10.0
)
spheres.add(sphere)
}
scene.add(spheres)
}
}
lowpoly
<Entity
lowpoly={{
color: '#D92B6A',
nodes: true,
opacity: 0.15,
wireframe: true
}}
primitive="a-octahedron"
detail={2}
radius={2}
position={{ x: 0.0, y: 4, z: -10.0 }}
color="#FAFAF1"
/>
raycaster
<Entity primitive="a-camera" look-controls>
<Entity
primitive="a-cursor"
cursor={{ fuse: false }}
material={{ color: 'white', shader: 'flat', opacity: 0.75 }}
geometry={{ radiusInner: 0.005, radiusOuter: 0.007 }}
event-set__1={{
_event: 'mouseenter',
scale: { x: 1.4, y: 1.4, z: 1.4 }
}}
event-set__1={{
_event: 'mouseleave',
scale: { x: 1, y: 1, z: 1 }
}}
raycaster="objects: .clickable"
/>
</Entity>
class="clickable"
<Entity
class="clickable"
// ... all the other props we've already added before
events={{
click: this._handleClick.bind(this)
}}
/>
_handleClick
setState
_handleClick() {
this.setState({
colorIndex: (this.state.colorIndex + 1) % COLORS.length
})
}
colorIndex
<Entity
class="clickable"
lowpoly={{
color: COLORS[this.state.colorIndex],
// The rest stays the same
/>
init
update
AFRAME.registerComponent('lowpoly', {
schema: {
// We've already filled this out
},
init: function() {
// We've already filled this out
}
update: function() {
// Get the ref of the object to which the component is attached
const obj = this.el.getObject3D('mesh')
// Modify the color of the material during runtime
obj.material.color = new THREE.Color(this.data.color)
}
}
animation__rotate
<Entity
class="clickable"
lowpoly
// A whole buncha props that we wrote already...
animation__rotate={{
property: 'rotation',
dur: 60000,
easing: 'linear',
loop: true,
to: { x: 0, y: 360, z: 0 }
}}
/>
animation__oscillate={{
property: 'position',
dur: 2000,
dir: 'alternate',
easing: 'linear',
loop: true,
from: this.state.spherePosition,
to: {
x: this.state.spherePosition.x,
y: this.state.spherePosition.y + 0.25,
z: this.state.spherePosition.z
}
}}
Scene
<Scene
effects="bloom, film, fxaa"
bloom="radius: 0.99"
film="sIntensity: 0.15; nIntensity: 0.15"
fxaa
// Everything else that was already there
/>
yarn build
npm install -g surge
重庆网站建设