Skip to content

Commit

Permalink
Axis constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonsturges committed Nov 19, 2024
1 parent 8950afb commit 545a945
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/geometry/rocks/RockGeometry.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Axis } from "../../constants/Axis";
import { BufferGeometry, SphereGeometry } from "three";
import { Direction } from "../../constants/Direction";
import { randomTransformVertices } from "../../utils/VertexUtils";

export class RockGeometry extends BufferGeometry {
constructor(radius = 1, widthSegments = 4, heightSegments = 4) {
super();

const sphere = new SphereGeometry(radius, widthSegments, heightSegments);
this.copy(randomTransformVertices(sphere, Direction.XYZ, 0.5, 1.0));
this.copy(randomTransformVertices(sphere, Axis.XYZ, 0.5, 1.0));
this.computeVertexNormals();
this.center();
}
Expand Down
16 changes: 8 additions & 8 deletions src/shaders/addNoiseDisplacement.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Direction } from "../constants/Direction";
import { Axis } from "../constants/Axis";
import { Material, Vector3 } from "three";

interface NoiseDisplacementOptions {
time?: number;
intensity?: number;
direction?: Vector3;
axis?: Vector3;
scale?: number;
}

Expand All @@ -14,24 +14,24 @@ interface NoiseDisplacementOptions {
* @param {Object} options - Options for noise modification.
* @param {number} [options.time=0.0] - The time value for the noise function.
* @param {number} [options.intensity=1.0] - The intensity of the displacement.
* @param {Vector3} [options.direction=new Vector3(1, 1, 1)] - The direction of displacement.
* @param {Vector3} [options.axis=new Vector3(1, 1, 1)] - The axis of displacement.
* @param {number} [options.scale=10.0] - The scale of the noise effect.
*/
export function addNoiseDisplacement<T extends Material>(
material: T,
{ time = 0.0, intensity = 1.0, direction = Direction.XYZ, scale = 10.0 }: NoiseDisplacementOptions = {},
{ time = 0.0, intensity = 1.0, axis = Axis.XYZ, scale = 10.0 }: NoiseDisplacementOptions = {},
) {
material.onBeforeCompile = (shader) => {
// Add uniforms for time, direction, intensity, and scale
// Add uniforms for time, axis, intensity, and scale
shader.uniforms.time = { value: time };
shader.uniforms.direction = { value: direction };
shader.uniforms.axis = { value: axis };
shader.uniforms.intensity = { value: intensity };
shader.uniforms.scale = { value: scale };

// Add noise function and modify the vertex shader to displace vertices
shader.vertexShader = `
uniform float time;
uniform vec3 direction;
uniform vec3 axis;
uniform float intensity;
uniform float scale;
Expand Down Expand Up @@ -68,7 +68,7 @@ export function addNoiseDisplacement<T extends Material>(
`
vec3 transformed = vec3(position);
float n = noise(transformed * scale + time);
transformed += normalize(direction) * n * intensity;
transformed += normalize(axis) * n * intensity;
vec3 transformedNormal = normal;
`,
);
Expand Down
8 changes: 4 additions & 4 deletions src/utils/VertexUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Axis } from "../constants/Axis";
import { BufferGeometry, Vector3 } from "three";
import { Direction } from "../constants/Direction";
import { mergeVertices } from "three-stdlib";

export function randomTransformVertices<T extends BufferGeometry>(geometry:T, direction = Direction.XYZ, minScale = 0.5, maxScale = 2.0) {
export function randomTransformVertices<T extends BufferGeometry>(geometry:T, axis = Axis.XYZ, minScale = 0.5, maxScale = 2.0) {
// Delete unnecessary attributes
geometry.deleteAttribute("uv");
geometry.deleteAttribute("normal");
Expand All @@ -16,9 +16,9 @@ export function randomTransformVertices<T extends BufferGeometry>(geometry:T, di
for (let i = 0; i < positionAttribute.count; i++) {
const vertex = new Vector3().fromBufferAttribute(positionAttribute, i);

// Randomly scale the displacement along the given direction
// Randomly scale the displacement along the given axis
const randomScale = Math.random() * (maxScale - minScale) + minScale;
const displacement = direction.clone().multiplyScalar(randomScale);
const displacement = axis.clone().multiplyScalar(randomScale);

// Apply the displacement to the vertex
vertex.add(displacement);
Expand Down

0 comments on commit 545a945

Please sign in to comment.