Beispiel #1
0
        public Effect(EffectModel model, Neutrino._math.vec3?position, Neutrino._math.quat?rotation)
        {
            model_ = model;

            if (position != null)
            {
                Neutrino._math.copyv3(out position_, position.Value);
            }
            else
            {
                Neutrino._math.setv3(out position_, 0, 0, 0);
            }

            if (rotation != null)
            {
                Neutrino._math.copyq(out rotation_, rotation.Value);
            }
            else
            {
                Neutrino._math.copyq(out rotation_, Neutrino._math.quat_(1, 0, 0, 0));
            }

            renderBuffer_   = new RenderBuffers(model_.context());
            neutrinoEffect_ = new Neutrino.Effect(model_.neutrinoEffectModel(), renderBuffer_, position, rotation);
        }
Beispiel #2
0
        public void OnBeforeSerialize()
        {
            Neutrino.Effect effect = neutrinoEffect_;
            if (effect == null)
            {
                return;
            }

            serializableEffect_.emitters_.Clear();

            for (int emitterIndex = 0; emitterIndex < effect.numEmitters(); ++emitterIndex)
            {
                Neutrino.Emitter emitter = effect.emitter(emitterIndex);

                var serialEmitter = new SerializableEmitter();
                serialEmitter.name_             = emitter.model().name();
                serialEmitter.paused_           = emitter.paused();
                serialEmitter.generatorsPaused_ = emitter.generatorsPaused();
                serializableEffect_.emitters_.Add(serialEmitter);

                for (int propIndex = 0; propIndex < emitter.model().numProperties(); ++propIndex)
                {
                    var serialProp = new SerializableProperty();
                    serialProp.name_ = emitter.model().propertyName(propIndex);
                    serialEmitter.properties_.Add(serialProp);

                    switch (emitter.model().propertyType(propIndex))
                    {
                    case Neutrino.EmitterModel.PropertyType.FLOAT:
                    {
                        float value = (float)emitter.propertyValue(propIndex);
                        serialProp.value_.Add(value);
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.VEC2:
                    {
                        _math.vec2 value = (_math.vec2)emitter.propertyValue(propIndex);
                        serialProp.value_.Add(value.x);
                        serialProp.value_.Add(value.y);
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.VEC3:
                    {
                        _math.vec3 value = (_math.vec3)emitter.propertyValue(propIndex);
                        serialProp.value_.Add(value.x);
                        serialProp.value_.Add(value.y);
                        serialProp.value_.Add(value.z);
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.QUAT:
                        // none until inspector is ready for quaternions
                        break;
                    }
                }
            }
        }
Beispiel #3
0
        void OnDisable()
        {
            //Debug.Log("OnDisable()");

            neutrinoEffectModel_ = null;
            neutrinoEffect_      = null;
            renderBuffer_        = null;
            neutrinoInited_      = false;

                        #if UNITY_EDITOR
            EditorApplication.update -= Update;
                        #endif
        }
Beispiel #4
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            NeutrinoRenderer renderer = (NeutrinoRenderer)target;

            Neutrino.Effect effect = renderer.neutrinoEffect();

            if (effect == null)
            {
                return;
            }

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Restart"))
            {
                renderer.reset();
            }

            bool paused = renderer.paused();

            if (GUILayout.Button(paused ? "Unpause" : "Pause"))
            {
                Undo.RecordObject(renderer, paused ? "Unpause particle effect" : "Pause particles effect");

                if (paused)
                {
                    renderer.unpause();
                }
                else
                {
                    renderer.pause();
                }
            }

            bool generatorsPaused = renderer.generatorsPaused();

            if (GUILayout.Button(generatorsPaused ? "Unpause generation" : "Pause generation"))
            {
                Undo.RecordObject(renderer, generatorsPaused ? "Unpause generation in particle effect" : "Pause generation in particles effect");

                if (generatorsPaused)
                {
                    renderer.unpauseGenerators();
                }
                else
                {
                    renderer.pauseGenerators();
                }
            }

            GUILayout.EndHorizontal();

            for (int emitterIndex = 0; emitterIndex < effect.numEmitters(); ++emitterIndex)
            {
                Neutrino.Emitter emitter = effect.emitter(emitterIndex);

                if (emitter.model().numProperties() == 0)
                {
                    continue;
                }

                EditorGUILayout.Space();
                EditorGUILayout.LabelField(emitter.model().name());
                EditorGUI.indentLevel++;

                for (int propIndex = 0; propIndex < emitter.model().numProperties(); ++propIndex)
                {
                    string name = emitter.model().propertyName(propIndex);

                    switch (emitter.model().propertyType(propIndex))
                    {
                    case Neutrino.EmitterModel.PropertyType.FLOAT:
                    {
                        float valueBefore = (float)emitter.propertyValue(propIndex);
                        float valueAfter  = EditorGUILayout.FloatField(name, valueBefore);
                        emitter.setPropertyValue(propIndex, valueAfter);
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.VEC2:
                    {
                        _math.vec2          valueBefore = (_math.vec2)emitter.propertyValue(propIndex);
                        UnityEngine.Vector2 valueAfter  = EditorGUILayout.Vector2Field(name,
                                                                                       new UnityEngine.Vector2(valueBefore.x, valueBefore.y));
                        emitter.setPropertyValue(propIndex, _math.vec2_(valueAfter.x, valueAfter.y));
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.VEC3:
                    {
                        _math.vec3          valueBefore = (_math.vec3)emitter.propertyValue(propIndex);
                        UnityEngine.Vector3 valueAfter  = EditorGUILayout.Vector3Field(name,
                                                                                       new UnityEngine.Vector3(valueBefore.x, valueBefore.y, valueBefore.z));
                        emitter.setPropertyValue(propIndex, _math.vec3_(valueAfter.x, valueAfter.y, valueAfter.z));
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.QUAT:
                        break;
                    }
                }

                EditorGUI.indentLevel--;
            }
        }
Beispiel #5
0
        private void deserializeToEffect()
        {
            if (serializableEffect_ == null)
            {
                return;
            }

            Neutrino.Effect effect = neutrinoEffect_;

            for (int emitterIndex = 0; emitterIndex < serializableEffect_.emitters_.Count; ++emitterIndex)
            {
                var serialEmitter = serializableEffect_.emitters_[emitterIndex];

                Neutrino.Emitter emitter = effect.emitter(serialEmitter.name_);
                if (emitter == null)
                {
                    continue;
                }

                if (emitter.paused() != serialEmitter.paused_)
                {
                    if (serialEmitter.paused_)
                    {
                        emitter.pause();
                    }
                    else
                    {
                        emitter.unpause();
                    }
                }

                if (emitter.generatorsPaused() != serialEmitter.generatorsPaused_)
                {
                    if (serialEmitter.generatorsPaused_)
                    {
                        emitter.pauseGenerators();
                    }
                    else
                    {
                        emitter.unpauseGenerators();
                    }
                }

                for (int propIndex = 0; propIndex < serialEmitter.properties_.Count; ++propIndex)
                {
                    var serialProp = serialEmitter.properties_[propIndex];

                    var propType = emitter.model().propertyType(serialProp.name_);
                    if (propType == null)
                    {
                        continue;
                    }

                    switch (propType.Value)
                    {
                    case Neutrino.EmitterModel.PropertyType.FLOAT:
                    {
                        if (serialProp.value_.Count == 1)
                        {
                            emitter.setPropertyValue(serialProp.name_, serialProp.value_[0]);
                        }
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.VEC2:
                    {
                        if (serialProp.value_.Count == 2)
                        {
                            emitter.setPropertyValue(serialProp.name_,
                                                     _math.vec2_(serialProp.value_[0], serialProp.value_[1]));
                        }
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.VEC3:
                    {
                        if (serialProp.value_.Count == 3)
                        {
                            emitter.setPropertyValue(serialProp.name_,
                                                     _math.vec3_(serialProp.value_[0], serialProp.value_[1], serialProp.value_[2]));
                        }
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.QUAT:
                        // none until inspector is ready for quaternions
                        break;
                    }
                }
            }
        }
Beispiel #6
0
        private void initNeutrino()
        {
            //Debug.Log("initNeutrino()");

            neutrinoEffectModel_ = createModel();

            Mesh mesh = new Mesh();

            gameObject.GetComponent <MeshFilter>().mesh = mesh;
            renderBuffer_ = new NeutrinoRenderBuffer(mesh);

            neutrinoEffect_ = new Neutrino.Effect(neutrinoEffectModel_, renderBuffer_,
                                                  simulateInWorldSpace ?
                                                  Neutrino._math.vec3_(gameObject.transform.position.x / gameObject.transform.localScale.x,
                                                                       gameObject.transform.position.y / gameObject.transform.localScale.y,
                                                                       gameObject.transform.position.z / gameObject.transform.localScale.z) :
                                                  Neutrino._math.vec3_(0, 0, 0),
                                                  simulateInWorldSpace ?
                                                  Neutrino._math.quat_(transform.rotation.w, transform.rotation.x, transform.rotation.y, transform.rotation.z) :
                                                  Neutrino._math.quat_(1, 0, 0, 0));

            deserializeToEffect();

            // preparing materials
            {
                string[]      textures     = neutrinoEffectModel_.textures();
                RenderStyle[] renderStyles = neutrinoEffectModel_.renderStyles();

                materials_ = new Material[renderStyles.Length];

                for (int i = 0; i < renderStyles.Length; ++i)
                {
                    Material material;

                    switch (neutrinoEffectModel_.materials()[renderStyles[i].material_])
                    {
                    default:
                        material = new Material(NeutrinoContext.Instance.shaderNormal());
                        break;

                    case Neutrino.RenderMaterial.Add:
                        material = new Material(NeutrinoContext.Instance.shaderAdd());
                        break;

                    case Neutrino.RenderMaterial.Multiply:
                        material = new Material(NeutrinoContext.Instance.shaderMultiply());
                        break;
                    }

                    string filename = Path.GetFileNameWithoutExtension(
                        textures[renderStyles[i].textureIndex_[0]]);

                    if (string.IsNullOrEmpty(filename))
                    {
                        Debug.LogError("Texture file name is empty or incorrect " +
                                       textures[renderStyles[i].textureIndex_[0]]);
                        return;
                    }

                    Texture texture = Resources.Load(filename) as Texture;
                    if (texture == null)
                    {
                        Debug.LogError("Unable to load texture from Resources: " + filename);
                        return;
                    }

                    material.name = filename;
                    material.SetTexture("_MainTex", texture);

                    materials_[i] = material;
                }
            }

            neutrinoInited_ = true;

                        #if UNITY_EDITOR
            lastFrameTime_ = Time.realtimeSinceStartup;
                        #else
            lastFrameTime_ = Time.time;
                        #endif

                        #if UNITY_EDITOR
            EditorApplication.update += Update;
                        #endif
        }
Beispiel #7
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            NeutrinoRenderer renderer = (NeutrinoRenderer)target;

            Neutrino.Effect effect = renderer.neutrinoEffect();

            for (int emitterIndex = 0; emitterIndex < effect.numEmitters(); ++emitterIndex)
            {
                Neutrino.Emitter emitter = effect.emitter(emitterIndex);

                if (emitter.model().numProperties() == 0)
                {
                    continue;
                }

                EditorGUILayout.Space();
                EditorGUILayout.LabelField(emitter.model().name());
                EditorGUI.indentLevel++;

                for (int propIndex = 0; propIndex < emitter.model().numProperties(); ++propIndex)
                {
                    string name = emitter.model().propertyName(propIndex);

                    switch (emitter.model().propertyType(propIndex))
                    {
                    case Neutrino.EmitterModel.PropertyType.FLOAT:
                    {
                        float valueBefore = (float)emitter.propertyValue(propIndex);
                        float valueAfter  = EditorGUILayout.FloatField(name, valueBefore);
                        emitter.setPropertyValue(propIndex, valueAfter);
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.VEC2:
                    {
                        _math.vec2          valueBefore = (_math.vec2)emitter.propertyValue(propIndex);
                        UnityEngine.Vector2 valueAfter  = EditorGUILayout.Vector2Field(name,
                                                                                       new UnityEngine.Vector2(valueBefore.x, valueBefore.y));
                        emitter.setPropertyValue(propIndex, _math.vec2_(valueAfter.x, valueAfter.y));
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.VEC3:
                    {
                        _math.vec3          valueBefore = (_math.vec3)emitter.propertyValue(propIndex);
                        UnityEngine.Vector3 valueAfter  = EditorGUILayout.Vector3Field(name,
                                                                                       new UnityEngine.Vector3(valueBefore.x, valueBefore.y, valueBefore.z));
                        emitter.setPropertyValue(propIndex, _math.vec3_(valueAfter.x, valueAfter.y, valueAfter.z));
                    }
                    break;

                    case Neutrino.EmitterModel.PropertyType.QUAT:
                        break;
                    }
                }

                EditorGUI.indentLevel--;
            }
        }