public void UpdateInstanceTexture() { if (m_instance_texture == null) { m_instance_texture = new RenderTexture(MPWorld.DataTextureWidth, MPWorld.DataTextureHeight, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Default); m_instance_texture.filterMode = FilterMode.Point; m_instance_texture.Create(); } if (m_texture_needs_update) { m_texture_needs_update = false; MPAPI.mpUpdateDataTexture(GetContext(), m_instance_texture.GetNativeTexturePtr(), m_instance_texture.width, m_instance_texture.height); } }
void Reset() { MPKernelParams p = MPAPI.mpGetKernelParams(); transform.position = p.WorldCenter; transform.localScale = p.WorldSize; solverType = (MPSolverType)p.SolverType; particleLifeTime = p.LifeTime; deceleration = p.Decelerate; pressureStiffness = p.PressureStiffness; wallStiffness = p.WallStiffness; coordScale = p.Scaler; particleSize = p.ParticleSize; maxParticleNum = p.MaxParticles; }
void OnRenderObject() { if (renderMode == RenderMode.Plugin) { UnityEngine.Camera cam = UnityEngine.Camera.current; if (cam) { Matrix4x4 view = cam.worldToCameraMatrix; Matrix4x4 proj = cam.projectionMatrix; Vector3 pos = cam.transform.position; MPAPI.mpSetViewProjectionMatrix(ref view, ref proj, ref pos); } GL.IssuePluginEvent(1); } }
public static void AddRadialSphereForce(Vector3 pos, float radius, float strength) { Matrix4x4 mat = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one * radius); MPForceProperties p = new MPForceProperties(); p.shape_type = MPForceShape.Sphere; p.dir_type = MPForceDirection.Radial; p.radial_center = pos; p.strength_near = strength; p.strength_far = 0.0f; p.attenuation_exp = 0.5f; p.range_inner = 0.0f; p.range_outer = radius; MPAPI.mpAddForce(ref p, ref mat); }
void Blow() { Vector3 pos = trans.position; float strength = 2000.0f; fprops.SetDefaultValues(); fprops.shape_type = MPForceShape.Box; fprops.dir_type = MPForceDirection.Radial; fprops.strength_near = strength; fprops.strength_far = strength; fprops.center = pos - (trans.forward * 6.0f); MPWorld.s_instances[0].AddOneTimeAction(() => { MPAPI.mpAddForce(MPWorld.s_instances[0].GetContext(), ref fprops, ref blowMatrix); }); }
static void ImmediateUpdate() { foreach (MPWorld w in s_instances) { w.UpdateKernelParams(); } UpdateMPObjects(); foreach (MPWorld w in s_instances) { MPAPI.mpUpdate(w.GetContext(), Time.deltaTime); s_current = w; MPAPI.mpCallHandlers(w.GetContext()); MPAPI.mpClearCollidersAndForces(w.GetContext()); w.CallUpdateRoutines(); s_current = null; } }
public void MPUpdate() { if (Time.deltaTime == 0.0f) { return; } Matrix4x4 mat = transform.localToWorldMatrix; switch (shape) { case Shape.Sphere: MPAPI.mpScatterParticlesSphereTransform(ref mat, emitCount, ref velosityBase, velosityDiffuse); break; case Shape.Box: MPAPI.mpScatterParticlesBoxTransform(ref mat, emitCount, ref velosityBase, velosityDiffuse); break; } }
public void MPUpdate() { if (m_emit_count_prev != m_emit_count) { m_emit_count_prev = m_emit_count; m_total_emit = Mathf.FloorToInt(m_local_time * m_emit_count); } m_local_time += Time.deltaTime; int emit_total = Mathf.FloorToInt(m_local_time * m_emit_count); int emit_this_frame = emit_total - m_total_emit; if (emit_this_frame == 0) { return; } m_total_emit = emit_total; m_params.velocity = m_velosity_base; m_params.velocity_random_diffuse = m_velosity_random_diffuse; m_params.lifetime = m_lifetime; m_params.lifetime_random_diffuse = m_lifetime_random_diffuse; m_params.userdata = m_userdata; m_params.handler = m_spawn_handler; Matrix4x4 mat = transform.localToWorldMatrix; switch (m_shape) { case Shape.Sphere: EachTargets((w) => { MPAPI.mpScatterParticlesSphereTransform(w.GetContext(), ref mat, emit_this_frame, ref m_params); }); break; case Shape.Box: EachTargets((w) => { MPAPI.mpScatterParticlesBoxTransform(w.GetContext(), ref mat, emit_this_frame, ref m_params); }); break; } }
void Update() { if (IsDead()) { if (scatterFractions) { float volume = trans.localScale.x * trans.localScale.y * trans.localScale.z; int num = (int)(volume * 500.0f); Matrix4x4 mat = trans.localToWorldMatrix; MPWorld.s_instances[0].AddOneTimeAction(() => { MPSpawnParams sp = new MPSpawnParams(); sp.velocity_random_diffuse = 3.0f; sp.lifetime = 30.0f; MPAPI.mpScatterParticlesBoxTransform(MPWorld.s_instances[0].GetContext(), ref mat, num, ref sp); }); } Destroy(gameObject); } if (Mathf.Abs(trans.position.x) > 30.0f || Mathf.Abs(trans.position.z) > 30.0f) { Destroy(gameObject); } if (rigid) { Vector3 vel = rigid.velocity; vel.x -= accel; vel.y = 0.0f; rigid.velocity = vel; Vector3 pos = rigid.transform.position; pos.y *= 0.95f; rigid.transform.position = pos; float speed = rigid.velocity.magnitude; rigid.velocity = rigid.velocity.normalized * (Mathf.Min(speed, maxSpeed) * deccel); rigid.angularVelocity = rigid.angularVelocity * 0.98f; } }
public void MPUpdate() { switch (directionType) { case MPForceDirection.Directional: fprops.directional_dir = direction; break; case MPForceDirection.Radial: fprops.radial_center = transform.position; break; } fprops.strength_near = strengthNear; fprops.strength_far = strengthFar; fprops.range_inner = rangeInner; fprops.range_outer = rangeOuter; fprops.attenuation_exp = attenuationExp; Matrix4x4 mat = transform.localToWorldMatrix; MPAPI.mpAddForce(ref fprops, ref mat); }
public void MPUpdate() { m_mpprops.dir_type = m_direction_type; m_mpprops.shape_type = m_shape_type; m_mpprops.strength_near = m_strength_near; m_mpprops.strength_far = m_strength_far; m_mpprops.rcp_range = 1.0f / (m_strength_far - m_strength_near); m_mpprops.range_inner = m_range_inner; m_mpprops.range_outer = m_range_outer; m_mpprops.attenuation_exp = m_attenuation_exp; m_mpprops.random_seed = m_random_seed; m_mpprops.random_diffuse = m_random_diffuse; m_mpprops.direction = m_direction; m_mpprops.center = transform.position; m_mpprops.rcp_cellsize = new Vector3(1.0f / m_cellsize.x, 1.0f / m_cellsize.y, 1.0f / m_cellsize.z); Matrix4x4 mat = transform.localToWorldMatrix; EachTargets((w) => { MPAPI.mpAddForce(w.GetContext(), ref m_mpprops, ref mat); }); }
void UpdateCubeMeshes() { int numParticles = MPAPI.mpGetNumParticles(); int numActiveChildren = numParticles / 2700 + (numParticles % 2700 == 0 ? 0 : 1); while (children.Count < numActiveChildren) { GameObject child = CreateChildMesh(); Vector3[] vertices = new Vector3[64800]; Vector3[] normals = new Vector3[64800]; Vector2[] uv = new Vector2[64800]; int[] indices = new int[97200]; fixed(Vector3 *v = vertices) { fixed(Vector3 *n = normals) { fixed(Vector2 *t = uv) { fixed(int *idx = indices) { meshData.vertices = v; meshData.normals = n; meshData.uv = t; meshData.indices = idx; MPAPI.mpGenerateCubeMesh(children.Count, ref meshData); } } } } Mesh mesh = child.GetComponent <MeshFilter>().mesh; mesh.vertices = vertices; mesh.normals = normals; mesh.uv = uv; mesh.SetIndices(indices, MeshTopology.Triangles, 0); children.Add(child); } UpdateChildMeshes(numActiveChildren); }
void UpdateKernelParams() { MPKernelParams p = MPAPI.mpGetKernelParams(); p.WorldCenter = transform.position; p.WorldSize = transform.localScale; p.WorldDiv_x = divX; p.WorldDiv_y = divY; p.WorldDiv_z = divZ; p.enableInteractions = enableInteractions ? 1 : 0; p.enableColliders = enableColliders ? 1 : 0; p.enableForces = enableForces ? 1 : 0; p.SolverType = (int)solverType; p.LifeTime = particleLifeTime; p.Timestep = Time.deltaTime * timeScale; p.Decelerate = deceleration; p.PressureStiffness = pressureStiffness; p.WallStiffness = wallStiffness; p.Scaler = coordScale; p.ParticleSize = particleSize; p.MaxParticles = maxParticleNum; MPAPI.mpSetKernelParams(ref p); }
void Awake() { m_context = MPAPI.mpCreateContext(); }
void OnEnable() { MPAPI.mpOnEnable(); }
public void UpdateDataTexture(RenderTexture rt) { MPAPI.mpUpdateDataTexture(GetContext(), rt.GetNativeTexturePtr()); }
void UpdateMPObjects() { colliders.Clear(); if (include3DColliders) { Collider[] colliders3d = Physics.OverlapSphere(transform.position, transform.localScale.magnitude); for (int i = 0; i < colliders3d.Length; ++i) { Collider col = colliders3d[i]; if (col.isTrigger) { continue; } MPColliderProperties cprops; bool recv = false; var attr = col.gameObject.GetComponent <MPColliderAttribute>(); if (attr) { if (!attr.sendCollision) { continue; } attr.UpdateColliderProperties(); recv = attr.receiveCollision; cprops = attr.cprops; } else { cprops = new MPColliderProperties(); cprops.SetDefaultValues(); } int id = colliders.Count; cprops.owner_id = recv ? id : -1; colliders.Add(col.gameObject); SphereCollider sphere = col as SphereCollider; CapsuleCollider capsule = col as CapsuleCollider; BoxCollider box = col as BoxCollider; if (sphere) { Vector3 pos = sphere.transform.position; MPAPI.mpAddSphereCollider(ref cprops, ref pos, sphere.radius * col.gameObject.transform.localScale.magnitude * 0.5f); } else if (capsule) { Vector3 e = Vector3.zero; float h = Mathf.Max(0.0f, capsule.height - capsule.radius * 2.0f); float r = capsule.radius * capsule.transform.localScale.x; switch (capsule.direction) { case 0: e.Set(h * 0.5f, 0.0f, 0.0f); break; case 1: e.Set(0.0f, h * 0.5f, 0.0f); break; case 2: e.Set(0.0f, 0.0f, h * 0.5f); break; } Vector4 pos1 = new Vector4(e.x, e.y, e.z, 1.0f); Vector4 pos2 = new Vector4(-e.x, -e.y, -e.z, 1.0f); pos1 = capsule.transform.localToWorldMatrix * pos1; pos2 = capsule.transform.localToWorldMatrix * pos2; Vector3 pos13 = pos1; Vector3 pos23 = pos2; MPAPI.mpAddCapsuleCollider(ref cprops, ref pos13, ref pos23, r); } else if (box) { Matrix4x4 mat = box.transform.localToWorldMatrix; Vector3 size = box.size; MPAPI.mpAddBoxCollider(ref cprops, ref mat, ref size); } } } if (include2DColliders) { Vector2 xy = new Vector2(transform.position.x, transform.position.y); Collider2D[] colliders2d = Physics2D.OverlapCircleAll(xy, transform.localScale.magnitude); for (int i = 0; i < colliders2d.Length; ++i) { Collider2D col = colliders2d[i]; if (col.isTrigger) { continue; } MPColliderProperties cprops; bool recv = false; var attr = col.gameObject.GetComponent <MPColliderAttribute>(); if (attr) { if (!attr.sendCollision) { continue; } attr.UpdateColliderProperties(); recv = attr.receiveCollision; cprops = attr.cprops; } else { cprops = new MPColliderProperties(); cprops.SetDefaultValues(); } int id = colliders.Count; cprops.owner_id = recv ? id : -1; colliders.Add(col.gameObject); CircleCollider2D sphere = col as CircleCollider2D; BoxCollider2D box = col as BoxCollider2D; if (sphere) { Vector3 pos = sphere.transform.position; MPAPI.mpAddSphereCollider(ref cprops, ref pos, sphere.radius * col.gameObject.transform.localScale.x); } else if (box) { Matrix4x4 mat = box.transform.localToWorldMatrix; Vector3 size = new Vector3(box.size.x, box.size.y, box.size.x); MPAPI.mpAddBoxCollider(ref cprops, ref mat, ref size); } } } foreach (MPCollider col in MPCollider.instances) { if (!col.sendCollision) { continue; } col.MPUpdate(); col.cprops.owner_id = colliders.Count; colliders.Add(col.gameObject); } foreach (MPForce force in MPForce.instances) { force.MPUpdate(); } foreach (MPEmitter emitter in MPEmitter.instances) { emitter.MPUpdate(); } mprenderer.MPUpdate(); }
// Update is called once per frame void Update() { text.text = "particles: " + MPAPI.mpGetNumParticles(); }
void OnDestroy() { MPAPI.mpDestroyContext(GetContext()); }
MPWorld() { #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN MPAPI.mphInitialize(); #endif }