开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
ComputeShaderTutorial
/
Assets
/
ComputeExample.cs
ComputeShaderTutorial
/
Assets
/
ComputeExample.cs
ComputeExample.cs 5.47 KB
一键复制 编辑 原始数据 按行查看 历史
lachlansleight 提交于 2017年05月27日 16:46 +08:00 . Minor tweaks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Note that this struct has to match EXACTLY the struct defined in our shaders.
public struct ParticleData {
public Vector3 pos;
public Vector3 velocity;
public Color color;
}
public class ComputeExample : MonoBehaviour {
//Just SteamVR stuff - reference to our controllers so we can interact with the particles
[Header("SteamVR Controllers")]
public SteamVR_TrackedObject LeftController;
public SteamVR_TrackedObject RightController;
private SteamVR_Controller.Device RightDevice;
private SteamVR_Controller.Device LeftDevice;
[Header("Shaders")]
public ComputeShader compute;
public Material graphics;
[Header("Shader Parameters")]
[Range(0, 1000000)] [Tooltip("Number of particles")]
public int count = 50000;
[Space(10)]
[Range(-0.5f, 0.5f)] [Tooltip("Charge (in nC) of the controllers when triggers are fully pressed")]
public float ControllerMaxCharge = -0.1f;
[Range(0f, 0.02f)] [Tooltip("Amount of damping to apply each frame")]
public float Damping = 0.005f;
[Range(0f, 0.001f)] [Tooltip("Charge (in nC) of each particle")]
public float ParticleCharge = 0.0001f;
[Range(0f, 10f)] [Tooltip("Mass (in kg) of each particle")]
public float ParticleMass = 1f;
[Range(0f, 1f)] [Tooltip("Softening factor to limit force amplitudes")]
public float SofteningFactor = 0.1f;
//shader info
ComputeBuffer Buffer;
ParticleData[] Data;
int Stride;
int KernelIndex;
void Start () {
//Set up all the data
InitialiseBuffers();
FillBuffers();
}
//This can fail a time or two on start while SteamVR initialises the controllers
void TryGetLeftDevice() {
try {
LeftDevice = SteamVR_Controller.Input((int)LeftController.index);
} catch (System.Exception e) {
Debug.Log("Failed getting left controller: " + e.Message);
}
}
void TryGetRightDevice() {
try {
RightDevice = SteamVR_Controller.Input((int)RightController.index);
} catch (System.Exception e) {
Debug.Log("Failed getting right controller: " + e.Message);
}
}
//Make sure we release the data when the program closes!
void OnDestroy() {
Buffer.Release();
Buffer.Dispose();
}
void InitialiseBuffers() {
//Calculate 'Stride' or how much data the GPU should get for each particle
//We calculate this by determining the size, in bytes, of a single ParticleData struct instance
int vector3Stride = sizeof(float) * 3;
int colorStride = sizeof(int) * 4;
Stride = vector3Stride * 2 + colorStride;
//Then we initialise the buffer and get the KernelIndex using the kernel name in the first line of our compute shader
Buffer = new ComputeBuffer(count, Stride);
KernelIndex = compute.FindKernel("ParticleFunction");
}
void FillBuffers() {
//initialise our data array
Data = new ParticleData[count];
//give it some starting data. For now, we'll make a bunch of white particles spawning in a sphere 1m off the ground
for(int i = 0; i < Data.Length; i++) {
Data[i] = new ParticleData();
Data[i].pos = Random.insideUnitSphere * 0.5f + new Vector3(0f, 1f, 0f);
Data[i].velocity = Vector3.zero;
Data[i].color = Color.white;
}
//then put the data array into our ComputeBuffer
Buffer.SetData(Data);
//And assign it to our ComputeShader and our Graphics shader!
compute.SetBuffer(KernelIndex, "outputBuffer", Buffer);
graphics.SetBuffer("inputBuffer", Buffer);
}
//Whenever we do a render pass
void OnRenderObject() {
//Update the parameters of our compute shader, especially the controller positions / trigger axes for input
SetData();
//Actually run the compute shader to update our Particle Data
//Note the 10, 10, 10 parameters - these are how many thread groups to run, and this must match the number of thread groups we define at the top of our compute shader
compute.Dispatch(KernelIndex, 10, 10, 10);
//And then draw it using our graphics material.
graphics.SetPass(0);
Graphics.DrawProcedural(MeshTopology.Points, Buffer.count);
}
void SetData() {
//apply general physics parameters
//note that we don't have to do this every frame, unless we allow players to change these values in real-time
compute.SetFloat("Damping", Damping);
compute.SetFloat("ParticleCharge", ParticleCharge);
compute.SetFloat("ParticleMass", ParticleMass);
compute.SetFloat("SofteningFactor", SofteningFactor);
//Make sure we actually have the devices before we do anything with them
if(LeftDevice == null) {
TryGetLeftDevice();
} else {
//Create a four-dimensional vector - XYZ will be the controller position, and W will be the 'charge' of the controller, attached to the trigger axis
Vector3 LeftPosition = LeftController.transform.position;
float LeftCharge = Mathf.Lerp(0f, ControllerMaxCharge, LeftDevice.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger).x);
compute.SetVector("LeftController", new Vector4(LeftPosition.x, LeftPosition.y, LeftPosition.z, LeftCharge));
}
//Make sure we actually have the devices before we do anything with them
if(RightDevice == null) {
TryGetRightDevice();
} else {
//Create a four-dimensional vector - XYZ will be the controller position, and W will be the 'charge' of the controller, attached to the trigger axis
Vector3 RightPosition = RightController.transform.position;
float RightCharge = Mathf.Lerp(0f, ControllerMaxCharge, RightDevice.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger).x);
compute.SetVector("RightController", new Vector4(RightPosition.x, RightPosition.y, RightPosition.z, RightCharge));
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/SimpleAI/ComputeShaderTutorial.git
git@gitee.com:SimpleAI/ComputeShaderTutorial.git
SimpleAI
ComputeShaderTutorial
ComputeShaderTutorial
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /