-
Notifications
You must be signed in to change notification settings - Fork 201
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
Falling snow brush #172
Open
msfeldstein
wants to merge
11
commits into
aframevr:master
Choose a base branch
from
msfeldstein:snow
base: master
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.
+115
−1
Open
Falling snow brush #172
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b650e46
Start on snow brush
b447206
Start on snow brush
70361df
fix merge
msfeldstein 1cf2328
Remove demo.json
msfeldstein 076e574
remove other change
msfeldstein 34ad2cb
make sure to only draw the right amount of flakes
msfeldstein 1d2421c
make sure to only draw the right amount of flakes
msfeldstein 4bcc09f
Snow Brush: Make all flakes start at cursor position, dont calculate …
msfeldstein bb1f1b1
Snow Brush, use the color to shade the flakes
msfeldstein 5b4b4bc
Merge branch 'master' into snow
msfeldstein 1961f33
Use timestamp and not date.time
msfeldstein 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,107 @@ | ||
/* globals AFRAME THREE */ | ||
(function(){ | ||
var FLAKES_PER_POINT = 2; | ||
var vertexShader = ` | ||
attribute float linePosition; | ||
|
||
uniform float time; | ||
uniform float size; | ||
|
||
varying float alpha; | ||
|
||
float rand(vec2 co){ | ||
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); | ||
} | ||
|
||
float rand(float i) { | ||
return rand(vec2(i)); | ||
} | ||
|
||
void main() { | ||
gl_PointSize = max(size * 100., 2.0); | ||
float r = rand(linePosition); | ||
float life = 1.0 - fract(time / (30.0 + 20.0 * r) + 120.0 * r); | ||
float xoff = cos(time / 10.0 + r * 10.0) / 30.0; | ||
float yoff = life / 2.0 + sin(time / 10.0 + r * 10.0) / 30.0 - 0.25; | ||
float zoff = sin(time / 10.0 + r * 120.0) / 30.0; | ||
vec3 pos = position + vec3(xoff, yoff, zoff); | ||
gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 ); | ||
alpha = 1.0 - (life - 0.5) * (life - 0.5) * 4.0; | ||
} | ||
`; | ||
|
||
var fragmentShader = ` | ||
uniform sampler2D flake; | ||
uniform float time; | ||
|
||
varying float alpha; | ||
|
||
void main() { | ||
vec4 c = texture2D(flake, gl_PointCoord); | ||
c.a *= alpha / 2.0; | ||
gl_FragColor = c; | ||
} | ||
`; | ||
|
||
AFRAME.registerBrush('falling-snow', | ||
{ | ||
init: function (color, brushSize) { | ||
this.idx = 0; | ||
this.geometry = new THREE.BufferGeometry(); | ||
this.subdivisions = 8 | ||
this.vertices = new Float32Array(this.options.maxPoints * 3 * FLAKES_PER_POINT); | ||
this.linePositions = new Float32Array(this.options.maxPoints * FLAKES_PER_POINT); | ||
this.currentLinePosition = 0; | ||
|
||
this.geometry.setDrawRange(0, 0); | ||
this.geometry.addAttribute('position', new THREE.BufferAttribute(this.vertices, 3).setDynamic(true)); | ||
this.geometry.addAttribute('linePosition', new THREE.BufferAttribute(this.linePositions, 1).setDynamic(true)); | ||
|
||
this.material = new THREE.ShaderMaterial({ | ||
vertexShader: vertexShader, | ||
fragmentShader: fragmentShader, | ||
side: THREE.DoubleSide, | ||
transparent: true, | ||
depthTest: false, | ||
uniforms: { | ||
flake: {type: 't', value: null}, | ||
time: {type: 'f', value: 0}, | ||
size: {type: 'f', value: this.data.size} | ||
} | ||
}); | ||
|
||
var mesh = new THREE.Points(this.geometry, this.material); | ||
mesh.frustumCulled = false; // So that when its origin point goes off camera, the points dont disappear | ||
mesh.vertices = this.vertices; | ||
this.startTime = Date.now() | ||
|
||
var textureLoader = new THREE.TextureLoader(); | ||
this.material.uniforms.flake.value = textureLoader.load("brushes/snowflake.png"); | ||
|
||
this.object3D.add(mesh); | ||
}, | ||
|
||
addPoint: function (position, orientation, pointerPosition, pressure, timestamp) { | ||
for (var i = 0; i < FLAKES_PER_POINT; i++) { | ||
var linepos = this.currentLinePosition++ | ||
this.linePositions[linepos] = linepos; | ||
this.vertices[ this.idx++ ] = pointerPosition.x; | ||
this.vertices[ this.idx++ ] = pointerPosition.y; | ||
this.vertices[ this.idx++ ] = pointerPosition.z; | ||
} | ||
|
||
this.geometry.attributes.position.needsUpdate = true; | ||
this.geometry.attributes.linePosition.needsUpdate = true; | ||
|
||
this.geometry.setDrawRange(0, this.data.numPoints * this.subdivisions); | ||
|
||
return true; | ||
}, | ||
|
||
tick: function(timeOffset, delta) { | ||
this.material.uniforms.time.value = (Date.now() - this.startTime) / 100.0; | ||
}, | ||
}, | ||
{thumbnail:'brushes/snowflake.png', maxPoints: 3000} | ||
); | ||
})(); |
This file contains 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 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
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.
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.
what does
subdivisions
means? I believe it should be ' * FLAKES_PER_POINT` isn't it?